Page 45 - Making PIC Microcontroller Instruments and Controllers
P. 45
3a uNDEBStAilOll{G lllCROCHlp IEGHIIOLOGVS PIC I6FA77A: A DESCRIPTIOII OF tHE ISCU
Very simply stated, an 8 bit limer will count up to 255 and then flip to zero and start
counting from 0 to 255 again. An intenupt occuls (a bit is set) every time the timer rcg-
ister overflows from 255 to 0. We respond to the interupt by doing whatever needs to
be done in response to the interrupt and then resetting the intefupt flag bit (to 0). On
timers that pemit the use of a prescalat the,prescalar lets us increase the time between
intenupts by mulliplying the time between jnterrupts by a definable value in a 2 , 3 ,
or 4-bit location. On timers that can be wdlten to, we can start the counter wherever we
like (to change the inteffupt timing), and on timers that can be read, we can read the
contents whenever we want to.
Example: A l-second timer setting with a prescalar set to 16 would provide us with
an interupt every I6 seconds, and we would have 16 seconds to do whalever we wanted
to do between the interrupts or we would miss the next interlupt.
If we needed an inlenupt every 14.5 seconds, we would use a timer set to 0.5 seconds
and a prescalar of 29 if 29 was specifiable (which it is noo. So not all time intervals can
be created with this strategy because there are limits as to what can be put in the timer
and what can be put in the prescalar when we are using 8/16 bit rcgisters and specific
oscillator speeds.
WATCHDOG TIMER
A watchdog timer is a timer that sets an inteffupt when it runs out so as to tell us the
program has hung up or gone awry for some reason. As such, it is expected that in a
properly writtel program, the watchdog timer wi]l never set an intemrpt. This is accom-
plished by resetiirg the watchdog timer every so often within the program. The com-
piler does this automatically if the watchdog limer option is set. Setting the option does
not guarantee a program that cannot hmg up. software erro$ and inlinite loops that reset
the timer within them can still cause hangups.
PRESCALARS AI{D POSTSCALARS
The value of the scaling factor that will be applied to the timer is determined by the con
tents of2 or 3 bits in the intenupt control register. These bits multiply the time between
inteffupts often by powers of two. Prcscalars and postscalars have the same effect on
the interrupts: They delay them.
COUNTERS
Only Timer0 and Timerl can be used as counters. Timer2 cannot be used as a counter
because it does not have an extemal input line. Both the timers and the counters ar€ cov-
ered in detail in Chapter 6.