大概寫了一下控制程序 希望對你們有幫助
- tpyedef i8 signed char
- tpyedef u8 unsigned char
- tpyedef i16 signed short int
- tpyedef u16 unsigned short int
- tpyedef i32 signed long int
- tpyedef u32 unsigned long int
- #define ELEVEL_LOW 0 //低電平
- #define ELEVEL_HIGH 1 //高電平
- #define TIMER_INIT 10 //定時器初始時間 單位US
- #define STRENG_MINTIME 500 //舵機最小角度時間 0.5MS
- #define STRENG_MAXTIME 2500 //舵機最大角度時間 2.5MS
- #define STRENG_TIMERPRO 20000 //舵機周期 20MS
- #define STRENG_MINANGLE 0 //最小角度
- #define STRENG_MAXANGLE 185 //最大角度
- #define IO_STRENG10 // PORT引腳
- #define IO_STRENG20
- #define IO_STRENG30
- #define IO_STRENG40
- #define IO_STRENG50
- #define IO_STRENG60
- #define IO_STRENG11
- #define IO_STRENG21
- #define IO_STRENG31
- #define IO_STRENG41
- #define IO_STRENG51
- #define IO_STRENG61
- u16 TimerCount = 0; //計數值
- float StrEngAccu = 0; //舵機精度
- u16 StrEngZeroAngle = 0; //零度對應的時間
- void StrEngTimer0_Init(void) //定時器初始化
- {
-
- StrEngAccu = STRENG_MAXANGLE/((STRENG_MAXTIME - STRENG_MINTIME)/TIMER_INIT);
- StrEngZeroAngle = STRENG_TIMEUS/TimerInitValue;
-
- TH0 = (65535 - TIMER_INIT)/256;
- TL0 = (65535 - TIMER_INIT);
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void STRENG_AngleControl(u8 angle,sbit *io)// 0-185角度控制
- {
- float regdat = 0;
- u16 angletime = 0;
-
- regdat = (float)angle;
- angletime = (u16)(regdat/StrEngAccu) + (STRENG_MINTIME/TIMER_INIT);
-
- if(angletime > TimerCount)
- {
- *io = ELEVEL_HIGH;
- }
- else
- {
- *io = ELEVEL_LOW;
- }
- }
- void timer0(void) interrupt 0
- {
- TimerCount ++;
- TH0 = (65535 - TIMER_INIT)/256;;
- TL0 = (65535 - TIMER_INIT);
- if(TimerCount > (STRENG_TIMERPRO/TIMER_INIT))
- {
- TimerCount = 0;
- }
- }
復制代碼
|