/****************************************************************************** *Name: keydisled.c *Desc: 按下一个按键,led0和串口终端显示该按键。运行该程序前,应使用insmod指令加载i2c.o驱动模块。 *Parameter: *Return: *Author: yoyoba(stuyou@126.com) *Date: 2010-9-19 *Modify: 2010-9-19 ********************************************************************************/
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <linux/i2c.h> #include <linux/fcntl.h> #include <sys/stat.h> #include "test-8led.h" #include <sys/types.h> /* control code */ #define I2C_SET_DATA_ADDR 0x0601 #define I2C_SET_BUS_CLOCK 0x0602 unsigned char digit[] ={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0xEE,0x3E,0x9C,0x7A,0x9E,0x8E};
char key_set(char ucChar) { switch(ucChar) { case 1: case 2: case 3: case 4: case 5: ucChar-=1; break; case 9: case 10: case 11: case 12: case 13: ucChar-=4; break; case 17: case 18: case 19: case 20: case 21: ucChar-=7; break; case 25: ucChar = 0xF; break; case 26: ucChar = '+'; break; case 27: ucChar = '-'; break; case 28: ucChar = '*'; break; case 29: ucChar = 0xFF; break; default: ucChar = 0xFE; // error
} return ucChar; }
int main() { int fd,index=0; char chon=0xff,choff=0x0,key=0; printf("this is a example for 8-led,writed by youhaidong!\n"); if((fd=open("/dev/i2c/0",O_RDWR))==-1) /*打开i2c设备*/ { printf("open led device FAILD!\n"); exit(1); } else printf("open led device SUCCESS!\n"); ioctl(fd, I2C_SLAVE_FORCE, ZLG_SLAVE_ADDR); /*设置zlg7290从设备地址*/ ioctl(fd, I2C_SET_BUS_CLOCK, 250*1000); /*设置i2c总线时钟频率为250khz*/ while(1) { ioctl(fd,I2C_SET_DATA_ADDR,REG_Sys); //设置SystemReg寄存器的地址
read(fd,&key,1); //读取SystemReg寄存器的内容,放入key中
if(key&0x01) //SystemReg寄存器的值为1,这时才相应按键。
{ ioctl(fd, I2C_SET_DATA_ADDR, REG_Key); //设置key寄存器的地址
read(fd,&key,1); //从key寄存器读取按键的值
write(fd,&choff,1); //让led0熄灭
key=key_set(key); //把key寄存器的值转换为0-15
printf("the press key is %d\n",key); ioctl(fd, I2C_SET_DATA_ADDR, REG_Dis0); //设置led0地址
index=(int)key; //该值为digit[]数组中的索引号
write(fd,&digit[index],1); //显示按键的值
} } printf("led example is over!\n");
return 0; }
|