⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key.c

📁 FREESCALE 16位单片机片MC9S12DG128的键盘驱动程序,键盘缓冲区大小可设,支持按键的快加快减操作
💻 C
📖 第 1 页 / 共 2 页
字号:


/*$PAGE*/
/*
*********************************************************************************************************
*                                              INCLUDE FILES
*********************************************************************************************************
*/

#include "KEY.H"
#include "IO_Map.H"
#include "TMR.H"
#include "defines.h"

/*
*********************************************************************************************************
*                                            LOCAL CONSTANTS
*********************************************************************************************************
*/

#define KEY_STATE_UP                 1      /* Key scanning states used in KeyScan()                   */
#define KEY_STATE_DEBOUNCE           2
#define KEY_STATE_RPT_START_DLY      3
#define KEY_STATE_RPT_DLY            4

/*
*********************************************************************************************************
*                                            GLOBAL VARIABLES
*********************************************************************************************************
*/

static  INT8U     KeyBuf[KEY_BUF_SIZE];     /* Keyboard buffer                                         */
static  INT8U     KeyBufInIx;               /* Index into key buf where next scan code will be inserted*/
static  INT8U     KeyBufOutIx;              /* Index into key buf where next scan code will be removed */
static  INT16U    KeyDownTmr;               /* Counts how long key has been pressed                    */
static  INT8U     KeyNRead;                 /* Number of keys read from the keyboard                   */

static  INT8U     KeyRptStartDlyCtr;        /* Number of scan times before auto repeat is started      */
static  INT8U     KeyRptDlyCtr;             /* Number of scan times before auto repeat executes again  */

static  INT8U     KeyScanState;             /* Current state of key scanning function                  */

/*
*********************************************************************************************************
*                                       LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  void      KeyBufIn(INT8U code);     /* Insert scan code into keyboard buffer                   */
static  INT8U     KeyDecode(void);          /* Get scan code from current key pressed                  */
static  BOOLEAN   KeyIsKeyDown(void);       /* See if key has been pressed                             */
static  void  KeyScanTask (void *data);                              

/*$PAGE*/
/*
*********************************************************************************************************
*                                INSERT KEY CHARACTER INTO KEYBOARD BUFFER
*
* Description : This function inserts a key character into the keyboard buffer
* Arguments   : code    is the keyboard scan code to insert into the buffer
* Returns     : none
*********************************************************************************************************
*/

static  void  KeyBufIn (INT8U code)
{
                          /* Start of critical section of code, disable ints    */
    if (KeyNRead < KEY_BUF_SIZE) {               /* Make sure that we don't overflow the buffer        */
        KeyNRead++;                              /* Increment the number of keys read                  */
        KeyBuf[KeyBufInIx++] = code;             /* Store the scan code into the buffer                */
        if (KeyBufInIx >= KEY_BUF_SIZE) {        /* Adjust index to the next scan code to put in buffer*/
            KeyBufInIx = 0;
        }

    } 
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                           DECODE KEYBOARD
*
* Description : This function is called to determine the key scan code of the key pressed.
* Arguments   : none
* Returns     : the key scan code
*********************************************************************************************************
*/

static  INT8U  KeyDecode (void)
{
    INT8U code;
    INT8U temp1,temp2;
    temp1 = PTIJ&0xC0;
    temp2 = PTIP&0xBC;
    if (temp1 == 0) 
    {
       
      if(temp2 == 0x04) 
      {
         code = KeyLeft;   
      } 
      else if (temp2 == 0x08) 
      {
         code = KeyUp;  
      }
      else if (temp2 == 0x10) 
      {
         code = KeyDown;  
      }
      else if(temp2 == 0x20) 
      {
         code = KeyRight;  
      }
      else if(temp2 == 0x80) 
      {
         code = KeyEsc;  
      } 
      else 
      {
         code = NoKey;    
      }
        
    } 
    else if(temp2 == 0)
    {
      if(temp1 == 0x40) 
      {
        code = KeyOnOff;  
      }else if(temp1 == 0x80) 
      {
       code = KeyEnter;  
      } else 
      {
         code = NoKey;   
      }
         
    }

     
    
   /* 
		 PPortVaule = PTIP&0xBC;
     switch ( PPortVaule) 
      {
        case 0x04: 
                   code = KeyLeft;
                   break;
      
        case 0x08: 
                   code = KeyDown;
                   break;
      
      
        case 0x10: 
                   code = KeyUp;
                   break;
      
      
        case 0x20: 
                   code = KeyRight;
                   break;
      
      
        case 0x80: 
                   code = KeyEsc;
                   break;
                  
                    
        default:  
                   code = NoKey; 
                   break;
                  
        }  
      

      switch(PTIJ&0xC0) 
      {
        case 0x40: code = KeyOnOff;
                   break;
                  
        
        case 0x80: code = KeyEnter;
                   break;                      
                   
        default:   code = NoKey; 
                   break;
      }
  
   */
  return (code);  
}

 /*$PAGE*/
/*
*********************************************************************************************************
*                                                 GET KEY
*
* Description : Get a keyboard scan code from the keyboard driver.
* Arguments   : 'to'     is the amount of time KeyGetKey() will wait (in number of ticks) for a key to be
*                        pressed.  A timeout of '0' means that the caller is willing to wait forever for
*                        a key to be pressed.
* Returns     : != 0xFF  is the key scan code of the key pressed
*               == 0xFF  indicates that there is no key in the buffer within the specified timeout
*********************************************************************************************************
*/

INT8U  KeyGetKey (void)
{
    INT8U code;
    if (KeyNRead > 0) {                          /* See if we have keys in the buffer                  */
        KeyNRead--;                              /* Decrement the number of keys read                  */
        code = KeyBuf[KeyBufOutIx];              /* Get scan code from the buffer                      */
        KeyBufOutIx++;
        if (KeyBufOutIx >= KEY_BUF_SIZE) {       /* Adjust index into the keyboard buffer              */
            KeyBufOutIx = 0;
        }
        return (code);                           /* Return the scan code of the key pressed            */
    } 
    //2006.07.10 modified.
    else
    {
    	return(0);
    }	
    
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                         FLUSH KEYBOARD BUFFER
*
* Description : This function clears the keyboard buffer
* Arguments   : none
* Returns     : none
*********************************************************************************************************
*/

void  KeyFlush (void)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -