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

📄 time.c

📁 基于c8051f020的应用
💻 C
字号:
//**************************************************************************************************************************
#include <c8051F020.h>
#include "HEAD.H"
//**************************************************************************************************************************
code unsigned char BCDTimeArray[12]={0x00,0x07,0x00,0x07,0x02,0x01,0x01,0x05,0x05,0x04,0x00,0x00};    // 保存转换后的时间数据
//**************************************************************************************************************************
void DS1302_Init(void)
{
	DS1302_Read();
	if(RTUaddress4[5]>=0x60)
	{
		DS1302_Write_Byte(0x8e,0x00);		// 停止计时
		DS1302_Write_Byte(0x80,BCDTimeArray[10]<<4 | BCDTimeArray[11]);// 启动芯片//sec
		DS1302_Write_Byte(0x82,BCDTimeArray[8] <<4 | BCDTimeArray[9] );// min
		DS1302_Write_Byte(0x84,BCDTimeArray[6] <<4 | BCDTimeArray[7] );// hour
		DS1302_Write_Byte(0x86,BCDTimeArray[4] <<4 | BCDTimeArray[5] );// day
		DS1302_Write_Byte(0x88,BCDTimeArray[2] <<4 | BCDTimeArray[3] );// month
		//DS1302_Write_Byte(0x8a,0x02);   	// week
		DS1302_Write_Byte(0x8c,BCDTimeArray[0]<<4  | BCDTimeArray[1] );// year
		DS1302_Write_Byte(0x8e,0x80);		// 开始计时
	}
}
//**************************************************************************************************************************
void DS1302_Write_Byte(unsigned char addr,unsigned char dat)
{
	xdata unsigned char i=0x00;

	TIME_RST = 0;
	TIME_CLK = 0;
	TIME_RST = 1;
	for(i=0;i<8;i++)
	{
		TIME_CLK = 0;
		if((addr&0x01)==0x01)
			TIME_IO = 1;
		else
			TIME_IO = 0;
		TIME_CLK = 1;
		addr>>=1;
	}
	for(i=0;i<8;i++)
	{
		TIME_CLK = 0;
		if((dat&0x01)==0x01)
			TIME_IO = 1;
		else
			TIME_IO = 0;
 		TIME_CLK = 1;
		dat>>=1;
	}
	TIME_CLK = 0;
	TIME_RST = 0;
}
//**************************************************************************************************************************
unsigned char DS1302_Read_Byte(unsigned char addr)
{
	xdata unsigned char i=0x00,out=0x00;
	
	TIME_RST = 0;
	TIME_CLK = 0;
	TIME_RST = 1;
	for(i=0;i<8;i++)
	{
		TIME_CLK = 0;
		if((addr&0x01)==0x01)
			TIME_IO = 1;
		else
			TIME_IO = 0;
		TIME_CLK = 1;
		addr>>=1;
	}
	for(i=0;i<8;i++)
	{
		P1MDOUT &=~0x40;
		TIME_IO = 1;
		out>>=1;
		TIME_CLK = 0;
		if(TIME_IO)
			out|=0x80;
		TIME_CLK = 1;
		P1MDOUT |= 0x40;
	}
	TIME_RST = 0;
	
	return out;
}
//**************************************************************************************************************************
void DS1302_Write(unsigned char * input_clock)
{
	DS1302_Write_Byte(0x8e,0x00);		// 停止计时
	DS1302_Write_Byte(0x80,input_clock[5]);// 启动芯片//sec
	DS1302_Write_Byte(0x82,input_clock[4]);// min
	DS1302_Write_Byte(0x84,input_clock[3]);// hour
	DS1302_Write_Byte(0x86,input_clock[2]);// day
	DS1302_Write_Byte(0x88,input_clock[1]);// month
	//DS1302_Write_Byte(0x8a,0x02);   	// week
	DS1302_Write_Byte(0x8c,input_clock[0]);// year
	DS1302_Write_Byte(0x8e,0x80);		// 开始计时

}
//**************************************************************************************************************************
void DS1302_Read(void)
{
	RTUaddress4[5]=DS1302_Read_Byte(0x81);	//sec
	RTUaddress4[4]=DS1302_Read_Byte(0x83); 	//min
	RTUaddress4[3]=DS1302_Read_Byte(0x85); 	//hr
	RTUaddress4[2]=DS1302_Read_Byte(0x87); 	//day
	RTUaddress4[1]=DS1302_Read_Byte(0x89); 	//month
	RTUaddress4[0]=DS1302_Read_Byte(0x8d); 	//year

	Clock_Date_Str[0]=(RTUaddress4[0]>>4|0x30);
	Clock_Date_Str[1]=(RTUaddress4[0]&0x0F|0x30);
	Clock_Date_Str[3]=(RTUaddress4[1]>>4|0x30);
	Clock_Date_Str[4]=(RTUaddress4[1]&0x0F|0x30);
	Clock_Date_Str[6]=(RTUaddress4[2]>>4|0x30);
	Clock_Date_Str[7]=(RTUaddress4[2]&0x0F|0x30);

	Clock_Time_Str[0]=(RTUaddress4[3]>>4|0x30);
	Clock_Time_Str[1]=(RTUaddress4[3]&0x0F|0x30);
	Clock_Time_Str[3]=(RTUaddress4[4]>>4|0x30);
	Clock_Time_Str[4]=(RTUaddress4[4]&0x0F|0x30);
	Clock_Time_Str[6]=(RTUaddress4[5]>>4|0x30);
	Clock_Time_Str[7]=(RTUaddress4[5]&0x0F|0x30);
}	
//**************************************************************************************************************************
// The End
//**************************************************************************************************************************



⌨️ 快捷键说明

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