Page 456 - Programming Microcontrollers in C
P. 456

Integrating Keyboard and Clock    441

                          when the ‘1’ button is depressed and the hours increase when the ‘2’
                          button is depressed. The program will need the interrupt service
                          routine, but the recording of the data into a buffer and all of the
                          attendant decoding is unnecessary here. We will use a single global
                          variable to set when a correct key is depressed. That is all required
                          for this application.
                              Let’s start with the initialization routine. This initialization routine
                          needs to do even less than was needed earlier. Consider the code:
                   /* key board initialization routine */
                   void kpinit(void)
                   {

                       KPCRN.LOROWS=0xf;        /* enable keypad rows */
                       KPDRB.COLUMNS=0x0;       /* write 0 to KPDR[15:8] */
                       KPCRN.LOCOLS=0xf;        /* make cols open drain */
                       KDDRN.LOCOLS=0xf;        /* make cols outputs */
                       KDDRN.LOROWS=0x0;        /* and rows inputs */
                       KPSR.KPKD=ON;     /* clear KPKD status flag */
                       KPSR.KDSC=ON;     /* clear key depress synchronizer */
                       KPSR.KDIE=ON;     /* set KDIE bit to enable depress
                                            Interrupt */
                       KPSR.KRIE=OFF;  /* disable release interrupt */
                       FIER.EF6=ON;      /* enable Fast Interrupt number 6*/
                   }
                              The main change observed is that no parameters are passed to the
                          initialization function and none of the variables saved earlier are
                          needed here. Otherwise, this function is identical to the earlier
                          kpinit() function.
                              The changes made in kb_isr() are instructive. There is no
                          substantive change in the function until after the depressed key has
                          been detected. For this program, we are interested only in the detection
                          of a ‘1’ or a ‘2’. These digits correspond to the position values 0x42
                          and 0x22 respectively. After the key has been detected, a simple case
                          statement converts these specific values to the proper digits. If the
                          position numbers are not one of the expected values, *data is
                          assigned a value of zero. This value is used by the main() program
                          to determine when a key has been depressed.

                   /* key board interrupt service routine */
                   void kb_isr(void)
                   {
                       UHWORD column,input;
   451   452   453   454   455   456   457   458   459   460   461