HW_PWM_ATY.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * @file HW_PWM_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar definition of PWM for users
  20. *
  21. * @version
  22. * - 1_01_220602 > ATY
  23. * -# Preliminary version, first Release
  24. * - Undone
  25. ********************************************************************************
  26. */
  27. #ifndef __HW_PWM_ATY_C
  28. #define __HW_PWM_ATY_C
  29. #include "HW_PWM_ATY.h"
  30. /******************************* For user *************************************/
  31. #if defined(__STC8G_ATY)
  32. /**
  33. * @brief set pulse period with specified duty with PCA
  34. * @param periodS pulse period, us
  35. * @param dutyS pulse duty, 0-100.0 @ %
  36. * @param channelS channel to start or to change param
  37. * @todo any freq with ITM1
  38. */
  39. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  40. {
  41. // P_SW1 |= 0x10; // PCA1 P36
  42. // P3M0 = 0x40; // P36 PP out
  43. CCON = 0x00;
  44. // PWM freq = SYS freq / n / 256
  45. if(periodS <= 10)
  46. CMOD = 0x08; // PCA clk = sys clk, 32/3us 93.75KHz @ 24MHz
  47. else if(periodS <= 21 && periodS > 10)
  48. CMOD = 0x02; // PCA clk = sys clk / 2,
  49. else if(periodS <= 42 && periodS > 21)
  50. CMOD = 0x0A; // PCA clk = sys clk / 4
  51. else if(periodS <= 64 && periodS > 42)
  52. CMOD = 0x0C; // PCA clk = sys clk / 6
  53. else if(periodS <= 85 && periodS > 64)
  54. CMOD = 0x0E; // PCA clk = sys clk / 8
  55. else if(periodS > 85)//periodS <= 128 && periodS > 85)
  56. CMOD = 0x00; // PCA clk = sys clk / 12
  57. else
  58. CMOD = 0x04; // PCA clk = TIM1 IT
  59. CL = 0x00; // init counter value
  60. CH = 0x00;
  61. if(channelS == 1)
  62. {
  63. CCAPM1 = 0x42; // PCA at PWM mode
  64. // PCA_PWM1 = 0x80; // 6bit PWM
  65. // PCA_PWM1 = 0x40; // 7bit PWM
  66. PCA_PWM1 = 0x00; // 8bit PWM
  67. // PCA_PWM1 = 0xC0; // 10bit PWM
  68. CCAP1L = (uint16_t)(0xFFFF * (float)((100 - dutyS) / 100.0));
  69. CCAP1H = (uint16_t)(0xFFFF * (float)((100 - dutyS) / 100.0)) >> 8;
  70. }
  71. CR = 1; // start PCA
  72. }
  73. /**
  74. * @brief stop PWM output with specified level with PCA
  75. * @param level IO level after stop PWM
  76. * @note voltage lower than write pin
  77. */
  78. void PWM_Stop(uint8_t level)
  79. {
  80. if(level)
  81. {
  82. PCA_PWM1 &= 0xC0;
  83. CCAP1H = 0x00;
  84. }
  85. else
  86. {
  87. PCA_PWM1 |= 0x3F;
  88. CCAP1H = 0xFF;
  89. }
  90. CR = 0;
  91. }
  92. /**
  93. * @brief set pulse period with specified duty with PCA
  94. * @param periodS pulse period, us
  95. * @param dutyS pulse duty, 0-100.0 @ %
  96. * @param channelS channel to start or to change param
  97. * @todo any freq with ITM1
  98. */
  99. void PWM_StartPulse(uint32_t periodS, uint8_t channelS)
  100. {
  101. CCON = 0x00;
  102. CMOD = 0x08; // PCA clk = sys clk, 32/3us 93.75KHz @ 24MHz
  103. CL = 0x00; // init counter value
  104. CH = 0x00;
  105. if(channelS == 1)
  106. {
  107. CCAPM1 = 0x4D; // PCA at fast pulse mode, enbale IT
  108. CCAP1L = periodS;
  109. CCAP1H = periodS >> 8;
  110. }
  111. CR = 1; // start PCA
  112. EA = 1;
  113. }
  114. void PCA_Isr() interrupt 7
  115. {
  116. CCF1 = 0;
  117. CL = 0x00;
  118. CH = 0x00;
  119. }
  120. #elif defined(__STC51_ATY)
  121. /**
  122. * @brief set pulse period with specified duty
  123. * @param periodS pulse period, us
  124. * @param dutyS pulse duty, 0-100.0 @ %
  125. * @param channelS channel to start or to change param
  126. * @note Undone
  127. */
  128. void PWM_Start(uint32_t periodS, float dutyS, uint8_t channelS)
  129. {
  130. if(channelS == 2)
  131. {
  132. P_SW2 |= 0x80;
  133. PWMA_CCER1 = 0x00;
  134. PWMA_CCMR2 = 0x60;
  135. PWMA_CCER1 = 0x50;
  136. PWMA_CCR2 = dutyS / 100.0 * periodS;
  137. PWMA_ARR = periodS;
  138. PWMA_ENO = 0x08;
  139. PWMA_PS = (PWMA_PS & 0xF3) | 0x04; // PWM2N_2 TMC_STEP
  140. PWMA_BKR = 0x80;
  141. PWMA_CR1 = 0x01;
  142. P_SW2 &= 0x7F;
  143. }
  144. else if(channelS == 3)
  145. {
  146. P_SW2 |= 0x80;
  147. PWMA_CCER2 = 0x00;
  148. PWMA_CCMR3 = 0x60;
  149. PWMA_CCER2 = 0x05;
  150. PWMA_CCR3 = dutyS / 100.0 * periodS;
  151. PWMA_ARR = periodS;
  152. PWMA_ENO = 0x20;
  153. PWMA_PS = (PWMA_PS & 0xCF) | 0x10; // PWM3N_2 Heat
  154. PWMA_BKR = 0x80;
  155. PWMA_CR1 = 0x01;
  156. P_SW2 &= 0x7F;
  157. }
  158. else if(channelS == 6)
  159. {
  160. P_SW2 |= 0x80;
  161. PWMB_CCER1 = 0x00; // Set 0 of CCERx before write CCMRx
  162. PWMB_CCMR2 = 0x60; // Set CC6 PWMB output mode
  163. PWMB_CCER1 = 0x10; // Enable CC6 channel
  164. PWMB_CCR6 = dutyS / 100.0 * periodS; // Set dutys time
  165. PWMB_ARR = periodS; // Set period time
  166. PWMB_ENO = 0x04; // Enable PWM6P output
  167. PWMB_PS = (PWMB_PS & 0xF3) | 0x04; // PWM6_2 DAC_PWM P54
  168. PWMB_BKR = 0x80; // Enable main output
  169. PWMB_CR1 = 0x01; // Start counter
  170. P_SW2 &= 0x7F;
  171. }
  172. else if(channelS == 8)
  173. {
  174. P_SW2 |= 0x80;
  175. PWMB_CCER1 = 0x00; // Set 0 of CCERx before write CCMRx
  176. PWMB_CCMR4 = 0x60; // Set CC8 PWMB output mode
  177. PWMB_CCER2 = 0x10; // Enable CC8 channel
  178. PWMB_CCR8 = dutyS / 100.0 * periodS; // Set dutys time
  179. PWMB_ARR = periodS; // Set period time
  180. PWMB_ENO = 0x40; // Enable PWM8P output
  181. PWMB_PS = (PWMB_PS & 0xF3) | 0x04; // PWM8 23
  182. PWMB_BKR = 0x80; // Enable main output
  183. PWMB_CR1 = 0x01; // Start counter
  184. P_SW2 &= 0x7F;
  185. }
  186. }
  187. /**
  188. * @brief stop PWM output with specified level
  189. * @param level IO level after stop PWM
  190. * @param channelS channel to start or to change param
  191. * @note Undone, not real stop
  192. */
  193. void PWM_Stop(uint8_t level, uint8_t channelS)
  194. {
  195. PWM_Start(1000, level * 1000, channelS);
  196. }
  197. #elif defined(__STM32_HAL_ATY)
  198. #include "tim.h"
  199. /**
  200. * @brief set pulse period with specified duty
  201. * @param periodS pulse period, us
  202. * @param dutyS pulse duty, 0-100.0 @ %
  203. * @param channelS channel to start or to change param
  204. * @note Undone
  205. */
  206. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  207. {
  208. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
  209. TIM1->CNT = 0;
  210. HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
  211. TIM1->ARR = periodS;
  212. TIM1->CCR4 = dutyS;
  213. TIM1->EGR = TIM_EGR_UG;
  214. }
  215. /**
  216. * @brief stop PWM output with specified level
  217. * @param level IO level after stop PWM
  218. * @note Undone
  219. */
  220. void PWM_Stop(uint8_t level)
  221. {
  222. if(level)
  223. {
  224. TIM1->ARR = 0;
  225. TIM1->CCR4 = 0;
  226. TIM1->EGR = TIM_EGR_UG;
  227. }
  228. else
  229. {
  230. TIM1->ARR = 0;
  231. TIM1->CCR4 = 1;
  232. TIM1->EGR = TIM_EGR_UG;
  233. }
  234. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
  235. }
  236. #elif defined(__ESP8266_RTOS_ATY)
  237. #include "driver/pwm.h"
  238. #define PWM_CHANNEL_FAN 0
  239. #define PWM_CHANNEL_COLD 1
  240. #define PWM_CHANNEL_WARM 2
  241. const uint32_t* channelPin = {4, 2, 15};
  242. uint8_t* dutiesPercent = {50, 50, 50};
  243. /**
  244. * @see https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/api-reference/peripherals/pwm.html
  245. *
  246. */
  247. void PWM_Init(void)
  248. {
  249. uint32_t period = 1000;
  250. uint32_t duties[3];
  251. duties[0] = dutiesPercent[0] / 100.0 * period;
  252. duties[1] = dutiesPercent[1] / 100.0 * period;
  253. duties[2] = dutiesPercent[2] / 100.0 * period;
  254. pwm_init(period, duties, 3, channelPin);
  255. }
  256. /**
  257. * @brief set pulse period with specified duty
  258. * @param periodS pulse period, us
  259. * @param dutyS pulse duty, 0-100.0 @ %
  260. * @param channelS channel to start or to change param
  261. * @todo not tested
  262. */
  263. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  264. {
  265. extern pwm_obj_t* pwm_obj;
  266. if(periodS = !pwm_obj->period)
  267. pwm_set_period(periodS);
  268. pwm_set_duty(channelS, (uint32_t)((float)(dutyS / 100.0) * periodS));
  269. // pwm_set_period_duties(period, allDuties);
  270. pwm_start();
  271. }
  272. /**
  273. * @brief stop PWM output with specified level
  274. * @param level IO level after stop PWM
  275. * @todo not tested
  276. */
  277. void PWM_Stop(uint8_t level)
  278. {
  279. if(level)
  280. pwm_stop(0x00);
  281. else
  282. pwm_stop(0xFF);
  283. pwm_deinit();
  284. }
  285. #endif /* PLATFORM */
  286. #ifdef __DEBUG_HW_PWM_ATY
  287. void PWM_Test(uint8_t testType)
  288. {
  289. if(testType == 11)
  290. {
  291. static dutyCount = 0;
  292. dutyCount++;
  293. UartSendStr("\r\n");
  294. UartSendByte(dutyCount / 100 + '0');
  295. UartSendByte(((uint8_t)dutyCount % 100) / 10 + '0');
  296. UartSendByte((uint8_t)dutyCount % 10 + '0');
  297. UartSendStr("\r\n");
  298. if(dutyCount <= 100)
  299. PWM_Start(10, dutyCount, 1);
  300. else if(dutyCount > 100 && dutyCount < 200)
  301. PWM_Start(10, 200 - dutyCount, 1);
  302. else
  303. dutyCount = 0;
  304. }
  305. else if(testType == 21)
  306. {
  307. static uint8_t tempa = 10;
  308. PWM_Start(tempa, 50, 1);
  309. if(tempa == 10)
  310. tempa = 21;
  311. else if(tempa == 21)
  312. tempa = 42;
  313. else if(tempa == 42)
  314. tempa = 64;
  315. else if(tempa == 64)
  316. tempa = 85;
  317. else if(tempa == 85)
  318. tempa = 128;
  319. else
  320. tempa = 10;
  321. }
  322. else if(testType == 31)
  323. {
  324. static uint8_t stopLevel = 0;
  325. PWM_Start(10, 50, 1);
  326. stopLevel = !stopLevel;
  327. UartSendByte(stopLevel + '0');
  328. PWM_Stop(stopLevel);
  329. // P36 = stopLevel;
  330. }
  331. else if(testType == 22)
  332. {
  333. static uint32_t freqCount = 0xFFFF;
  334. P37 = 0;
  335. P35 = 0;
  336. if(freqCount > 100)
  337. freqCount -= 100;
  338. else if(freqCount > 0 && freqCount <= 100)
  339. freqCount -= 1;
  340. else if(freqCount == 0)
  341. freqCount = 0xFFFF;
  342. PWM_StartPulse(freqCount, 1);
  343. UartSendStr("\r\n");
  344. UartSendByte(freqCount / 10000 + '0');
  345. UartSendByte(((uint16_t)freqCount % 10000) / 1000 + '0');
  346. UartSendByte(((uint16_t)freqCount % 1000) / 100 + '0');
  347. UartSendByte(((uint16_t)freqCount % 100) / 10 + '0');
  348. UartSendByte((uint16_t)freqCount % 10 + '0');
  349. }
  350. }
  351. #endif /* __DEBUG_HW_PWM_ATY */
  352. /******************************************************************************/
  353. /**
  354. * @brief set pulse frequence with half dutycycle
  355. * @param periodS pulse frequence
  356. */
  357. void PwmFreqSet(uint32_t periodS, uint8_t channelS)
  358. {
  359. PWM_Start(periodS, 50, channelS);
  360. }
  361. // /**
  362. // * @brief set pulse frequence with half period
  363. // * @param periodS pulse frequence
  364. // */
  365. // void PwmFreqSet(uint32_t periodS, uint8_t channelS)
  366. // {
  367. // PWM_Start(periodS, periodS / 2, channelS);
  368. // }
  369. #endif /* __HW_PWM_ATY_C */
  370. /******************************** End Of File *********************************/