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

TIMERS AND COUNTERS
















                   General


                   If  you have no knowledge about timers, you should read this chapter  carefully before
                   taking on the chapters that follow. Those chapters  provide a much morc advanced  dis-
                   cussion of the devices based on their usage, as opposed to the introductory approach
                   prcvided  here. However, there  is some  repetition, so as to allow each  part of this book
                   to stand independendy.
                                                               j
                     Most users will  frnd that using time$ and counters s the  hardest  part of leaming how
                   to use PIC microcontrolle$. With this in mind, we will  Foceed  in a step-by-step manner
                   and build up the  programs  in pieces  that are easier to digest. Once  you get  comfoftable
                   with their setup  procedures, you  will  find that timels and counters arc not so  intimidating.
                     We will  cover  timers and counters separately- Counters are essentially timers that  get
                   their clock input ftom an outside so rce. Therc are two counters in the 16F877A, and
                   they are associated with Timero and Timerl . Timet  cannot be used as a counter because
                   it has no way to rcad an extemal signal  into this timer.

                   No|€  The clockfre.quercy utilizd  by the timers is o e'foul'th ofthe os,:illator
                                                                                  frequencj.
                   This is the  frequencr  of the instuction  clock. This means that the counte^ are allected
                   by every  founh   count of the mdin oscillator. The  Irequenct  is rcfeffed to as Fosc/4 in
                   the literaturc . When rcsponding  to an extemal clock sigwl,  the response is to the actual
                   frequencr  ofthe input.

                   Caulion   The PICBASIC PRO Compiler  generutes  code  that does not respond to inter
                   rupts \thile a conpiled instruction is being executed. Therefore, lone PAUSEs  (meanins
                   long enough to los. ar interrupt sig/al, dependiB on how the  tirur  is set up) cdn l.ad to
                   lost interrupts if more than one interrupt occws during the  paute.  Since  interrupts are used
                   for  the express  purpose  oJ handling critical response/timing neeAr,  this is most undesirable .
                   PAUSE comnands should be usedwith care unAer these co ditions.The  prcgrum  samples
                   ptorided nen give  emmples  oI how theJ can be wrixen to generate  shorter  pauses.
   88   89   90   91   92   93   94   95   96   97   98