U8 CloseBit=0; //当为1时,指示进入关机状态,如果未松手,黑屏
static U8 KeyBuf[KEY_BUF_SIZE]; /* Keyboard buffer */
static U8 KeyBufInIx; /* Index into key buf where next scan code will be inserted*/
static U8 KeyBufOutIx; /* Index into key buf where next scan code will be removed */
static U16 KeyDownTmr; /* Counts how long key has been pressed */
static U8 KeyNRead; /* Number of keys read from the keyboard */
static U8 KeyRptStartDlyCtr; /* Number of scan times before auto repeat is started */
static U8 KeyRptDlyCtr; /* Number of scan times before auto repeat executes again */
static U8 KeyScanState; /* Current state of key scanning function */
static U32 KeyScanTaskStk[KEY_SCAN_TASK_STK_SIZE/4]; /* Keyboard scanning task stack */
static OS_SEM KeySemPtr; /* Pointer to keyboard semaphore */
OS_TID TIDKeySTask;
U8 const UserKeyMapTbl[]={
3,2,1,12,0xff,
6,5,4,13,18,
9,8,7,14,17,
10,0,11,15,16
};
void KeyBufIn(U8 code); /* Insert scan code into keyboard buffer */
static U8 KeyDecode(void); /* Get scan code from current key pressed */
static BOOL KeyIsKeyDown(void); /* See if key has been pressed */
void KeyScanTask(void); /* Keyboard scanning task */
void KeyInit (void)
{
KeySelRow(KEY_ALL_ROWS); /* Select all row */
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 */
os_sem_init (KeySemPtr,0); /* Initialize the keyboard semaphore */
KeyInitPort(); /* Initialize I/O ports used in keyboard driver */
TIDKeySTask=os_tsk_create_user (KeyScanTask, KEY_SCAN_TASK_PRIO, &KeyScanTaskStk, sizeof(KeyScanTaskStk));
}
void KeyScanTask (void) __task{ 出错位置
U8 code;
U8 CloseCnt;
U8 OpenBit=0; //初始化为0,只有松手后,才置1,关机只有1后有效
U32 Buf;
//增加关机检测功能
FIO0DIR |= (1 << 0); //P0.0 初始化为输出
FIO0SET = (1 << 0); //上电强制拉高
CloseCnt=0;
for (;;) {
//增加关机检测
Buf=FIO2PIN; //读IO口
if((Buf&(1 << 13))==0){
if(CloseCnt<200) CloseCnt++;
if((CloseCnt>8)&&OpenBit){ //当大于8时,且不是开机按键未松手,给关机标志
CloseBit=1;
} //当大于10时,给关机标志
}
else {
CloseCnt=0; //一松手,就清0
OpenBit=1; //只有松手,才能关机
}
os_dly_wait (KEY_SCAN_TASK_DLY); /* Delay between keyboard scans */
switch (KeyScanState) {
case KEY_STATE_UP: /* See if need to look for a key pressed */
KeyDownTmr = 0; /* Reset key down timer */
//当没有键时,自动清0,相当于记录正在按下键的时间
if (KeyIsKeyDown()) { /* See if key is pressed */
KeyScanState = KEY_STATE_DEBOUNCE; /* Next call we will have debounced the key */
}
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 {
KeySelRow(KEY_ALL_ROWS); /* Select all row */
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;
}
}
}
程序有点多,粘了部分出错的地方,可能是加RL的原因,本来已经是很老的程序了,调的还剩这句,大家法眼过一下
SRC\KEY.C(358): error: #130: expected a "{" |