Rearranged thus, we can rapidly compute the numbers
required to set the frequency of the PWM waveform. In
particular, begin with a prescaler value of one, then plug
in the PIC system clock frequency and the desired PWM
frequency. After a few punches on the calculator
keyboard, you’ll end up with that number PR2 mentioned
above. Remember, this is supposed to be an eight-bit
number, so if it exceeds 255 in value, you’ll then try a
prescaler of four and recompute. If it’s still too big, then
go back to the drawing board and see what happens
when the prescaler is 16. If the final result remains
greater than 255, then you’re asking the PIC for the
impossible and will need to rethink things.
In case it isn’t clear, in all of these formulas we’re
talking integer arithmetic. So, round any fractional result
to the nearest whole number. Assuming things went well,
at this point you’ll have values for PR2 and the prescaler.
Formula 4 is a reshuffled version of Formula 2. Just
plug in PR2 and the desired duty cycle (in percent), and
away you go! You’ll now have that number we decided to
A Sample Calculation
I promised to make this easy, and the best way to
convince you of that is by example. So, get out your
calculator and let’s see what happens.
For the sake of argument, let’s suppose we’re
running the PIC16F88 on its own internal 8 MHz system
clock. Further imagine that we desire a 10 kHz pulse wave
set to a 75% duty cycle.
Returning to Formula 3, let’s try a prescaler value of
one. Plugging in the numbers and cranking away yields a
PR2 result of 199, which can indeed be represented as an
eight-bit number. Then, computing with Formula 4, we
see that the number for duty should be 600. We have
everything we need.
Incidentally, the numbers turned out perfectly here,
but you won’t always be able to hit the frequency or duty
cycle smack-dab on the button.
Here are a couple niceties not given specifically in the
datasheet, yet easily derived nonetheless. If you put the
value for PR2 into Formula 5, you can then predict the
largest usable pulse width number. I get 800 for our
example above, and so should you. Thus, the possible
duty cycle numbers may range from zero (corresponding
to 0%, of course) all the way up to 800 (representing
100%). Any number larger than this simply keeps the
output pinned at full on.
Formula 6 will tell you what the increments are.
Staying with our example, then, we see that each change
from zero to one to two on up to 799 to 800 corresponds
to an increment of 0.125% — a very fine division indeed.
Finally, the computation described in Formula 7 will
tell you what sort of resolution to expect for your choice
of frequency. We just saw in our example that the highest
allowable number is 800, so pretty clearly this will lend a
SERVO 05.2014 55
% increment per step, in percent
Timer2 period register value
( ) ( ) 10
log 4 1
number of bits resolution, no greater than 10
value in Timer2 period register
value indicated by (1, 4 or 16)
( ) max 4 1 duty =× + ;;;
value for maximum duty cycle (100%)
Timer2 period register value