您的位置 首页 电子

单片机(8bit)的16路舵机调速剖析与完成

//main.c[cpp]viewplaincopyprint?#includeSTC_NEW_8051.H>#includeControlRobot.h#includeintrins.h&gt

// 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_angle[g_readyBufferIndex][ROBOT_RIGHT_SHOULDER_VERTICAL];
  190. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_SHOULDER_HORIZEN];
  191. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_ELBOW]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_ELBOW];
  192. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_HIP_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_LEFT_HIP_VERTICAL];
  193. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_HIP_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_HIP_VERTICAL];
  194. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_HIP_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_LEFT_HIP_HORIZEN];
  195. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_KNEE]=g_angle[g_readyBufferIndex][ROBOT_LEFT_KNEE];
  196. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_ANKLE]=g_angle[g_readyBufferIndex][ROBOT_LEFT_ANKLE];
  197. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_FOOT]=g_angle[g_readyBufferIndex][ROBOT_LEFT_FOOT];
  198. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_HIP_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_HIP_HORIZEN];
  199. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_KNEE]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_KNEE];
  200. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_ANKLE]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_ANKLE];
  201. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_FOOT]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_FOOT];
  202. //复位舵机管脚索引
  203. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_VERTICAL]=ROBOT_PIN_MASK_LEFT_SHOULDER_VERTICAL;
  204. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_HORIZEN]=ROBOT_PIN_MASK_LEFT_SHOULDER_HORIZEN;
  205. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_ELBOW]=ROBOT_PIN_MASK_LEFT_ELBOW;
  206. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_VERTICAL]=ROBOT_PIN_MASK_RIGHT_SHOULDER_VERTICAL;
  207. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_HORIZEN]=ROBOT_PIN_MASK_RIGHT_SHOULDER_HORIZEN;
  208. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_ELBOW]=ROBOT_PIN_MASK_RIGHT_ELBOW;
  209. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_HIP_VERTICAL]=ROBOT_PIN_MASK_LEFT_HIP_VERTICAL;
  210. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_HIP_VERTICAL]=ROBOT_PIN_MASK_RIGHT_HIP_VERTICAL;
  211. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_HIP_HORIZEN]=ROBOT_PIN_MASK_LEFT_HIP_HORIZEN;
  212. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_KNEE]=ROBOT_PIN_MASK_LEFT_KNEE;
  213. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_ANKLE]=ROBOT_PIN_MASK_LEFT_ANKLE;
  214. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_FOOT]=ROBOT_PIN_MASK_LEFT_FOOT;
  215. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_HIP_HORIZEN]=ROBOT_PIN_MASK_RIGHT_HIP_HORIZEN;
  216. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_KNEE]=ROBOT_PIN_MASK_RIGHT_KNEE;
  217. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_ANKLE]=ROBOT_PIN_MASK_RIGHT_ANKLE;
  218. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_FOOT]=ROBOT_PIN_MASK_RIGHT_FOOT;
  219. }
  220. #definePWM_STEERING_ENGINE(group)\
  221. {\
  222. counter=STEERING_ENGINE_INIT_DELAY;\
  223. for(i=0;i<8;++i)\
  224. counter+=g_diffAngle[PIN_GROUP_##group][i];\
  225. \
  226. for(i=0;i<30;++i)\
  227. {\
  228. GROUP_##group##_CONTROL_PIN=0xFF;\
  229. Delay10us(STEERING_ENGINE_INIT_DELAY);\
  230. \
  231. for(j=0;j<8;++j)\
  232. {\
  233. Delay10us(g_diffAngle[PIN_GROUP_##group][j]);\
  234. GROUP_##group##_CONTROL_PIN&=~(g_diffAngleMask[PIN_GROUP_##group][j]);\
  235. }\
  236. \
  237. Delay10us(STEERING_ENGINE_CYCLE-counter);\
  238. }\
  239. }
  240. voidmain()
  241. {
  242. UCHAR8i;
  243. UCHAR8j;
  244. UINT16counter;
  245. InitSerialPort();
  246. P1=0xFF;
  247. //初始化舵机视点
  248. for(i=0;i
  249. {
  250. g_angle[0][i]=45;
  251. g_angle[1][i]=45;
  252. }
  253. for(i=0;i
  254. for(j=0;j<8;++j)
  255. g_diffAngle[i][j]=0;
  256. FillDiffAngle();
  257. InitPwmPollint();
  258. while(1)
  259. {
  260. #ifdefDEBUG_PROTOCOL
  261. if(g_fillingBufferIndex)
  262. {
  263. P11=0;
  264. P10=1;
  265. }
  266. else
  267. {
  268. P11=1;
  269. P10=0;
  270. }
  271. if(g_swapBuffer)
  272. P12=0;
  273. else
  274. P12=1;
  275. #endif
  276. if(g_swapBuffer)
  277. {
  278. FillDiffAngle();
  279. g_swapBuffer=FALSE;
  280. InitPwmPollint();
  281. }
  282. PWM_STEERING_ENGINE(1)
  283. PWM_STEERING_ENGINE(2)
  284. }
  285. }

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/dianzi/266836.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部