欢迎访问电子工程网!   登录 | 免费注册 ]   

Embedsky007的个人空间 http://www.eechina.com/space-uid-114045.html [收藏] [复制] [分享] [RSS]

博客

S3C2440如何按键中断 去抖

热度 4已有 293 次阅读2016-7-11 14:13 |个人分类:技术文章| 关键词: S3C2440, 嵌入式, 开发板, 中断, 板卡

  /*****************************************************

  功能:

  能实现按键中断。

  实现去抖动功能,按键基本准确。

  ******************************************************/

  #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/


路过

鸡蛋
1

鲜花

握手

雷人

刚表态过的朋友 (1 人)

发表评论 评论 (4 个评论)

回复 adeechan36 2016-7-12 14:39
2440的资料还是比较多的,适合学习。
回复 sally_cheng 2016-7-12 14:54
谢谢楼主的分享
回复 张晓然 2016-7-13 11:42
2440资料丰富,非常不错,谢楼主!
回复 lzh8 2016-7-13 17:28
值得分享

facelist

您需要登录后才可以评论 登录 | 立即注册

回顶部