|
【GD32E231_DIY】-04:6路PWM输出
[复制链接]
基于TIMER2 CH0\CH1\CH2\CH3、TIMER15 CH0_N、TIMER16 CH0_N 6路PWM输出
- void PWM_GPIO_config(void)
- {
- rcu_periph_clock_enable(RCU_GPIOA);
- /* configure PA6 7(TIMER2 CH0 CH1) as alternate function */
- gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6 | GPIO_PIN_7);
- gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_6 | GPIO_PIN_7);
- rcu_periph_clock_enable(RCU_GPIOB);
- /* configure PB0 1(TIMER2 CH2 CH3) as alternate function */
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0 | GPIO_PIN_1);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_0 | GPIO_PIN_1);
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_0|GPIO_PIN_1);
- rcu_periph_clock_enable(RCU_GPIOB);
- gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_2);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
- gpio_bit_reset(GPIOA,GPIO_PIN_2);
- /* configure PB6 7 8(TIMER15 CH0_ON TIMER16_CH0_ON TIMER15 CH0) as alternate function */
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7 );
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6 | GPIO_PIN_7);
- gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_6 | GPIO_PIN_7);
- }
- void TIM2_Config(void)
- {
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
-
- PWM_GPIO_config();
- rcu_periph_clock_enable(RCU_TIMER2);
- timer_deinit(TIMER2);
- /* TIMER configuration */
- timer_initpara.prescaler = 500;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 5999;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER2,&timer_initpara);
- /* configurate CH0 in PWM mode0 */
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_LOW;
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
- timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,632);
- timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
- /* configurate CH1 in PWM mode0 */
- timer_channel_output_config(TIMER2,TIMER_CH_1,&timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_1,940);
- 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);
- /* configurate CH2 in PWM mode0 */
- timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,1500);
- 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);
- /* configurate CH3 in PWM mode0 */
- timer_channel_output_config(TIMER2,TIMER_CH_3,&timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3,632);
- 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);
- timer_primary_output_config(TIMER2,ENABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER2);
- /* auto-reload preload enable */
- timer_enable(TIMER2);
- //timer_channel_output_state_config(TIMER2, TIMER_CH_0, TIMER_CCX_DISABLE);
- }
- void TIM15_Config(void)
- {
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
-
- rcu_periph_clock_enable(RCU_TIMER15);
- timer_deinit(TIMER15);
- /* TIMER configuration */
- timer_initpara.prescaler = 0;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 1895;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER15,&timer_initpara);
- /* configurate CH0 in PWM mode0 */
- timer_ocintpara.outputstate = TIMER_CCX_DISABLE;
- timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
- timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
- timer_channel_output_config(TIMER15,TIMER_CH_0,&timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER15,TIMER_CH_0,632);
- timer_channel_output_mode_config(TIMER15,TIMER_CH_0,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER15,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
- timer_primary_output_config(TIMER15,ENABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER15);
- /* auto-reload preload enable */
- timer_enable(TIMER15);
- }
- void TIM16_Config(void)
- {
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
-
- rcu_periph_clock_enable(RCU_TIMER16);
- timer_deinit(TIMER16);
- /* TIMER configuration */
- timer_initpara.prescaler = 0;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 1895;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER16,&timer_initpara);
- /* configurate CH0 in PWM mode0 */
- timer_ocintpara.outputstate = TIMER_CCX_DISABLE;
- timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
- timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
- timer_channel_output_config(TIMER16,TIMER_CH_0,&timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER16,TIMER_CH_0,500);
- timer_channel_output_mode_config(TIMER16,TIMER_CH_0,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER16,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
- timer_primary_output_config(TIMER16,ENABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER16);
- /* auto-reload preload enable */
- timer_enable(TIMER16);
- }
复制代码
|
|