|
本帖最后由 eddiewwm 于 2019-5-21 17:44 编辑
嘗試在 LGT 的 SDKBuilder 下編寫 LGT8F690A版本的 Signal Reverser
以下是當中的main.c 程序: - //===================================================
- // Auto generated file : 8/4/2019 17:51:37
- //===================================================
- #include "allinone.h"
- //define from http://www.koch-engineering.com/fileadmin/code/delay.h.pdf
- #define DelayUs(x) { unsigned int _dcnt; \
- _dcnt = (x)/8|1; \
- while(--_dcnt != 0) \
- continue; } //this define is not accuracy, need multiply factor 2.86
- // Import external definitions
- extern void init_modules(void);
- char caTmp[8];
- u16 periodTime =1500;
- int temp=0;
- unsigned int u16tmp0 =0; //total count per cycle
- unsigned int u16tmp1 =0; //total count for High period
- unsigned int u16tmp =0; //total count to used as calibration factor
- #define standardFactor1500 480
- #define standardFactor750 330
- unsigned int factor1500 = standardFactor1500;
- unsigned int factor750 = standardFactor750;
- int main(void)
- {
- // Device initialization
- init_modules();
-
- TRISA4 =0x1;
- TRISB3 =0x0;
- ECP1_IF1=0x0;
- CCP1IE=0x1;
- T1CON = 0x01; // TMR1ON
- eepWrite(0, 0x92); //used to set main clock compensation value
- //OSCTUNE = eepRead(0); //used to change the clock compensation value
- //for calibration Start
- //============================================
- gppPullupEnable(GPPB,P6); //need to enable global Pullup as well
- delayms(300);
- char calStatus = gppReadSingle(GPPB,P6);
- if (calStatus==0)
- {
- char inCal = 1;
- char calCount = 0;
- while (inCal==1)
- {
- if (ECP1_IF1)
- {
- CCP1IE=0;
- ECP1_IF1=0x0;
- if (temp==0)
- {
- u16tmp0 = t1c1GetCapture();
- gppHigh(GPPA, P0);
-
- // 8MSB of 16bit Top Value
- TMR1H = 0xff;
- // 8LSB of 16bit Top Value
- TMR1L = 0xff;
- ECP1CR2 = 0x02;
- CCP1IE=1;
- temp=1;
- }
- else
- {
- u16tmp1 = t1c1GetCapture();
- gppLow(GPPA, P0);
- if (calCount>1)
- {
- u16tmp = (u16tmp + u16tmp1)/2;
- }
- else if (calCount == 1)
- {
- u16tmp = u16tmp1;
- }
-
- if (calCount ==20)
- {
- inCal = 0;
- }
- calCount++;
-
- ECP1CR2 = 0x00;
- CCP1IE=1;
- temp=0;
- /*
- //send u16tmp for debug purpose
- axu_utoa(caTmp, u16tmp, 16);
- //axu_utoa(caTmp, OSCTUNE, 16);
- usartPutStr(caTmp);
- usartPutStr("\r\n");
- */
- }
- }
- }
- eepWrite(1,u16tmp); //low byte of calFactor
- eepWrite(2,u16tmp>>8); //high byte of calFactor
- }
- u16tmp = eepRead(2)<<8;
- u16tmp = u16tmp | eepRead(1);
- /*
- //send u16tmp for debug purpose
- axu_utoa(caTmp, u16tmp, 16);
- //axu_utoa(caTmp, OSCTUNE, 16);
- usartPutStr(caTmp);
- usartPutStr("\r\n");
- */
- // float factor = (12000.000/u16tmp);
- //float factor = 1.5;
- if (u16tmp > 12000 && u16tmp < 12500)
- {
- factor1500 = (standardFactor1500-(u16tmp -12000));
- factor750 = (standardFactor750+(u16tmp -12000)/2);
- }
- else if (u16tmp <12000 && u16tmp >11500)
- {
- factor1500 = (standardFactor1500+(12000-u16tmp));
- factor750 = (standardFactor750-(12000 - u16tmp)/2);
- }
-
- // Main loop
- while(1)
- {
- if (ECP1_IF1)
- {
- CCP1IE=0;
- ECP1_IF1=0x0;
- if (temp==0)
- {
- u16tmp0 = t1c1GetCapture();
- // 8MSB of 16bit Top Value
- TMR1H = 0xff;
- // 8LSB of 16bit Top Value
- TMR1L = 0xff;
- ECP1CR2 = 0x02;
- CCP1IE=1;
- temp=1;
-
- //no timer1 Interrupt (less than 6ms) is 750us mode
- if(!TMR1IF)
- {
- periodTime = (6000 - (u16tmp1 * 0.533)+ factor750); //calculate the 750us mode compensated reversed period time
- }
-
- }
- else
- {
- u16tmp1 = t1c1GetCapture();
- gppLow(GPPA, P0);
-
- //has timer1 Interrupt (longer than 6ms) is 1500us mode
- if(TMR1IF)
- {
- periodTime = (12000 - (u16tmp1 * 0.525)-factor1500); //calculate the 1500us mode compensated reversed period time
- TMR1IF =0;
- }
-
- DelayUs(periodTime); //reversed output period time
- gppHigh(GPPA, P0);
-
- ECP1CR2 = 0x00;
- CCP1IE=1;
- temp=0;
- }
- /*
- //send u16tmp1 for debug purpose
- axu_utoa(caTmp, u16tmp1, 16);
- //axu_utoa(caTmp, OSCTUNE, 16);
- usartPutStr(caTmp);
- usartPutStr("\r\n");
- */
- }
- }
- return 0;
- }
复制代码
SDK 中的設定:
1) System and Power:
0x0 16MHz HFINTOSC
0x6 HFINTOSC/2
0x0 1T
Gobal Pullup Enable
2) I/O Subsystem:
RA0
3) USART Mode:
0x0 Async. Mode
19200 19200bps
0x0 8 bit data
Enable TX
4) Timer/Counter 1:
0x1 System clock (Fsys)
0x0 no scaler
65000 (value of period register (PR1))
Common Setting for all ECP1 PWM/Capture channels
0x0 Instant Mode
0x0 disable encoder
0x0 invert, decrease counter
Check to enable PWM/Capture for channel T1C1
0x1 Capture Mode
0x1 T1C1 on RA4
0x0 from T1C1
5) EEPROM Module
6) Miscellany
7) Configration Words
FOSC_INTOSC - internal 16MHz HFINTOSC
WDT_OFF - disable WDT
FSEN_OFF - disable crystal fail-safe
LVR_2V4 - 2.4V LVR threshold
SUT_2MS 2ms startup timer
RSTE_ON - enable external reset
TCYC_1T - 1T instrction cycle
LVRPM_ON - disable LVR in sleep mode
|
|