By Thomas Henry
SERVO 08.2014 43
Post comments on this article and find any associated files and/or downloads at
of some output pin.
To keep things specific, let’s look in
particular at the PIC16F88 which is not
only common and inexpensive, but fairly
representative of many microcontrollers.
Refer to Figure 1 now. This depicts
the functional arrangement of the
components comprising the compare
module. You’ll note a timer — really, just
a binary counter; always incrementing,
then rolling over to zero, only to
continue — chugging away in the
background. In the PIC, this is Timer 1
and it’s 16 bits in size. It’s composed of
the two bytes TMR1H and TMR1L
concatenated. The value contained
within the timer at any given moment is
applied to one input of the numeric
At the other input is a fixed number
provided by the compare register, made
up of CCPR1H and CCPR1L. As the
designer, you get to decide what this
number should be. Now, it becomes clear what’s going to
happen. The timer keeps incrementing one step at a time,
but sooner or later must become equal to the number you
stuffed into the compare register. When that occurs, the
output of the comparator goes high, indicating a match
has been made.
Let’s continue to trace the output of the comparator.
A software switch determines whether the signal should set
or reset a flip-flop, or perhaps just do nothing. In Figure 1
when the switch is in the top position, the flip-flop will be
reset (cleared); in the second position, it will be set; and in
the bottom position with the no-connection (n.c.), no
action is taken — at least not at the port level.
The flip-flop output is routed to yet another switch, and
its job is to connect to one of two port lines, B.0 or B. 3.
Think of the switch as a multiplexer, giving you the freedom
to decide which pin you want to be using here. This
decision must be made when you Flash (burn) the PIC,
since your choice is set in one of the configuration bits.
Whichever pin you decide upon will be referred to as
the CCP1 pin from now on. It should be clear that CCP1
can be toggled back and forth under the control of the
timer; this will be the source of the pulses — either single or
repeating. We’ll see exactly how to set all these options in
just a moment.
Back up to the output of the comparator. Note that it
also gets routed around to a switch-selectable clear input. If
you so choose, you can have the timer reset or clear itself
whenever it hits the target value.
Lastly, observe that the comparator output is also sent
to the so-called CCP1IF flag. This is an interrupt flag
maintained by the PIC, and when acted upon will cause the
main program to be put on the backburner while a more
pressing interrupt routine is attended to. This may sound
mysterious just now, but as we’ll find out shortly, it all
works quite nicely. Making Your Choices
With the overview out of the way, let’s inch a little
closer to the practical details and see how to set up the
various options. The first step is to decide which port line
(B.0 or B. 3) you’d like to connect to the output of the
compare module. As mentioned earlier, this is locked into
place when you Flash the PIC. The choice is made in the CCPMX bit of configuration word CONFIG1.
Figure 2 shows the particulars. When we look into
some lab exercises, you’ll get to see examples of this in
action. Incidentally, don’t forget that you always have the
option to completely bypass connecting the compare
module to a pin; in this case, B.0 and B. 3 are free for any
other use you have in mind.
Figure 2. Bit assignments in the
configuration word CONFIG1.