View Full Version : Updating PWM-OUT values
When I change the value of PWM-OUT, does that affect a PWM duty-cycle that is already in progress, or does it wait until the next PWM period? I am measuring the width of a servo pulse with PWM-IN on a channel that is running at about 46 Hz. I'm trying to essentially echo the signal using PWM-OUT, but the slowest clock rate on a PWM output is 76 Hz, assuming I don't want to resort to half-speed mode. My servos are fine with this, but it does mean that I'm updating the PWM-OUT value at about 46 Hz while the PWM output is running at 76 Hz. I'm wondering if this asynchronous update is going to garble my output PWM. If a new PWM-OUT value always takes affect on the next PWM period, I think I'm ok. My PWM output changes will not be in perfect step with the RC receiver, but at least the pulse-widths will be correct.
nmitech
02-05-08, 10:28 AM
When I change the value of PWM-OUT, does that affect a PWM duty-cycle that is already in progress, or does it wait until the next PWM period?
No, it does not affect the pwm duty-cycle when you change the value of PWM-OUT. But when you change the duty-cycle, all pwm output channels associate with that group A, or B will stop.
I am measuring the width of a servo pulse with PWM-IN on a channel that is running at about 46 Hz. I'm trying to essentially echo the signal using PWM-OUT, but the slowest clock rate on a PWM output is 76 Hz, assuming I don't want to resort to half-speed mode. My servos are fine with this, but it does mean that I'm updating the PWM-OUT value at about 46 Hz while the PWM output is running at 76 Hz. I'm wondering if this asynchronous update is going to garble my output PWM. If a new PWM-OUT value always takes affect on the next PWM period, I think I'm ok. My PWM output changes will not be in perfect step with the RC receiver, but at least the pulse-widths will be correct.
You can use any timer pins to generate PWM frequency down to 38Hz at full speed CPU. See the appnote below,
http://www.newmicros.com/store/product_manual/IOProgramming.pdf
My code seems to work great, at least for one channel if I use a SCHEDULE-RUNS of about 10000. 50000 is definitely too slow and I get periodic glitches. Bumping up the rate fixed the problem. Seems to confirm what you said, no glitching cause by writing PWM-OUT faster than the PWM period.
BTW there's a bug in the code I posted earlier as well. I can't just echo the value from PWM-IN to PWM-OUT since PWM-IN is ticks of the 2.5 Mhz clock, and PWM-OUT values are the duty-cycle expressed as 1/65535 of whatever PWM-PERIOD that's running. It turned out that when running the PWAM0 pin at 76 Hz (32767), multiplying the PWM-IN value by two and plugging it into PWM-OUT on PWMA0 works out fine.
Thanks for the note about 38 Hz on TA0 - TD3. I'm reluctant to use them for PWM since I want to save the timer input pins. 76Hz seems to work fine with my servos.
vBulletin v3.0.7, Copyright ©2000-2012, Jelsoft Enterprises Ltd.