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

2021-09-24 19:03:26 字數 3208 閱讀 1983

button.c

#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]);

return 0;

}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;

return 1;

}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 = ;

int major;

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");

buttons_test.c

#include #include #include #include #include #include #include #include #include /* sixthdrvtest 

*/int fd;

void my_signal_fun(int signum)

int main(int argc, char **ar**)

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

//oflags = fcntl(fd, f_getfl);

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

while (1)

return 0;

}

makefile

kern_dir = /work/system/linux-2.6.22.6

all:

make -c $(kern_dir) m=`pwd` modules

clean:

make -c $(kern_dir) m=`pwd` modules clean

rm -rf modules.order

obj-m += buttons.o

(3 5)乙個按鍵所能涉及的 定時器防抖動

author pinus creat on 2018 10 25 kernel linux 4.4.145 board jz2440 arm9 s3c2440 通過定時器完成防抖動的總結 除顫嗎。能看的應該都懂為什麼,微控制器上不也是常用什麼delay 或者也是定時器進行除顫嗎,避免誤觸唄。當按鍵發...

通過定時器完成防抖動的總結

由於按鍵是機械按鍵,裡面的金屬彈片在按下或鬆開的時候會發生抖動,在這樣的條件下按下一次就有可能發生多次中斷,為了解決這個問題,我們應該新增防抖動。我們可以用定時器實現防抖動。假如按下一次產生了多次中斷,那麼在第一次中斷產生之後,呼叫中斷處理函式,在這裡面設定10ms之後執行某乙個操作,下次中斷再來,...

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

1.定時器 1 超時時間 2 處理函式 include include include include include include include include include include include include static struct class sixthdrv class...