OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4171|回复: 2

无刷直流电机不转

[复制链接]

2

主题

3

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2015-4-21
在线时间
0 小时
发表于 2015-7-9 10:25:14 | 显示全部楼层 |阅读模式
5金钱
  1. /* MAIN.C file<br />
  2. &nbsp;*&nbsp;<br />
  3. &nbsp;* Copyright (c) 2002-2005 STMicroelectronics<br />
  4. &nbsp;*/<br />
  5. <br />
  6. <br />
  7. /* MAIN.C file<br />
  8. 摘要:<br />
  9. 1.PD7延时反转,指示系统工作。<br />
  10. 2.上电,按指定PWM输出换相,实现电机基本运转功能。<br />
  11. 3.换相依据为HALl信号,采用PORTD中断功能完成.<br />
  12. 总之,上电根据HALL状态换相,并在输出PWM信号后,电机会转动,<br />
  13. 同时HALL信号不断改变,然后进入捕获中断,进行及时换相。<br />
  14. 如此实现电机的运转控制。<br />
  15. 2013.5.8<br />
  16. &nbsp;*/<br />
  17. #include "stm8s.h"<br />
  18. <br />
  19. #define STM8_FREQ_MHZ 16<br />
  20. #define PWM_FREQUENCY 16000<br />
  21. <br />
  22. static const u16 hArrPwmVal = ((u16)((STM8_FREQ_MHZ * (u32)1000000)/PWM_FREQUENCY));<br />
  23. //PWM信号周期<br />
  24. <br />
  25. #define PWMOUT 20<br />
  26. //按15%占空比输出<br />
  27. <br />
  28. const unsigned char PWM_EN1_TAB[6]={0x00,0x00,0x10,0x10,0x01,0x01};<br />
  29. //六步法中,CH1\CH2通道极性及使能配置<br />
  30. const unsigned char PWM_EN2_TAB[6]={0x01,0x01,0x00,0x00,0x0,0x00};<br />
  31. //六步法中,CH3通道极性及使能配置<br />
  32. <br />
  33. //上桥臂开关控制端口定义<br />
  34. #define MCO1_PORT GPIOC<br />
  35. #define MCO1_PIN GPIO_PIN_1<br />
  36. #define MCO3_PORT GPIOC<br />
  37. #define MCO3_PIN GPIO_PIN_2<br />
  38. #define MCO5_PORT GPIOC<br />
  39. #define MCO5_PIN GPIO_PIN_3<br />
  40. <br />
  41. //下桥臂开关控制端口定义<br />
  42. #define MCO0_PORT GPIOB<br />
  43. #define MCO0_PIN GPIO_PIN_0<br />
  44. #define MCO2_PORT GPIOB<br />
  45. #define MCO2_PIN GPIO_PIN_1<br />
  46. #define MCO4_PORT GPIOB<br />
  47. #define MCO4_PIN GPIO_PIN_2<br />
  48. //下桥臂低电平开关管导通<br />
  49. #define PWM_A_OFF MCO0_PORT-&gt;ODR |= (u8)MCO0_PIN;&nbsp;<br />
  50. #define PWM_B_OFF MCO2_PORT-&gt;ODR |= (u8)MCO2_PIN;&nbsp;<br />
  51. #define PWM_C_OFF MCO4_PORT-&gt;ODR |= (u8)MCO4_PIN;&nbsp;<br />
  52. <br />
  53. #define PWM_A_ON MCO0_PORT-&gt;ODR &amp;= (u8)(~MCO0_PIN);&nbsp;<br />
  54. #define PWM_B_ON MCO2_PORT-&gt;ODR &amp;= (u8)(~MCO2_PIN);&nbsp;<br />
  55. #define PWM_C_ON MCO4_PORT-&gt;ODR &amp;= (u8)(~MCO4_PIN);&nbsp;<br />
  56. <br />
  57. //霍尔引脚定义<br />
  58. #define H1_PORT GPIOD-&gt;IDR<br />
  59. #define H1_PIN &nbsp;BIT2<br />
  60. <br />
  61. #define H2_PORT GPIOD-&gt;IDR<br />
  62. #define H2_PIN &nbsp;BIT3<br />
  63. <br />
  64. #define H3_PORT GPIOD-&gt;IDR<br />
  65. #define H3_PIN &nbsp;BIT4<br />
  66. <br />
  67. unsigned char bHallSteps[2][8]={<br />
  68. {7,5,3,4,1,0,2,7},//正转<br />
  69. {7,2,0,1,4,3,5,7}//反转<br />
  70. };<br />
  71. <br />
  72. /* Private vars and define */<br />
  73. #define BIT0 0x01<br />
  74. #define BIT1 0x02<br />
  75. #define BIT2 0x04<br />
  76. #define BIT3 0x08<br />
  77. #define BIT4 0x10<br />
  78. #define BIT5 0x20<br />
  79. #define BIT6 0x40<br />
  80. #define BIT7 0x80<br />
  81. <br />
  82. //换相子函数声明<br />
  83. void Commutation(unsigned char bHallStartStep,unsigned int OutPwmValue);<br />
  84. <br />
  85. unsigned int outpwm=0;<br />
  86. <br />
  87. <br />
  88. //初始化按键,指示灯端口<br />
  89. void GPIO_int(void)<br />
  90. {<br />
  91. /* LEDs */<br />
  92. GPIO_Init(GPIOD, GPIO_PIN_7, GPIO_MODE_OUT_PP_HIGH_FAST);<br />
  93. }<br />
  94. <br />
  95. //系统时钟配置:内部16M<br />
  96. void Clock_init(void)<br />
  97. {<br />
  98. /* Select fCPU = 16MHz */<br />
  99. &nbsp; CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);<br />
  100. }<br />
  101. <br />
  102. //换相电路开关管IO初始化<br />
  103. void PWM_IO_init(void)<br />
  104. { <br />
  105. &nbsp; //PB012 下桥臂0有效 ,配置为高电平<br />
  106. GPIO_Init(MCO0_PORT, MCO0_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);<br />
  107. GPIO_Init(MCO2_PORT, MCO2_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);<br />
  108. GPIO_Init(MCO4_PORT, MCO4_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);<br />
  109. <br />
  110. //PC123 上桥臂1有效,配置为低电平<br />
  111. GPIO_Init(MCO1_PORT, MCO1_PIN,GPIO_MODE_OUT_PP_LOW_FAST);<br />
  112. GPIO_Init(MCO3_PORT, MCO3_PIN,GPIO_MODE_OUT_PP_LOW_FAST);<br />
  113. GPIO_Init(MCO5_PORT, MCO5_PIN,GPIO_MODE_OUT_PP_LOW_FAST); <br />
  114. }<br />
  115. <br />
  116. void Tim1_init(void)<br />
  117. {<br />
  118. /* TIM1 Peripheral Configuration */&nbsp;<br />
  119. &nbsp; TIM1_DeInit();<br />
  120. <br />
  121. &nbsp; TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, hArrPwmVal, 0);<br />
  122. <br />
  123. &nbsp; TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, hArrPwmVal*0, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_SET);&nbsp;<br />
  124. <br />
  125. &nbsp; TIM1_OC2Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, hArrPwmVal*0, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_SET);&nbsp;<br />
  126. <br />
  127. &nbsp; TIM1_OC3Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, hArrPwmVal*0, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_SET);&nbsp;<br />
  128. <br />
  129. &nbsp; TIM1_CCPreloadControl(DISABLE);<br />
  130. TIM1_Cmd(ENABLE);<br />
  131. }<br />
  132. <br />
  133. //初始化HALL捕获输入定义器,采用PD口外部中断捕获输入<br />
  134. void HALLEXTI_INIT(void)<br />
  135. {<br />
  136. GPIO_Init(GPIOD, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4,GPIO_MODE_IN_PU_IT); //HALL端口模式配置<br />
  137. }<br />
  138. <br />
  139. //根据HALL状态换相,启动电机运行<br />
  140. void HALL_START(void)<br />
  141. {<br />
  142. u8 bHStatus = 0,bHallStartStep;<br />
  143. <br />
  144. // Read status of H1<br />
  145. if (H1_PORT &amp; H1_PIN)<br />
  146. {<br />
  147. bHStatus |= BIT2;<br />
  148. }<br />
  149. <br />
  150. // Read status of H2&nbsp;<br />
  151. if (H2_PORT &amp; H2_PIN)<br />
  152. {<br />
  153. bHStatus |= BIT1;<br />
  154. }<br />
  155. <br />
  156. // Read status of H3&nbsp;<br />
  157. if (H3_PORT &amp; H3_PIN)<br />
  158. { <br />
  159. bHStatus |= BIT0;<br />
  160. }<br />
  161. <br />
  162. bHallStartStep = bHallSteps[1][bHStatus];//得到换相步序<br />
  163. <br />
  164. if (bHallStartStep == 7)//不该出现的HALL状态<br />
  165. { <br />
  166. return;<br />
  167. }<br />
  168. <br />
  169. <br />
  170. GPIOD-&gt;CR2|=BIT3|BIT2|BIT4;<br />
  171. EXTI-&gt;CR1|=BIT6|BIT7;<br />
  172. Commutation(bHallStartStep,outpwm);//输出PWM信号,启动电机<br />
  173. }<br />
  174. <br />
  175. @far @interrupt void EXTI_PORTD_IRQHandler(void)<br />
  176. {<br />
  177. /* in order to detect unexpected events during development,&nbsp;<br />
  178. &nbsp; it is recommended to set a breakpoint on the following instruction<br />
  179. */<br />
  180. u8 bHStatus =0,i,bHallStartStep;<br />
  181. <br />
  182. bHStatus=(GPIO_ReadInputData(GPIOD)&gt;&gt;2)&amp;0x07;<br />
  183. for(i=0;i&lt;50;i++);//滤波时间<br />
  184. i=(GPIO_ReadInputData(GPIOD)&gt;&gt;2)&amp;0x07;<br />
  185. if(bHStatus!=i)return;<br />
  186. <br />
  187. bHStatus=0; <br />
  188. // Read status of H1<br />
  189. if (H1_PORT &amp; H1_PIN)<br />
  190. {<br />
  191. bHStatus |= BIT2;<br />
  192. }<br />
  193. <br />
  194. // Read status of H2&nbsp;<br />
  195. if (H2_PORT &amp; H2_PIN)<br />
  196. {<br />
  197. bHStatus |= BIT1;<br />
  198. }<br />
  199. <br />
  200. // Read status of H3&nbsp;<br />
  201. if (H3_PORT &amp; H3_PIN)<br />
  202. {<br />
  203. bHStatus |= BIT0;<br />
  204. }<br />
  205. <br />
  206. bHallStartStep = bHallSteps[1][bHStatus];//得到换相步序<br />
  207. <br />
  208. if (bHallStartStep == 7)<br />
  209. { //故障,停止输出<br />
  210. TIM1-&gt;BKR &amp;= (uint8_t)(~TIM1_BKR_MOE);//禁止PWM输出<br />
  211. PWM_A_OFF;<br />
  212. PWM_B_OFF;<br />
  213. PWM_C_OFF;<br />
  214. return;<br />
  215. }<br />
  216. <br />
  217. <br />
  218. EXTI-&gt;CR1|=BIT6|BIT7;<br />
  219. Commutation(bHallStartStep,outpwm);//输出PWM信号,启动电机<br />
  220. return;<br />
  221. }<br />
  222. <br />
  223. void ChongD(void)<br />
  224. { <br />
  225. &nbsp; unsigned int tem_c=0;<br />
  226. PWM_A_ON;PWM_B_ON;PWM_C_ON;<br />
  227. for(tem_c=0;tem_c&lt;50000;tem_c++);<br />
  228. PWM_A_OFF;PWM_B_OFF;PWM_C_OFF;<br />
  229. }<br />
  230. <br />
  231. main()<br />
  232. {<br />
  233. &nbsp; unsigned int tem_c=0;<br />
  234. unsigned char step=0;<br />
  235. <br />
  236. for(tem_c=0;tem_c&lt;50000;tem_c++);//上电延时,等待系统稳定<br />
  237. <br />
  238. Clock_init();//指示灯端口初始化<br />
  239. GPIO_int();//时钟配置<br />
  240. <br />
  241. PWM_IO_init();//开关管控制端口初始化<br />
  242. Tim1_init();//高级定时器配置 <br />
  243. HALLEXTI_INIT();//HALL端口初始化配置<br />
  244. ChongD();<br />
  245. outpwm=hArrPwmVal*PWMOUT/100;//固定占空比的PWM值<br />
  246. HALL_START();//根据HALL状态输出PWM状态,启动电机运行<br />
  247. enableInterrupts();<br />
  248. <br />
  249. while (1);<br />
  250. {<br />
  251. for(tem_c=0;tem_c&lt;50000;tem_c++);//延时时间<br />
  252. GPIO_WriteReverse(GPIOD,GPIO_PIN_7);//PD7指示灯反转<br />
  253. }<br />
  254. }<br />
  255. <br />
  256. //换向输出PWM值,<br />
  257. //bHallStartStep:当前换相步序0-5,OutPwmValue 输出PWM值<br />
  258. void Commutation(unsigned char bHallStartStep,unsigned int OutPwmValue)<br />
  259. { <br />
  260. &nbsp; &nbsp;TIM1-&gt;BKR &amp;= (uint8_t)(~TIM1_BKR_MOE);//禁止PWM输出<br />
  261. if(bHallStartStep!=3&amp;&amp;bHallStartStep!=4)<br />
  262. PWM_A_OFF;<br />
  263. if(bHallStartStep!=0&amp;&amp;bHallStartStep!=5)<br />
  264. PWM_B_OFF;<br />
  265. if(bHallStartStep!=1&amp;&amp;bHallStartStep!=2)<br />
  266. PWM_C_OFF;<br />
  267. &nbsp;<br />
  268. //根据换相步序,打开不同的开关管,并施加正确的PWM信号<br />
  269. if(bHallStartStep==0)//AB<br />
  270. {<br />
  271. TIM1-&gt;CCR3H = (uint8_t)(OutPwmValue &gt;&gt; 8);<br />
  272. &nbsp; &nbsp; TIM1-&gt;CCR3L = (uint8_t)(OutPwmValue);<br />
  273. PWM_B_ON;<br />
  274. }<br />
  275. &nbsp; else if(bHallStartStep==1) //AC<br />
  276. {<br />
  277. TIM1-&gt;CCR3H = (uint8_t)(OutPwmValue &gt;&gt; 8);<br />
  278. &nbsp; &nbsp; TIM1-&gt;CCR3L = (uint8_t)(OutPwmValue);<br />
  279. &nbsp;PWM_C_ON;<br />
  280. }<br />
  281. else if(bHallStartStep==2) //BC<br />
  282. {<br />
  283. TIM1-&gt;CCR2H = (uint8_t)(OutPwmValue &gt;&gt; 8);<br />
  284. &nbsp; &nbsp; TIM1-&gt;CCR2L = (uint8_t)(OutPwmValue);<br />
  285. &nbsp;PWM_C_ON;<br />
  286. }<br />
  287. else if(bHallStartStep==3) //BA<br />
  288. {<br />
  289. TIM1-&gt;CCR2H = (uint8_t)(OutPwmValue &gt;&gt; 8);<br />
  290. &nbsp; &nbsp; TIM1-&gt;CCR2L = (uint8_t)(OutPwmValue);<br />
  291. &nbsp;PWM_A_ON;<br />
  292. }<br />
  293. else if(bHallStartStep==4)//CA<br />
  294. {<br />
  295. TIM1-&gt;CCR1H = (uint8_t)(OutPwmValue &gt;&gt; 8);<br />
  296. &nbsp; &nbsp; TIM1-&gt;CCR1L = (uint8_t)(OutPwmValue);<br />
  297. &nbsp;PWM_A_ON;<br />
  298. }<br />
  299. else if(bHallStartStep==5) //CB<br />
  300. {<br />
  301. TIM1-&gt;CCR1H = (uint8_t)(OutPwmValue &gt;&gt; 8);<br />
  302. &nbsp; &nbsp; TIM1-&gt;CCR1L = (uint8_t)(OutPwmValue);<br />
  303. &nbsp;PWM_B_ON;<br />
  304. }<br />
  305. <br />
  306. TIM1-&gt;CCER1=PWM_EN1_TAB[bHallStartStep];<br />
  307. TIM1-&gt;CCER2=PWM_EN2_TAB[bHallStartStep]; <br />
  308. TIM1-&gt;BKR|=TIM1_BKR_MOE;//使能PWM输出<br />
  309. }<br />
  310. <br />
  311. #ifdef USE_FULL_ASSERT<br />
  312. <br />
  313. /**<br />
  314. &nbsp; * @brief &nbsp;Reports the name of the source file and the source line number<br />
  315. &nbsp; * &nbsp; where the assert_param error has occurred.<br />
  316. &nbsp; * @param file: pointer to the source file name<br />
  317. &nbsp; * @param line: assert_param error line source number<br />
  318. &nbsp; * @retval : None<br />
  319. &nbsp; */<br />
  320. void assert_failed(u8* file, u32 line)<br />
  321. {&nbsp;<br />
  322. &nbsp; /* User can add his own implementation to report the file name and line number,<br />
  323. &nbsp; &nbsp; &nbsp;ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */<br />
  324. <br />
  325. &nbsp; /* Infinite loop */<br />
  326. &nbsp; while (1)<br />
  327. &nbsp; {<br />
  328. &nbsp; }<br />
  329. }<br />
  330. #endif
复制代码



程序控制三相120°无刷直流电机,芯片为stm8s207rb其中有六状态,本应该是下面六种相序
UH WL           UH VL
WH  VL         WH UL
VH UL          VH  WL
用PD口外部中断捕获霍尔信号,却出现
UH VL WL       UH UL WL   
WH WL UL      WH WL UL
VH VL UL       VH VL WL 
写的程序出现了上下桥同时导通的情况
请问各位大神,这个该怎么解决?????

专注,坚持
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

13

主题

611

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1929
金钱
1929
注册时间
2014-10-6
在线时间
625 小时
发表于 2015-7-9 13:04:18 | 显示全部楼层
出现上下桥同时开的情况,这是绝以对不充许的,说明你的换相逻辑不对了,你把换相次秩排查一下!
回复

使用道具 举报

2

主题

3

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2015-4-21
在线时间
0 小时
 楼主| 发表于 2015-7-9 15:16:21 | 显示全部楼层
回复【2楼】烟酒不沾:
---------------------------------
你确定我的外部中断采集霍尔信号的程序没问题?
专注,坚持
回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-22 19:29

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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