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

📄 1307.c

📁 DS1307实时时钟器件的驱动
💻 C
字号:
#include "include.H"
bit write_RTC_time()/* ----- write RTC registers ------ */
{
	function_I2C_init();
	function_I2C_start();
	if(function_I2C_send(ADDRTC)==0)/* write slave address + write */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(0)==0)/* position the address pointer to 0 */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Sec&0x7f)==0)/* write Sec and clear the CH bit */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Min)==0)/* Min */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Hr)==0)/* Hr */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Dy)==0)/* Dy */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Dt)==0)/* Dt */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Mn)==0)/* Mn */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Yr)==0)/* Yr */
	{
		function_I2C_stop();
		return(0);
	}
	function_I2C_stop();
	return(1);
}

bit read_RTC_time()/* ----- read RTC registers ------ */
{
	function_I2C_init();
	function_I2C_start();
	if(function_I2C_send(ADDRTC)==0)/* write slave address + write */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(0)==0)/* position the address pointer to 0 */
	{
		function_I2C_stop();
		return(0);
	}
	function_I2C_start();
	if(function_I2C_send(ADDRTC | 1)==0)	/* write slave address + read */
	{
		function_I2C_stop();
		return(0);
	}
	Sec = function_I2C_receive();
	function_I2C_Ack();
	Min = function_I2C_receive();
	function_I2C_Ack();
	Hr = function_I2C_receive();
	function_I2C_Ack();
	Dy = function_I2C_receive();
	function_I2C_Ack();
	Dt = function_I2C_receive();
	function_I2C_Ack();
	Mn = function_I2C_receive();
	function_I2C_Ack();
	Yr = function_I2C_receive();
	function_I2C_clock();
	function_I2C_stop();
	return(1);
}

bit RTC_init(void)/* -- initialize the time and date -- */
{
	function_I2C_init();		/* The following Enables the Oscillator */
	function_I2C_start();
	if(function_I2C_send(ADDRTC)==0)/* address the part to write */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(0)==0)	/* position the address pointer to 0 */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Sec&0x7f)==0)	/* write 0 to the 1st register, clear the CH bit */
	{
		function_I2C_stop();
		return(0);
	}
	function_I2C_stop();

	function_I2C_start();
	if(function_I2C_send(ADDRTC)==0)/* address the part to write */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(2)==0)	/* position the address pointer to 1 */
	{
		function_I2C_stop();
		return(0);
	}
	if(hour_mode)
	{
		if(function_I2C_send(Hr|0x40)==0)/* select 12-hour mode */
		{
			function_I2C_stop();
			return(0);
		}
	}
	else
	{
		if(function_I2C_send(Hr&0xbf)==0)/* select 24-hour mode */
		{
			function_I2C_stop();
			return(0);
		}
	}
	function_I2C_stop();

	function_I2C_start();
	if(function_I2C_send(ADDRTC)==0)/* write slave address + write */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(7)==0)	/* write register address, 1st clock register */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(0x10)==0)/* enable sqwe, 1Hz output */
	{
		function_I2C_stop();
		return(0);
	}
	function_I2C_stop();
	return(1);
}

bit burstramwrite(unsigned char Start_addr,unsigned char Dat_length,
					unsigned char *Write_dat)/* -------- write RAM -------- */
{
	unsigned char j;
	function_I2C_init();
	function_I2C_start();
	if(function_I2C_send(ADDRTC)==0)/* write slave address + write */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Start_addr)==0)/* write register address, start addr RAM location */
	{
		function_I2C_stop();
		return(0);
	}
	for (j = 0; j < Dat_length; j++)	/* write until the pointer wraps around */
	{
		if(function_I2C_send(*Write_dat++)==0)
		{
			function_I2C_stop();
			return(0);
		}
	}
	function_I2C_stop();
	return(1);
}

bit burstramread(unsigned char Start_addr,unsigned char Dat_length,
					unsigned char *Save_addr)/* ------ read RAM ------- */
{
	unsigned char j;
	function_I2C_init();
	function_I2C_start();
	if(function_I2C_send(ADDRTC)==0)/* write slave address + write */
	{
		function_I2C_stop();
		return(0);
	}
	if(function_I2C_send(Start_addr)==0)/* position the address pointer to 0 */
	{
		function_I2C_stop();
		return(0);
	}
	function_I2C_start();
	if(function_I2C_send(ADDRTC | 1)==0)	/* write slave address + read */
	{
		function_I2C_stop();
		return(0);
	}
	for (j = 0; j < Dat_length; j++)
	{
		*Save_addr++ = function_I2C_receive();
		if(j==Dat_length-1)
			break;
		function_I2C_Ack();
	}
	function_I2C_clock();
	function_I2C_stop();
	return(1);
}

⌨️ 快捷键说明

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