Alright I didnt read over the datasheet enough but I still don't understand how I know what "Tosc" is in the PWM period equation. I found a confusing chart on page 273 that says that in HS mode, which is what my 18F252 is in with a 20 MHz oscillator, Tosc has a range of 40 to 250 ns. How do I find what it is? Once I know how to correctly find Tosc I can solve the equation and calculate what I need to set PR2 to.
I know that in order to generate a 40 kHz signal I need to generate a square wave with a pulse duration of 25 microseconds.
Ok,
The formula in the datasheet for the PIC18FXX2:
PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)
and 1/PWM period = frequency so in my case:
1/PWM period = 40,000
PWM period = .000025 (25 microseconds)
So my equation should be
.000025 = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)
Since TMR2 prescale is 1, I can rearrange it so that (PR2), the unknown, is on one side:
(20MHz / (4 * TMR2 prescale value * 40KHz)) - 1= PR2.
Solve it all out...
PR2 = 124
...
And it works!
Thanks everyone!
In case anyone in the future needs to use the hardware PWMs and has no idea what to set PR2 to, use this equation:
(Oscillator speed / (4 * TMR2 prescale value * Desired Output)) - 1= PR2.
Bookmarks