Ruth Hendrix

problem when changing prescaler during PWM operation

Discussion created by Ruth Hendrix Employee on Jan 19, 2006
This message contains an entire topic ported from a separate forum. The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value to you as you search for answers to your questions.
 
Posted: Fri Jan 21, 2005 1:37 pm    
 
Dear all,

I'm using the timer as unbuffered OC to generate a PWM. Thus TMOD set the period and TCH set the pulse width.
My application requires to switch the frequency of the PWM between 3 frequencies which are 32, 64 or 128Hz
When changing PS0:2, the frequency of PWM is changing, but the first period after the change is slightly altered.

For instance, if my PWM is 64Hz and I'm changing to 32Hz, then the first 32Hz period is 32.19Hz (i.e. -184microseconds than expected). Other example : If I'm in 32Hz and then changing for 128Hz, then the first périod is 127Hz (+61microseconds than expected). The second period and the followings ones are correct (32.00 or 128.00 Hz)

When debugging, the TMOD value isn't modified and TCNT is correctly runninge even around the PSx modification. I guess the PSx modification makes something not working correctlty during a while in the CPU but I don't know how to investigate....


Is anybody has already experienced something like that or could give me some ideas to debug ?

I'm using HC08MR8 and Metrowerks Codewarrior compiler & debbuger.
Posted: Mon Jan 24, 2005 8:54 am    
 
Hi,

If you just change the prescaler without stopping or reseting the Timer, the counter never stops, nor reset.

What is happening is that during the first period you didn't start the count at zero. Therefore your count is wrong depending on the value already counted on the previous prescaler:
Let's take an example. You want to change from 32Hz to 128Hz. It means you have to decrease prescaler by a factor 4.
When you change the prescaler you have 0< TCNT < TMOD (you always have this anyway), your first period will be longer. The start of the count (between 0 & TCNT) will be counted at 32Hz and the end (TCNT to TMOD) at 128Hz.

And vice versa Laughing

Outcomes