微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机(8bit)的16路舵机调速分析与实现

单片机(8bit)的16路舵机调速分析与实现

时间:11-18 来源:互联网 点击:
// main.c

[cpp]view plaincopyprint?

  1. #include1.H>
  2. #include"ControlRobot.h"
  3. #include
  4. #defineDEBUG_PROTOCOL
  5. typedefunsignedcharUCHAR8;
  6. typedefunsignedintUINT16;
  7. #undefTRUE
  8. #undefFALSE
  9. #defineTRUE1
  10. #defineFALSE0
  11. #defineMEMORY_MODEL
  12. UINT16MEMORY_MODELdelayVar1;
  13. UCHAR8MEMORY_MODELdelayVar2;
  14. #defineBAUD_RATE0xF3
  15. #defineUSED_PIN2
  16. #definePIN_GROUP_10
  17. #definePIN_GROUP_21
  18. #defineGROUP_1_CONTROL_PINP0
  19. #defineGROUP_2_CONTROL_PINP2
  20. #defineSTEERING_ENGINE_CYCLE2000
  21. #defineSTEERING_ENGINE_INIT_DELAY50
  22. #defineSTEERING_ENGINE_FULL_CYCLE((STEERING_ENGINE_CYCLE)-(STEERING_ENGINE_INIT_DELAY))
  23. volatileUCHAR8MEMORY_MODELg_angle[2][ROBOT_JOINT_AMOUNT];
  24. volatilebitMEMORY_MODELg_fillingBufferIndex=0;
  25. volatilebitMEMORY_MODELg_readyBufferIndex=1;
  26. volatilebitMEMORY_MODELg_swapBuffer=FALSE;
  27. volatileUINT16MEMORY_MODELg_diffAngle[USED_PIN][8];
  28. volatileUCHAR8MEMORY_MODELg_diffAngleMask[USED_PIN][8]=
  29. {
  30. {
  31. ROBOT_PIN_MASK_LEFT_SHOULDER_VERTICAL,
  32. ROBOT_PIN_MASK_LEFT_SHOULDER_HORIZEN,
  33. ROBOT_PIN_MASK_LEFT_ELBOW,
  34. ROBOT_PIN_MASK_RIGHT_SHOULDER_VERTICAL,
  35. ROBOT_PIN_MASK_RIGHT_SHOULDER_HORIZEN,
  36. ROBOT_PIN_MASK_RIGHT_ELBOW,
  37. ROBOT_PIN_MASK_LEFT_HIP_VERTICAL,
  38. ROBOT_PIN_MASK_RIGHT_HIP_VERTICAL
  39. },
  40. {
  41. ROBOT_PIN_MASK_LEFT_HIP_HORIZEN,
  42. ROBOT_PIN_MASK_LEFT_KNEE,
  43. ROBOT_PIN_MASK_LEFT_ANKLE,
  44. ROBOT_PIN_MASK_LEFT_FOOT,
  45. ROBOT_PIN_MASK_RIGHT_HIP_HORIZEN,
  46. ROBOT_PIN_MASK_RIGHT_KNEE,
  47. ROBOT_PIN_MASK_RIGHT_ANKLE,
  48. ROBOT_PIN_MASK_RIGHT_FOOT
  49. }
  50. };
  51. #ifdefDEBUG_PROTOCOL
  52. sbitP10=P1^0;//正在填充交换区1
  53. sbitP11=P1^1;//正在填充交换区2
  54. sbitP12=P1^2;//交换区变换
  55. sbitP13=P1^3;//协议是否正确
  56. #endif
  57. voidDelay10us(UINT16ntimes)
  58. {
  59. for(delayVar1=0;delayVar1
  60. for(delayVar2=0;delayVar2<21;++delayVar2)
  61. _nop_();
  62. }
  63. voidInitPwmPollint()
  64. {
  65. UCHAR8i;
  66. UCHAR8j;
  67. UCHAR8k;
  68. UINT16temp;
  69. for(i=0;i
  70. {
  71. for(j=0;j<7;++j)
  72. {
  73. for(k=j;k<8;++k)
  74. {
  75. if(g_diffAngle[i][j]>g_diffAngle[i][k])
  76. {
  77. temp=g_diffAngle[i][j];
  78. g_diffAngle[i][j]=g_diffAngle[i][k];
  79. g_diffAngle[i][k]=temp;
  80. temp=g_diffAngleMask[i][j];
  81. g_diffAngleMask[i][j]=g_diffAngleMask[i][k];
  82. g_diffAngleMask[i][k]=temp;
  83. }
  84. }
  85. }
  86. }
  87. for(i=0;i
  88. {
  89. for(j=0;j<8;++j)
  90. {
  91. if(INVALID_JOINT_VALUE==g_diffAngle[i][j])
  92. {
  93. g_diffAngle[i][j]=STEERING_ENGINE_FULL_CYCLE;
  94. }
  95. }
  96. }
  97. for(i=0;i
  98. {
  99. for(j=7;j>=1;--j)
  100. {
  101. g_diffAngle[i][j]=g_diffAngle[i][j]-g_diffAngle[i][j-1];
  102. }
  103. }
  104. }
  105. voidInitSerialPort()
  106. {
  107. AUXR=0x00;
  108. ES=0;
  109. TMOD=0x20;
  110. SCON=0x50;
  111. TH1=BAUD_RATE;
  112. TL1=TH1;
  113. PCON=0x80;
  114. EA=1;
  115. ES=1;
  116. TR1=1;
  117. }
  118. voidOnSerialPort()interrupt4
  119. {
  120. staticUCHAR8previousChar=0;
  121. staticUCHAR8currentChar=0;
  122. staticUCHAR8counter=0;
  123. if(RI)
  124. {
  125. RI=0;
  126. currentChar=SBUF;
  127. if(PROTOCOL_HEADER[0]==currentChar)//协议标志
  128. {
  129. previousChar=currentChar;
  130. }
  131. else
  132. {
  133. if(PROTOCOL_HEADER[0]==previousChar&&PROTOCOL_HEADER[1]==currentChar)//协议开始
  134. {
  135. counter=0;
  136. previousChar=currentChar;
  137. g_swapBuffer=FALSE;
  138. }
  139. elseif(PROTOCOL_END[0]==previousChar&&PROTOCOL_END[1]==currentChar)//协议结束
  140. {
  141. previousChar=currentChar;
  142. if(ROBOT_JOINT_AMOUNT==counter)//协议接受正确
  143. {
  144. if(0==g_fillingBufferIndex)
  145. {
  146. g_readyBufferIndex=0;
  147. g_fillingBufferIndex=1;
  148. }
  149. else
  150. {
  151. g_readyBufferIndex=1;
  152. g_fillingBufferIndex=0;
  153. }
  154. g_swapBuffer=TRUE;
  155. #ifdefDEBUG_PROTOCOL
  156. P13=0;
  157. #endif
  158. }
  159. else
  160. {
  161. g_swapBuffer=FALSE;
  162. #ifdefDEBUG_PROTOCOL
  163. P13=1;
  164. #endif
  165. }
  166. counter=0;
  167. }
  168. else//接受协议正文
  169. {
  170. g_swapBuffer=FALSE;
  171. previousChar=currentChar;
  172. if(counter
  173. g_angle[g_fillingBufferIndex][counter]=currentChar;
  174. ++counter;
  175. }
  176. }//if(PROTOCOL_HEADER[0]==currentChar)
  177. SBUF=currentChar;
  178. while(!TI)
  179. ;
  180. TI=0;
  181. }//(RI)
  182. }
  183. voidFillDiffAngle()
  184. {
  185. //设置舵机要调整的角度
  186. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_LEFT_SHOULDER_VERTICAL];
  187. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_LEFT_SHOULDER_HORIZEN];
  188. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_ELBOW]=g_angle[g_readyBufferIndex][ROBOT_LEFT_ELBOW];
  189. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_VERTICAL]=g_an

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top