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

📄 ds1302.c

📁 基于S3C4510的家庭网关的通讯进程程序源码
💻 C
📖 第 1 页 / 共 2 页
字号:
*This routine SETs the clock and start its oscillator, write-*protecting the clock registers.*Obtains time from TimeBuffer[].********************************************************************/void Set_Time(unsigned char * TimeBuffer)   {   TimeBuffer[7] = '\x80';         //write protect the chip after write   Enable_1302();                  //write enable the clock   Write_1302_Data(TimeBuffer);    //write the clock and start it up   } /* end proc Set_Time() *//********************************************************************Get_Time*This routine GETs the time from the Dallas 1302 clock.*The time is returned in TimeBuffer.*This function returns 0 when the clock is operating properly.*If the clock was locked (stopped) the default time is returned*as 1/1/99, 12:00Noon and the function returns non-zero.********************************************************************/unsigned int Get_Time(unsigned char * TimeBuffer)   {	unsigned int clock_locked;	clock_locked = Read_1302_Data(TimeBuffer);		//store the time in system variable time....	//Note that since the values in the RTC and TimeBuffer are in	//BCD, we need to convert back to normal numbers!	ds_1302_time.year = (((TimeBuffer[6] & 0xf0)>>4)*10)+(TimeBuffer[6] & 0xf);	ds_1302_time.month = (((TimeBuffer[4] & 0xf0)>>4)*10)+(TimeBuffer[4] & 0xf);	ds_1302_time.date = (((TimeBuffer[3] & 0xf0)>>4)*10)+(TimeBuffer[3] & 0xf);	ds_1302_time.hour = (((TimeBuffer[2] & 0x30)>>4)*10)+(TimeBuffer[2] & 0xf);	ds_1302_time.min = (((TimeBuffer[1] & 0xf0)>>4)*10)+(TimeBuffer[1] & 0xf);	ds_1302_time.sec = (((TimeBuffer[0] & 0xf0)>>4)*10)+(TimeBuffer[0] & 0xf);	ds_1302_time.dayOfWeek = TimeBuffer[5];	return(clock_locked);	} /* end proc Get_Time() */BYTE bcd2bin ( BYTE x ){ return ( ( x >> 4 ) * 10 + ( x & 0x0F ));}BYTE bin2bcd ( BYTE x )		//x : [0..99]{ return ((( x / 10 ) << 4 ) + ( x % 10 ));}/********************************************************************date_encoder*功能:此函数将6个字节的日期转换为传输协议要求的4字节日期********************************************************************/WORD date_encoder (BYTE * TimeBuffer){ BYTE x; WORD y; WORD clock_locked;  clock_locked = Read_1302_Data(TimeBuffer);	 x = TimeBuffer[6];		//year  x = bcd2bin ( x );			//BCD -> BIN y = ( x + 20 ) * 512;			//from 1980 x = TimeBuffer[4];		//month x = bcd2bin ( x );			//BCD -> BIN y |= x * 32; x = TimeBuffer[3];		//date x = bcd2bin ( x );			//BCD -> BIN y |= x; return ( y );}	/********************************************************************time_encoder*功能:此函数将6个字节的时间转换为传输协议要求的4字节时间********************************************************************/WORD time_encoder ( BYTE * TimeBuffer ){ BYTE x; WORD y; WORD clock_locked; clock_locked = Read_1302_Data(TimeBuffer); x = TimeBuffer[2] & 0x3F;	//hour   x = bcd2bin ( x );			//BCD -> BIN y = x * 2048; x = TimeBuffer[1];		//minute x = bcd2bin ( x );			//BCD -> BIN y |= x * 32; x = TimeBuffer[0] & 0x7F;	//second x = bcd2bin ( x );			//BCD -> BIN y |= x / 2; return ( y );}/********************************************************************coder_endate*功能:此函数将传输协议要求的4字节日期还原为6个字节的日期********************************************************************/void coder_endate (WORD z){ BYTE y,x;  x = (BYTE)(z&0x001F);	        //day   x = bin2bcd ( x );			//BCD -> BIN y = x ; printf("0=%x\n",y); ds_1302_time.date = (((y & 0xf0)>>4)*10)+(y & 0xf); x = (BYTE)((z>>5)&0x000f);		//minute x = bcd2bin ( x );			//BCD -> BIN y = x; printf("1=%x\n",y); ds_1302_time.month = (((y & 0xf0)>>4)*10)+(y & 0xf); x = (BYTE)(((z>>9)-20)&0x007f);	 //day   x = bin2bcd ( x );			//BCD -> BIN y = x ; printf("3=%x\n",y); ds_1302_time.year = (((y & 0xf0)>>4)*10)+(y & 0xf);}/********************************************************************coder_entime*功能:此函数将传输协议要求的4字节时间还原为6个字节的时间********************************************************************/void coder_entime (WORD z){ BYTE y,x;  x = (BYTE)(z&0x001F);	        //sec  x = bin2bcd ( 2*x );			//BCD -> BIN y = x ; printf("0=%x\n",y); ds_1302_time.sec = (((y & 0xf0)>>4)*10)+(y & 0xf); x = (BYTE)((z&0x07e0)>>5);		//minute x = bcd2bin ( x );			//BCD -> BIN y = x; printf("1=%x\n",y); ds_1302_time.min = (((y & 0xf0)>>4)*10)+(y & 0xf);  x = (BYTE)((z>>11)&0x001f);        //hour   x = bin2bcd ( x );			//BCD -> BIN y = x ; printf("3=%x\n",y); ds_1302_time.hour = (((y & 0x30)>>4)*10)+(y & 0xf); }int set_clock(int size, unsigned char buffer[256]){   int x;	time_t the_time;	struct tm *tm_ptr;	struct tm tm_struct;  	unsigned char TimeBuffer[8];  	unsigned char tmp_buffer[256];	char * ctime_result;		const char days[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};	tm_ptr = &tm_struct;	//get Dallas clock port ready	Setup_1302_Port();   if (size > 0 && size<18)   	{		 	     if (size != 17)			{					htmltext("<P>输入字符长度错误,请重新输入.</P>");                        return(1);	//error			}                if ((buffer[2] != '/') || (buffer[5] != '/'))			{			htmltext("<P>输入日期格式错误,请重新输入.</P>");			return(1);			} 	     if (buffer[8] != ';')	 		{			htmltext("<P>输入日期与时间的分隔符错误,请重新输入.</P>");			return(1);			}	 if ((buffer[11] != ':') || (buffer[14] != ':'))			{							htmltext("<P>输入时间格式错误,请重新输入.</P>");		        return(1);			}			buffer[2] = '0';			buffer[5] = '0';			buffer[8] = '0';			buffer[11] = '0';			buffer[14] = '0';			for (x = 0;x<17;x++)				{				if ((buffer[x]<'0') || (buffer[x]>'9'))					{					htmltext("<P>输入数据错误,请重新输入.</P>");		                        return(1);					}				}						//tm_year is # of years since 1900, so assume 00-98 is 2000-2098			tm_ptr->tm_year = ((buffer[0] -'0') * 10) + (buffer[1] -'0');			if (tm_ptr->tm_year < 99) tm_ptr->tm_year = tm_ptr->tm_year+100;			//tm_tm_mon: Jan = 0			tm_ptr->tm_mon = ((buffer[3] -'0') * 10) + (buffer[4] -'0')-1;			tm_ptr->tm_mday = ((buffer[6] -'0') * 10) + (buffer[7] -'0');			tm_ptr->tm_hour = ((buffer[9] -'0') * 10) + (buffer[10] -'0');			tm_ptr->tm_min = ((buffer[12] -'0') * 10) + (buffer[13] -'0');			tm_ptr->tm_sec = ((buffer[15] -'0') * 10) + (buffer[16] -'0');			//all values for year are valid			if (tm_ptr->tm_mon>11)	//Jan = 0				{				//printf("Bad month.\n");				htmltext("<P>输入月份错误,请重新输入.</P>");		                        return(1);				}			if ((!tm_ptr->tm_mday) || (tm_ptr->tm_mday>31))				{				htmltext("<P>输入日期错误,请重新输入.</P>");		                        return(1);				}			if (tm_ptr->tm_hour>23)				{				htmltext("<P>输入小时错误,请重新输入.</P>");		                        return(1);				}			if (tm_ptr->tm_min>59)				{				htmltext("<P>输入分钟错误,请重新输入.</P>");		                        return(1);				}			if (tm_ptr->tm_sec>59)				{				htmltext("<P>输入秒错误,请重新输入.</P>");		                        return(1);				}			//sets up the Linux Time			//use mktime to validate settings			the_time = mktime(tm_ptr);			if (the_time < 0)				{				htmltext("<P>输入时间或日期错误,请重新输入.</P>");		                        return(1);				}			//set the Linux OS time			stime(&the_time);	//# of sec in epoch			TimeBuffer[0] = ((buffer[15] -'0')<<4) + (buffer[16] -'0');	//BCD Seconds			TimeBuffer[1] = ((buffer[12] -'0')<<4) + (buffer[13] -'0');	//BCD Minutes			TimeBuffer[2] = ((buffer[9] -'0')<<4) + (buffer[10] -'0');	//BCD Hour			TimeBuffer[3] = ((buffer[6] -'0')<<4) + (buffer[7] -'0');	//BCD Day of Month			TimeBuffer[4] = ((buffer[3] -'0')<<4) + (buffer[4] -'0');	//BCD Month, 1=Jan			TimeBuffer[6] = ((buffer[0] -'0')<<4) + (buffer[1] -'0');	//BCD Year			//use strftime to get the day of the week			ctime_result = asctime(tm_ptr);             			for (x = 0; x < 7; x++)				{				if (strncmp(ctime_result,&days[x][0],3) == 0) break;				} //next x			TimeBuffer[5] = x+1;		//BCD Way of Week, 1=Sunday         			Set_Time(&TimeBuffer[0]);			//Jan = 0, year is # of years since 1900			sprintf(tmp_buffer,"初始日期设定:%s %02i/%02i/%02i",&days[TimeBuffer[5]-1][0],					tm_ptr->tm_year%100, tm_ptr->tm_mon+1, tm_ptr->tm_mday);			htmltext(tmp_buffer);			sprintf(tmp_buffer,"初始时间设定: %02i:%02i:%02i\n", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);			htmltext(tmp_buffer);						return(0);			         	}	else		{		htmltext("<P>日期和时间输入字符长度不正确,请重新输入.</P>");		return(0);	//all was well		} 	return(0);}

⌨️ 快捷键说明

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