Skip to main content
Visitor II
May 19, 2024
Solved

PWM stop work

  • May 19, 2024
  • 1 reply
  • 1312 views

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?

    This topic has been closed for replies.
    Best answer by waclawek.jan

    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

    1 reply

    Super User
    May 19, 2024

    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