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

📄 ds1302.c

📁 M200一个与单片机通信的C语言软件源代码
💻 C
字号:
/***********************************************************************************
/----------------------Copyright (c) 2005 ~ 2008 Miartech. All Rights Reserved.-----------------------/
/***********************************************************************************
**-----------------------------------------File Info--------------------------------------------
** Last Modified Date:  2007-09-04
** Descriptions: DS1302(Trickle Charge Timekeeping Chip)'s Primary Functions
**--------------------------------------------------------------------------------------------
** Created By:  	Kelven
** Created Date:	2007-04-17
**--------------------------------------------------------------------------------------------
** Modified by: 	Kelven
** Modified date:	2007-09-04
** Version:		V3.2.0
** Descriptions:
**--------------------------------------------------------------------------------------------
** Modified by: 	Li Sufen
** Modified date:	2008-05-20
** Version:		V3.2.3
** Descriptions:	Updating Key Configrating Function, Added PLC Chip Register Value Setting Module
***********************************************************************************/

#include "..\inc\DS1302.h"

/***********************************************************************************
** Function Name:	RTC_Write
** Input Parameters:cRTCCmd:Command Value;cRTCData:Data of Register Value
** Output Parameters:None
** Implement:Wite Command and Value to  DS1302's Relative Register
***********************************************************************************/
void RTC_Write(BYTE xdata  cRTCCmd, BYTE xdata  cRTCData)
{
	BYTE xdata cForCnt;

	RTC_RST = 0;
	RTC_CLK  = 0;
	RTC_DAT = 0;
	RTC_RST = 1;               //RST Must Set to High After SCLK Set to Low
	
	for(cForCnt = 0;cForCnt < 8;cForCnt ++)
	{		
		if(cRTCCmd & 0x01)
	       {
	           RTC_DAT = 1;
	       }
		else
	       {
	           RTC_DAT = 0;
	       }
		RTC_CLK = 1;
		RTC_CLK = 0;
		cRTCCmd = cRTCCmd>>1;
        }
        for(cForCnt = 0;cForCnt < 8;cForCnt ++)
        {
		if(cRTCData & 0x01)
		{
			RTC_DAT = 1; 
		}
		else
		{
			RTC_DAT = 0;
		}
		RTC_CLK = 1;
		RTC_CLK = 0;
		cRTCData = cRTCData>>1;
	}
	RTC_RST = 0;
}

/***********************************************************************************
** Function Name:	RTC_Read
** Input Parameters:cRTCCmdCommand Value
** Output Parameters:cRTCData:Data of Register Value
** Implement:Wite Command and Read Value of DS1302's Relative Register
***********************************************************************************/
BYTE RTC_Read(BYTE xdata  cRTCCmd)
{
	BYTE xdata cForCnt;
	BYTE xdata cRTCData;
	
	cRTCCmd += 1;			//Read Register Flag
	cRTCData = 0;
	
	RTC_RST = 0;
	RTC_CLK = 0;
	/*----------------------------------------------------------------------------------------
	** ----------------Note: RTC_DAT=0 Must be Here to Ensure that Read Data is Correct------------** 
	**--------------------------------------------------------------------------------------*/
	RTC_DAT = 0;
	RTC_RST = 1;               	//RST Must Set to High After SCLK Set to Low

	for(cForCnt = 0;cForCnt < 7;cForCnt ++)
	{
		if(cRTCCmd & 0x01)
		{
			RTC_DAT = 1;
		}
		else
		{
			RTC_DAT = 0;
		}
		RTC_CLK = 1;
		RTC_CLK = 0;
		cRTCCmd = cRTCCmd>>1;
	}
	if(cRTCCmd & 0x01)
	{
		RTC_DAT = 1;
	}
	else
	{
		RTC_DAT = 0;
	}
	RTC_CLK = 1;
	
	for(cForCnt = 0;cForCnt < 8;cForCnt ++)
	{
		cRTCData = cRTCData>>1;
		RTC_CLK = 0;

		if(RTC_DAT == 1)
		{
			 cRTCData |= 0x80;
		}
		else
		{
			 cRTCData &= 0x7f;
		}
		RTC_CLK = 1;
	}
        RTC_RST = 0;
        return(cRTCData);
}

/***********************************************************************************
** Function Name:	RTC_Init
** Input Parameters:None
** Output Parameters:None
** Implement:Initialization Of DS1302 & Wirte Intial Value Of MI200's Registers
***********************************************************************************/
void RTC_Init(void)
{
	BYTE xdata cRTCData;
	
        RTC_Write(DS1302_Ctrl_REG,0x00);				//Write Protection Off
        cRTCData = RTC_Read(DS1302_Sec_REG);		//Read Second Valus
        cRTCData &= 0x7f;							//Clear Second Register's Bit7:CH
        RTC_Write(DS1302_Sec_REG,cRTCData);     		//Enable DS1302 Clock Oscillator Working
        RTC_Write(DS1302_Charger_REG,0xa6);  		//Enable Trickle Charge,Value:1010;2Diodes,R2(4kohm)
        RTC_Write(DS1302_Ctrl_REG,0x80);				//Write Protection On
}

/***********************************************************************************
** Function Name:	RTC_Write_Time
** Input Parameters:None
** Output Parameters:None
** Implement:Write Time Value From Time_Buffers to DS1302's Relative Register as Start Time
***********************************************************************************/
void RTC_Write_Time(PBYTE xdata pTime_Vlaue_Buf)
{
	PBYTE xdata pTime_Vlaue;
	pTime_Vlaue = pTime_Vlaue_Buf;
	RTC_Write(DS1302_Ctrl_REG,0x00);						//Write Protection Off
	RTC_Write(DS1302_Year_REG,*(pTime_Vlaue++));			//Set Year Value to Year Register
	RTC_Write(DS1302_Month_REG,*(pTime_Vlaue++));		//Set Month Value to Month Register
	RTC_Write(DS1302_Date_REG,*(pTime_Vlaue++));			//Set Date Value to Date Register
	RTC_Write(DS1302_Hour_REG,*(pTime_Vlaue++));			//Set Hour Value to Hour Register
	RTC_Write(DS1302_Min_REG,*(pTime_Vlaue++));			//Set Minute Value to Minute Register
	RTC_Write(DS1302_Sec_REG,*(pTime_Vlaue++));			//Set Second Value to Second Register
	RTC_Write(DS1302_Ctrl_REG,0x80);						//Write Protection On
}

/***********************************************************************************
** Function Name:	RTC_Read_Time
** Input Parameters:None
** Output Parameters:None
** Implement:Read Time Value of DS1302's Relative to Register Time_Buffers as Current Time
***********************************************************************************/
void RTC_Read_Time(PBYTE xdata pTime_Vlaue_Buf)
{
	PBYTE xdata pTime_Vlaue;
	pTime_Vlaue = pTime_Vlaue_Buf;

	*(pTime_Vlaue++) = RTC_Read(DS1302_Year_REG);		//Read Year Register
	*(pTime_Vlaue++) = RTC_Read(DS1302_Month_REG);		//Read Month Register
	*(pTime_Vlaue++) = RTC_Read(DS1302_Date_REG);		//Read Date Register
	*(pTime_Vlaue++) = RTC_Read(DS1302_Hour_REG);		//Read Hour Register
	*(pTime_Vlaue++) = RTC_Read(DS1302_Min_REG);			//Read Minute Register
	*(pTime_Vlaue++) = RTC_Read(DS1302_Sec_REG);			//Read Second Register
}

/***********************************************************************************
** Function Name:	Display_RTC_Time
** Input Parameters:None
** Output Parameters:None
** Implement:Display The Time Value of DS1302
***********************************************************************************/
void Display_RTC_Time(PBYTE xdata pTime_Vlaue_Buf)
{
	PBYTE xdata pTime_Vlaue;
	pTime_Vlaue = pTime_Vlaue_Buf;

	RTC_Read_Time(pTime_Vlaue_Buf);
	Clear_DSP();

	DSP_Write_Cmd(0x80);
	DSP_Write_Data(_cror_((*(pTime_Vlaue++) & 0xf0),4) + 0x30);
	DSP_Write_Cmd(0x81);
	DSP_Write_Data((*(pTime_Vlaue++) & 0x0f) + 0x30);
	DSP_Show(0x82,"Year");	
	
	DSP_Write_Cmd(0x86);
	DSP_Write_Data(_cror_((*(pTime_Vlaue++) & 0xf0),4) + 0x30);
	DSP_Write_Cmd(0x87);
	DSP_Write_Data((*(pTime_Vlaue++) & 0x0f) + 0x30);
	DSP_Show(0x88,"Mon");	

	DSP_Write_Cmd(0x8b);
	DSP_Write_Data(_cror_((*(pTime_Vlaue++) & 0xf0),4) + 0x30);
	DSP_Write_Cmd(0x8c);
	DSP_Write_Data((*(pTime_Vlaue++) & 0x0f) + 0x30);
	DSP_Show(0x8d,"Dat");

	DSP_Write_Cmd(0xc0);
	DSP_Write_Data(_cror_((*(pTime_Vlaue++) & 0xf0),4) + 0x30);
	DSP_Write_Cmd(0xc1);
	DSP_Write_Data((*(pTime_Vlaue++) & 0x0f) + 0x30);
	DSP_Show(0xc2,"Hour");

	DSP_Write_Cmd(0xc6);
	DSP_Write_Data(_cror_((*(pTime_Vlaue++) & 0xf0),4) + 0x30);
	DSP_Write_Cmd(0xc7);
	DSP_Write_Data((*(pTime_Vlaue++) & 0x0f) + 0x30);
	DSP_Show(0xc8,"Min");

	DSP_Write_Cmd(0xcb);
	DSP_Write_Data(_cror_((*(pTime_Vlaue++) & 0xf0),4) + 0x30);
	DSP_Write_Cmd(0xcc);
	DSP_Write_Data((*(pTime_Vlaue++) & 0x0f) + 0x30);
	DSP_Show(0xcd,"Sec");
}

/***********************************************************************************
**										    End Of File											**
***********************************************************************************/

⌨️ 快捷键说明

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