LGT8F690A SDK 版本的 Signal Reverser
本帖最后由 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;
u16 periodTime =1500;
int temp=0;
unsigned intu16tmp0 =0; //total count per cycle
unsigned intu16tmp1 =0; //total count for High period
unsigned intu16tmp =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");
*/
// floatfactor = (12000.000/u16tmp);
//floatfactor = 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_2MS2ms startup timer
RSTE_ON - enable external reset
TCYC_1T - 1T instrction cycle
LVRPM_ON - disable LVR in sleep mode
為配合上面的690A signal reverser的測試,使用 LGT8F328P 在 Arduino下編寫的一個簡單測試程序:
/*
Servo Signal Generator For Test Purpose
*/
# define ServoOut 13
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin as Servo output.
pinMode(ServoOut, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
///*
digitalWrite(ServoOut, HIGH); // turn the ServoOut on (HIGH is the voltage level)
delayMicroseconds(1500 * 1.33); // wait for a second
digitalWrite(ServoOut, LOW); // turn the ServoOut off by making the voltage LOW
delay(15); // wait for a second
//*/
/*
for (int i = 8; i < 20; i++) {
for (int j = 750; j < 2250; j=j+10) {
digitalWrite(ServoOut, HIGH); // turn the ServoOut on (HIGH is the voltage level)
delayMicroseconds(j * 1.33); // wait for a second
digitalWrite(ServoOut, LOW); // turn the ServoOut off by making the voltage LOW
delay(i); // wait for a second
}
}
/*
digitalWrite(ServoOut, HIGH); // turn the ServoOut on (HIGH is the voltage level)
delayMicroseconds(750 * 1.33); // wait for a second
digitalWrite(ServoOut, LOW); // turn the ServoOut off by making the voltage LOW
delayMicroseconds(1500*1.33); // wait for a second
*/
/*
for (int j = 400; j < 1120; j++) {
digitalWrite(ServoOut, HIGH); // turn the ServoOut on (HIGH is the voltage level)
delayMicroseconds(j * 1.33); // wait for a second
digitalWrite(ServoOut, LOW); // turn the ServoOut off by making the voltage LOW
delayMicroseconds(2000*1.33); // wait for a second
}
*/
}
页:
[1]