cancel
Showing results for 
Search instead for 
Did you mean: 

PWM stop work

MichaelR
Associate II

Hello

I have configured TIM2 CH1 with PWM Generation on my board STM32F412G-DISCO. I capture the rising and falling edges by other board NUCLEO-G071RB, using interrupt input capture for both edges (one channel for each edge).

Now I try to change the period of the PWM in the F412G board. However, sometimes the signal stops (I capture it using an oscilloscope too).

The code I use to change the period:

 

fanCount=(int)(fanPeriod/1000*clock/(htim2.Init.Prescaler+1));

__HAL_TIM_SET_AUTORELOAD(&htim2,fanCount);

 

Do anyone have an idea what is the problem?

1 ACCEPTED SOLUTION

Accepted Solutions

When changing TIMx_ARR "on the fly" (without stopping the timer), you have to use ARR preload, i.e. you have to set TIMx_CR1.ARPE.

Otherwise, without preload, if you set TIMx_ARR below current TIMx_CNT, you would have to wait until TIMx_CNT rolls over the maximum, which in case of TIM2 (a 32-bit timer) is 0xFFFF'FFFF, and that can take a surprisingly long time.

(Alternatively, after setting TIMx_ARR, force update by setting TIMx_EGR.UG. Or just zero the counter, TIMx_CNT = 0.)

JW

View solution in original post

1 REPLY 1

When changing TIMx_ARR "on the fly" (without stopping the timer), you have to use ARR preload, i.e. you have to set TIMx_CR1.ARPE.

Otherwise, without preload, if you set TIMx_ARR below current TIMx_CNT, you would have to wait until TIMx_CNT rolls over the maximum, which in case of TIM2 (a 32-bit timer) is 0xFFFF'FFFF, and that can take a surprisingly long time.

(Alternatively, after setting TIMx_ARR, force update by setting TIMx_EGR.UG. Or just zero the counter, TIMx_CNT = 0.)

JW