Page 94 - Making PIC Microcontroller Instruments and Controllers
P. 94

A4   TIIIIERSAI{DCOUIITERS



                    Timers


                    Timedl will be covered in more detail  as a  protollpical timer, and discussion  and exam
                    ples  for the use ofTimerl  and Tirner2  will be  providcd.
                                                            is a bit more  complicated than  what we
                      The use oftimers  internal to microprocessors
                                                                  amount ofsetup  required before
                    have  been doing so far because  therc is a considerable
                    the limercan  be used,  rnd also because the options  for setting up  the timers are exten-
                    sivc.  We will cover lhe timers  one at a time  in an intoductory  manner, but be  warned
                    that there  is an entirc man\al  (Plcnicro  Micl Range MCU Faniry  Rekrence Manual
                     lDS33023l)  available liom Microchip  Technology  Inc- that covers nothing  but timers'
                     so our coverage  here will, olnccessity,  be rudimentary.
                                                                  how  to tum them ON and OFF
                       Understanding  timers  has to do with understanding
                     and  how to read and set  the various bits and  bytes rhat relatc  to them. Essentially,  in ihe
                     typical  timer applicatiot,  you  turn a tiner ON by turning on its enable bit. The timer
                     then counts  a certain number ofclock cycles and  sets all interrupt  bit, thus causing  an
                     intemrpt. Youlll  progmm  responds  to the intenupt by execnting a specifc inkrrupt  han'
                     dling routine ard iJ|'et\  clearing the intenupt bit. ]lhe  progra"n  then relurns  to wherever
                     it was when the  inteffupt occurred.  The  pre/postscalars have to do with modifying the
                     time  it takes for an inteffupt to take  place.  The hard  pail is finding ou! which  bit does
                     what and where it is located  ,  so reading and  understarding the  datasheet chaptcr on the
                     tiner you  are using is imperative.  There is no escaping  ihis horrorl
                       Timers allow  the micrcontmller to cr€aF and rcact to  chronological cvents  These include:

                     :  Timing  events
                     I  The creation  of clocks lbr various  purposes
                     I  Generating  timed interrupts
                     I  Controling  PWM generation
                                                                to do  work (and  back to sleep)
                     !  Waking the PIC  from its sleep mode at  jntervals
                     !  Special  use oflhe watchdog timer

                       The  PIC 16F877Ahas thrce  intern.rl timers.  There is also the watchdog  timer,which
                     is discussed  after the standard timers.

                     t  Timero  An 8-bit  free-running timer/counter  with an optioDal  prescalar. It's the
                       simplest  of the timers  to use.
                     !  Timerl   A 16 bi! tiner that can be used as  a timer or as a counter-  It is the only I6-bit
                       timer  that can be used as  a counter.It is also the  most complicaled of the timers.
                     r  Timer2  An 8-bit timer  with a prescalar  and  postscalar  and cannot  be used as  a
                              There i. n,J Inpul  lrne for  rhi.  lim(r.
                       counrer.
                       Each timer  has a timer control  register that sels the options  and  properties  that the timer
                     will exhibit.All the time$ are similar  and each of them  has special features  that  givc  it
                     special  propertics. It is imperative  you  refer to your  datasheet  for the PIC l6F877A as
                     you  experiment with the timer functions.  Once  you start to understand what  ihe PIC
                     designers  are up to wilh lhe  timerfunctions. it will start to come  together in yourmind
   89   90   91   92   93   94   95   96   97   98   99