cancel
Showing results for 
Search instead for 
Did you mean: 

Change Timer PWM Output Pulse On the Fly

Rogers.Gary
Senior II
Posted on January 28, 2015 at 21:29

Hi,

I have figured out how to change the period of the Timer 3 on the fly by using

TIM3->CNT = someNewValue; and it works good.

However, the duty cycle requires updating as well, set up originally as PWM pulse (TIM_Pulse) on Output Channel 1. Here is the code, where dutyCycle is a value from 0.1 to 0.5:

    TIM_OCInitStructure.TIM_Pulse = (uint16_t)(Period * dutyCycle);

    TIM_OC1Init(TIM3, &TIM_OCInitStructure);

I would like to be able to modify a TIMx register directly to do this. I've looked through the manual but did not see what register(s) I would need to write to do this.

Any help/suggestions? Or do I have to re-run the configuration completely?

Thank you...

3 REPLIES 3
Posted on January 28, 2015 at 22:07

TIM3->CCR1 = (Period * dutyCycle);

You'd also want it to load at the Update, ie not Preload/Immediate

If you change it immediately you'll get phase noise or cycles where it's high or low for the remained of the period. The timer normally has shadow registers which copy to the live ones at the update event.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Rogers.Gary
Senior II
Posted on January 28, 2015 at 23:56

clive1,

There is no effect when I try writing that register, with either timer 3 or timer 4:

    TIM3->CCR1 = (timerPeriod * dutyCycle);

    TIM4->CCR1 = (timerPeriod * dutyCycle);    

The duty cycle is at 50% (0.5) when I start, and after 10 seconds I do the write above with a new dutyCycle = 0.15, but there is no change.

Rogers.Gary
Senior II
Posted on January 29, 2015 at 00:00

screwed up....it is working. Had the assignment of new dutyCycle in a commented out area. Sorry!!