欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: 51單片機控制舵機 總框 [打印本頁]

作者: c505085670    時間: 2016-11-19 20:43
標題: 51單片機控制舵機 總框
大概寫了一下控制程序  希望對你們有幫助
  1. tpyedef i8 signed char
  2. tpyedef u8 unsigned char

  3. tpyedef i16 signed short int
  4. tpyedef u16 unsigned short int

  5. tpyedef i32 signed long int
  6. tpyedef u32 unsigned long int

  7. #define ELEVEL_LOW                        0                        //低電平
  8. #define ELEVEL_HIGH                        1                        //高電平

  9. #define TIMER_INIT                        10                        //定時器初始時間  單位US

  10. #define STRENG_MINTIME                500                        //舵機最小角度時間 0.5MS
  11. #define STRENG_MAXTIME                2500                //舵機最大角度時間 2.5MS
  12. #define STRENG_TIMERPRO                20000                //舵機周期 20MS

  13. #define STRENG_MINANGLE                0                        //最小角度
  14. #define STRENG_MAXANGLE                185                        //最大角度

  15. #define IO_STRENG10                                                // PORT引腳
  16. #define IO_STRENG20
  17. #define IO_STRENG30
  18. #define IO_STRENG40
  19. #define IO_STRENG50
  20. #define IO_STRENG60

  21. #define IO_STRENG11
  22. #define IO_STRENG21
  23. #define IO_STRENG31
  24. #define IO_STRENG41
  25. #define IO_STRENG51
  26. #define IO_STRENG61

  27. u16 TimerCount = 0;                                                //計數值

  28. float StrEngAccu = 0;                                        //舵機精度
  29. u16 StrEngZeroAngle = 0;                                //零度對應的時間       

  30. void StrEngTimer0_Init(void)                        //定時器初始化
  31. {
  32.        
  33.         StrEngAccu = STRENG_MAXANGLE/((STRENG_MAXTIME - STRENG_MINTIME)/TIMER_INIT);
  34.         StrEngZeroAngle = STRENG_TIMEUS/TimerInitValue;
  35.        
  36.         TH0 = (65535 - TIMER_INIT)/256;
  37.         TL0 = (65535 - TIMER_INIT);
  38.         EA = 1;
  39.         ET0 = 1;
  40.         TR0 = 1;
  41. }

  42. void STRENG_AngleControl(u8 angle,sbit *io)// 0-185角度控制
  43. {
  44.         float regdat = 0;
  45.         u16 angletime = 0;
  46.        
  47.         regdat = (float)angle;
  48.         angletime = (u16)(regdat/StrEngAccu) + (STRENG_MINTIME/TIMER_INIT);
  49.        
  50.         if(angletime > TimerCount)
  51.         {
  52.                 *io = ELEVEL_HIGH;
  53.         }
  54.         else
  55.         {
  56.                 *io = ELEVEL_LOW;
  57.         }
  58. }

  59. void timer0(void) interrupt 0
  60. {
  61.         TimerCount ++;
  62.         TH0 = (65535 - TIMER_INIT)/256;;
  63.         TL0 = (65535 - TIMER_INIT);

  64.         if(TimerCount > (STRENG_TIMERPRO/TIMER_INIT))
  65.         {
  66.                 TimerCount = 0;
  67.         }
  68. }
復制代碼







歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1