pwm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("AutoChips Software") are
  4. * protected under relevant copyright laws. The information contained herein is
  5. * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
  6. * the prior written permission of AutoChips inc. and/or its licensors, any
  7. * reproduction, modification, use or disclosure of AutoChips Software, and
  8. * information contained herein, in whole or in part, shall be strictly
  9. * prohibited.
  10. *
  11. * AutoChips Inc. (C) 2016. All rights reserved.
  12. *
  13. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  14. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
  15. * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
  16. * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
  17. * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  19. * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
  20. * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
  21. * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
  22. * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
  23. * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
  24. * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
  25. * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
  26. * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  27. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
  28. * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
  29. * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
  30. * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
  31. * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
  32. */
  33. /**********<Incldue>**********/
  34. #include "ac780x_gpio.h"
  35. #include "ac780x.h"
  36. #include "ac780x_timer.h"
  37. #include "ac780x_pwm.h"
  38. #include "pwm.h"
  39. /**********<Prototype>*********/
  40. /**********<Variable>*********/
  41. uint32_t g_positvePulse;///<pulse width positive value
  42. uint32_t g_negativePulse;///<pulse width negative value
  43. uint32_t g_freq;///<pulse width period value
  44. float g_duty;///<pulse duty value
  45. uint32_t g_hallStatus;///<hall status value
  46. uint32_t g_overFlowCount;//PWM1计数溢出中断
  47. /**
  48. * PWM1_CallBack
  49. *
  50. * @param[in] device: device pointer
  51. * @param[in] wpara: callback parameter
  52. * @param[in] lpara: callback parameter
  53. * @return none
  54. * @brief PWM CallBack
  55. */
  56. void PWM1_CallBack(void *device, uint32_t wpara, uint32_t lpara)
  57. {
  58. //PWM计数器溢出中断
  59. if (wpara & PWM_INIT_CNTOF_Msk)
  60. {
  61. g_overFlowCount ++;
  62. //user add
  63. }
  64. //PWM通道中断
  65. //通过判断回调返回的lpara对应PWM_STR_CHnSF_Msk位状态判断是否发生通道中断
  66. if (lpara & PWM_STR_CHSF_Msk)
  67. {
  68. //user add
  69. //通道0中断
  70. if (lpara & PWM_STR_CH0SF_Msk)
  71. {
  72. }
  73. //通道1中断
  74. if (lpara & PWM_STR_CH1SF_Msk)
  75. {
  76. }
  77. }
  78. }
  79. /**
  80. *
  81. * @param[in] none
  82. *
  83. * @return none
  84. *
  85. * @brief PWM1 Init
  86. *
  87. */
  88. void Pwm1_Init(void)
  89. {
  90. PWM_ConfigType pwmConfig;
  91. PWM_ModulationConfigType initModeStruct;
  92. PWM_IndependentChConfig independentChConfig[2];
  93. memset(&pwmConfig, 0, sizeof(pwmConfig));
  94. memset(&initModeStruct, 0, sizeof(initModeStruct));
  95. //MSP_PWM_Init(PWM1);
  96. /* independent channel 2 configuration */
  97. independentChConfig[0].channel = PWM_CH_2;
  98. independentChConfig[0].chValue = 0;
  99. independentChConfig[0].levelMode = PWM_HIGH_TRUE;
  100. independentChConfig[0].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
  101. independentChConfig[0].interruptEn = DISABLE;
  102. independentChConfig[0].initLevel = PWM_LOW_LEVEL;
  103. independentChConfig[0].triggerEn = DISABLE;
  104. /* independent channel 3 configuration */
  105. independentChConfig[1].channel = PWM_CH_3;
  106. independentChConfig[1].chValue = 0; //MOD_PWM>>1;
  107. independentChConfig[1].levelMode = PWM_HIGH_TRUE;
  108. independentChConfig[1].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
  109. independentChConfig[1].interruptEn = DISABLE;
  110. independentChConfig[1].initLevel = PWM_LOW_LEVEL;
  111. independentChConfig[1].triggerEn = DISABLE;
  112. initModeStruct.countMode = PWM_UP_COUNT;
  113. initModeStruct.deadtime = 24;
  114. initModeStruct.deadtimePsc = PWM_DEADTIME_DIVID_1;
  115. initModeStruct.initChOutputEn = ENABLE;
  116. initModeStruct.initTriggerEn = DISABLE;
  117. initModeStruct.independentChannelNum = 2;
  118. initModeStruct.independentChConfig = independentChConfig;
  119. pwmConfig.mode = PWM_MODE_MODULATION;
  120. pwmConfig.clkSource = PWM_CLK_SOURCE_APB;
  121. pwmConfig.clkPsc = PWM_PRES;
  122. pwmConfig.initValue = 0;
  123. pwmConfig.maxValue = MOD_PWM - 1;
  124. pwmConfig.overflowInterrupEn = ENABLE;
  125. pwmConfig.cntOverflowFreq = 0;
  126. pwmConfig.interruptEn = DISABLE;
  127. pwmConfig.callBack = PWM1_CallBack; //PWM中断回调
  128. pwmConfig.initModeStruct = &initModeStruct;
  129. PWM_Init(PWM1, &pwmConfig);
  130. NVIC_SetPriority(PWM1_IRQn, 0); //设置PWM模块中断的优先级
  131. }
  132. //设置电机转运速度 , speed 取值 0--100;
  133. void Motor_Setspeed(int8_t speed)
  134. {
  135. uint16_t chValue=0;
  136. if(speed >= 0){ //电机正转, 开锁
  137. if(speed > 100) speed = 100;
  138. chValue = (uint16_t)(speed*1.0*MOD_PWM/100);
  139. PWM_SetChannelCountValue(PWM1, PWM_CH_3, 0);
  140. PWM_SetChannelCountValue(PWM1, PWM_CH_2, chValue);
  141. }else{//电机反转, 关锁
  142. speed = -speed;
  143. if(speed > 100) speed = 100;
  144. chValue = (uint16_t)(speed*1.0*MOD_PWM/100);
  145. PWM_SetChannelCountValue(PWM1, PWM_CH_2, 0);
  146. PWM_SetChannelCountValue(PWM1, PWM_CH_3, chValue);
  147. }
  148. }
  149. void Motor_Positive(int8_t speed)
  150. {
  151. uint16_t chValue=0;
  152. if(speed > 100) speed = 100;
  153. chValue = (uint16_t)(speed*1.0*MOD_PWM/100);
  154. PWM_SetChannelCountValue(PWM1, PWM_CH_2, chValue);
  155. }
  156. void Motor_Negative(int8_t speed)
  157. {
  158. uint16_t chValue=0;
  159. if(speed > 100) speed = 100;
  160. chValue = (uint16_t)(speed*1.0*MOD_PWM/100);
  161. PWM_SetChannelCountValue(PWM1, PWM_CH_3, chValue);
  162. }
  163. # if 0
  164. /**
  165. * PWM1_GenerateFrequency
  166. *
  167. * @param[in] none
  168. * @return none
  169. *
  170. * @brief
  171. * 1.PWM1_CH0,PWM1_CH1组合模式,PWM1_CH0输出频率为20K,占空比为25%的波形,PWM1_CH1与PWM1_CH0波形互补,带1us死区插入。
  172. * 2.PWM1_CH6,PWM1_CH7独立PWM模式(边沿对齐),生成频率为20K,PWM1_CH6占空比50%,PWM1_CH7占空比25%的波形。
  173. * 3.PWM计数器溢出中断,对输出PWM波进行计数
  174. *
  175. */
  176. void PWM1_GenerateFrequency(void)
  177. {
  178. PWM_CombineChConfig combineChConfig[1]; //组合模式相关结构体
  179. PWM_IndependentChConfig independentChConfig[2];//独立模式相关结构体
  180. PWM_ModulationConfigType pwmConfig; //PWM模式相关结构体
  181. PWM_ConfigType config; //PWM模块结构体
  182. //结构体数据清零
  183. memset(&combineChConfig, 0, sizeof(combineChConfig));
  184. memset(&independentChConfig, 0, sizeof(independentChConfig));
  185. memset(&pwmConfig, 0, sizeof(pwmConfig));
  186. memset(&config, 0, sizeof(config));
  187. /*通道0/1配成组合模式PWM输出*/
  188. /*
  189. 向上计数组合模式:
  190. 周期=(MCVR-CNTIN+1)*PWM计数器时钟周期
  191. 占空比=|CH(n+1)V-CH(n)V|*PWM计数器时钟周期
  192. 向上-向下计数组合模式:
  193. 周期=2*(MCVR-CNTIN)*PWM计数器时钟周期
  194. 占空比=2*(|CH(n+1)V-CH(n)V|)*PWM计数器时钟周期
  195. */
  196. combineChConfig[0].pairChannel = PWM_CH_0; //PWM通道对数,PWM_CH_0/2/4/6对应PAIR0/1/2/3
  197. /*
  198. 组合模式占空比由ch1stValue,ch2ndValue共同决定,pwmConfig.countMode配为PWM_UP_COUNT(计数器向上计数)时,占空比为:(ch1stValue-ch2ndValue)/(config.maxValue + 1)
  199. */
  200. combineChConfig[0].ch1stValue = MOD_PWM >> 2; //通道2n channel值,n为PWM对数编号
  201. combineChConfig[0].ch2ndValue = MOD_PWM >> 1; //通道2n+1 channel值,n为PWM对数编号
  202. combineChConfig[0].levelMode = PWM_HIGH_TRUE; //输出PWM高有效,如果占空比设为25%,是指的高有效电平占比25%
  203. combineChConfig[0].deadtimeEn = ENABLE;//死区插入使能,组合模式才支持死区插入
  204. combineChConfig[0].complementEn = ENABLE;//互补模式使能,使能后,PWM通道波形互补,DISABLE波形输出同向
  205. combineChConfig[0].ch1stMatchDir = PWM_MATCH_DIR_DOWN;//仅在向上-向下计数(countMode为PWM_UP_DOWN_COUNT)组合模式有效,用于选择匹配生效点方向
  206. combineChConfig[0].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;//仅在向上-向下计数(countMode为PWM_UP_DOWN_COUNT)组合模式有效,用于选择匹配生效点方向
  207. combineChConfig[0].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;//输出极性高有效,PWM mask后PWM输出低电平
  208. combineChConfig[0].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;//输出极性高有效,PWM mask后PWM输出低电平
  209. combineChConfig[0].ch1stInterruptEn = DISABLE;//PWM通道匹配中断使能位
  210. combineChConfig[0].ch2ndInterruptEn = DISABLE;//PWM通道匹配中断使能位
  211. combineChConfig[0].ch1stInitLevel = PWM_LOW_LEVEL;//PWM初始电平输出为低,该配置受initChOutputEn控制,决定PWM计数器未工作前PWM口的输出电平值。
  212. combineChConfig[0].ch2ndInitLevel = PWM_LOW_LEVEL;//PWM初始电平输出为低,该配置受initChOutputEn控制,决定PWM计数器未工作前PWM口的输出电平值。
  213. combineChConfig[0].ch1stTriggerEn = DISABLE;//通道2n外部触发使能,n为PWM对数编号
  214. combineChConfig[0].ch2ndTriggerEn = DISABLE;//通道2n+1外部触发使能,n为PWM对数编号
  215. /*
  216. 边沿对齐PWM模式:
  217. 周期=(MCVR-CNTIN+1)*PWM计数器时钟周期
  218. 占空比=(CHnV-CNTIN+1)*PWM计数器时钟周期
  219. 中心对齐PWM模式:
  220. 周期=2*(MCVR-CNTIN)*PWM计数器时钟周期
  221. 占空比=2*(CH(n)V-CNTIN)*PWM计数器时钟周期
  222. */
  223. /*channel 6*/
  224. independentChConfig[0].channel = PWM_CH_6; //通道6
  225. independentChConfig[0].chValue = MOD_PWM >> 1;//通道6 channel值,输出占空比 = chValue / (config.maxValue + 1) = 50%
  226. independentChConfig[0].levelMode = PWM_HIGH_TRUE; //输出PWM高有效
  227. independentChConfig[0].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH; //输出极性高有效,PWM mask后PWM输出低电平
  228. independentChConfig[0].interruptEn = DISABLE;//PWM通道匹配中断使能位
  229. independentChConfig[0].initLevel = PWM_LOW_LEVEL;//PWM初始电平输出为低,该配置受initChOutputEn控制,决定PWM计数器未工作前PWM口的输出电平值。
  230. independentChConfig[0].triggerEn = DISABLE;//通道外部触发使能
  231. /*channel 7*/
  232. independentChConfig[1].channel = PWM_CH_7; //通道7
  233. independentChConfig[1].chValue = MOD_PWM >> 2;//通道7 channel值,输出占空比 = chValue / (config.maxValue + 1) = 25%
  234. independentChConfig[1].levelMode = PWM_HIGH_TRUE; //输出PWM高有效
  235. independentChConfig[1].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH; //输出极性高有效,PWM mask后PWM输出低电平
  236. independentChConfig[1].interruptEn = DISABLE;//PWM通道匹配中断使能位
  237. independentChConfig[1].initLevel = PWM_LOW_LEVEL;//PWM初始电平输出为低,该配置受initChOutputEn控制,决定PWM计数器未工作前PWM口的输出电平值。
  238. independentChConfig[1].triggerEn = DISABLE;//通道外部触发使能
  239. /*modulation mode config*/
  240. pwmConfig.countMode = PWM_UP_COUNT; //PWM计数器模式 (不同的计数模式频率及占空比计算方式不同)
  241. pwmConfig.independentChannelNum = 2; //独立通道数
  242. pwmConfig.combineChannelNum = 1; //组合对数
  243. pwmConfig.independentChConfig = independentChConfig; //独立通道配置变量地址赋值
  244. pwmConfig.combineChConfig = combineChConfig; //组合通道配置变量地址赋值
  245. pwmConfig.deadtimePsc = PWM_DEADTIME_DIVID_1;//死区插入分频值,与deadtime一起决定插入死区的时间。
  246. pwmConfig.deadtime = 24; //死区时间 = (DTPSC * DTVAL)/PWM计数器时钟周期 = 1*24/24000000 = 1us
  247. pwmConfig.initChOutputEn = ENABLE; //使能初始化通道输出,使能后独立PWM模式的initLevel和组合PWM模式的ch1stInitLevel和ch2ndPolarity配置才会生效
  248. pwmConfig.initTriggerEn = DISABLE; //通道外部触发使能
  249. /*pwm config*/
  250. config.mode = PWM_MODE_MODULATION;//PWM模块配置为PWM模式
  251. config.initModeStruct = &pwmConfig;//PWM配置结构体地址赋值
  252. config.clkSource = PWM_CLK_SOURCE_APB; //PWM时钟源配置
  253. config.clkPsc = PWM_PRES;//PWM时钟源分频
  254. config.initValue = 0;//计数器初始寄存器值
  255. config.maxValue = MOD_PWM - 1; //PWM计数器最大值
  256. config.overflowInterrupEn = ENABLE;//计数器溢出中断使能
  257. config.cntOverflowFreq = 0;//CNTOF中断产生的频率与计数器频率的关系(0-127), 0表示每次计数器溢出都产生溢出中断,1表示间隔1次,2表示间隔2次,以此内推。
  258. config.interruptEn = ENABLE; //PWM中断使能
  259. config.callBack = PWM1_CallBack; //PWM中断回调
  260. PWM_Init(PWM1, &config); //配置初始化生效
  261. NVIC_SetPriority(PWM1_IRQn, 0); //设置PWM模块中断的优先级
  262. }
  263. #endif
  264. /**********<End>*********/