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

CONTFOLLING THE OUIFUf  AI{D READII{G THE IIIPUT



                    Using the HPWM  (hadware  PWM) connnand is abit more compucated  in that we must
                    defiDe certain  parameters  belbre we can usc thc comnrand.  The necessary defines arc



                     DEFINE  CCP1  REG   PORTC     ;  Port   to  be  used  by  IIPI!'I4 1
                     DEFINE  CCP1  BIT  2          ,  Pin  to  be  used  by  HPVil4 1
                     DEFINE  CCP2  REG  PORIIC     ;  Port   to  be  used  by  HPIiTM 2
                     DEFINE  CCP2  BIT  1          ;  Pin  !o  be  used  by  HPidM 2

                    You  also  have to define which timefrhe signal will use so other timers canbc used  for
                    other  purposes  while the signal is being  generated.  If  a timer is no1 spccjiicd,  the
                    syslem dclaults to  Timerl. the  16  bit rimer
                      The command is
                     HPt'lM Channel,   Dutl,cycle,   Flequency



                     DEFINE.OSC  4
                     gPm,t  1,  127,  1500         '  qe  a,  o  6  oJ  <q  o  I  d  Drl\  a   00   p

                    creales a 50  pcrccnl  duly cycle  PWM signal at 1500 Hz (as  affected by the definltion
                    of OSC) on PORTC.2 conlinuously  in the background.  Also see  program  5.9.
                      The sonmand can be updalcd during  runlimc from within a  program.  As might bc
                    expected, the  pir  cannot be used lbr any othcr  puryose  as  long as it is generating  the
                    PWM signal. Turn OFF the PWM mode at lhe CCPconllol rcgister  to use the  pin  as a
                    normal  pin.  See the datasheet for mofe infomatior.
                      The frequencies  generated  afe limited by the frequency ol thc oscillalor being  used
                    to clock the  PIC  processo.  The minimum freqrency for the PIC 16F877A is 1221 Hz
                    (with  a 20 MHz oscillator).  Not  all frequencies can be  generated.  See thc PICBASIC
                    PRO Compiler manual lbr morc infonnalion on other  frequencies.
                    'l:P.&attim
                              5;9:  Generates a  tone on the piezo  speaker  (There  is no looping  in ihis
                     prograrn)
                     CIJEAR
                     DEFINE  OSC  4
                     DEFINE  CCP1  REG  PORTC        pori   to  be  used  by  HP\tl4 1
                     DEFTNE  CCP1  BIT  2            pln  !'o De uceo  oy  Hlwu  r
                                                     since  no  riner  is  denned,
                                                     Tinerl  vi11  lre  used;
                     HPtill{ !,L27,2500              lhe  tone  comand
                     FAUSE  100                      pause  -1  sec  to  hear  tone
                     END                             end  proqran  to  sLop  tone-
                    Next. we will generate  sone telephone louchlone loncs on the  speaker to demoNtrale
                    the capability  provided  by the DTMFOUT command.  (Scc  Program 5.10.)

                     DIIMFOVT Pin,   {Oms,   Offns}   ITon€#tTone#,,.}]
                    Since  \\,e will be using Pin C2. ourcommand will look similar !o thc  preceding  because
                    rvc  are using default  vatues for ONms  and OFFms
   67   68   69   70   71   72   73   74   75   76   77