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

couxtEEs    t0t



                   lRgx!ffij][&*   ljsing  Tlmero: Program  counts the  pulses  trom  a motorddven
                    encoder (You  can change  the speed  olthe moior and  thetime for counts wilh  the 2  pOTs)
                    (Continued)

                    PAUSE  .A.DVAIJI
                    X=EI{Ro
                    IF  ADVAIJO>2o  EIIEN
                      EPWM 2.  ADVAIJO, 32000
                      LCDOUT  $FE,  $80,  DEC4 X,    "DEC   jIwAl-l,   "   .
                      r,cDouT  $FE.  $c0.         ,DEC  ADVAIJO,  Z
                    EI,SE
                      t cDorx! sFE,  $c0,  .P!t!l  roo r,ow  \rDEc   aDvlrlo,  z  \
                    EIIDIF
                    GOTO  LOOP
                    ENI)

                     Play  with the values  of the two potentiometers  to see what happens.  Be carcful about
                   overllowing the counter  past  255 . Unexpected results  can appear.
                     The Timer0 counter  is affected  by rhe OPTION REGISTER bits as follows:

                     OPIION_REG.6 =  1     ;  Inrerrupr  on rising edge
                     OPTION_REG.s  = 0    ;  Extemal clock
                     OPTION-REG4  = 1     ;  Increment  on falling edge  not used

                     OPTION_REG,3  =  O   ;  Assign prcscalar  to Time
                     OPTION-REG.2  = I    ; I  These  3 bits set rhe  prescalar
                    OPTION  REG.I  = 1    ; I  You can  experiment wirh changing these 3

                                   =
                    OPTION_REG.O  1       ; I  bits to see what  happens.
                    Put these  and other values  in the  program  and run the program.  Se€ what happens.

                  USING IIMERI  AS A COUIITER
                  The operation  ofTimeil  as a counter  is similar to the operation  ofTimer0, but because
                  Timerl is a |  6-bit timer, much  longer counts  can be handled,  and counrs  coming in at
                  faster rates  can be counted.  h also means  that a lot more  can be done in the  Timerloop
                  and Blinkerloop  routines  if the program  is designed  to do so. However,  the setup for
                  Timerl  is more complicated  because  of the more numercus  oDrions  available.
                    The  differcnce.  belween the use  of rhe r$o timers  have to do  s rth  lhe serup ot lhe
                  controlling rcgisters. Timerl  is controlled  by/uses six registen as compa(ed  to three for
                  Timero.  They are:

                    INTCON      Intenupt control rcgister
                    PIR1        Peripheral  intefiupt register I
   112   113   114   115   116   117   118   119   120   121   122