Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120038
  • 博文数量: 24
  • 博客积分: 616
  • 博客等级: 中士
  • 技术积分: 375
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-01 10:54
文章分类
文章存档

2012年(24)

我的朋友

分类: LINUX

2012-07-16 15:38:34

外部中断以及LED显示驱动用我先前公布的驱动
程序如下:
 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int buttons_fd;
int leds_fd;
int eeprom_fd;
char buttons[4] = {'0', '0', '0', '0',};
int value[512];
int main(void)
{
  value[0]=0x12;
  value[1]=0x23;
  value[2]=0x34;
  value[3]=0x45;
  value[4]=0x56;
  value[5]=0x67;
  buttons_fd = open("/dev/keys_eint", O_RDWR);
  leds_fd=open("/dev/LED_CONTROL",O_RDWR);
  eeprom_fd=open("/dev/at24lc04",O_RDWR);
  if (buttons_fd < 0) {
     perror("open device buttons");
     exit(1);
     }
  if(leds_fd<0)
  {
    perror("open device leds");
   }
 while(1) {
  char current_buttons[4];
  int count_of_changed_key;
  int i;
  if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
   perror("read buttons:");
   exit(1);
  }
  for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
   if (buttons[i] != current_buttons[i]) {
    buttons[i] = current_buttons[i];
    printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "up" : "down");
    switch(i)
    {
         case 0:
                 ioctl(leds_fd,0);
                
                 write(eeprom_fd,value,1);
                 sleep(1);
                 value[0]=0;
                 read(eeprom_fd,value,1);
                 printf("\n write reg[0]data:%x to at24lc04\n",value[0]);
                 break;
         case 1:
                 ioctl(leds_fd,1);
                 //write(eeprom_fd,value+sizeof(value[0]),1);
                 write(eeprom_fd,value+1,1);
                 sleep(1);
                 value[1]=0;
                 read(eeprom_fd,value+1,1);
                 printf("\n write reg[1]data:%x to at24lc04\n",value[1]);
                 break;
         case 2:
                 ioctl(leds_fd,2);
                 write(eeprom_fd,value+2,1);
                 sleep(1);
                  value[2]=0;
                 read(eeprom_fd,value+2,1);
                 printf("\n write reg[2]data:%x to at24lc04\n",value[2]);
                 break;
         case 3:
                 ioctl(leds_fd,3);
                 write(eeprom_fd,value+3,1);
                 sleep(1);
                  value[3]=0;
                 read(eeprom_fd,value+3,1);
                 printf("\n write reg[3]data:%x to at24lc04\n",value[3]);
               break;
         default:
                  break; 
    }
    count_of_changed_key++;
   }
  }
  if (count_of_changed_key) {
   printf("\n");
  }
  
  
 }
  close(eeprom_fd);
 close(buttons_fd);
 close(leds_fd);
 return 0;
}
阅读(1457) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~