pwm.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "pwm.h"
  2. #include "led.h"
  3. #include "All_define.h"
  4. #include "gd32f30x.h"
  5. /**
  6. \brief configure the TIMER peripheral
  7. \param[in] none
  8. \param[out] none
  9. \retval none
  10. */
  11. void timer0_config(void)
  12. {
  13. timer_parameter_struct timer_initpara;
  14. timer_oc_parameter_struct timer_ocintpara;
  15. timer_break_parameter_struct timer_breakpara;
  16. rcu_periph_clock_enable(RCU_TIMER0);
  17. rcu_periph_clock_enable(RCU_GPIOA);
  18. rcu_periph_clock_enable(RCU_AF);
  19. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  20. timer_deinit(TIMER0);
  21. /* TIMER0 configuration */
  22. timer_initpara.prescaler = 119;
  23. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  24. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  25. timer_initpara.period = 999;
  26. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  27. timer_initpara.repetitioncounter = 0;
  28. timer_init(TIMER0,&timer_initpara);
  29. /* CH0,CH1 and CH2 configuration in PWM mode0 */
  30. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  31. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  32. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  33. /* CH0 configuration in PWM mode0,duty cycle 25% */
  34. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
  35. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  36. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  37. /* automatic output enable, break, dead time and lock configuration*/
  38. /* timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
  39. timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE ;
  40. timer_breakpara.deadtime = 164;
  41. timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
  42. timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
  43. timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
  44. timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
  45. timer_break_config(TIMER0,&timer_breakpara);*/
  46. /* TIMER0 primary output function enable */
  47. timer_primary_output_config(TIMER0,ENABLE);
  48. /* TIMER0 counter enable */
  49. timer_enable(TIMER0);
  50. }
  51. void timer2_config()//PB0,1 PA 7
  52. {
  53. timer_parameter_struct timer_initpara;
  54. timer_oc_parameter_struct timer_ocintpara;
  55. rcu_periph_clock_enable(RCU_TIMER2);
  56. rcu_periph_clock_enable(RCU_GPIOA);
  57. rcu_periph_clock_enable(RCU_GPIOB);
  58. rcu_periph_clock_enable(RCU_AF);
  59. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  60. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
  61. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
  62. timer_deinit(TIMER2);
  63. /* TIMER2 configuration */
  64. timer_initpara.prescaler = 119;
  65. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  66. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  67. timer_initpara.period = 999;
  68. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  69. timer_initpara.repetitioncounter = 0;
  70. timer_init(TIMER2,&timer_initpara);
  71. /* CH0,CH1 and CH2 configuration in PWM mode0 */
  72. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  73. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  74. timer_channel_output_config(TIMER2,TIMER_CH_1,&timer_ocintpara);
  75. timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);
  76. timer_channel_output_config(TIMER2,TIMER_CH_3,&timer_ocintpara);
  77. timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_1, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
  78. timer_channel_output_mode_config(TIMER2,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  79. timer_channel_output_shadow_config(TIMER2,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  80. timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
  81. timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  82. timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  83. timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
  84. timer_channel_output_mode_config(TIMER2,TIMER_CH_3,TIMER_OC_MODE_PWM0);
  85. timer_channel_output_shadow_config(TIMER2,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
  86. /* auto-reload preload enable */
  87. timer_auto_reload_shadow_enable(TIMER2);
  88. /* auto-reload preload enable */
  89. timer_enable(TIMER2);
  90. }
  91. void timer4_config()//PA0
  92. {
  93. timer_parameter_struct timer_initpara;
  94. timer_oc_parameter_struct timer_ocintpara;
  95. rcu_periph_clock_enable(RCU_TIMER4);
  96. rcu_periph_clock_enable(RCU_GPIOA);
  97. rcu_periph_clock_enable(RCU_AF);
  98. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
  99. timer_deinit(TIMER4);
  100. timer_initpara.prescaler = 119;
  101. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  102. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  103. timer_initpara.period = 999;
  104. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  105. timer_initpara.repetitioncounter = 0;
  106. timer_init(TIMER4,&timer_initpara);
  107. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  108. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  109. timer_channel_output_config(TIMER4,TIMER_CH_0,&timer_ocintpara);
  110. timer_channel_output_pulse_value_config(TIMER4,TIMER_CH_0, 300);
  111. timer_channel_output_mode_config(TIMER4,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  112. timer_channel_output_shadow_config(TIMER4,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  113. timer_auto_reload_shadow_enable(TIMER4);
  114. timer_enable(TIMER4);
  115. }
  116. void timer7_config()//PC6 7 8 9
  117. {
  118. timer_parameter_struct timer_initpara;
  119. timer_oc_parameter_struct timer_ocintpara;
  120. rcu_periph_clock_enable(RCU_TIMER7);
  121. rcu_periph_clock_enable(RCU_GPIOC);
  122. rcu_periph_clock_enable(RCU_AF);
  123. gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  124. gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  125. gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  126. gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  127. timer_deinit(TIMER7);
  128. /* TIMER4 configuration */
  129. timer_initpara.prescaler = 119;
  130. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  131. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  132. timer_initpara.period = 999;
  133. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  134. timer_initpara.repetitioncounter = 0;
  135. timer_init(TIMER7,&timer_initpara);
  136. /* CH0,CH1 and CH2 configuration in PWM mode0 */
  137. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  138. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  139. timer_channel_output_config(TIMER7,TIMER_CH_0,&timer_ocintpara);
  140. timer_channel_output_config(TIMER7,TIMER_CH_1,&timer_ocintpara);
  141. timer_channel_output_config(TIMER7,TIMER_CH_2,&timer_ocintpara);
  142. timer_channel_output_config(TIMER7,TIMER_CH_3,&timer_ocintpara);
  143. timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_0, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
  144. timer_channel_output_mode_config(TIMER7,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  145. timer_channel_output_shadow_config(TIMER7,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  146. timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_1, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
  147. timer_channel_output_mode_config(TIMER7,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  148. timer_channel_output_shadow_config(TIMER7,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  149. timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_2, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
  150. timer_channel_output_mode_config(TIMER7,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  151. timer_channel_output_shadow_config(TIMER7,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  152. timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_3, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
  153. timer_channel_output_mode_config(TIMER7,TIMER_CH_3,TIMER_OC_MODE_PWM0);
  154. timer_channel_output_shadow_config(TIMER7,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
  155. /* TIMER0 primary output function enable */
  156. timer_primary_output_config(TIMER7,ENABLE);
  157. /* auto-reload preload enable */
  158. timer_auto_reload_shadow_enable(TIMER7);
  159. /* auto-reload preload enable */
  160. timer_enable(TIMER7);
  161. }
  162. void BSP_PWMOUT_Init(void)
  163. {
  164. timer0_config();
  165. timer2_config();
  166. timer4_config();
  167. timer7_config();
  168. }