cancel
Showing results for 
Search instead for 
Did you mean: 

Modify PWM frequency on the fly (STM32F4)

quintanabarcia.pablo
Associate II

Hello, Community.

I am currently trying to control a power converter with a STM32F4 microcontroller. The thing is that the control variable is the frequency of the PWM. I have programmed a timer (TIM3) that generates an interruption every 100us. During this interruption, the MCU has to calculate the new frequency value of the PWM in order to switch the power MOSFET.

I tried two options but none of them work as I want.

case a) Modifying the ARR register on the fly.

If I do this, I have seen that many times, the PWM doesn't work and remains at high level during a certain time (400us or so). Please, kindly see the yellow pulses. It obviously has effects over the power converter.

0690X000008iQqvQAE.jpg

case b) I thought that since the register is not updated inmediately, I could reset the counter of the PWM timer. This time, the PWM no longer remains high during a certain time but it provides strange pulses.

TIM8 -> CNT = 0;			
TIM8->ARR = "calculated value";	

0690X000008iQrPQAU.jpg

I read about diferent registers to make the updates of the registers inmediately but I am not capable. I tried many things but they don't work.

Could you give me a hint? Thanks a lot.

11 REPLIES 11

The AUTO/PRELOAD impacts when the new values are loaded. You can also pay attention to the phase by checking the CNT register.

Seeing as the Update state is CNT==0, perhaps write ARR before CNT

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

Min and Max PWM frequency?

Is duty cycle constant? (on the scope it is not)

When the frequency change, should it be at the rise/fall edge of the pulse?

[del-already-answered]

quintanabarcia.pablo
Associate II

i tried changing the order of the instructions and it works the same way.

Duty is not constant because it depends on the ARR.

I read about enabling ARPE and OC1PE but it doesn't work either.

quintanabarcia.pablo
Associate II

I have tried one simple thing.

Every 100us I change the frequency of the PWM by modifying the ARR register just by adding or substracting 10 to the original value. And every time the microcontroller does the same as in picture 2. The very first 2 or 3 cycles, the pulses have wrong duty cycle.

turboscrew
Senior III

Just came to mind... how does the timer react to prescaler change?

S.Ma
Principal

Get the interrupt on UEV and change immediately both the output compare and the overflow ARR values.

If this works, then use other timer HW assists.

quintanabarcia.pablo
Associate II

Thank you. I have forced the update of the PWM and it works as it should. However, I have to keep working on it because I cannot have two pulses right away. My strategy of getting into the control loop every 100us is not suitable because it interferes with the behaviour of the power converter. I can only change the frequency of the PWM every 100us but ALSO the CNT register must be equal or close to ARR. This way, I would not have two consecutive pulses.

I only came up with the idea of:

while (TIM8->CNT < TIM8->ARR) 
      ;

But this is very dangerous. I do not know how else I can do it.