cancel
Showing results for 
Search instead for 
Did you mean: 

How best to turn PWMs ON and OFF from interrupt handler

Jim Seymour
Senior
Posted on January 20, 2016 at 19:57

We have a design that drives 12 LEDs using PWM outputs spread out over five different timers on a STM32F We further control the LED dimming by turning the PWMs ON and OFF from a timer interrupt handler.

I've found that sometimes turning a LED on or off will affect a different LED. I'm wondering if I'm doing the right steps in my interrupt handler. Because the PWMs in question may or may not be running when the interrupt fires, I do the following to turn them ON: (Example is for channel 1 of TIM15)

if (htimpInstance->CCR1 != 0) {
TIM_CCxChannelCmd(htimpInstance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
}

And then this to turn the PWMs OFF:

TIM_CCxChannelCmd(htimpInstance, TIM_CHANNEL_1, TIM_CCx_DISABLE);

Does this seem correct (and safe to do in an interrupt handler)?
3 REPLIES 3
Radosław
Senior II
Posted on January 20, 2016 at 21:15

Disabling in this way cause set pin in high impedance.

Stop timer, and set  pwm duty to 0 or max depending on output polarization.

joemccarr
Senior
Posted on January 23, 2016 at 13:16

Can you set a bit in the interrupt handler and have your main loop handle the turning off the pwm?

re.wolff9
Senior
Posted on January 23, 2016 at 15:47

Could it be that you are using the same structure for turning it off in the interrupt while the main program is trying to turn a different PWM signal on using the same structure? Then when the mainprogram resumes it will affect the wrong PWM channel. 

Either disable interrupts in main when you work with that structure, or better, use two different structures.