PR2 should be loaded with 249 for 1kHz @4MHz. Plug this into the calculation shown in the
data sheet for PWM resolution, and you'll see it's just short of 10-bits.
PR2 should be loaded with 249 for 1kHz @4MHz. Plug this into the calculation shown in the
data sheet for PWM resolution, and you'll see it's just short of 10-bits.
Bruce,
Why should it be loaded with 249?
If I calculate the possible resolution with this given formula, it makes exactly 10 bits with PR2 set to 255 "+1".
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2417&stc=1&d=120549250 2">
I have to admit that the PWM calculation is still a mystery to me; I only roughtly understand the different parameters involved.
But this is exactly why I would like to understand where this value "249" comes from.
Tosc is a fourth of one microsecond (using the 4MHz XTal), isnt 'it?
Roger
Hi Roger
I'm pretty new to this as well but I'd like a shot at answering your question. If I'm wrong I get shot down as well!
Freq Calc
PWM freq = Fosc/(PR2 + 1) x 4 x Prescale
With PR2 at 249 this is 4,000,000/250 x 4 x 4 = 1000 Hz.
Your 255 gives 4,000,000/256 x 4 x 4 = 976.56Hz which is what you measured.
Resolution formula as you stated. With PR2 at 249 then Resolution is Log 4 x 250/Log2 = 9.9657 = 1000 bit resolution (999.94 actually)
With PR2 ar 255 then resolution will be Log 4 x 256/Log2 = 10 bit but your frequency then can't be 1kHz ...only 976 Hz
Hope I got this all OK guys
Adrian
It's very clear now.
Thanks for your explanations.
Roger
It's a bit confusing...
The FREQ of the PWM can only be (X) and you might not be able to set it to (X+1) because the 'setter' of the FREQ is a divisor of Fosc. The duty cycle of that particular frequency of the PWM can be practically anything.
There are other ways to get an exact PWM frequency with an exact duty cycle at that exact frequency...but they go quite a bit further than a standard PIC.
Bookmarks