S3C6410加了去抖的按键驱动程序

本文介绍了一种使用定时器实现按键去抖的技术,通过系统提供的函数简化了硬件操作,提高了按键输入的稳定性。代码示例展示了如何在驱动程序中实现这一功能,并通过实例比较了有去抖与无去抖效果的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


用定时器去抖,定时器的初始化、启动、赋初值都是使用系统提供的函数,完全不用去理会硬件的操作,用起来感觉很不错

直接上代码下面是驱动程序button.c 其实也只是在以前的驱动程序上加上了定时器,去抖的原理和以前的单片机按键去抖的原理一样

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


//#include <asm/arch/regs-gpio.h>
//#include <asm/hardware.h>

static struct class *thirddrv_class;
static struct class_device	*thirddrv_class_dev;
static unsigned char keyval;


static struct timer_list button_timer;


static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;

struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};
/* 
	按键值按下时0x01,0x02,0x03,0x04,0x05,0x06
	松开时                0x81,0x82,0x83,0x84,0x85,0x86
*/

struct pin_desc pins_desc[6] = {
	{S3C64XX_GPN(0), 0x01},
	{S3C64XX_GPN(1), 0x02},
	{S3C64XX_GPN(2), 0x03},
	{S3C64XX_GPN(3), 0x04},
	{S3C64XX_GPN(4), 0x05},
	{S3C64XX_GPN(5), 0x06},
};

static struct pin_desc * irq_pd;

static irqreturn_t botton_irq(int irq, void *dev_id)
{
	irq_pd = (struct pin_desc *)dev_id;
	mod_timer(&button_timer, jiffies + HZ/100);

	return IRQ_HANDLED;
}


static int third_drv_open(struct inode *inode, struct file *file)
{
	//printk("third_drv_open\n");


	//request_irq(unsigned int irq,irq_handler_t handler,unsigned long flags,const char * name,void * dev)
	request_irq(IRQ_EINT(0), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s2", &pins_desc[0]);
	request_irq(IRQ_EINT(1), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s3", &pins_desc[1]);
	request_irq(IRQ_EINT(2), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s4", &pins_desc[2]);
	request_irq(IRQ_EINT(3), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s5", &pins_desc[3]);
	request_irq(IRQ_EINT(4), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s6", &pins_desc[4]);
	request_irq(IRQ_EINT(5), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s7", &pins_desc[5]);
	
	return 0;
}

static ssize_t third_drv_read(struct file *file, const char __user *buf, size_t size, loff_t * ppos)
{

	if(size != 1)
		return -EINVAL;
	/* 如果没有按键动作发生就休眠 */
	wait_event_interruptible(button_waitq, ev_press);
	/* 如果有按键动作发生就返回 */
	/* 传递给用户 */
	copy_to_user(buf, &keyval, 1);
	ev_press = 0;

	return 1;
}

int third_drv_close(struct inode *inode, struct file *file)
{
	free_irq(IRQ_EINT(0), &pins_desc[0]);
	free_irq(IRQ_EINT(1), &pins_desc[1]);
	free_irq(IRQ_EINT(2), &pins_desc[2]);
	free_irq(IRQ_EINT(3), &pins_desc[3]);
	free_irq(IRQ_EINT(4), &pins_desc[4]);
	free_irq(IRQ_EINT(5), &pins_desc[5]);
	return 0;

}


static struct file_operations third_drv_fops = {
    .owner   =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open    =   third_drv_open,     
	.read	 =	 third_drv_read,	 
	.release =   third_drv_close,
};


int major;

static void buttons_timer_function(unsigned long data)
{
	struct pin_desc * pindes = irq_pd;
	unsigned int pinval;
	if (!pindes)
		return;
	pinval = gpio_get_value(pindes->pin);
	if (pinval)
	{
	/* 松开的 */
		keyval = 0x80 | pindes->key_val;
	}
	else 
	{
	/* 按下的 */
		keyval = pindes->key_val;
	}
//	printk(KERN_ERR "irq = %d\n", irq);
	ev_press = 1;
	wake_up_interruptible(&button_waitq);

}


static int third_drv_init(void)
{
//	printk(KERN_ERR "third_drv_init\n");
	init_timer(&button_timer);
//	button_timer.expires  = jiffies + HZ/100;
	button_timer.function = buttons_timer_function;
	add_timer(&button_timer);

	major = register_chrdev(0, "thirdt_drv", &third_drv_fops); // 注册, 告诉内核

	thirddrv_class = class_create(THIS_MODULE, "thirddrv");

	thirddrv_class_dev = device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "bottons"); /* /dev/bottons */


	return 0;
}

static void third_drv_exit(void)
{
//	printk(KERN_ERR "third_drv_exit\n");
	unregister_chrdev(major, "thirdt_drv"); // 卸载

	device_unregister(thirddrv_class_dev);
	class_destroy(thirddrv_class);
	
}

module_init(third_drv_init);
module_exit(third_drv_exit);


MODULE_LICENSE("GPL");



测试程序没有变,就不贴出来了

看一下没有加去抖的效果


可以看到0x85连续出现了两次

加上去抖之后按一天按键也不会出现这样的现象。
    前几天一直在帮别人做课设没有时间来写这个,这两个星期还有四门考试....................... 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值