完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
/*****************************************************
功能: 能实现按键中断。 实现去抖动功能,按键基本准确。 ******************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEVICE_NAME "driver_button" static volatile int ev_press = 0; static volatile int dev_num=0; static volatile int press_cnt [] = {0, 0, 0, 0}; static volatile int flag_press[] = {0, 0, 0, 0}; static DECLARE_WAIT_QUEUE_HEAD(button_waitq); static irqreturn_t irq_interrupt(int irq, int dev_id) { // //disable_irq(IRQ_EINT0); ???????????????????????????ú // //printk("dev_id= %dn",dev_id); dev_num=dev_id; ev_press=1; switch( dev_num) { case 1: flag_press[0]=1;break; case 2: flag_press[1]=1;break; case 3: flag_press[2]=1;break; case 4: flag_press[3]=1;break; default: break; } wake_up_interruptible(&button_waitq); //enable_irq(IRQ_EINT0); return IRQ_RETVAL(IRQ_HANDLED); } // ------------------- OPEN ------------------------ ssize_t button_open (struct inode * inode ,struct file * file) { request_irq(IRQ_EINT0, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY1", 1); request_irq(IRQ_EINT1, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY2", 2); request_irq(IRQ_EINT2, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY3", 3); request_irq(IRQ_EINT4, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY4", 4); printk("-----------------drive button open ok----------------n"); return 0; } // ------------------- RELEASE/CLOSE --------------- ssize_t button_release (struct inode * inode ,struct file * file) { free_irq(IRQ_EINT0, 1); free_irq(IRQ_EINT1, 2); free_irq(IRQ_EINT2, 3); free_irq(IRQ_EINT4, 4); return 0; } // ------------------- READ ------------------------ ssize_t button_read (struct file * file ,char * buf, size_t count, loff_t * f_ops) { wait_event_interruptible(button_waitq, ev_press); /* ???????????±??ev_press????1?????ü??0 */ ev_press = 0; msleep(200); if(flag_press[0]) { press_cnt[0]++; } else if(flag_press[1]) { press_cnt[1]++; } else if(flag_press[2]) { press_cnt[2]++; } else if(flag_press[3]) { press_cnt[3]++; } flag_press[0]=0; flag_press[1]=0; flag_press[2]=0; flag_press[3]=0; printk("------------device read --------------------n"); printk("press_cnt[0]= %dn",press_cnt[0]); copy_to_user( buf,(const void *)press_cnt,sizeof(press_cnt) ); return 0; } // ------------------- WRITE ----------------------- ssize_t button_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops) { return 0; } // ------------------- IOCTL ----------------------- ssize_t button_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long arg) { return 0; } // ------------------------------------------------- static struct file_operations button_ops ={ .owner = THIS_MODULE, .open = button_open, .read = button_read, .write = button_write, .ioctl = button_ioctl, .release = button_release, }; static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &button_ops, }; static int __init init_button_init(void) { int ret; ret = misc_register(&misc); printk("-----------------drive button init ok----------------n"); return 0; } static void __exit exit_button_ctl(void) { misc_deregister(&misc); } module_init(init_button_init); module_exit(exit_button_ctl); MODULE_LICENSE("GPL"); #include #include #include #include // open() close() #include // read() write() #define DEVICE_NAME "/dev/driver_button" //------------------------------------- main --------------------------------------------- int main(int argc, char **argv) { int fd,ret; int cnt=0; unsigned int key_val[4]; fd = open(DEVICE_NAME, O_RDWR); if (fd == -1) { printf("can't open device mknod %s c zhu ci n",DEVICE_NAME); return 0; } while(1) { read(fd,key_val,sizeof(key_val)); printf(" key_val= %d %d %d %dn",key_val[0],key_val[1],key_val[2],key_val[3]); } // close ret = close(fd); printf ("close gpio_led_driver testn"); return 0; }// end main |
|
|
相关推荐
|
|
|
Linux基础入门好贴。
|
|
|
|
|
|
|
|
有人有STM8H1K08T连接TM1650的源码吗,可以直接使用的那种,我的代码在烧录之后数码管一直处于熄灭状态
271 浏览 0 评论
【瑞萨RA6E2】瑞萨E2S软件安装过程,等待过程玩下97_e2 studio_ZGZZ
493 浏览 0 评论
589 浏览 0 评论
【原创】【RA4M2-SENSOR开发板评测】低功耗+USB综合测试
891 浏览 0 评论
1436 浏览 2 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
16926 浏览 31 评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-4 09:57 , Processed in 0.694175 second(s), Total 71, Slave 53 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖