字元裝置驅動筆記 定時器防抖動(十)

2021-09-06 12:11:18 字數 3383 閱讀 1109

1.定時器:

1).超時時間

2).處理函式

#include #include 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

static

struct

class       *sixthdrv_class;

static

struct class_device *sixthdrv_class_dev;

volatile unsigned long *gpfcon;

volatile unsigned long *gpfdat;

volatile unsigned long *gpgcon;

volatile unsigned long *gpgdat;

//定時器

static struct timer_list buttons_timer;

static

declare_wait_queue_head(button_waitq);

/*中斷事件標誌, 中斷服務程式將它置1,sixth_drv_read將它清0

*/static

volatile

int ev_press = 0

;static

struct fasync_struct *button_async;

struct

pin_desc;

/*鍵值: 按下時, 0x01, 0x02, 0x03, 0x04

*//*

鍵值: 鬆開時, 0x81, 0x82, 0x83, 0x84

*/static unsigned char

key_val;

struct pin_desc pins_desc[4] =,,,

,};static

struct pin_desc *irq_pd;

//static atomic_t canopen = atomic_init(1);

//定義原子變數並初始化為1

static declare_mutex(button_lock); //

定義互斥鎖

/** 確定按鍵值

*/static irqreturn_t buttons_irq(int irq, void *dev_id)

static

int sixth_drv_open(struct inode *inode, struct file *file)

#endif

if (file->f_flags &o_nonblock)

else

/*配置gpf0,2為輸入引腳

*//*

配置gpg3,11為輸入引腳

*/request_irq(irq_eint0, buttons_irq, irqt_bothedge, "s2

", &pins_desc[0

]); request_irq(irq_eint2, buttons_irq, irqt_bothedge, "s3

", &pins_desc[1

]); request_irq(irq_eint11, buttons_irq, irqt_bothedge, "s4

", &pins_desc[2

]); request_irq(irq_eint19, buttons_irq, irqt_bothedge, "s5

", &pins_desc[3

]);

return0;

}ssize_t sixth_drv_read(

struct file *file, char __user *buf, size_t size, loff_t *ppos)

else

/*如果有按鍵動作, 返回鍵值

*/copy_to_user(buf, &key_val, 1

); ev_press = 0

;

return1;

}int sixth_drv_close(struct inode *inode, struct file *file)

static unsigned sixth_drv_poll(struct file *file, poll_table *wait)

static

int sixth_drv_fasync (int fd, struct file *filp, int

on)static

struct file_operations sencod_drv_fops =;

intmajor;

static

void buttons_timer_function(unsigned long

data)

else

ev_press = 1; /*

表示中斷發生了

*/wake_up_interruptible(&button_waitq); /*

喚醒休眠的程序

*/kill_fasync (&button_async, sigio, poll_in);

}static

int sixth_drv_init(void

)static

void sixth_drv_exit(void

)module_init(sixth_drv_init);

module_exit(sixth_drv_exit);

module_license(

"gpl

");

2.測試程式

#include #include 

#include

#include

#include

#include

#include

#include

#include

/*sixthdrvtest

*/intfd;

void my_signal_fun(int

signum)

int main(int argc, char **argv)

//fcntl(fd, f_setown, getpid());

//oflags = fcntl(fd, f_getfl);

//fcntl(fd, f_setfl, oflags | fasync);

while (1

)

return0;

}

使用定時器實現按鍵防抖動

button.c include include include include include include include include include include include include static struct class sixthdrv class static str...

Linux字元裝置驅動之定時器去抖動按鍵驅動

當按一次按健時,由於按健有反應時間 有抖動,可能按一次機器感應到幾次,防抖就是讓在按鍵正常反應時間內機器只感應一次按鍵效果,防止誤操作。當按鍵發生時產生中斷,在中斷服務程式中修改乙個定時器的定時時間為10ms,並從10ms開始重新計數。定時器時間到後產生定時器中斷,按鍵處理放在定時器中斷裡面。這樣當...

字元裝置驅動筆記

在所有linux裝置驅動中,字元裝置驅動最為基礎,本筆記將講解linux字元裝置驅動的結構,並解釋其主要組成部分的程式設計方法。cdev結構 struct cdev cdev結構體的dev t成員定義了裝置號,為32位,其中12bit為主裝置號,20bit為次裝置號。使用如下巨集可以從dev t獲得...