C51 COMPILER V7.09   XKEY                                                                  06/15/2006 17:47:33 PAGE 1   


C51 COMPILER V7.09, COMPILATION OF MODULE XKEY
OBJECT MODULE PLACED IN xKey.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE xKey.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /*
   2          *********************************************************************************************************
   3          *                                     Simple Keypad driver for AT89S52
   4          *
   5          * Programmer    : John Leung, TechToys. Hong Kong
   6          * Web presence  : www.TechToys.com.hk
   7          * Note                  : 
   8          * Language              : Keil C 7.xx 2K limited version under uVision2 IDE
   9          * Hardware              : AT89S52-gLCD-STK1
  10          * Date                  : Version 0.0 (7th June 2006)
  11          *********************************************************************************************************
  12          *                                          USER DEFINED CONSTANTS
  13          *
  14          * Note: These #defines would normally reside in your application specific code.
  15          *********************************************************************************************************
  16          *                                             DESCRIPTION
  17          *
  18          *    The keypad is assumed to be a non-matrix simple keypad. Only single pin (or a few pins)is/are needed
  19          *
  20          *    xKEY_RPT_DLY             Number of scan times before auto repeat executes the function again
  21          *    xKEY_RPT_START_DLY       Number of scan times before auto repeat function engages
  22          *
  23          *    xKEY_SCAN_TASK_DLY       The number of milliseconds between keyboard scans
  24          *
  25          *********************************************************************************************************
  26          */
  27          
  28          /*
  29          *********************************************************************************************************
  30          * Date : 7th June 2006, ported to AT89S52 
  31          *
  32          * A new scan state 'xKEY_STATE_DOWN' and a global variable xKeyRptEn added
  33          * Functions modified: xKeyInit(void), xKeyScanTask(void).
  34          * One more scan-state in xKeyScanTask(void) as xKEY_STATE_DOWN adds the flexiblity of auto repeat
  35          * enable/disable feature. 
  36          * If xKeyRptEn==FALSE, which is the default condition set by xKeyInit(), no auto repeat engages. 
  37          * On the other hand, if xKeyRptEn==TRUE, auto repeat engages after a scan delay of xKEY_RPT_START_DLY
  38          * Please notice that xKEY_RPT_START_DLY increment by one scan-time because of one more scan-state.
  39          *********************************************************************************************************
  40          */
  41          
  42          /*
  43          *********************************************************************************************************
  44          * Date : 7th June 2006, ported to AT89S52
  45          *
  46          * Modification history : Add unsigned int  xKeyGetKeyDownTime(void)
  47          *********************************************************************************************************
  48          */
  49          
  50          
  51          /*$PAGE*/
  52          /*
  53          *********************************************************************************************************
  54          *                                              INCLUDE FILES
  55          *********************************************************************************************************
C51 COMPILER V7.09   XKEY                                                                  06/15/2006 17:47:33 PAGE 2   

  56          */
  57          #include <REGX52.h>
  58          #define  xKEY_GLOBALS
  59          #include "xKey.h"
  60          
  61          /*
  62          *********************************************************************************************************
  63          *                                            LOCAL CONSTANTS
  64          *********************************************************************************************************
  65          */
  66          
  67          #define xKEY_STATE_UP                   1
  68          #define xKEY_STATE_DEBOUNCE             2
  69          #define xKEY_STATE_DOWN                                 3
  70          #define xKEY_STATE_RPT_START_DLY        4
  71          #define xKEY_STATE_RPT_DLY              5
  72          
  73          /*
  74          *********************************************************************************************************
  75          *                                            LOCAL VARIABLES
  76          *********************************************************************************************************
  77          */
  78          
  79          static  unsigned char           xKeyBuf[xKEY_BUF_SIZE]; /* Keypad buffer                                                                                */
  80          static  unsigned char           xKeyBufInIx;                    /* Index into key buf where next scan code inserted             */
  81          static  unsigned char           xKeyBufOutIx;                   /* Index into key buf where next scan code removed              */
  82          static  unsigned int            xKeyDownTmr;                    /* Counts how long a key has been presssed                              */
  83          static  unsigned char           xKeyNRead;
  84          
  85          static  unsigned char           xKeyRptStartDlyCtr;             /* Number of scan times before auto repeat is started   */
  86          static  unsigned char           xKeyRptDlyCtr;                  /* Number of scan times before auto repeat execute again*/
  87          
  88          static  unsigned char           xKeyScanState;          /* Current state of key pins                                    */
  89          
  90          /*
  91          *********************************************************************************************************
  92          *                                        LOCAL FUNCTION PROTOTYPES
  93          *********************************************************************************************************
  94          */
  95          static void     xKeyBufIn(unsigned char keyCode);       /* Insert scan code into keypad buffer                          */
  96          static bit      xKeyIsKeyDown(void);                            /* See if key has been pressed                                          */
  97          
  98          /*
  99          *********************************************************************************************************
 100          *                                               INSERT KEY CHARACTER INTO KEYPAD BUFFER
 101          *
 102          * Description : This function inserts a key character into the keypad buffer
 103          * Arguments   : keyCode    is the key scan code to insert into the buffer
 104          * Returns     : none
 105          *********************************************************************************************************
 106          */
 107          
 108          static  void  xKeyBufIn (unsigned char keyCode)
 109          {
 110   1          if (xKeyNRead < xKEY_BUF_SIZE) {              /* Make sure that we don't overflow the buffer        */
 111   2              xKeyNRead++;                              /* Increment the number of keys read                  */
 112   2              xKeyBuf[xKeyBufInIx++] = keyCode;         /* Store the scan code into the buffer                */
 113   2              if (xKeyBufInIx >= xKEY_BUF_SIZE) {       /* Adjust index to the next scan code to put in buffer*/
 114   3                  xKeyBufInIx = 0;
 115   3              }
 116   2              }
 117   1      }
C51 COMPILER V7.09   XKEY                                                                  06/15/2006 17:47:33 PAGE 3   

 118          
 119          /*$PAGE*/
 120          /*
 121          *********************************************************************************************************
 122          *                                                 GET KEY
 123          *
 124          * Description : Get a keypad scan code from the keypad buffer
 125          * Arguments   : none
 126          * Returns     : != 0xFF  is the key scan code of the key pressed
 127          *               == 0xFF  indicates that there is no key in the buffer
 128          *********************************************************************************************************
 129          */
 130          
 131          unsigned char  xKeyGetKey (void)
 132          {
 133   1          unsigned char keyCode;
 134   1      
 135   1          if (xKeyNRead > 0) {                        /* See if we have keys in the buffer                  */
 136   2              xKeyNRead--;                            /* Decrement the number of keys read                  */
 137   2              keyCode = xKeyBuf[xKeyBufOutIx];        /* Get scan code from the buffer                      */
 138   2              xKeyBufOutIx++;
 139   2              if (xKeyBufOutIx >= xKEY_BUF_SIZE) {    /* Adjust index into the keyboard buffer              */
 140   3                  xKeyBufOutIx = 0;
 141   3              }
 142   2              return (keyCode);                       /* Return the scan code of the key pressed            */
 143   2          } else {
 144   2              return (0xFF);                          /* No scan codes in the buffer, return -1             */
 145   2          }
 146   1      }
 147          
 148          
 149          /*$PAGE*/
 150          /*
 151          *********************************************************************************************************
 152          *                                        GET HOW LONG KEY HAS BEEN PRESSED
 153          *
 154          * Description : This functions return the amount of time the key has been pressed
 155          * Arguments   : none
 156          * Returns     : key down time in millisec
 157          *********************************************************************************************************
 158          */
 159          unsigned int  xKeyGetKeyDownTime(void)
 160          {
 161   1              unsigned int tmr;
 162   1      
 163   1              tmr = xKeyDownTmr;
 164   1              return (tmr*xKEY_SCAN_TASK_DLY);        
 165   1      }
 166          
 167          /*$PAGE*/
 168          /*
 169          *********************************************************************************************************
 170          *                                        SEE IF ANY KEY IN BUFFER
 171          *
 172          * Description : This function checks to see if a key was pressed
 173          * Arguments   : none
 174          * Returns     : TRUE   if a key has been pressed
 175          *               FALSE  if no key pressed
 176          *********************************************************************************************************
 177          */
 178          
 179          bit  xKeyHit (void)
C51 COMPILER V7.09   XKEY                                                                  06/15/2006 17:47:33 PAGE 4   

 180          {
 181   1              static bit hit;
 182   1      
 183   1              hit =(bit)(xKeyNRead > 0) ? TRUE : FALSE;
 184   1      
 185   1              return (hit);
 186   1      }
 187          
 188          
 189          /*
 190          *********************************************************************************************************
 191          *                                           SEE IF KEY PRESSED
 192          *
 193          * Description : This function checks to see if a key is pressed
 194          * Arguments   : none
 195          * Returns     : TRUE   if a key is     pressed
 196          *               FALSE  if a key is not pressed
 197          * Note        :
 198          *********************************************************************************************************
 199          */
 200          
 201          static bit  xKeyIsKeyDown (void)
 202          {
 203   1              if (xKeyCheckPins()){
 204   2                      xKeyDownTmr++;
 205   2                      return (TRUE);
 206   2              } else {
 207   2                      return (FALSE);
 208   2              }
 209   1      }
 210          
 211          /*
 212          *********************************************************************************************************
 213          *                                         KEYPAD SCANNING TASK
 214          *
 215          * Description : This function contains the body of the keypad scanning task. 
 216          *                               The scanning period is determined by KEY_SCAN_TASK_DLY defined under xKey.h.
 217          * Arguments   : none
 218          * Returns     : xKeyScanTask() never returns.
 219          * Remarks      : - An auto repeat of the key pressed will be executed after the key has been pressed for
 220          *                 more than KEY_RPT_START_DLY scan times.  Once the auto repeat has started, the key will
 221          *                 be repeated every KEY_RPT_DLY scan times as long as the key is pressed.  For example,
 222          *                 if the scanning of the keyboard occurs every 50 mS and KEY_RPT_START_DLY is set to 40
 223          *                 and KEY_RPT_DLY is set to 2, then the auto repeat function will engage after 2 seconds
 224          *                 and will repeat every 100 mS (10 times per second).
 225          *********************************************************************************************************
 226          */
 227          
 228          void xKeyScanTask(void)
 229          {
 230   1              unsigned char   keyCode;
 231   1      
 232   1              switch(xKeyScanState) {
 233   2                      case xKEY_STATE_UP:
 234   2                                      if(xKeyIsKeyDown()){
 235   3                                              xKeyScanState = xKEY_STATE_DEBOUNCE;
 236   3                                              xKeyDownTmr = 0;
 237   3                                      }
 238   2                              break;
 239   2      
 240   2                      case xKEY_STATE_DEBOUNCE:
 241   2                                      if(xKeyIsKeyDown()){
C51 COMPILER V7.09   XKEY                                                                  06/15/2006 17:47:33 PAGE 5   

 242   3                                              keyCode = xKeyDecode();
 243   3                                              xKeyBufIn(keyCode);
 244   3                                              xKeyScanState           = xKEY_STATE_DOWN;
 245   3                                      } else {
 246   3                                              xKeyScanState           = xKEY_STATE_UP;
 247   3                                      }
 248   2                              break;
 249   2      
 250   2                      case xKEY_STATE_DOWN:
 251   2                                      if(xKeyIsKeyDown()){
 252   3                                              if (xKeyRptEn == TRUE){                            /* Start delay to auto-repeat function */
 253   4                                                      xKeyRptStartDlyCtr      = xKEY_RPT_START_DLY;   
 254   4                              xKeyScanState           = xKEY_STATE_RPT_START_DLY;
 255   4                                              }
 256   3                                      } else {
 257   3                                              xKeyScanState = xKEY_STATE_DEBOUNCE;
 258   3                                      }
 259   2                              break;
 260   2      
 261   2                      case xKEY_STATE_RPT_START_DLY:
 262   2                                      if(xKeyIsKeyDown()){
 263   3                                              if (xKeyRptStartDlyCtr > 0) {          /* See if we need to delay before auto rpt  */
 264   4                               xKeyRptStartDlyCtr--;             /* Yes, decrement counter to start of rpt   */
 265   4                               if (xKeyRptStartDlyCtr == 0) {    /* If delay to auto repeat is completed ... */
 266   5                                                               keyCode = xKeyDecode();
 267   5                                                               xKeyBufIn(keyCode);
 268   5                                   xKeyRptDlyCtr = xKEY_RPT_DLY; /* Load delay before next repeat            */
 269   5                                   xKeyScanState = xKEY_STATE_RPT_DLY;
 270   5                               }
 271   4                           }
 272   3                                      } else {
 273   3                                              xKeyScanState = xKEY_STATE_DEBOUNCE;   /* Key was not pressed after all            */
 274   3                                      }
 275   2                              break;
 276   2      
 277   2                      case xKEY_STATE_RPT_DLY:
 278   2                                      if (xKeyIsKeyDown()){
 279   3                                              if (xKeyRptDlyCtr > 0) {              /* See if we need to wait before repeat key */
 280   4                               xKeyRptDlyCtr--;                 /* Yes, dec. wait time to next key repeat   */
 281   4                               if (xKeyRptDlyCtr == 0) {        /* See if it's time to repeat key           */
 282   5                                                               keyCode = xKeyDecode();
 283   5                                                               xKeyBufIn(keyCode);
 284   5                                   xKeyRptDlyCtr = xKEY_RPT_DLY;/* Reload delay counter before auto repeat  */
 285   5                               }
 286   4                           }
 287   3                                      } else {
 288   3                                              xKeyScanState = xKEY_STATE_DEBOUNCE;  /* Key was not pressed after all            */
 289   3                                      }
 290   2                              break;
 291   2                              }
 292   1      }
 293          
 294          /*
 295          *********************************************************************************************************
 296          *                                       KEYPAD INITIALIZATION
 297          *
 298          * Description: Keyboard initialization function.  xKeyInit() must be called before calling any other of
 299          *              the user accessible functions.
 300          * Arguments  : none
 301          * Returns    : none
 302          * Remarks        : 
 303          *********************************************************************************************************
C51 COMPILER V7.09   XKEY                                                                  06/15/2006 17:47:33 PAGE 6   

 304          */
 305          
 306          void  xKeyInit (void)
 307          {   
 308   1          xKeyScanState = xKEY_STATE_UP;          /* Keypad should not have a key pressed                                     */
 309   1              xKeyNRead         = 0;
 310   1              xKeyDownTmr   = 0;
 311   1              xKeyBufInIx       = 0;
 312   1              xKeyBufOutIx  = 0;
 313   1              xKeyRptEn         = FALSE;
 314   1              xKeyInitPins();                                                 /* Initialize I/O pins                                                                  */
 315   1      }
 316          
 317          /*
 318          *********************************************************************************************************
 319          *                                  INITIALIZE I/O PINS (HARDWARE DEPENDENT)
 320          *********************************************************************************************************
 321          */
 322          
 323          void  xKeyInitPins (void)
 324          {
 325   1              xKEY_DN = 1;    //set all pins to input
 326   1              xKEY_RT = 1;
 327   1              xKEY_LT = 1;
 328   1              xKEY_UP = 1;
 329   1              xKEY_CTR= 1;
 330   1      }
 331          
 332          
 333          /*$PAGE*/
 334          /*
 335          *********************************************************************************************************
 336          *                                              READ PINS
 337          *
 338          * Description : This function is called to read the pins.
 339          * Arguments   : none
 340          * Returns     : Reading individual pins with internal/external pullup, zeros are keys pressed
 341          *                               TRUE            if any key pressed
 342          *                               FALSE           if no key pressed
 343          * Remarks         : 
 344          *********************************************************************************************************
 345          */
 346          
 347          bit xKeyCheckPins(void)
 348          {
 349   1              static bit hit;
 350   1      
 351   1              hit = (bit)((xKEY_UP==0)||(xKEY_DN==0)||(xKEY_RT==0)||(xKEY_LT==0)||(xKEY_CTR==0)) ? TRUE: FALSE;
 352   1              return (hit);
 353   1      }
 354          
 355          
 356          /*$PAGE*/
 357          /*
 358          *********************************************************************************************************
 359          *                                             DECODE KEYPAD
 360          *
 361          * Description : This function is called to determine the key scan code of the key pressed.
 362          * Arguments   : none
 363          * Returns     : The key scan code (1...MAX. NUMBER OF KEYS DEFINED), else -1 if invalid key.
 364          * Remarks         : 
 365          *********************************************************************************************************
C51 COMPILER V7.09   XKEY                                                                  06/15/2006 17:47:33 PAGE 7   

 366          */
 367          
 368          unsigned char  xKeyDecode (void)
 369          {
 370   1              if (xKEY_UP == 0)
 371   1                      return (xKEY_UP_FLAG);
 372   1              else if (xKEY_DN == 0)
 373   1                      return (xKEY_DN_FLAG);
 374   1              else if (xKEY_RT == 0)
 375   1                      return (xKEY_RT_FLAG);
 376   1              else if (xKEY_LT == 0)
 377   1                      return (xKEY_LT_FLAG);
 378   1              else if (xKEY_CTR == 0)
 379   1                      return (xKEY_CTR_FLAG);
 380   1              else
 381   1                      return (0xFF);
 382   1      }
 383          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    336    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     11       1
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      3    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
