[GD32E231 DIY Contest] 02. Why can’t the PWM duty cycle be changed in the external interrupt service function?
[Copy link]
This post was last edited by Media Student on 2019-5-4 15:04 [GD32E231 DIY Contest] 02. Why can't the PWM duty cycle be changed in the external interrupt service function? I tried to debug PWM this afternoon. Because I followed the routine, the whole process was relatively smooth, but something abnormal happened. When I tried to change the PWM duty cycle in the external interrupt function void EXTI4_15_IRQHandler(void), I found that it didn't work. This problem has been solved: I misunderstood the routine and should have changed it in the EXT0_1 interrupt service function. . . The specific situation is as follows: First, in order to control the servo, I output a PWM wave with a duty cycle of 20ms and adjustable between 1ms and 2ms. GPIO initialization:- void gpio_config(void) { rcu_periph_clock_enable(RCU_GPIOA); /* configure PA6(TIMER2 CH0) as alternate function */ gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6); gpio_output_options_set(GPIOA, GPIO _OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6); gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_6); }
复制代码 External interrupt initialization:- void exti_config(void) { /* enable the clock */ rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_CFGCMP) ; /* configure GPIO as input */ gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_5); /* enable and set EXTI interrupt */ nvic_irq_enable(EXTI4_15_IRQn, 1U); /* connect EXTI line to GPIO pin */ syscfg_exti_line_config(EXTI_SOURCE_GPIOA, SOURCE_PIN5); /* configure EXTI line */ exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_FALLING); exti_interrupt_flag_clear(EXTI_5); }
复制代码Timer initialization: - void timer_config(void) { /* ----------------------------------------------------------------------------- TIMER2CLK is TIME 100KHz R2 channel0 cycle = (25000/ 50000)* 100 = 50% ----------------------------------------------------------------------------- */ timer_oc_parameter_struct timer_ocintpara; timer_parameter_struct timer_initpara; rcu_periph_clock_enable(RCU_TIMER2); timer_deinit(TIMER2); /* TIMER configuration */ timer_initpara.prescaler = 719; duty _initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; //timer_initpara.period = 49999; timer_initpara.period = 1999; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_initpara.repetitioncounter = 0; timer_init(TIMER2,&timer_initpara); /* configure CH0 in PWM mode0 */ timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; timer_ocintpara.outputstate = TIMER_CCX_ENABLE; timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara); //timer_channel_output_pulse_value_config( TIMER2,TIMER_CH_0,24999); timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,149); timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM0); timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,void EXTI4_15_IRQHandler(void) { if(RESET != exti_interrupt_flag_get(EXTI_5)){ gd_eval_led_toggle(LED1); timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,199); } exti_interrupt_flag_clear(EXTI_5); } I don't know why it doesn't work here? I searched some posts about stm32 on the Internet, and it seems that this problem exists, but I didn't find any relevant explanation.... I tried to change the duty cycle in the SysTick interrupt service function: void SysTick_Handler(void) { delay_decrement(); timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,199); } It can also be adjusted in the main function: while(1){ delay_1ms(5000); timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,100); delay_1ms(5000); timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,200); }
复制代码This program is in the folder GD32E23x_Demo_Suites_V1.0.1\GD32E230C_START_Demo_Suites\Projects\04_TIMER_Key_EXTI. Please help me solve my problem. Thanks~ The attached picture is the PWM waveform I generated:
|