[GD32E231 DIY大赛] 02. 为何PWM的占空比不能在外部中断服务函数中更改?
[复制链接]
本帖最后由 传媒学子 于 2019-5-4 15:04 编辑
[GD32E231 DIY大赛] 02. 为何PWM的占空比不能在外部中断服务函数中更改?
今天下午尝试着调试PWM,因为仿照着例程,整个过程还是比较顺利,但是反常的事情出现了,当我试图在外部中断函数void EXTI4_15_IRQHandler(void)中改变PWM占空比时,却发现不行。 此问题已经解决:是我把例程理解错了,应该在EXT0_1中断服务函数中改。。。
具体情况是这样的:
首先,我为了控制舵机,输出20ms, 占空比可在1ms到2ms之间可调的PWM波。 GPIO初始化:
- 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);
- }
复制代码
外部中断初始化:
- 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, EXTI_SOURCE_PIN5);
- /* configure EXTI line */
- exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
- exti_interrupt_flag_clear(EXTI_5);
- }
复制代码
定时器初始化:
- void timer_config(void)
- {
- /* -----------------------------------------------------------------------
- TIMER2CLK is 100KHz
- TIMER2 channel0 duty 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;
- timer_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);
- /* configurate 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,TIMER_OC_SHADOW_DISABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER2);
- /* auto-reload preload enable */
- timer_enable(TIMER2);
- }
复制代码
然后,我想着在外部中断函数中,也就是处理key的按键中,改变PWM占空比,却失败了,code如下:
- 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);
- }
复制代码
不知道什么原因, 为什么这里不行?
在网上搜了一些stm32的帖子,貌似存在这个问题,没有找到相关的解释....
而我尝试在SysTick 中断服务函数中,改变占空比就可以:
- void SysTick_Handler(void)[/align]
- {
- delay_decrement();
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,199);
- }
复制代码
在主函数中调整也可以: - 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);
- }
复制代码
这个程序是在 GD32E23x_Demo_Suites_V1.0.1\GD32E230C_START_Demo_Suites\Projects\04_TIMER_Key_EXTI文件夹下,
还请arm大神,给予解惑。Thanks~
附图是我生成的PWM波形:
|