HW_PWM_ATY.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. }
  173. /**
  174. * @brief stop PWM output with specified level
  175. * @param level IO level after stop PWM
  176. * @param channelS channel to start or to change param
  177. * @note Undone, not real stop
  178. */
  179. void PWM_Stop(uint8_t level, uint8_t channelS)
  180. {
  181. PWM_Start(1000, level * 1000, channelS);
  182. }
  183. #elif defined(__STM32_HAL_ATY)
  184. #include "tim.h"
  185. /**
  186. * @brief set pulse period with specified duty
  187. * @param periodS pulse period, us
  188. * @param dutyS pulse duty, 0-100.0 @ %
  189. * @param channelS channel to start or to change param
  190. * @note Undone
  191. */
  192. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  193. {
  194. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
  195. TIM1->CNT = 0;
  196. HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
  197. TIM1->ARR = periodS;
  198. TIM1->CCR4 = dutyS;
  199. TIM1->EGR = TIM_EGR_UG;
  200. }
  201. /**
  202. * @brief stop PWM output with specified level
  203. * @param level IO level after stop PWM
  204. * @note Undone
  205. */
  206. void PWM_Stop(uint8_t level)
  207. {
  208. if(level)
  209. {
  210. TIM1->ARR = 0;
  211. TIM1->CCR4 = 0;
  212. TIM1->EGR = TIM_EGR_UG;
  213. }
  214. else
  215. {
  216. TIM1->ARR = 0;
  217. TIM1->CCR4 = 1;
  218. TIM1->EGR = TIM_EGR_UG;
  219. }
  220. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
  221. }
  222. #elif defined(__ESP8266_RTOS_ATY)
  223. #include "driver/pwm.h"
  224. #define PWM_CHANNEL_FAN 0
  225. #define PWM_CHANNEL_COLD 1
  226. #define PWM_CHANNEL_WARM 2
  227. const uint32_t* channelPin = {4, 2, 15};
  228. uint8_t* dutiesPercent = {50, 50, 50};
  229. /**
  230. * @see https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/api-reference/peripherals/pwm.html
  231. *
  232. */
  233. void PWM_Init(void)
  234. {
  235. uint32_t period = 1000;
  236. uint32_t duties[3];
  237. duties[0] = dutiesPercent[0] / 100.0 * period;
  238. duties[1] = dutiesPercent[1] / 100.0 * period;
  239. duties[2] = dutiesPercent[2] / 100.0 * period;
  240. pwm_init(period, duties, 3, channelPin);
  241. }
  242. /**
  243. * @brief set pulse period with specified duty
  244. * @param periodS pulse period, us
  245. * @param dutyS pulse duty, 0-100.0 @ %
  246. * @param channelS channel to start or to change param
  247. * @todo not tested
  248. */
  249. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  250. {
  251. extern pwm_obj_t* pwm_obj;
  252. if(periodS = !pwm_obj->period)
  253. pwm_set_period(periodS);
  254. pwm_set_duty(channelS, (uint32_t)((float)(dutyS / 100.0) * periodS));
  255. // pwm_set_period_duties(period, allDuties);
  256. pwm_start();
  257. }
  258. /**
  259. * @brief stop PWM output with specified level
  260. * @param level IO level after stop PWM
  261. * @todo not tested
  262. */
  263. void PWM_Stop(uint8_t level)
  264. {
  265. if(level)
  266. pwm_stop(0x00);
  267. else
  268. pwm_stop(0xFF);
  269. pwm_deinit();
  270. }
  271. #endif /* PLATFORM */
  272. #ifdef __DEBUG_HW_PWM_ATY
  273. void PWM_Test(uint8_t testType)
  274. {
  275. if(testType == 11)
  276. {
  277. static dutyCount = 0;
  278. dutyCount++;
  279. UartSendStr("\r\n");
  280. UartSendByte(dutyCount / 100 + '0');
  281. UartSendByte(((uint8_t)dutyCount % 100) / 10 + '0');
  282. UartSendByte((uint8_t)dutyCount % 10 + '0');
  283. UartSendStr("\r\n");
  284. if(dutyCount <= 100)
  285. PWM_Start(10, dutyCount, 1);
  286. else if(dutyCount > 100 && dutyCount < 200)
  287. PWM_Start(10, 200 - dutyCount, 1);
  288. else
  289. dutyCount = 0;
  290. }
  291. else if(testType == 21)
  292. {
  293. static uint8_t tempa = 10;
  294. PWM_Start(tempa, 50, 1);
  295. if(tempa == 10)
  296. tempa = 21;
  297. else if(tempa == 21)
  298. tempa = 42;
  299. else if(tempa == 42)
  300. tempa = 64;
  301. else if(tempa == 64)
  302. tempa = 85;
  303. else if(tempa == 85)
  304. tempa = 128;
  305. else
  306. tempa = 10;
  307. }
  308. else if(testType == 31)
  309. {
  310. static uint8_t stopLevel = 0;
  311. PWM_Start(10, 50, 1);
  312. stopLevel = !stopLevel;
  313. UartSendByte(stopLevel + '0');
  314. PWM_Stop(stopLevel);
  315. // P36 = stopLevel;
  316. }
  317. else if(testType == 22)
  318. {
  319. static uint32_t freqCount = 0xFFFF;
  320. P37 = 0;
  321. P35 = 0;
  322. if(freqCount > 100)
  323. freqCount -= 100;
  324. else if(freqCount > 0 && freqCount <= 100)
  325. freqCount -= 1;
  326. else if(freqCount == 0)
  327. freqCount = 0xFFFF;
  328. PWM_StartPulse(freqCount, 1);
  329. UartSendStr("\r\n");
  330. UartSendByte(freqCount / 10000 + '0');
  331. UartSendByte(((uint16_t)freqCount % 10000) / 1000 + '0');
  332. UartSendByte(((uint16_t)freqCount % 1000) / 100 + '0');
  333. UartSendByte(((uint16_t)freqCount % 100) / 10 + '0');
  334. UartSendByte((uint16_t)freqCount % 10 + '0');
  335. }
  336. }
  337. #endif /* __DEBUG_HW_PWM_ATY */
  338. /******************************************************************************/
  339. /**
  340. * @brief set pulse frequence with half dutycycle
  341. * @param periodS pulse frequence
  342. */
  343. void PwmFreqSet(uint32_t periodS, uint8_t channelS)
  344. {
  345. PWM_Start(periodS, 50, channelS);
  346. }
  347. // /**
  348. // * @brief set pulse frequence with half period
  349. // * @param periodS pulse frequence
  350. // */
  351. // void PwmFreqSet(uint32_t periodS, uint8_t channelS)
  352. // {
  353. // PWM_Start(periodS, periodS / 2, channelS);
  354. // }
  355. #endif /* __HW_PWM_ATY_C */
  356. /******************************** End Of File *********************************/