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

📄 test_rct.c

📁 标准linux 2.4.20 针对ds1302 的RTC 源码,具有充电功能!
💻 C
字号:
#include <sys/ioctl.h>#include <sys/time.h>#include <sys/utsname.h>#include <ctype.h>#include <fcntl.h>#include <getopt.h>#include <stdlib.h>#include <string.h>#include <syslog.h>#include <time.h>#include <unistd.h>#include "ds1302.h"

//#include <asm/arch/major.h>


#define DEVICE_DS1302 "/dev/ds1302"

static time_t read_rtc(){	int rtc;	int currenty;	struct rtc_time tm;	char *oldtz = 0;	time_t t = 0;	if (( rtc = open ( "/dev/ds1302", O_RDONLY )) < 0 ) {	//if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) {		if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 )			printf ( "Could not access RTC" );	}	memset ( &tm, 0, sizeof( struct tm ));	if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 )		//bb_perror_msg_and_die ( "Could not read time from RTC" );		{		printf ( "Could not read time from RTC" );		return -1;		}	else	{		//printf("\n");		/*printf(" sec is %d \n",tm.tm_sec);		printf(" min is %d \n",tm.tm_min);		printf(" hour is %d \n",tm.tm_hour);		printf(" mday is %d \n",tm.tm_mday);		printf(" mon is %d \n",tm.tm_mon);		printf(" year is %d \n",tm.tm_year);*/		if(tm.tm_year>100)		{			tm.tm_year-=100;			currenty=2000;		}		else 		currenty=1900;		printf(" current date & time is :\n");		printf(" %d年%d月%d日 星期%d %02d:%02d:%02d \n",tm.tm_year+currenty,tm.tm_mon,tm.tm_mday,tm.tm_wday,tm.tm_hour,tm.tm_min,tm.tm_sec);		printf("\n");		}		t = mktime ( &tm );		return t;	//tm. tm_isdst = -1; // not known	close ( rtc );/*	if ( utc ) {		oldtz = getenv ( "TZ" );		setenv ( "TZ", "UTC 0", 1 );		tzset ( );	}	t = mktime ( &tm );	if ( utc ) {		if ( oldtz )			setenv ( "TZ", oldtz, 1 );		else			unsetenv ( "TZ" );		tzset ( );	}	*/	//return 0;}static void write_rtc(struct rtc_time tm){	int rtc;	//struct tm tm;	if (( rtc = open ( "/dev/ds1302", O_WRONLY )) < 0 ) {		if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )			printf ( "Could not access RTC" );	}    //printf(" seting the time.....\n");	if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )		printf ( "Could not set the RTC time" );  //printf(" set the time completely....\n");	close ( rtc );}

int main()
{
	int fd; 
	int val=-1;
	int ret = 0;	struct rtc_time tm;	struct timeval tv = { 0, 0 };	//const struct timezone tz = { timezone/60 - 60*daylight, 0 };	tm.tm_sec=00;	tm.tm_min=11;	tm.tm_hour=16;	tm.tm_mday=29;	tm.tm_mon=11;	tm.tm_year=107;	tm.tm_wday=4;
  write_rtc(tm);  /*
  while(1)  {  	read_rtc();  	sleep(1);  	sleep(1);  	}  	*/  //tv. tv_sec = read_rtc ();	  //if(gettimeofday(&tv,NULL));  	//printf("gettimeofday err...,\n");  if ( settimeofday ( &tv, NULL ))  	printf(" settimeofday err...\n");
	return 0;
}

⌨️ 快捷键说明

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