cancel
Showing results for 
Search instead for 
Did you mean: 

Timers other than timer 1 don't have functioning PWM? (Blue pill)

JShel.1
Associate II

This problem just popped up for me. I'm not sure if I'm doing anything wrong, but timers other than timer 1 don't have functioning PWM. I configured the timers the exact same (aside from ignoring timer 1's special features,) making sure to include HAL_TIM_PWM_Start() and HAL_TIM_Base_Start(). I even triple-checked the pin I was connecting my oscilloscope to. Still, no timer other than timer 1 wants to output PWM. If necessary, I can put my old code (working code, which I tested today,) and new code (non-functioning) here. I tried searching for my problem, but nothing came up.

And as a last note, the new code WAS working about a month ago when I last tested it (I'm sure I have not made any changes to the timer configuration since.)

7 REPLIES 7
TDK
Guru

It's unlikely that PWM mode doesn't work for other timers, so there is probably a bug in your code.

It's also unlikely that code which worked a month ago no longer works today.

If you feel a post has answered your question, please click "Accept as Solution".
Nikita91
Lead II

You said new code without any change to the timer configuration.

So your problem comes from the other new part of the application...

A modification made less than a month ago.

JShel.1
Associate II

So there does seem to be an issue with the configuration, because I got the PWM to work again.

The difference was, when it wasn't working, timer 1 update event interrupt was on, and because I had an auto reload of 12, was occurring at ~6MHz (assuming timer 1 runs with the base clock of 72MHz.) This time, I disabled the update event, and the PWM started working.

Unfortunately, part of my code requires those updates to happen very often, so I need it to happen. My question now is: why does having interrupts stop timers 2 and 3 PWM from working? Is there a way I can fix it, or do I have to find a way around the very frequent interrupts?

The way around very frequent interrupts is it disable the interrupts or reduce their frequency. It doesnt stop other timers from working but it will prevent you from running any code in the main loop since your mcu is always in the interrupt handler.
If you feel a post has answered your question, please click "Accept as Solution".

There's no way your interrupts would execute at 6MHz rate at all; the entry sequence alone is 12 clocks (and that's without FLASH latency, software overhead in the ISR itself, exit, and the body of ISR itself). You may be able to see some toggling at that frequency, but that's just those interrupts tail-chaining on each other, which is a completely useless thing.

JW

PWM generation doesn't need interrupts, the TIM can do this automatically, you might want to modulate the width or frequency, but at high speeds (>100 KHz) you would want to use DMA to service the TIM via a table/array.

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

Thank you for the advice on using the timers' PWM, but the problem I had was different, and wacky in general. The timer 1 interrupt, I assume, has no relation to the function of timers 2 and 3 PWM. There is no code in the interrupt that changes any settings of those timers. At this point I think the frequency of the interrupt somehow takes priority over loading the timer configuration values first. My friend is also working on a program that uses PWM at high-ish frequencies, so this information will be helpful to him as well.