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

24e   UI{DERST  NDIiIG tHE  GOUITERS:  COUt{tl  G MAEBIES



                    ialligliffr7!si  Demonstration ofthe use ofTMRo as  a Counter  (Contitued)
                         IIfECON-0                 reset  INTCON register
                       ELSE
                       ENDIF
                      GOTO  MAII{

                      END
                       In Program 17.4, we  press  SWI again  and again a  see what  happens in the LCD.
                    For each key press,  you need to both release and hold the button down for a good part
                    of a second  to actually observe the key-down  and key-up effect so that the  750  pause
                    that allows us to look at the LCD can be discounted.


                    Usins  Timerl in Counter  Mode


                    We will  use ?lr?ert as the counter for the marbles because  it is the only timer that can
                    count to more than 255 without having to count fhe interrupts at every overliow of the
                    counter and nake the related calculations.  It will count to the 1000  marbles designated
                    jn  Mr  Marbles' specification.
                       Actually, the microprocessor is much faster than the marbles coming out the chute,
                    but we will  pretend  that we need to really hurry in our counting  prccess  for some  other
                     prccess  we mjght encounter  in the future.
                       If the marbles are coming so  fast we do not have time to update the registers,  we
                     have to set up an inlernal counter and feed the signals that  the intenupt  provides
                     directly into that counter.  The  signals  will have to be debounced  before they can be
                     connected  to t}Ie intemal counter because  these counten arc extremely  fast and will
                     pick up every litdejitter as a countable event.

                     THE BASICS OF COUI{TII{G  DIRECTLY II{TO A TITUER BEING
                     USED AS A COUIITER
                     In order to use  a timer as a cou  er, $Ie must set up the timer as usual and then  change
                     the input to the timer from the intemal clock to the external  signal and set the appro-
                     priate pammeters  for ihe incoming signal  (rising edge, and so on).
                       For Timerl. the 16-bit counter. this  is done  as follows:
                       Setiins Timerl uD as a countef.
                      T1COriI.4 antt  tlCON  5  =  0  for  no  pre  scale
                      P I E 1 . 0 = 1               to  enable  intelrupl
                      I&aCON.6=1
                      T1CON.0=1                     ro  start   the  counter.   We have  to

                                                    lncomlng  s1gnal
                      E1CON.l=1                     selects  PORTC.0  for  input
                                                                                   l
                      t1CON.3=3                     " l s o  . - - d - d  o . e  e c c  D O o l ^ . 0 o -  - n p , L
   245   246   247   248   249   250   251   252   253   254   255