/****************************************************************************** *Name: timeled.c *Desc: led实时动态显示系统时间,其中led3/led2显示小时,led1/led0显示分钟 * led7/led6显示秒。该程序运行前,应先使用insmod i2c.o命令加载驱动。 *Parameter: *Return: *Author: yoyoba(stuyou@126.com) *Date: 2010-9-20 *Modify: 2010-9-20 ********************************************************************************/
#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}; //led显示0-F的编码
void gettime(int returntime[6]) //获得当前时间的时、分、秒信息,共6位数字,依次放入数组returntime中。
{ time_t mytime_t; struct tm* mytm; time(&mytime_t); mytm=localtime(&mytime_t); returntime[0]=mytm->tm_hour/10; returntime[1]=mytm->tm_hour%10; returntime[2]=mytm->tm_min/10; returntime[3]=mytm->tm_min%10; returntime[4]=mytm->tm_sec/10; returntime[5]=mytm->tm_sec%10; }
int main() { int fd,index=0; //char chon=0xff,choff=0x0,key=0;
int mygettime[6]; 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) { gettime(mygettime); ioctl(fd,I2C_SET_DATA_ADDR,REG_Dis3); //led3显示小时的高位
write(fd,&digit[mygettime[0]],1); ioctl(fd,I2C_SET_DATA_ADDR,REG_Dis2); //led2显示小时的低位
write(fd,&digit[mygettime[1]],1); ioctl(fd,I2C_SET_DATA_ADDR,REG_Dis1); //led1显示分钟的高位
write(fd,&digit[mygettime[2]],1); ioctl(fd,I2C_SET_DATA_ADDR,REG_Dis0); //led0显示分钟的低位
write(fd,&digit[mygettime[3]],1); ioctl(fd,I2C_SET_DATA_ADDR,REG_Dis7); //led7显示秒的高位
write(fd,&digit[mygettime[4]],1); ioctl(fd,I2C_SET_DATA_ADDR,REG_Dis6); //led6显示秒的低位
write(fd,&digit[mygettime[5]],1); }
printf("led example is over!\n");
return 0; }
|