Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337554
  • 博文数量: 61
  • 博客积分: 2816
  • 博客等级: 少校
  • 技术积分: 880
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-18 23:38
文章存档

2012年(1)

2011年(19)

2010年(41)

分类: LINUX

2010-09-05 14:20:52

/******************************************************
按键驱动测试程序,在TX2440A开发板做测试
驱动用法:
  设备名称:TX2440-button
  测试程序名称:led_buttn
功能:按键点灯
*******************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(void)
{
 int i;
 int key_fd;
 int key_value[4];
 int led_fd;
/*打开设备,自动分配设备号,设备号自己找到
你的那个设备,他们必须是大于0的。key_fd和
led_fd是设备号,如果小于0,直接退出exit(1)*/

 key_fd = open("/dev/TX2440-button", 0);
 led_fd = open("/dev/TX2440-led", 0);
 if (key_fd < 0||led_fd < 0)
 {
  perror("open device buttons");
  exit(1);
 }
 while(1)
 {
  int len;
     /*其的进入read,后如果没中断,它就在里休眠了
    所以也自然不会往下执行了,如果中断了read返
    回4和sizeof(key_value)相等中断成功
         设备号, 保存地址 ,保存多大空间*/   
  len = read(key_fd, key_value, sizeof(key_value));
  if (len != sizeof(key_value))
  {
   perror("read button\n");
   continue; /*结束while(1)循环*/
  }
  /*如果中断成功,检查数据,做出判断,这里要记住,
    设备号是你来找到设备的唯一方法。led_fd*/
  else
  {
   ioctl(led_fd, 3, 0); /*先把所有的灯关了*/
    printf("off all---");
   for (i = 0; i < 4; i++) 
   {
    if(key_value[i] != 0)
     switch(key_value[i])
     {
     case 1:
       ioctl(led_fd, 1, 0);
       printf("open %d led\n",key_value[i]);break;
     case 2:
       ioctl(led_fd, 1, 1);
       printf("open %d led\n",key_value[i]);break;
     case 3:
       ioctl(led_fd, 1, 2);
       printf("open %d led\n",key_value[i]);break;
     case 4:
       ioctl(led_fd, 1, 3);
       printf("open %d led\n",key_value[i]);break;
     default : perror("read button not data\n");
    }
   } 
  }
 }
 /*释放设备*/
 close(key_fd);
 close(led_fd);
 return 0;
}
阅读(2344) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-07 11:16:06

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com