极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9191|回复: 1

LGT8F690A SDK 版本的 Signal Reverser

[复制链接]
发表于 2019-5-14 12:51:14 | 显示全部楼层 |阅读模式
本帖最后由 eddiewwm 于 2019-5-21 17:44 编辑

嘗試在 LGT 的 SDKBuilder 下編寫 LGT8F690A版本的 Signal Reverser
以下是當中的main.c 程序:
  1. //===================================================
  2. // Auto generated file : 8/4/2019 17:51:37
  3. //===================================================

  4. #include "allinone.h"

  5. //define from http://www.koch-engineering.com/fileadmin/code/delay.h.pdf
  6. #define DelayUs(x) { unsigned int _dcnt; \
  7. _dcnt = (x)/8|1; \
  8. while(--_dcnt != 0) \
  9. continue; }  //this define is not accuracy, need multiply factor 2.86

  10. // Import external definitions
  11. extern void init_modules(void);

  12. char caTmp[8];
  13. u16 periodTime =1500;
  14. int temp=0;
  15. unsigned int  u16tmp0 =0; //total count per cycle
  16. unsigned int  u16tmp1 =0; //total count for High period
  17. unsigned int  u16tmp =0; //total count to used as calibration factor

  18. #define standardFactor1500 480
  19. #define standardFactor750 330
  20. unsigned int factor1500 = standardFactor1500;
  21. unsigned int factor750 = standardFactor750;


  22. int main(void)
  23. {
  24.         // Device initialization
  25.         init_modules();
  26.        
  27.         TRISA4 =0x1;
  28.         TRISB3 =0x0;
  29.         ECP1_IF1=0x0;
  30.         CCP1IE=0x1;
  31.         T1CON = 0x01;                // TMR1ON

  32.         eepWrite(0, 0x92); //used to set main clock compensation value
  33.         //OSCTUNE = eepRead(0); //used to change the clock compensation value

  34.         //for calibration Start
  35.         //============================================
  36.         gppPullupEnable(GPPB,P6); //need to enable global Pullup as well
  37.         delayms(300);
  38.         char calStatus = gppReadSingle(GPPB,P6);
  39.         if (calStatus==0)
  40.         {
  41.                 char inCal = 1;
  42.                 char calCount = 0;
  43.                 while (inCal==1)
  44.                 {
  45.                         if (ECP1_IF1)
  46.                         {
  47.                                 CCP1IE=0;
  48.                                 ECP1_IF1=0x0;
  49.                                 if (temp==0)
  50.                                 {
  51.                                         u16tmp0 =  t1c1GetCapture();
  52.                                         gppHigh(GPPA, P0);
  53.                                
  54.                                         // 8MSB of 16bit Top Value
  55.                                         TMR1H = 0xff;
  56.                                         // 8LSB of 16bit Top Value
  57.                                         TMR1L = 0xff;

  58.                                         ECP1CR2 = 0x02;
  59.                                         CCP1IE=1;                               
  60.                                         temp=1;       
  61.                                 }
  62.                                 else
  63.                                 {
  64.                                         u16tmp1 =  t1c1GetCapture();
  65.                                         gppLow(GPPA, P0);
  66.                                         if (calCount>1)
  67.                                         {
  68.                                                 u16tmp = (u16tmp + u16tmp1)/2;
  69.                                         }
  70.                                         else if (calCount == 1)
  71.                                         {
  72.                                                 u16tmp = u16tmp1;
  73.                                         }
  74.                                
  75.                                         if (calCount ==20)
  76.                                         {
  77.                                                 inCal = 0;
  78.                                         }
  79.                                         calCount++;
  80.                                
  81.                                         ECP1CR2 = 0x00;
  82.                                         CCP1IE=1;                                                               
  83.                                         temp=0;               
  84. /*                               
  85.                                         //send u16tmp for debug purpose                       
  86.                                         axu_utoa(caTmp, u16tmp, 16);
  87.                                         //axu_utoa(caTmp, OSCTUNE, 16);
  88.                                         usartPutStr(caTmp);
  89.                                         usartPutStr("\r\n");
  90. */                       
  91.                                 }
  92.                         }       
  93.                 }
  94.                 eepWrite(1,u16tmp); //low byte of calFactor
  95.                 eepWrite(2,u16tmp>>8); //high byte of calFactor
  96.         }

  97.         u16tmp = eepRead(2)<<8;
  98.         u16tmp = u16tmp | eepRead(1);
  99. /*
  100.         //send u16tmp for debug purpose                       
  101.         axu_utoa(caTmp, u16tmp, 16);
  102.         //axu_utoa(caTmp, OSCTUNE, 16);
  103.         usartPutStr(caTmp);
  104.         usartPutStr("\r\n");
  105. */
  106.         //        float  factor = (12000.000/u16tmp);
  107.         //float  factor = 1.5;

  108.         if (u16tmp > 12000 && u16tmp < 12500)
  109.         {
  110.                 factor1500 = (standardFactor1500-(u16tmp -12000));
  111.                 factor750 = (standardFactor750+(u16tmp -12000)/2);
  112.         }
  113.         else if (u16tmp <12000 && u16tmp >11500)
  114.         {
  115.                 factor1500 = (standardFactor1500+(12000-u16tmp));
  116.                 factor750 = (standardFactor750-(12000 - u16tmp)/2);
  117.         }
  118.        
  119.         // Main loop
  120.         while(1)
  121.         {
  122.                 if (ECP1_IF1)
  123.                 {
  124.                         CCP1IE=0;
  125.                         ECP1_IF1=0x0;
  126.                         if (temp==0)
  127.                         {
  128.                                 u16tmp0 =  t1c1GetCapture();
  129.                                 // 8MSB of 16bit Top Value
  130.                                 TMR1H = 0xff;
  131.                                 // 8LSB of 16bit Top Value
  132.                                 TMR1L = 0xff;

  133.                                 ECP1CR2 = 0x02;
  134.                                 CCP1IE=1;                               
  135.                                 temp=1;       
  136.                                        
  137.                                 //no timer1 Interrupt (less than 6ms) is 750us mode
  138.                                 if(!TMR1IF)
  139.                                 {
  140.                                         periodTime = (6000 - (u16tmp1 * 0.533)+ factor750); //calculate the 750us mode compensated reversed period time
  141.                                 }               
  142.                        
  143.                         }
  144.                         else
  145.                         {
  146.                                 u16tmp1 =  t1c1GetCapture();
  147.                                 gppLow(GPPA, P0);
  148.                                
  149.                                 //has timer1 Interrupt (longer than 6ms) is 1500us mode
  150.                                 if(TMR1IF)                               
  151.                                 {
  152.                                         periodTime = (12000 - (u16tmp1 * 0.525)-factor1500); //calculate the 1500us mode compensated reversed period time
  153.                                 TMR1IF =0;
  154.                                 }
  155.                                
  156.                                 DelayUs(periodTime); //reversed output period time
  157.                                 gppHigh(GPPA, P0);
  158.                        
  159.                                 ECP1CR2 = 0x00;
  160.                                 CCP1IE=1;                                                               
  161.                                 temp=0;                                               
  162.                         }
  163. /*                       
  164.                         //send u16tmp1 for debug purpose                       
  165.                         axu_utoa(caTmp, u16tmp1, 16);
  166.                         //axu_utoa(caTmp, OSCTUNE, 16);
  167.                         usartPutStr(caTmp);
  168.                         usartPutStr("\r\n");
  169. */                       
  170.                 }
  171.         }

  172.         return 0;
  173. }
复制代码


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








回复

使用道具 举报

 楼主| 发表于 2019-5-14 12:57:11 | 显示全部楼层
為配合上面的690A signal reverser的測試,使用 LGT8F328P 在 Arduino下編寫的一個簡單測試程序:
  1. /*
  2.   Servo Signal Generator For Test Purpose

  3. */

  4. # define ServoOut 13

  5. // the setup function runs once when you press reset or power the board
  6. void setup() {
  7.   // initialize digital pin as Servo output.
  8.   pinMode(ServoOut, OUTPUT);
  9. }

  10. // the loop function runs over and over again forever
  11. void loop() {
  12. ///*
  13.       digitalWrite(ServoOut, HIGH);   // turn the ServoOut on (HIGH is the voltage level)
  14.       delayMicroseconds(1500 * 1.33);                     // wait for a second
  15.       digitalWrite(ServoOut, LOW);    // turn the ServoOut off by making the voltage LOW
  16.       delay(15);                       // wait for a second
  17. //*/
  18. /*
  19.   for (int i = 8; i < 20; i++) {
  20.     for (int j = 750; j < 2250; j=j+10) {
  21.       digitalWrite(ServoOut, HIGH);   // turn the ServoOut on (HIGH is the voltage level)
  22.       delayMicroseconds(j * 1.33);                     // wait for a second
  23.       digitalWrite(ServoOut, LOW);    // turn the ServoOut off by making the voltage LOW
  24.       delay(i);                       // wait for a second
  25.     }
  26.   }
  27. /*
  28.       digitalWrite(ServoOut, HIGH);   // turn the ServoOut on (HIGH is the voltage level)
  29.       delayMicroseconds(750 * 1.33);                     // wait for a second
  30.       digitalWrite(ServoOut, LOW);    // turn the ServoOut off by making the voltage LOW
  31.       delayMicroseconds(1500*1.33);                       // wait for a second
  32. */

  33. /*
  34.     for (int j = 400; j < 1120; j++) {
  35.       digitalWrite(ServoOut, HIGH);   // turn the ServoOut on (HIGH is the voltage level)
  36.       delayMicroseconds(j * 1.33);                     // wait for a second
  37.       digitalWrite(ServoOut, LOW);    // turn the ServoOut off by making the voltage LOW
  38.       delayMicroseconds(2000*1.33);                       // wait for a second
  39.     }
  40. */
  41. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 17:18 , Processed in 0.045070 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表