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

Trr/tERs llto  cout{tERs



                                                          limer operalion that depends value ol
                   iiB!!6iii!U$!i:  Timerl usage  (Rudimentary                   on
                     POr-l)  (Continued)
                       IF  PORIB.s   =  0  THEI  STOPCI,OCK  ;
                      TMRIB=AIRESE
                       PIRl=0
                     RESttltE                      ;  go  back  to  the  nain   routine
                     ENABIJE

                     DISABIJE
                     sraRtct ocx:
                       rNacoN  =  %10100011        ;  enable  T'MR1 interrupts
                       TICRS  =  0
                     Gcfro  uarNt  ooP

                     sroPct  ocK:
                       rNEcoN  -  %10000011         ;  disable   TMR1 interrupts
                       PAUSE  2
                       TICKS  =  0
                     GOITO !4AINI.OOP

                     CUEIICLOCK.
                       rMtcoN  =  %10000011         ;  disable  TMR1 interrupts
                       UINS  =  0
                       SECS  =  0
                       fEMEBS  =  0
                       TICI(g  =  0
                     GdTO !4AINI,OOP
                     ENAEI.E
                     END


                      Run this  Fogram   to see how  the setting of the potentiometer  affects the operation  of
                    the stopwatch.  It becomes clear that choosing  how the intempt  will  serve our purposes
                    is very  impoitant,  and a bad choice can pretty  much  compromise  the operation  of  the
                    program.
                      We can read the limer  and the intenxpts  at our discretion either befol€ or after an irter-
                    rupt has occufed,  and the interupt  flag can be cleafed whenever we lvish, if  it has been
                    set. lf  it has not been set, fhere is no need to clear it.
                      Figure  6.1  provides  a diagrammatic  rcpresentation  of  how  an interrupt  routine  is
                    implemented  in a typical  program.
                      Even the 16 bit  Timerl  on the  l6F877A  cannot time a long interval.  Repeated inter-
                    vals  have  to  be put  together  to  create  long  time  pedods.  The  longest  possible  time
                    between interupts  for Timerl  (with a 4-MHz  clock) is 0.524288 seconds. The maxr-
                    mum prescale  value is 1:8.  The postscalar  is only available on Timer2 (which in any
                    case  is a shorter s-bit  timer). This results in a maximum  time and is determined by multi-
                    plying  the instruction  clock  cycle  (1  tlsec  @ 4 MHz)  by the prescale (8) by the number
                                                                 *  *
                    of counts from  one overfiow  to the next (65536).  1  psec  8  655 36  =  0.524288 seconds
                    On a 20-MHz machine,  the interval would be one-fifth of this.
   103   104   105   106   107   108   109   110   111   112   113