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

READIIIG  THE II{PUIS  7.I


                    program  with the scanning  of the nibbles  in PORIB added  to dete.mine  what happened
                    and  when it happened.
                      A loop scans  the high  nibbte  ofPORTB, the outpur  from the keyboard.  When  all
                    4 bits are  pulled  high, rhis nibble  will  be  reaal as HEX F. If it is HEX F, no keys are  down
                    and we rescan  the keys. If, howevet a key has  been  pressed,  the answer  will  be orher
                    than HEX F and  can be interpreted  as follows:

                      If 84 is low the answer  wi]I be HEX E (15  t=14)   10 Column  I
                      If85  is low the answer  will be HEX D (15-2=t3)   01  Cotumn 2

                      It86  is low the answer  will be HEX B (15  4=11) 10  Colunn 3
                      IfBT is low  the answer  will be  HEX T  (15-8=7)  0tLtCotumn4

                      To determine  which ro\'r'  the key ihat was  pressed  is in, we have to know which of
                   the  bits in rhe  low nibble had been  faken LOW by the scanning rouline.
                     The values  ior the low nibble are as follow..

                     IfB0  is low the low nibble  will be HEX E (15-l=14) I I l0 Row I
                     IfBl  is low the low nibble  witl be HEX D (15-2=t  3) 1tol Row  2

                     ff 82 is low the  low nibble  will be  HEX B (  15  4=ll)  I0   Row  3
                     IfB3  is low the low  nibble  willbe HEX 7 (15-8=7)  011I  Row 4

                     Haying Ihe tuo pieces of preceding infomation  lets us identify  rhe key thar was
                   pressed.  No matter how many keys we have and  no maner  how they are laid out, the
                   scanning  routine to read the  keyboard  will  be something  like what wasjust explained.
                     Next,  in Program  5.14  we will display the  contents  ofthe entire  byte on the first line of
                   the  l,CD so we can actually  see  what is happening  in the  regisrer reFesenteal  by PORTB
                   as  we scan the  lines. Then,  on the  second line we will  show the low byte and  the high byte
                   sepantely  so we can  see what each  key  press  does. We  have added  a l/20 second delay  in
                   the loop (so  we can  se€ the scanned  value),  so we have  to holal  each key down for at least
                   l/20 second  for rhe  scan to make  sure  the key press  will register  and show  in the display.

                   *!8in*.!!i61tr,   Read  keyboard (Reading  lhe keyboard  rows  and cotumns)
                    Ct.EAR
                    DEFINE   OSC   {
                    DEF1NE  ICD_DREG  PORTD              LCD defines  follou
                    DEFIIIE   IiCD  DBIT  4
                    DEFTNE  LCD  RSREG  PORTE
                    DEAINE  LCD  RSBIT   O
                    DEFINE   LCD  EREG  PORTE
                    DEFINE  IJCD  EB1T  1
                    ADCONI  -  800000111                 nake  PORTA  and  PORTE  digital

                                                                                 (conti  uea
   76   77   78   79   80   81   82   83   84   85   86