Oddity in setting up PWM outputs ON MOD54415
Posted: Tue Apr 16, 2013 11:57 pm
Hi all,
Here is bit of extra info for those who need to work with PWM outputs on the MOD54115.
What I need to generate are two clock signals, one as close as possible to 8 MHz and another one as close to 4 MHz as possible. I also need a start pulse with a duration of +/ 160 nanoseconds which repeats every 1250 clock periods of the 8 MHz signal. Most importantly: everything needs to be synchronized perfectly: the 8MHz signal must be exactly twice as fast as the 4 MHz signal and the start pulse must always be at exactly the right position relative to the 4 MHz signal.
I got it all to work perfectly but I noticed that the information on the mcPWM module in the MCF54418 reference manual is not quite perfect.
The flaw is that the documentation says that each PWM submodule's PWM counter will count from the INIT value up to the VAL1 value, at which point it reloads the INIT value and counts up again at the next clock cycle. Unfortunately that is not the case, in reality it works slightly differently.
Example: the base frequency is 125 MHz => divided by 16 you would get a frequency of 7,8125 MHz. According to the documentation I would expect the INIT value to be 0 and the VAL1 value to be 16. That would be logical because as values 16 and 0 are present at the same moment it should take exactly 16 periods at 125 MHz to run from 0 to 16 => 7,8125 MHz.
That is not what happens: you actually get a frequency of 7.3529 MHz which is 125MHz/17 !
In order to get the 7,8125 MHz you need to enter a VAL1 of 15 (16-1).
Getting the other signal of 3,90625 MHz on another PWM submodule requires an INIT value of 0 and a VAL1 of 31 (32-1).
Doing that gets you a frequency which is exactly half of the first and stays perfectly in sync with the first signal.
The same applies to the start pulse: an INIT value of 0 and a VAL1 of 19999 ((16x1250)-1) gets you a start pulse which is perfectly in sync with both clock signals and always at the same position.
Bottom line: the PWM counter does not run from INIT to VAL1, then immediately reloads to INIT and counts up again at the next clock cycle. Instead it runs from INIT To VAL1, sets up the reload to INIT, performs it on the next clock cycle and starts counting up again at the next clock cycle.
The resulting total number of periods is "(VAL1-INIT)+1" instead of just "VAL1-INIT".
The end result is now 100% OK: three perfectly timed signals which are permanently synced (as indicated by my digital scope) but as always it helps a lot if you know what is really happening.
PS: What does work a treat are the VAL2 and VAL3 registers, with it you can change the ON and OFF switch points of the PWM signals. In my case that was very useful in order to set the relative positions of the signals to each other.
Here is bit of extra info for those who need to work with PWM outputs on the MOD54115.
What I need to generate are two clock signals, one as close as possible to 8 MHz and another one as close to 4 MHz as possible. I also need a start pulse with a duration of +/ 160 nanoseconds which repeats every 1250 clock periods of the 8 MHz signal. Most importantly: everything needs to be synchronized perfectly: the 8MHz signal must be exactly twice as fast as the 4 MHz signal and the start pulse must always be at exactly the right position relative to the 4 MHz signal.
I got it all to work perfectly but I noticed that the information on the mcPWM module in the MCF54418 reference manual is not quite perfect.
The flaw is that the documentation says that each PWM submodule's PWM counter will count from the INIT value up to the VAL1 value, at which point it reloads the INIT value and counts up again at the next clock cycle. Unfortunately that is not the case, in reality it works slightly differently.
Example: the base frequency is 125 MHz => divided by 16 you would get a frequency of 7,8125 MHz. According to the documentation I would expect the INIT value to be 0 and the VAL1 value to be 16. That would be logical because as values 16 and 0 are present at the same moment it should take exactly 16 periods at 125 MHz to run from 0 to 16 => 7,8125 MHz.
That is not what happens: you actually get a frequency of 7.3529 MHz which is 125MHz/17 !
In order to get the 7,8125 MHz you need to enter a VAL1 of 15 (16-1).
Getting the other signal of 3,90625 MHz on another PWM submodule requires an INIT value of 0 and a VAL1 of 31 (32-1).
Doing that gets you a frequency which is exactly half of the first and stays perfectly in sync with the first signal.
The same applies to the start pulse: an INIT value of 0 and a VAL1 of 19999 ((16x1250)-1) gets you a start pulse which is perfectly in sync with both clock signals and always at the same position.
Bottom line: the PWM counter does not run from INIT to VAL1, then immediately reloads to INIT and counts up again at the next clock cycle. Instead it runs from INIT To VAL1, sets up the reload to INIT, performs it on the next clock cycle and starts counting up again at the next clock cycle.
The resulting total number of periods is "(VAL1-INIT)+1" instead of just "VAL1-INIT".
The end result is now 100% OK: three perfectly timed signals which are permanently synced (as indicated by my digital scope) but as always it helps a lot if you know what is really happening.
PS: What does work a treat are the VAL2 and VAL3 registers, with it you can change the ON and OFF switch points of the PWM signals. In my case that was very useful in order to set the relative positions of the signals to each other.