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

📄 set_time.c

📁 正在使用的嵌入式系统获取本地时间的源码文件
💻 C
字号:
#include <fcntl.h>
#include <linux/sem.h>
#include <errno.h>
#include "i2c.h"

#define SEMKEY 100

struct i2c_faraday_msg {
	__u16 addr;	/* device address */
	__u16 waddr;	/* word address */
	short len;	/* msg length */
	char *buf;	/* pointer to msg data */
	int clockdiv;	/* for clock div */
    int multiread_once ; /* decide to read multibyte once. if 0: read one byte by one; if 1: read multibyte once*/
};

struct i2c_faraday_msg i2c_ap;
static unsigned char buf[10];
static unsigned char hour,minute,second;
static unsigned char year,month,day;
static int file;
static unsigned char hour_mode;

unsigned char BCDToDec(unsigned char bcd_number)
{
	return((bcd_number>>4)*10 + (bcd_number&0x0f));
}

unsigned char DECToBCD(unsigned char dec_number)
{
	return(((dec_number/10)<<4) | (dec_number%10));
}

int GetControlReg1(void)
{
    int temp;

    i2c_ap.addr = 0x65;
    i2c_ap.waddr = 0xE0;
    i2c_ap.len = 1;
    i2c_ap.clockdiv = 0;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if ( temp < 0 )
    {
        printf("read  control register 1  failure!\n");
        return -1;
    }
    hour_mode = buf[0];
	
    return (temp);
}
int SetRTCControlReg(void)
{
    int temp;
    
    i2c_ap.addr = 0x64;
    i2c_ap.waddr = 0x00;
    i2c_ap.len = 3;
    buf[0] = 0xE0;
    //buf[1] = control_reg1;
    //buf[2] = control_reg2;
    buf[1] = 0x20;
    buf[2] = 0x00;
    i2c_ap.clockdiv = 0;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if ( temp < 0 )
    {
        printf("write RTC control register failure!\n");
        return -1;
    }
   // printf("write RTC control register successfully!\n");
    return (temp);
}

int SetCurSysTime(unsigned char hour, unsigned char minute, unsigned char second)
{
    int temp;
    i2c_ap.addr = 0x64;
    i2c_ap.waddr = 0x00;
    i2c_ap.len = 4;
    buf[0] = 0x00;
    buf[1] = DECToBCD(second);
    buf[2] = DECToBCD(minute);
    buf[3] = DECToBCD(hour);
    i2c_ap.clockdiv = 0;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if (temp < 0)
    {
        printf("write RTC current time failure!\n");
        return -1;
    }
   // printf("write RTC current time successfully!\n");
    return(temp);
}
int SetCurSysDate(unsigned char year, unsigned char month, unsigned char day)
{
    int temp;
    i2c_ap.addr = 0x64;
    i2c_ap.waddr = 0x00;
    i2c_ap.len = 4;
    buf[0] = 0x40;
    buf[1] = DECToBCD(day);
    buf[2] = DECToBCD(month);
    buf[3] = DECToBCD(year);
    i2c_ap.clockdiv = 0;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if (temp < 0)
    {
        //printf("write RTC current date failure!\n");
        return -1;
    }
    //printf("write RTC current date successfully!\n");
    return (temp);
}

int GetCurSysTime(void)
{
    int temp;

    //read second
    i2c_ap.addr = 0x65;
    i2c_ap.waddr = 0x00;
    i2c_ap.len = 1;
    i2c_ap.clockdiv = 0;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if ( temp < 0 )
    {
        printf("read second failure!\n");
        return -1;
    }
   // printf("read second successfully!\n");
    second = buf[0];

    //read minute
    i2c_ap.waddr = 0x10;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if ( temp < 0 )
    {
        printf("read minute failure!\n");
        return -1;
    }
  //  printf("read minute successfully!\n");
    minute = buf[0];
    //read hour
    i2c_ap.waddr = 0x20;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if ( temp < 0 )
    {
        printf("read hour failure!\n");
        return -1;
    }
  //  printf("read hour successfully!\n");
    hour = buf[0];
	return 0;
}

int GetCurSysDate(void)
{
    int temp;
    
    //read day
    i2c_ap.addr = 0x65;
    i2c_ap.waddr = 0x40;
    i2c_ap.len = 1;
    i2c_ap.clockdiv = 0;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if ( temp < 0 )
    {
        //printf("read day failure!\n");
        return -1;
    }
    //printf("read day successfully!\n");
    day = buf[0];

    //read month
    i2c_ap.waddr = 0x50;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if ( temp < 0 )
    {
        //printf("read month failure!\n");
        return -1;
    }
    //printf("read month successfully!\n");
    month = buf[0];
    
    //read year
    i2c_ap.waddr = 0x60;
    i2c_ap.buf = &buf[0];
    temp = ioctl(file, I2C_RDWR, &i2c_ap);
    if ( temp < 0 )
    {
        //printf("read year failure!\n");
        return -1;
    }
    //printf("read year successfully!\n");
    year = buf[0];
	return 0;
}


int main()
{
	char cmd[64];
	int y,mo,d,h,mi,s, temp;
	unsigned char pon;
	int semid;
	union semun sunionr;
	
	file = open("/dev/i2c-0", O_RDWR);

	i2c_ap.addr = 0x65;
	i2c_ap.waddr = 0xF0;
	i2c_ap.len = 1;
	i2c_ap.clockdiv = 0;
	i2c_ap.buf = &buf[0];
	temp = ioctl(file, I2C_RDWR, &i2c_ap);
	if ( temp < 0 )
	{
	    printf("read control register 2  failure!\n");
	    return -1;
	}
	pon = buf[0];
	 
	if(pon&0x10)   //PON =1  设置24小时制日期时间
	{
		SetRTCControlReg();
		SetCurSysDate(7,1, 1);
		SetCurSysTime(0,0,0);
		sprintf(cmd, "date -s  010100002007");
		system(cmd);
	}
	else             //PON=0  读时间
	{	
		GetControlReg1();
		
		if(hour_mode&0x20)   //24 mode
		{
			GetCurSysDate();
			GetCurSysTime();

			
			y = BCDToDec(year);
			mo = BCDToDec(month);
			d = BCDToDec(day);
			h = BCDToDec(hour) ;
			mi = BCDToDec(minute) ;
			s = BCDToDec(second) ;
			
			memset(cmd, 0, sizeof(cmd));

			//printf("y=%d, mo=%d, d=%d, h=%d, mi=%d \n",y, mo, d, h, mi);
			if(y >= 70)
			{
				SetRTCControlReg();
				SetCurSysDate(7,1, 1);
				SetCurSysTime(0,0,0);
				sprintf(cmd, "date -s  010100002007");
			}
			else
				sprintf(cmd, "date -s %02d%02d%02d%02d20%02d", mo, d, h, mi, y);

			system(cmd);

		}
		else                          // 12 mode
		{
			SetRTCControlReg();
			SetCurSysDate(7,1, 1);
			SetCurSysTime(0,0,0);
			sprintf(cmd, "date -s  010100002007");
			system(cmd);
		}
			
	}

	close(file);
	
/* initialization semahphore. */
	semid=semget(SEMKEY, 1, IPC_CREAT);
	if(semid==-1){
		printf("Semaphore request failed: %s. \n", strerror(errno));
		return 0;
	}
	sunionr.val=1;
	if(semctl(semid, 0, SETVAL, sunionr) == -1)
		 perror("semctl setval error");
	
	return 0;
}


⌨️ 快捷键说明

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