0
\$\begingroup\$

I'm using an STM32F407VG. I created a 28 kHz square wave by using timer PWM generation. When I look at the oscilloscope, I can see it clearly. I'm having trouble changing the frequency of the PWM without stopping it.

I found two functions which are __HAL_TIM_SET_PRESCALER() and __HAL_TIM_SET_AUTORELOAD().

HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
__HAL_TIM_SET_AUTORELOAD(&htim2,99);
__HAL_TIM_SET_PRESCALER(&htim2,29);

I set prescaler and autoreload in CubeMX. Prescaler is 2 and ARR is 999, but I want to change the prescaler to 29 and ARR to 99. I can see a square wave with HAL_TIM_PWM_Start(), but when I add 2 other functions it goes away. I commented those prescaler and ARR functions and I saw that PWM was working.

I don't know how to change the prescaler and ARR values without stopping the PWM.

\$\endgroup\$
1
  • \$\begingroup\$ Is ARR buffer/preload enabled? TIM->CR1:ARPE bit controls this. \$\endgroup\$
    – Tagli
    Apr 16, 2021 at 12:55

2 Answers 2

1
\$\begingroup\$

I solved my problem today. I was changing ARR and Prescaler together. But i have seen a guy's comment which was about missing part of changing.

So i decided to change all of them at the same time.

__HAL_TIM_SET_AUTORELOAD();
__HAL_TIM_SET_PRESCALER();
__HAL_TIM_SET_COMPARE();

When i had done like this. It worked. I hope it will be helpful for anyone who faces the same struggle.

\$\endgroup\$
0
\$\begingroup\$

Let's say you update ARR as 90 when your CNT register is 95, when do you think the next update/reload happens?

Options:

when you set ARR check CNT if it's higher than next ARR value.(poor)

set CNT to 0 when you are updating ARR.(OK)

Enable relevant timer interrupt, update ARR only in timer overflow ISR.(Best in my opinion)

\$\endgroup\$
1
  • \$\begingroup\$ There is no need for that. There are settings to make ARR & CCR registers preloaded, so that they are updated not as soon as the registers are written, but only after a update event occurs. \$\endgroup\$
    – Tagli
    Feb 4, 2022 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.