⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_i2c.c

📁 基于I2C的RTC(实时时钟)的小程序,用c编写的,实现的是年历功能
💻 C
字号:
/*******************************file : testi2c.c******************************                        Version :  1.0                        Function:  The access of the i2c based rtc                        Auther: Haijian Cao                        Copyright Reserved                        2006.6.25*******************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <errno.h>#include <fcntl.h>#include <sys/stat.h>//#include <linux/i2c.h>#include <linux/i2c-dev.h>#include <linux/rtc.h>	//struct rtc_time#define I2C_ADAPTER "/dev/i2c-0"#define RTC_SLAVE_ADDR 0x68#define I2C_SLAVE       0x0703	//This is the ioctl command of set i2c slave address #define SET_DATE 0#define SET_TIME 1#define SET_DAY 2const char * day[7]={"SUN","MON","TUS","WEN","THI","FRI","SAT"};#ifndef BCD_TO_BIN#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)#endifint check_date(char * date,struct rtc_time * now_time);int check_time(char * time,struct rtc_time * now_time);void get_hard_time(struct rtc_time * now_time);void set_hard_time(struct rtc_time * now_time,int tim_day);unsigned char bin_to_bcd(unsigned char bin);/*Examples of the command line:1. testi2c -d 2006-06-12	//set the data2. testi2c -t 23:14:54		//set the time3. testi2c -y 7			//set the day to Satday*/int main(int argc,char * argv[]){	struct rtc_time now_time;	int temp;	if(argc==1)	{	}	else if(argc==3)	{		if(strcmp(argv[1],"-d")==0)		{				if(check_date(argv[2],&now_time)==-1)			{				printf("usage: testi2c -d xxxx-xx-xx\n");				return -1;			}			set_hard_time(&now_time,SET_DATE); //write the year,month,date to the ds1339		}		else if(strcmp(argv[1],"-t")==0)			{			if(check_time(argv[2],&now_time)==-1)			{				printf("usage: testi2c -t xx:xx:xx\n");				return -1;			}			set_hard_time(&now_time,SET_TIME); //write the hour,minute,second to the ds1339		}		else if (strcmp(argv[1],"-y")==0)		{			temp=atoi(argv[2]);			if(temp>7||temp<1)			{				printf("usage : testi2c -y num\n");				return -1;			}			now_time.tm_mday=temp;				set_hard_time(&now_time,SET_DAY);//write the day to the ds1339		}		else		{			printf("usage: testi2c [-d xxxx-xx-xx][-t xx:xx:xx][-y x]\n");		}	}	else	{		printf("usage: testi2c [-d xxxx-xx-xx][-t xx:xx:xx][-y x]\n");	}	get_hard_time(&now_time);}/****************************************************************************read the second,minute,hour,day,date,month,year from the ds1339 register,andput them to the struct rtc_time,and disply****************************************************************************/void get_hard_time(struct rtc_time * now_time){	int fd;	int ret_value;	int cur_addr=0x00;	char read_buff[7]={0,0,0,0,0,0,0};	if (now_time==NULL) return;	fd=open(I2C_ADAPTER,O_RDWR);	if(fd<0)	{		perror("Open I2C_ADAPTER:");		exit(1);	}	ret_value=ioctl(fd,I2C_SLAVE,RTC_SLAVE_ADDR);	//set the slave address	if (ret_value<0)				//of the ds1339	{		perror("Ioctl:");		exit(1);	}	cur_addr=0x0;		//set the Read addr to 0x0        ret_value=write(fd,&cur_addr,1);         if (ret_value<0)                        {                perror("Write:");                exit(1);        }	ret_value=read(fd,read_buff,7);		//read 7 ds1339 registers 	if (ret_value<0)				{		perror("Read:");		exit(1);	}//convert data reading from the ds1339 to the rtc_time	now_time->tm_sec=BCD_TO_BIN(read_buff[0]);	//second	now_time->tm_min=BCD_TO_BIN(read_buff[1]);	//minite	read_buff[2]&=0x3f;				//delete the 12/24 flag	now_time->tm_hour=BCD_TO_BIN(read_buff[2]);	//hour	now_time->tm_mday=BCD_TO_BIN(read_buff[3]);	//day	now_time->tm_wday=BCD_TO_BIN(read_buff[4]);	//date	now_time->tm_mon=read_buff[5]&0x7f;		//delete the centry information	now_time->tm_mon=BCD_TO_BIN(now_time->tm_mon);	//month	now_time->tm_year=BCD_TO_BIN(read_buff[6]);	//year	if(read_buff[5]&0x80) now_time->tm_year+=100;	//centry	printf("%d-%d-%d %s %d:%d:%d\n",now_time->tm_year+1900,now_time->tm_mon,now_time->tm_wday,		day[now_time->tm_mday-1],now_time->tm_hour,now_time->tm_min,now_time->tm_sec);	}/****************************************************************************tim_date=0 : set the year,month,datetim_date=1 : set the hour,minute,second****************************************************************************/void set_hard_time(struct rtc_time * now_time,int tim_date){	int fd;	int ret_value;	char write_buff[4]={0,0,0,0};	unsigned int w_count;		if (now_time==NULL) return;//Set time	if (tim_date==SET_TIME)			{				write_buff[0]=0;		write_buff[1]=bin_to_bcd(now_time->tm_sec);		write_buff[2]=bin_to_bcd(now_time->tm_min);		write_buff[3]=bin_to_bcd(now_time->tm_hour);		write_buff[3]&=0xbf;	//set the 12/24 flag		w_count=4;	}//Set date	else if(tim_date==SET_DATE)	{		write_buff[0]=0x04;		write_buff[1]=bin_to_bcd(now_time->tm_wday);		write_buff[2]=bin_to_bcd(now_time->tm_mon);		if(now_time->tm_year>=100)		{							//year 20xx			write_buff[3]=bin_to_bcd(now_time->tm_year-100);			write_buff[2]|=0x80;				//centry=1		}		else write_buff[3]=bin_to_bcd(now_time->tm_year);	//year 19xx		w_count=4;	}//Set day	else if(tim_date==SET_DAY)	{		write_buff[0]=0x03;		write_buff[1]=now_time->tm_mday;		w_count=2;	}	else return;	fd=open(I2C_ADAPTER,O_RDWR);	if(fd<0)	{		perror("Open I2C_ADAPTER:");		exit(1);	}	ret_value=ioctl(fd,I2C_SLAVE,RTC_SLAVE_ADDR);	//set the slave address	if (ret_value<0)				//of the ds1339	{		perror("Ioctl:");		exit(1);	}	ret_value=write(fd,write_buff,w_count);   //write w_count-1 registers to the ds1339 	if (ret_value<0)	{		perror("Write:");		exit(1);	}	close(fd);}/******************************************************************************check and get the year,month,date from the (char * date)char * date format : 2006-10-25******************************************************************************/int check_date(char * date,struct rtc_time * now_time){	char temp[5];	unsigned int i;	//check the format	if (strlen(date)!=10) return -1;	if (date[4]!='-'||date[7]!='-') return -1;	for (i=0;i<=9;i++)	{		if(i==4||i==7) i++;		if (!isdigit(date[i])) return -1;	}//check and get the year,month,date	for(i=0;i<=3;i++) temp[i]=date[i];	temp[4]='\0';	i=atoi(temp);	if(i>2016||i<1900) return -1;	now_time->tm_year=i-1900;	//year	temp[0]=date[5];	temp[1]=date[6];	temp[2]='\0';	i=atoi(temp);	if(i>12) return -1;	now_time->tm_mon=i;		//month	temp[0]=date[8];	temp[1]=date[9];	temp[2]='\0';	i=atoi(temp);	if(i>31) return -1;	now_time->tm_wday=i;		//date}/**************************************************************************************check and get the hour,minute,second from the (char * time)char * time format : 15:54:25**************************************************************************************/int check_time(char * time,struct rtc_time * now_time){	char temp[3];	unsigned int i;//check the format	if (strlen(time)!=8) return -1;	if (time[2]!=':'||time[5]!=':') return -1;	for (i=0;i<=7;i++)	{		if(i==2||i==5) i++;		if (!isdigit(time[i])) return -1;	}//check and get the year,month,date	temp[0]=time[0];	temp[1]=time[1];	temp[2]='\0';	i=atoi(temp);	if(i>23) return -1;	now_time->tm_hour=i;	//hour	temp[0]=time[3];	temp[1]=time[4];	temp[2]='\0';	i=atoi(temp);	if(i>59) return -1;	now_time->tm_min=i;	//minute	temp[0]=time[6];	temp[1]=time[7];	temp[2]='\0';	i=atoi(temp);	if(i>59) return -1;	now_time->tm_sec=i;	//second}/**********************************************************************************Convert from bin to bcd code**********************************************************************************/unsigned char bin_to_bcd(unsigned char bin){	unsigned int temp;	temp=bin;	bin=temp % 10;	temp=(temp / 10)<<4;	return temp|bin;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -