3703 views|4 replies

530

Posts

4

Resources
The OP
 

[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:
  1. 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:
  1. 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:
  1. 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:




GD32E23x_Demo_Suites_V1.0.1.rar

10.91 MB, downloads: 2

This post is from GD32 MCU

Latest reply

Maybe it's the privilege mechanism. When entering the interrupt, it becomes user mode and cannot be modified.  Details Published on 2019-5-3 19:38
 

1w

Posts

16

Resources
2
 
I think I need to reinitialize after the change, timer_init(TIMER2,&timer_initpara);
This post is from GD32 MCU

Comments

However, there will be no problem if you do not modify it in the external interrupt.  Details Published on 2019-5-3 17:35
 
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 

530

Posts

4

Resources
3
 
ddllxxrr posted on 2019-5-2 09:40 I think you have to reinitialize it after the change, timer_init(TIMER2,&timer_initpara);
However, there is no problem if you don't change it in the external interrupt.
This post is from GD32 MCU

Comments

Maybe it's the privilege mechanism. When entering the interrupt, it becomes user mode and cannot be changed.  Details Published on 2019-5-3 19:38
 
 
 

1w

Posts

16

Resources
4
 
Published by Media Student on 2019-5-3 17:35 However, if you don't modify it in the external interrupt, there will be no problem.
Maybe it's the privilege mechanism. When entering the interrupt, it becomes user mode and cannot be modified.
This post is from GD32 MCU

Comments

It's not the privilege mechanism, it's the routine that has a problem, see my subsequent post..  Details Published on 2019-5-4 15:00
 
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 

530

Posts

4

Resources
5
 
This post was last edited by Media Student on 2019-5-4 15:04
ddllxxrr posted on 2019-5-3 19:38 Maybe it's the privilege mechanism. When entering the interrupt, it becomes user mode and cannot be changed
I made a mistake. It can be changed in the interrupt. . .
This post is from GD32 MCU
 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list