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

📄 key.c

📁 FREESCALE 16位单片机片MC9S12DG128的键盘驱动程序,键盘缓冲区大小可设,支持按键的快加快减操作
💻 C
📖 第 1 页 / 共 2 页
字号:
    while (KeyHit()) {                           /* While there are keys in the buffer...              */
        KeyGetKey();                            /* ... extract the next key from the buffer           */
    }
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                  GET HOW LONG KEY HAS BEEN PRESSED
*
* Description : This function returns the amount of time the key has been pressed.
* Arguments   : none
* Returns     : key down time in 'milliseconds'
*********************************************************************************************************
*/

INT32U  KeyGetKeyDownTime (void)
{
    INT16U tmr;

    tmr = KeyDownTmr;
    return (tmr * KEY_SCAN_TASK_DLY);
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                        SEE IF ANY KEY IN BUFFER
*
* Description : This function checks to see if a key was pressed
* Arguments   : none
* Returns     : TRUE   if a key has been pressed
*               FALSE  if no key pressed
*********************************************************************************************************
*/

BOOLEAN  KeyHit (void)
{
    BOOLEAN hit;

    hit = (BOOLEAN)(KeyNRead > 0) ? TRUE : FALSE;
    return (hit);
}

/*
*********************************************************************************************************
*                                       KEYBOARD INITIALIZATION
*
* Description: Keyboard initialization function.  KeyInit() must be called before calling any other of
*              the user accessible functions.
* Arguments  : none
* Returns    : none
*********************************************************************************************************
*/

void  KeyInit (void)
{
    KeyScanState = KEY_STATE_UP;                 /* Keyboard should not have a key pressed             */
    KeyNRead     = 0;                            /* Clear the number of keys read                      */
    KeyDownTmr   = 0;
    KeyBufInIx   = 0;                            /* Key codes inserted at  the beginning of the buffer */
    KeyBufOutIx  = 0;                            /* Key codes removed from the beginning of the buffer */
    DDRP = 0x02;      	
   	DDRJ = 0x00;       

}





/*$PAGE*/
/*
*********************************************************************************************************
*                                           SEE IF KEY PRESSED
*
* Description : This function checks to see if a key is pressed
* Arguments   : none
* Returns     : TRUE   if a key is     pressed
*               FALSE  if a key is not pressed
* Note        : (1 << KEY_MAX_COLS) - 1   is used as a mask to isolate the column inputs (i.e. mask off
*                                         the SHIFT keys).
*********************************************************************************************************
*/

static  BOOLEAN  KeyIsKeyDown (void)
{ 
   
    INT8U temp1,temp2;
     temp1 = PTIJ&0xC0;
     temp2 = PTIP&0xBC;
    if (temp1 == 0) 
    {
       
      if(temp2 == 0x04) 
      {
        KeyDownTmr++;
        return (TRUE);   
      } 
      else if (temp2 == 0x08) 
      {
        KeyDownTmr++;
        return (TRUE);  
      }
      else if (temp2 == 0x10) 
      {
        KeyDownTmr++;
        return (TRUE);  
      }
      else if(temp2 == 0x20) 
      {
        KeyDownTmr++;
        return (TRUE);  
      }
      else if(temp2 == 0x80) 
      {
        KeyDownTmr++;
        return (TRUE);  
      } 
      else 
      {
        return (FALSE);   
      }
        
    } 
    else if(temp2 == 0)
    {
      if(temp1 == 0x40) 
      {
        KeyDownTmr++;                                      /* Update key down counter                  */
        return (TRUE);  
      }else if(temp1 == 0x80) 
      {
        KeyDownTmr++;                                      /* Update key down counter                  */
        return (TRUE);   
      } else 
      {
         return (FALSE);   
      }
         
    }

    
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                         KEYBOARD SCANNING TASK
*
* Description : This function contains the body of the keyboard scanning task.  The task should be
*               assigned a low priority.  The scanning period is determined by KEY_SCAN_TASK_DLY.
* Arguments   : 'data'   is a pointer to data passed to task when task is created (NOT USED).
* Returns     : KeyScanTask() never returns.
* Notes       : - An auto repeat of the key pressed will be executed after the key has been pressed for
*                 more than KEY_RPT_START_DLY scan times.  Once the auto repeat has started, the key will
*                 be repeated every KEY_RPT_DLY scan times as long as the key is pressed.  For example,
*                 if the scanning of the keyboard occurs every 50 mS and KEY_RPT_START_DLY is set to 40
*                 and KEY_RPT_DLY is set to 2, then the auto repeat function will engage after 2 seconds
*                 and will repeat every 100 mS (10 times per second).
*********************************************************************************************************
*/

/*$PAGE*/
static  void  KeyScanTask (void *data)
{
    INT8U code;

			 data = data;  
        switch (KeyScanState) {
            case KEY_STATE_UP:                             /* See if need to look for a key pressed    */
                 if (KeyIsKeyDown()) {                     /* See if key is pressed                    */
                     KeyScanState = KEY_STATE_DEBOUNCE;    /* Next call we will have debounced the key */
                     KeyDownTmr   = 0;                     /* Reset key down timer                     */
                 }
                 break;

            case KEY_STATE_DEBOUNCE:                       /* Key pressed, get scan code and buffer    */
                 if (KeyIsKeyDown()) {                     /* See if key is pressed                    */
                     code              = KeyDecode();      /* Determine the key scan code              */
                     KeyBufIn(code);                       /* Input scan code in buffer                */
                     KeyRptStartDlyCtr = KEY_RPT_START_DLY;/* Start delay to auto-repeat function      */
                     KeyScanState      = KEY_STATE_RPT_START_DLY;
                 } else {
                     KeyScanState      = KEY_STATE_UP;     /* Key was not pressed after all!           */
                 }
                 break;

            case KEY_STATE_RPT_START_DLY:
                 if (KeyIsKeyDown()) {                     /* See if key is still pressed              */
                     if (KeyRptStartDlyCtr > 0) {          /* See if we need to delay before auto rpt  */
                         KeyRptStartDlyCtr--;              /* Yes, decrement counter to start of rpt   */
                         if (KeyRptStartDlyCtr == 0) {     /* If delay to auto repeat is completed ... */
                             code         = KeyDecode();   /* Determine the key scan code              */
                             KeyBufIn(code);               /* Input scan code in buffer                */
                             KeyRptDlyCtr = KEY_RPT_DLY;   /* Load delay before next repeat            */
                             KeyScanState = KEY_STATE_RPT_DLY;
                         }
                     }
                 } else {
                     KeyScanState = KEY_STATE_DEBOUNCE;    /* Key was not pressed after all            */
                 }
                 break;

            case KEY_STATE_RPT_DLY:
                 if (KeyIsKeyDown()) {                     /* See if key is still pressed              */
                     if (KeyRptDlyCtr > 0) {               /* See if we need to wait before repeat key */
                         KeyRptDlyCtr--;                   /* Yes, dec. wait time to next key repeat   */
                         if (KeyRptDlyCtr == 0) {          /* See if it's time to repeat key           */
                             code         = KeyDecode();   /* Determine the key scan code              */
                             KeyBufIn(code);               /* Input scan code in buffer                */
                             KeyRptDlyCtr = KEY_RPT_DLY;   /* Reload delay counter before auto repeat  */
                         }
                     }
                 } else {
                     KeyScanState = KEY_STATE_DEBOUNCE;    /* Key was not pressed after all            */
                 }
                 break;
        }

 //复位键盘扫描定时器      
 TmrReset (KEYSCAN_TIMER);
 //重新启动键盘扫描定时器
 TmrStart (KEYSCAN_TIMER);
}



void KeyScanTimerInit(void)
{
	//配置键盘扫描定时器                    
    TmrCfgFnct(KEYSCAN_TIMER, KeyScanTask, NULL);      //config the function.
    //设定键盘扫描时间为50ms
	TmrSetT(KEYSCAN_TIMER, 1);	
    //启动键盘扫描计时器
    TmrStart (KEYSCAN_TIMER);
}

⌨️ 快捷键说明

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