内核定时器消除按键抖动

来源:互联网 发布:mac xampp phpmyadmin 编辑:程序博客网 时间:2024/05/17 07:58

按键抖动

按键所用的开关为机械弹性开关,当机械触点断开、闭合时,由于机械触点的弹性作用,开关不会马上稳定地接通或断开。因而在闭合及断开的瞬间总是伴随有一连串的抖动。

按键去抖动的方法主要有两种:一是硬件电路去抖动;另外一种是软件延时去抖。而延时又一般分为两种,一种是for循环等待,另外一种是定时器延时。在操作系统中,由于效率方面的原因,一般不允许使用for循环来等待,只能使用定时器


范例代码

#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/gpio.h>
#include<linux/cdev.h>
#include <linux/poll.h>
#include<linux/slab.h>


int key_id;
struct cdev cdev;
dev_t devno;


wait_queue_head_t button_q;
static struct timer_list button_timer;//定义一个定时器
unsigned int key_num=0;


struct class *my_class;


static int id=0;


static irqreturn_t key_func(int irq,void *dev_id)
{
id = (int)dev_id;
//10ms后启动定时器
mod_timer(&button_timer,jiffies+HZ/100);//修改定时器的超时时间 jiffes是个全局变量,每隔10ms就会有个系统时钟中断,在系统时钟中断里面这个值就会累加。1s就是1hz。HZ就是100,这个意思是1s中jiffes这个值会增加100。
return IRQ_RETVAL(IRQ_HANDLED);
}


static void button_timer_function(unsigned long data)
{
key_num=1;
if(id==0)
{
return;
}//因为在把定时器加载到内核里面去的时候,没有设置超时所以会直接进来
wake_up_interruptible(&button_q);
return 0;




}


static int key_open(struct inode *inode,struct file *filp)
{
request_irq(IRQ_EINT(16),key_func,IRQF_TRIGGER_FALLING,"k1",1);
request_irq(IRQ_EINT(17),key_func,IRQF_TRIGGER_FALLING,"k2",2);
request_irq(IRQ_EINT(18),key_func,IRQF_TRIGGER_FALLING,"k3",3);
request_irq(IRQ_EINT(19),key_func,IRQF_TRIGGER_FALLING,"k4",4);
request_irq(IRQ_EINT(24),key_func,IRQF_TRIGGER_FALLING,"k5",5);
request_irq(IRQ_EINT(25),key_func,IRQF_TRIGGER_FALLING,"k6",6);
request_irq(IRQ_EINT(26),key_func,IRQF_TRIGGER_FALLING,"k7",7);
request_irq(IRQ_EINT(27),key_func,IRQF_TRIGGER_FALLING,"k8",8);
return 0;
}


static int key_close(struct inode *inode,struct file *file)
{
free_irq(IRQ_EINT(16),1);
free_irq(IRQ_EINT(17),2);
free_irq(IRQ_EINT(18),3);
free_irq(IRQ_EINT(19),4);
free_irq(IRQ_EINT(24),5);
free_irq(IRQ_EINT(25),6);
free_irq(IRQ_EINT(26),7);
free_irq(IRQ_EINT(27),8);
return 0;
}


ssize_t key_read(struct file *filp,char __user *buf,size_t size,loff_t *pos)
{

wait_event_interruptible(button_q,key_num);
copy_to_user(buf,&id,4);
key_num=0;


return 0;
}


static struct file_operations key_fops=
{
.owner = THIS_MODULE,
.open = key_open,
.release = key_close,
.read = key_read,


};


static int  key_timerlist_init(void)
{
init_timer(&button_timer);//初始化定时器
// button_timer.data = //传递给处理函数的参数
//button_timer.expires = //超时时间
//button_timer.function = //处理函数
button_timer.function=button_timer_function;
add_timer(&button_timer);//把定时器告诉给内核
cdev_init(&cdev,&key_fops);
alloc_chrdev_region(&devno, 0 , 1 , "mykey_timerlist");
cdev_add(&cdev, devno, 1);
my_class = class_create(THIS_MODULE, "key_timerlist_class");
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return -1;
}
device_create(my_class, NULL, devno,NULL,"key_timerlist_driver");
init_waitqueue_head(&button_q);
return 0;
}


static void key_timerlist_exit(void)
{
device_destroy(my_class,devno);
class_destroy(my_class);
cdev_del(&cdev);
unregister_chrdev_region(devno,1);
}




module_init(key_timerlist_init);
module_exit(key_timerlist_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("EIGHT_FIVE");



测试程序

#include<stdio.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>


int main(int argv,char **argc)
{
int fd,buf;
fd=open("/dev/key_timerlist_driver",0x666);
if(fd<0)
{
printf("can't open\n");
return -1;
}
while(1)
{


read(fd,&buf,4);
printf("key_val =%d\n",buf);
}


close(fd);
return 0;


}

0 0
原创粉丝点击