void Scanf_Key() { //printf("In time call back"); if(JUDGE_KEY==0) //有按键按下 并且按键状态为1(长按键松手) { if(KeyOverTime < KEYTIME) // 去抖 { KeyOverTime++; } else { if(KEY_ONE_INPUT==0) //判断按键1是否按下 { KeyNemu=1; KeyPutDown=1; KeyPutLongTime++; } else if(KEY_TWO_INPUT==0) //判断按键1是否按下 { KeyNemu=2; KeyPutDown=1; KeyPutLongTime++; } } } else if(JUDGE_KEY&&KeyPutDown) //松手检测 { KeyPutDown=0; //松手后清楚长按键状态。 if(KeyNemu==1) { if(KeyPutLongTime>KEYLONGTIME) KeyVolat=0x02; else KeyVolat=0x01; KeyPutLongTime=0; } else if(KeyNemu==2) { if(KeyPutLongTime>KEYLONGTIME) KeyVolat=0x03; else KeyVolat=0x03; KeyPutLongTime=0; KeyOverTime=0; } } 这个按键的优点是简单方便,缺点是不容易移植,如果添加按键,那么就要重新修改添加代码。很麻烦!!!所以草想到能不能弄一个移植非常方便,只要初始化就行的按键检测. 不知道写的怎么样,欢迎大家拍砖
举报
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 };
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 };
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 };
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 };
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 };
您需要 登录 才可以下载或查看,没有账号?注册
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
等级类勋章
发帖类勋章
时间类勋章
人才类勋章
15
624
1
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号