按键去抖动防反跳技巧
来源:互联网 发布:sqlserver offset 编辑:程序博客网 时间:2024/04/30 10:41
在公司看到一段关于按键去抖动方面的程序感觉设计比较好,贴出来分享一下。
废话少说上代码:按键处理要求,处理函数在10ms的timer 中断内运行 ,连续三次(10ms*3)检测到按键按下状态,判断按键为短按,按键按下持续10ms*200判断为长按(包括防反跳时间),连续三次(10ms*3)检测到按键松开状态,判断按键为抬起。
#define KEY_DEBOUNCE_NUM 3#define KEY_LONG_PRESS_NUM (200 - KEY_DEBOUNCE_NUM)#define KEY_SW_NUM 2/* SW State Define */typedef enum{ KEY_SW_OFF=0, KEY_SW_ON, KEY_SW_ON_2_SEC}SW_State_t;SW_State_t KeySW6;SW_State_t KeySW5;typedef struct{ unsigned char SWStatus[KEY_SW_NUM]; unsigned char LongSWStatus[KEY_SW_NUM]; unsigned char* SwitchProcess[KEY_SW_NUM];}Switch_Status_t;static Switch_Status_t SwitchStatus;typedef struct{ unsigned char* Port ; unsigned char Pin;}Switch_Input_t;static Switch_Input_t SwitchInput[KEY_SW_NUM] = { //pin的第6位 pin的第7位 {(unsigned char*)&P2IN,6}, {(unsigned char*)&P2IN,7}}; #pragma vector=TIMER0_B0_VECTOR__interrupt void TIMER1_B0_ISR(void) //10timer interrupt { KeyHandle(0);}
void KeyInit(){ int temp; PortInit(); KeySW6 = KEY_SW_OFF; KeySW5 = KEY_SW_OFF; SwitchStatus.SwitchProcess[0] = (unsigned char*)&KeySW6; SwitchStatus.SwitchProcess[1] = (unsigned char*)&KeySW5;}void KeyHandle(void *UserParameter){ int temp; for(temp = 0;temp < KEY_SW_NUM;temp++){ if(!((*SwitvhInput[temp].Port >> SwitvhInput[temp].Pin) & 0x01)){ if(SwitchStatus.SWStatus[temp] < KEY_DEBOUNCE_NUM){ SwitchStatus.SWStatus[temp]++; }else{ SwitchStatus.SWStatus[temp] = 0xff; SwitchStatus.SwitchProcess[temp]= KEY_SW_ON; if(SwitchStatus.LongSWStatus[temp] < KEY_LONG_PRESS_NUM){ SwitchStatus.LongSWStatus[temp]++; }else{ SwitchStatus.SwitchProcess[temp]= KEY_SW_ON_2_SEC; } } }else{ if(SwitchStatus.SWStatus[temp] > (0xff - KEY_DEBOUNCE_NUM)){ SwitchStatus.SWStatus[temp]--; }else{ SwitchStatus.SWStatus[temp] = 0x00; SwitchStatus.LongSWStatus[temp] = 0x00; } } }}
0 0
- 按键去抖动防反跳技巧
- 按键软件去抖动设计
- 按键抖动及软件去抖
- linux字符驱动之定时器去抖动按键驱动
- 增加去抖动的新型的按键扫描程序
- linux字符驱动之定时器去抖动按键驱动
- linux字符驱动之定时器去抖动按键驱动
- 4412驱动-7th_buttons_all 定时器去抖动按键驱动
- linux字符驱动之定时器去抖动按键驱动
- Linux字符设备驱动之定时器去抖动按键驱动
- 按键防抖动程序
- 按键抖动与处理
- iOS 按键 抖动
- 按键防抖动
- 按键驱动在2440上的实例开发(带去抖动2)
- 按键驱动在2440上的实例开发(带去抖动)
- 按键驱动在2440上的实例开发(带去抖动)
- 字符设备驱动程序开发之基于中断的按键驱动加去抖动
- 1008 元素右移
- VC++中给对话框设置背景图片的方法
- Arccatlog10.0中的SDE空间数据库导出shp文件时出错
- C++primer plus第六版课后编程题答案8.7(补)
- poj3468——by rfy
- 按键去抖动防反跳技巧
- 跟张志东深聊,腾讯的“进化力”
- 美国航空航天四大机构报告(PB报告、AD报告、NASA报告和DOE报告)
- Vmware 8安装Mac Lion狮子系统 总结
- 数字信号处理中求频谱图与滤除噪声的问题
- hdu1018(解题报告)
- apue和unp的学习之旅06——简单引出进程和信号的学习
- meta 之 viewport
- delphi数据类型表