按键所用开关为物理机械弹性开关, 当机械触点断开, 闭合时, 由于机械弹性作用, 开关不会马上稳定的接通或者断开. 因而在闭合及断开的瞬间总是伴随有一连串的抖动
按键去抖的方法主要有两种
(1) 硬件电路去抖
(2) 软件延时去抖
* for 循环等待
* 定时器延时
Linux 内核使用 struct timer_list 来描述一个定时器
- struct timer_list {
- struct list_head entry;
- unsigned long expires;
- void (*function)(unsigned long);
- unsigned long data;
- struct tvec_base *base;
- };
定时器使用流程
(1) 定义定时器变量
(2) 初始化定时器: 用 init_timer, 主要设置超时函数
(3) 注册定时器: add_timer
(4) 启动定时器: mod_timer
简单示例
来源: http://www.bubuko.com/infodetail-2736219.html