?? key.c
字號:
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 + -