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

q6   .r'N-rROLLlxG THE OUTPU-I AND NEADIiIG THE INPUT



                     ln general,  the  ports  on  the microcontrollers  (MCUS)  are designed  so they can  be used
                   as inputs  or as outputs,  In fact, the  ports  can  be  programmed so celtain  pins  on a  po
                   are inputs and others are outputs.  All  we have  to do is lell the  program  what we want
                   done and the compiler  will handle  the details. The compiler  not only allows  you  to define
                   how  you  will use the  pins  of each  port,  it can also set them  up as inputs  or as outputs
                   automatically,  depending  on the instructions  you use to address  the  pin in your program.
                   You have a choice of setting  PORTD to an output port and then setting  pin  I on this
                   ponhigh, or  you  can simplytell  the compilerto  make  pin 1 of PORTD high and  it will
                    take care of the setting up details.
                      The  ports  can be treatcd  just  like any other  memory location  in the microcontroller'
                    By name,  you  can  read them, setthem,  aLnd use them  in calculations and  manipulatlons
                   just  like  you  canwith  any other named orunnamedmemory  location.lfthings  are con_
                    nected  to the po  s and  pins, ihe program  will  interact with, and respond 1o,  whatever
                    is connected  to them.  (Any named  port,  register,  or pin can be addressed  directly by name
                    for all purposes  when using the PBP Compiler They are called out as they are named
                    in the datasheet  for each individual PIC by lhe manufacturer)



                    Blink One  LED


                    Type the  following  program (Prognm5.1) into  yourPC  and  save it  lt does  notneecl
                    to be saved  in the same directory  as t}Ie PBPexe  program. To keep with the con-
                    ventions being  used in the compiler  manual, call  this  program myBLINK.BAS so it
                    does not overwrite  the BLINK.BAS program  provided  on the disk thal came  with
                    the  LAB  Xl.
                      The program  to control pin 0 of PORTD is as fbllows:


                    lliP$iim$lll   controlling  (blinking)  an LED  (Blinking  an  LED  [We  are usins  lhe
                     rightmost  LED on the bargraphl)

                     CLEJAR                 clear  nenory  LocaLions
                     DEFINE  OgC  {
                     nooP:
                       HIGE  PORTD. O       lurns   LED connecled   Lo  D0  oN
                       PAUSE  500           deLay  0.5  seconds
                       IJO!'  PORTD.  O     turns  LED  connecled  to  D0  oFF
                       PAUSE  500           delay  0.5  seconds
                     GOIO  LOOP             qro back  to  Loop  and  repeat  operation
                     END                    aL1  prograns   nust  end  with  END


                      The  program  demonstrates  the most  elementary control we have over an output: that
                    of turning it ON and OFF In this program,  we did not have to set the  port directions
                    (with  the  TRIS conmand), because  the HIGH and  Low  command  tale care of that aulo
                    matically. (If we used PORTD.0=I instead ol HIGH PORTD.0,  we would have  had to
   55   56   57   58   59   60   61   62   63   64   65