123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #include "pwm.h"
- #include "led.h"
- #include "All_define.h"
- #include "gd32f30x.h"
- /**
- \brief configure the TIMER peripheral
- \param[in] none
- \param[out] none
- \retval none
- */
- void timer0_config(void)
- {
- timer_parameter_struct timer_initpara;
- timer_oc_parameter_struct timer_ocintpara;
- timer_break_parameter_struct timer_breakpara;
- rcu_periph_clock_enable(RCU_TIMER0);
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_AF);
-
- gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
- timer_deinit(TIMER0);
- /* TIMER0 configuration */
- timer_initpara.prescaler = 119;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 999;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER0,&timer_initpara);
-
- /* CH0,CH1 and CH2 configuration in PWM mode0 */
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
- /* CH0 configuration in PWM mode0,duty cycle 25% */
- timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
- timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
-
-
- /* automatic output enable, break, dead time and lock configuration*/
- /* timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
- timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE ;
- timer_breakpara.deadtime = 164;
- timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
- timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
- timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
- timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
- timer_break_config(TIMER0,&timer_breakpara);*/
- /* TIMER0 primary output function enable */
- timer_primary_output_config(TIMER0,ENABLE);
-
- /* TIMER0 counter enable */
- timer_enable(TIMER0);
- }
- void timer2_config()//PB0,1 PA 7
- {
- timer_parameter_struct timer_initpara;
- timer_oc_parameter_struct timer_ocintpara;
- rcu_periph_clock_enable(RCU_TIMER2);
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_GPIOB);
- rcu_periph_clock_enable(RCU_AF);
-
- gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);
- gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
- gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
-
- timer_deinit(TIMER2);
- /* TIMER2 configuration */
- timer_initpara.prescaler = 119;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 999;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER2,&timer_initpara);
-
- /* CH0,CH1 and CH2 configuration in PWM mode0 */
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_channel_output_config(TIMER2,TIMER_CH_1,&timer_ocintpara);
- timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);
- timer_channel_output_config(TIMER2,TIMER_CH_3,&timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_1, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
- timer_channel_output_mode_config(TIMER2,TIMER_CH_1,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER2,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
- timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
- timer_channel_output_mode_config(TIMER2,TIMER_CH_3,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER2,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER2);
-
- /* auto-reload preload enable */
- timer_enable(TIMER2);
- }
- void timer4_config()//PA0
- {
- timer_parameter_struct timer_initpara;
- timer_oc_parameter_struct timer_ocintpara;
- rcu_periph_clock_enable(RCU_TIMER4);
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_AF);
-
- gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
-
- timer_deinit(TIMER4);
-
- timer_initpara.prescaler = 119;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 999;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER4,&timer_initpara);
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_channel_output_config(TIMER4,TIMER_CH_0,&timer_ocintpara);
-
- timer_channel_output_pulse_value_config(TIMER4,TIMER_CH_0, 300);
- timer_channel_output_mode_config(TIMER4,TIMER_CH_0,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER4,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
- timer_auto_reload_shadow_enable(TIMER4);
-
- timer_enable(TIMER4);
- }
- void timer7_config()//PC6 7 8 9
- {
- timer_parameter_struct timer_initpara;
- timer_oc_parameter_struct timer_ocintpara;
- rcu_periph_clock_enable(RCU_TIMER7);
- rcu_periph_clock_enable(RCU_GPIOC);
- rcu_periph_clock_enable(RCU_AF);
-
- gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
- gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);
- gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
- gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
-
- timer_deinit(TIMER7);
- /* TIMER4 configuration */
- timer_initpara.prescaler = 119;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 999;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER7,&timer_initpara);
-
- /* CH0,CH1 and CH2 configuration in PWM mode0 */
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_channel_output_config(TIMER7,TIMER_CH_0,&timer_ocintpara);
- timer_channel_output_config(TIMER7,TIMER_CH_1,&timer_ocintpara);
- timer_channel_output_config(TIMER7,TIMER_CH_2,&timer_ocintpara);
- timer_channel_output_config(TIMER7,TIMER_CH_3,&timer_ocintpara);
-
- timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_0, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
- timer_channel_output_mode_config(TIMER7,TIMER_CH_0,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER7,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
- timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_1, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
- timer_channel_output_mode_config(TIMER7,TIMER_CH_1,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER7,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
- timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_2, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
- timer_channel_output_mode_config(TIMER7,TIMER_CH_2,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER7,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
- timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_3, (dis_page_param.face_plate_param.Led_Brightness &&0x0100 != 0) ? 0:30);
- timer_channel_output_mode_config(TIMER7,TIMER_CH_3,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER7,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
- /* TIMER0 primary output function enable */
- timer_primary_output_config(TIMER7,ENABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER7);
-
- /* auto-reload preload enable */
- timer_enable(TIMER7);
- }
- void BSP_PWMOUT_Init(void)
- {
- timer0_config();
- timer2_config();
- timer4_config();
- timer7_config();
- }
|