Page 447 - Programming Microcontrollers in C
P. 447

432    Chapter 8  MCORE, A RISC Machine

                          Every function in the program with the exception of main() has a
                          function prototype at the beginning of the program. In this case, the
                          functions are all void functions that require no arguments. It is
                          important, however, that every programmer get into the habit of
                          placing these prototypes at the beginning of their programs, either in
                          the form of a header file or a direct listing in the program.

            Keyboard

                              The MMC2001 chip has a built-in keyboard interface. This
                          interface can be either synchronously polled by the program, or it
                          can be asynchronously controlled through the use of an interrupt.
                          We will examine the asynchronous control here. This program requires
                          three parts. The first is the initialization routine. This routine sets up
                          the operation of the keyboard to cause an interrupt whenever a key is
                          depressed. The interrupt service detects which key is depressed and
                          saves it in a buffer. The program also saves a semaphore passed to
                          the initialization routine. This semaphore is released when the end of
                          a line is detected in the input. Then the main program prints the
                          buffer to the terminal screen.
                              The initialization routine is shown below:
                   #include “mmc2001.h”
                   #include “keypad.h”
                   #include “intctl.h”

                   static int semaph;
                   static BYTE *data,*savedata;
                   static int leng;

                   /* key board initialization routine */
                   void kpinit(int sem, BYTE *s, int length)
                   {

                      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 */
   442   443   444   445   446   447   448   449   450   451   452