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

fllitEFo



                     Timers can have  prescalars  and/or  postscalaN  associared with rhem  that can be used
                   to multiply the timer setting by a limited number  of integer  counts. The scalitg ability
                   is not adequate to allow all exact time intervals to be created  but is adequate for all prac-
                   tical purposes.  To the inability to create  perfectly  timed intenupts, we have to add the
                   uncertainty in the fiequency of the oscillator crystal, which is usually rct  exacrly what
                   it is stated to be (and  which is affected  by the ambient temperature as the circuirry warms
                   up). Though fairly accurate  timings can be achieved with the hardware  as rec€ived, addi
                   tional software adjustments  may have to be added if  exrremely accumte results are
                   desired. The software  can be designed to make a correction to the timing every so often
                   to make it more accurate. We  will  also need an extemal  sourc€ that is at least as accu-
                   rate as we want our timer to be, so we can veriry the acclrmcy of the device we create.


                   TimerO



                   l-€t's write a simple  program  to see how Time  works. We will  use the LED bargraph
                   to show us what is going on inside the miqocontroller.
                     As always, the bargraph is connected to the eight lines of PORTD o{ the LAB  Xl.
                     First,Iet's write a  program  that will  light the two LEDS connected  to D0 and Dl  alter
                   nately.  (See  Program  6.1 .) Having them light altemarely  lers  you know thar the  program
                   is running, or morc accurately, it lets you know that the segment of the program that
                   conlains  this  pan  of the code is working. These  two LEDS will be used to repr€sent the
                   foreground  task in our program.  There is no timer process  in this program  at this srage.
                   There  are no interrupts. The program  just blinks the LEDS.


                   lF!i0E!]!ffiS:   Foreground  program  blinks two LEDS alternately  (No  timer is  being
                    used in this  program  atthis lime)
                    CIEJAR
                    DEFINE  OSC  {
                    TRISD  .  t11110000          make  D0  lo  D3  outputs
                                                 turn  off  bi!  D0
                    PORTD.I  -  1                turn  on  bit   D1
                    IIIIPHA  1IAR  WORD

                    MAIAILOOP !
                      I F P O R I F D . 1 = 0 X H E N  the  next  tines  of  code  turn  the  LEDS ON
                        PORTD.I  =  1            if  lhey  are  oFF
                        PORTD.O =  O
                      EIJSE
                       PORTD.I   =  O            and  OFF if   they
                       PORTD.o  -  1
                      EI1IDII'
                      F O R . N T P S A = 1 T O 3 0 0  this  loop  replaces  a  lonq  pause  comand
                       PAUSTOS  100              uith  short  pauses  thal  are  essentially
                      NEJI(T II,PIIA                            r ' 1 1 6 . | ^ . | , ' c ' . | ' ' '
                    GOTO  MAIIILOOP              do  it  all   forever
                    END                          all   prosrds   need  to  end  with  END
   90   91   92   93   94   95   96   97   98   99   100