天嵌 ARM开发社区

 找回密码
 注册
查看: 2383|回复: 0

tq2440 按键中断 去抖

[复制链接]
freewing 发表于 2016-7-12 11:32:52 | 显示全部楼层 |阅读模式
  /*****************************************************
  功能:
  能实现按键中断。
  实现去抖动功能,按键基本准确。
  ******************************************************/
  #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

  感谢chen4013874的分享!

  供货情况:
  天嵌科技提供专业嵌入式板卡和行业解决方案。如有需要,可以联系天嵌科技的销售人员。
  销售电话:020-38219416 38373101
  技术支持:020-38219416转807 820
  网址:http://www.embedsky.com
  官方淘客:https://embedsky.taobao.com/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-11-1 07:08 , Processed in 1.046875 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表