贴出来的是按键处理的一段小程序,附件中有关于按键,离散输入输出,还有ad的处理程序源码,所有程序都是从《嵌入式系统构件》一书中抄的,程序写的挺短,但思路特别清晰,高手就是高手。书本太大 上传不了不过网上挺容易搜索到的。
在这里感谢dong_abc 大侠。
static void KeyBufIn(INT8U code)
{
OS_ENTER_CRITICAL();
if(KeyNRead < KEY_BUF_SIZE)
{
KeyNRead++;
KeyBuf[KeyBufInIx++] = code;
if(KeyBufInIx >= KEY_BUF_SIZE)
{
KeyBufInIx = 0;
}
OS_EXIT_CRITICAL();
OSSemPost(KeySemPtr);
}
else
{
OS_EXIT_CRITICAL();
}
}
static INT8U KeyDecode(void)
{
switch(GPIOB->IDR & 0xff00)
{
case 0x7f00: return KeyDown;
case 0xbf00: return KeyRgt;
case 0xdf00: return KeyLef;
case 0xef00: return KeyUp;
case 0xf700: return KeyWav;
case 0xfb00: return KeyEnt;
case 0xfd00: return KeyFun;
case 0xfe00: return KeyBack;
default: return 0xff;
}
}
void KeyFlush(void)
{
while(KeyHit())
{
KeyGetKey(0);
}
}
INT8U KeyGetKey(INT16U t0)
{
INT8U code;
INT8U err;
OSSemPend(KeySemPtr,t0,&err);
OS_ENTER_CRITICAL();
if(KeyNRead > 0)
{
KeyNRead--;
code = KeyBuf[KeyBufOutIx];
KeyBufOutIx++;
if(KeyBufOutIx >= KEY_BUF_SIZE)
{
KeyBufOutIx = 0;
}
OS_EXIT_CRITICAL();
return (code);
}
else
{
OS_EXIT_CRITICAL();
return (0xff);
}
}
INT32U KeyGetKeyDownTime(void)
{
INT16U tmr;
OS_ENTER_CRITICAL();
tmr = KeyDownTmr;
OS_EXIT_CRITICAL();
return (tmr * KEY_SCAN_TASK_DLY);
}
BOOLEAN KeyHit(void)
{
BOOLEAN hit;
OS_ENTER_CRITICAL();
hit = (BOOLEAN)(KeyNRead > 0)? TRUE:FALSE;
OS_EXIT_CRITICAL();
return (hit);
}
void KeyInit(void)
{
KeyScanState = KEY_STATE_UP;
KeyNRead = 0;
KeyDownTmr = 0;
KeyBufInIx = 0;
KeyBufOutIx = 0;
KeySemPtr = OSSemCreate(0);
KeyInitPort();
OSTaskCreate(KeyScanTask,
(void *)0,
&KeyScanTaskStk[KEY_SCAN_TASK_STK_SIZE],
KEY_SCAN_TASK_PRIO);
}
static BOOLEAN KeyIsKeyDown(void)
{
if((GPIOB->IDR &0xff00) != 0xff00)
{
OS_ENTER_CRITICAL();
KeyDownTmr++;
OS_EXIT_CRITICAL();
return (TRUE);
}
else
{
return (FALSE);
}
}
static void KeyScanTask(void *pdata)
{
INT8U code;
pdata = pdata;
while(1)
{
OSTimeDly(KEY_SCAN_TASK_DLY);
switch(KeyScanState)
{
case KEY_STATE_UP:
if(KeyIsKeyDown())
{
KeyScanState = KEY_STATE_DEBOUNCE;
KeyDownTmr = 0;
}
break;
case KEY_STATE_DEBOUNCE:
if(KeyIsKeyDown())
{
code = KeyDecode();
KeyBufIn(code);
KeyRptStartDlyCtr = KEY_RPT_START_DLY;
KeyScanState = KEY_STATE_RPT_START_DLY;
}
else
{
KeyScanState = KEY_STATE_UP;
}
break;
case KEY_STATE_RPT_START_DLY:
if(KeyIsKeyDown())
{
if(KeyRptStartDlyCtr > 0)
{
KeyRptStartDlyCtr--;
if(KeyRptStartDlyCtr == 0)
{
code = KeyDecode();
KeyBufIn(code);
KeyRptDlyCtr = KEY_RPT_DLY;
KeyScanState = KEY_STATE_RPT_DLY;
}
}
}
else
{
KeyScanState = KEY_STATE_DEBOUNCE;
}
break;
case KEY_STATE_RPT_DLY:
if(KeyIsKeyDown())
{
if(KeyRptDlyCtr > 0)
{
KeyRptDlyCtr--;
if(KeyRptDlyCtr == 0)
{
code = KeyDecode();
KeyBufIn(code);
KeyRptDlyCtr = KEY_RPT_DLY;
}
}
}
else
{
KeyScanState = KEY_STATE_DEBOUNCE;
}
break;
}
}
|