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

couItEhg    t05



                   I  Decide which  counter  (timer)  to use.
                   I  Tell the counter  where the signal is coming from.
                   t  Tell it whether  to count on a rising or falling edge.
                   t  Decide what target count we are  looking for.
                   I  Tell the counler wheie to start countirg because rhe interrupt will  occur when the
                     counter  gets  full.
                   I  Decide whether  we will  need to scale the count by sedng the scalar(s).


                     Once we start a  counter, the counting contin es until we deactivate it. There is no  other
                   way to stop it, nor any reason for doing so.  It will  reset if the MCU is r€set and it can
                   be reset  by writirg  to it. The rest has  to do with knowing what bits to ser  in the control
                   registers to get  the counlers  fo operate in the way we want them to.

                   USIIIG TIIIIERO AS A COUNTER

                   Note  Though often called TIMER0, a  d referred to as TIMER'  herc andi   the
                   datasheet, the real designation  of this timer address i  TMRj.
                     The thr€e registers r€lated  to the control ofthe TIMER0 module  are TMRo,INTCON,
                   and OPIION_REG. INTCON is the intenxpt control register.  See the datasheet for mol€
                   information.
                     This counter  has the following  properties:

                   I  8-bit timer/counter
                   r  Readable and writable
                   r  8-bit software  programmable prescalar
                   :  Internal or extemal clock select
                   :  Interupt  on overllow ftom FFh to 00h
                   I  Edge select for extemal clock

                     Counter mode is selected by setting  OPIION_REG.5= l.

                   I  The extemal input for the  timer will  come in or PORTA.4,  which is pin 6 on the PIC.
                   I  The edge dir€ction is selected in OPION_RXG.4 (l=Rising  edge).
                   I  The prescalar  is assigned  with bits 0 to 3 of the OPTION  REc  register.  (See page
                     49 oftbe datasheet.)

                     Note again that the watchdog timer.d,rror  use the prescalar  when the  prescalar  is
                   being used by Time  .
                     Since this is an 8-bit counter,  it is suited to the counting  of small number of counts,
                   but longer counts can be accomrnodated  by using a murine ro k€ep  t{ack of the ifterrupts.
                     Let's use the counter  to count the pulses  received from a 20,s1ot  encoder mounted
                   on a small DC motor. This same sowce will  be used later lor the Timerl  experiment
                   for comparison  between countets,
                     We will set up to use the LCD display  so  we  calt display ce(ain rcgisters during the  oper-
                   ation  of Progmm  65. We will  also set  up to rcad the  potentiometers  so we can use  th€ir
                   values  to modily the  pmgram  as it runs. ODly POT0 and POT1 are  used in the  program.
   110   111   112   113   114   115   116   117   118   119   120