本帖最后由 kyzb001 于 2011-12-14 08:57 编辑
二楼,用结构体定义了按键的对应IO 和键值
检测IO键值的程序为不可重入函数,所以引人了 基本的 “秘钥”KeyBoard_Key这个变量,
代码如下uint8_t Search_Key() //查找按键
{
switch(KeyBoard_Key)
{
switch(KeyBoard_Key)
{
case 0:
{
if(Key_Board(&KEY_ONE))
return 1;
else
break; //检测按键1
}
case 1:
{
if(Key_Board(&KEY_TWO))
return 1;
else
break; //检测按键2
}
}
}
void Key_Struct_Init() //初始化按键
{
KEY_ONE.isKeyDownFunc=Key_One;
KEY_ONE.KeyShort=KEY_UP; //短按键键值
KEY_ONE.KeyLong=KEY_MENU; //长按键键值
KEY_TWO.isKeyDownFunc=Key_Two; //同上
KEY_TWO.KeyShort=KEY_DOWN;
KEY_TWO.KeyLong=KEY_ESC;
}
static uint8_t Key_Board(key_t *CurrenKey)
{
if(CurrenKey->isKeyDownFunc()==0) //有按键按下 并且按键状态为1(长按键松手)
{
if(KeyOverTime < KEYTIME) // 去抖
{
KeyOverTime++;
}
else
{
if(CurrenKey->isKeyDownFunc()==0)
{
KeyPutDown=1;
KeyPutLongTime++;
}
}
}
else if(((CurrenKey->isKeyDownFunc()==1)&&KeyPutDown)) //松手检测
{
if(KeyPutLongTime>KEYLONGTIME)
KeyVolat=CurrenKey->KeyLong;
else
KeyVolat=CurrenKey->KeyShort;
CleatVolat(); //松手后清楚长按键状态和其他值。
KeyBoard_Key++; //密钥+1,表示当前任务已经使用完毕,释放改函数,选择下一个
}
else
{
KeyVolat=KEY_NONE;
KeyBoard_Key++;
}
if(KeyBoard_Key>1)
KeyBoard_Key=0;
return KeyVolat;
}
[code]void CleatVolat()
{
KeyOverTime=0;
KeyPutLongTime=0;
KeyPutDown=0;
}
[/code]
int32_t Key_Two(void) { return DrvGPIO_GetBit(E_GPB,15);}
int32_t Key_One(void) { return DrvGPIO_GetBit(E_GPB,14);}
uint8_t KeyVolat=0;
uint8_t KeyOverTime=0;
uint32_t KeyPutLongTime=0;
uint8_t KeyPutDown=0;
uint8_t KeyBoard_Key=0;
uint8_t KeyNemu;
key_t KEY_ONE,KEY_TWO;
extern uint8_t KeyVolat;
typedef struct
{
int32_t (*isKeyDownFunc)(void); // 函数指针,该函数用来检测按键是否按下
uint8_t KeyBit;
uint8_t KeyResult;
uint8_t KeyShort;
uint8_t KeyLong;
}key_t;
enum {
KEY_NONE,
KEY_UP,
KEY_DOWN,
KEY_MENU,
KEY_ESC
};
|