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

📄 pcf8563.h

📁 CMOS实时时钟/日历芯片驱动函数,供初学者学习。
💻 H
字号:
//***************************************************************************************
//*文件:PCF8653.H                      											    *
//*说明:PCF8653驱动函数库              											    *
//*平台:AT89S5X																   	    *
//*作者:陈崇                           											    *
//*日期:2007-03-21                     											    *
//***************************************************************************************
#include <I2C.H>
#ifndef  _PCF8653_DRIVER
#define  _pcf8653_DRIVER
#define SCL_SETB		P1_1=1
#define SCL_CLR			P1_1=0
#define SDA_SETB		P1_0=1
#define SDA_CLR			P1_0=0 
#define	PCF8563_SLA		0xA2
unsigned char init_time[7]={0,0,10,11,3,4,7};
//***************************************************************************************
//*名称: unsigned char bcd_convert_hex(unsigned char bcd_data)   						*						
//*功能: BCD码转16进制				     												*
//*参数: bcd_data  要转换的BCD码数据(0-100)												*
//*返回: 转换后的16进制数据              												*
//***************************************************************************************
unsigned char bcd_convert_hex(unsigned char bcd_data)
	{
	  unsigned char temp;
	  temp=((bcd_data>>4)*10)+(bcd_data&0x0F);
	  return temp;
	
	}
//***************************************************************************************
//*名称:  unsigned char hex_convert_bcd(unsigned char hex_dataa)   					*						 
//*功能:  16进制转BCD码				     											*
//*参数:  hex_data  要转换的16进制数据(0-100)											*
//*返回:  转换后的BCD码数据              												*
//***************************************************************************************
unsigned char hex_convert_bcd(unsigned char hex_data)
	{
	  unsigned char temp;
	  unsigned char bcd_data;
	  temp=hex_data%100;
	  bcd_data=(temp/10<<4)|(temp%10);
	  return bcd_data;
	}
//***************************************************************************************
//*名称: void pcf8563_star(void)  														* 
//*功能: 启动PCF8563芯片运行		     												*
//*参数: 																				*
//*返回:               																	*
//***************************************************************************************	
void pcf8563_star(void)
	{
		i2c_write_byte(PCF8563_SLA,0x00,0x00);	
	}
//***************************************************************************************
//*名称: void pcf8563_get_time(unsigned char *ptr)										* 
//*功能: 从RTC芯片获取时钟数据 并存放到指定数组											*
//*参数:  																				*
//*返回:                 																*
//***************************************************************************************		
void pcf8563_get_time(unsigned char *ptr)
	{
		i2c_read_nbyte(PCF8563_SLA,0x02,ptr,7);
	 	ptr[0]=bcd_convert_hex(ptr[0]&0x7F);			//	秒	 		
		ptr[1]=bcd_convert_hex(ptr[1]&0x7F);			//	分
		ptr[2]=bcd_convert_hex(ptr[2]&0x3F);			//	小时
		ptr[3]=bcd_convert_hex(ptr[3]&0x3F);			//	日
		ptr[4]=ptr[4];
		ptr[5]=bcd_convert_hex(ptr[5]&0x1F);			//	月
		ptr[6]=bcd_convert_hex(ptr[6]);	  				//	年
	}
//***************************************************************************************
//*名称:  pcf8563_rewirte_time(unsigned char *ptr)										* 
//*功能:  将时钟数据重写入PCF8563片														*
//*参数:  *ptr	时钟数据数组															*
//*返回:                  																*
//*************************************************************************************** 
void pcf8563_rewirte_time(unsigned char *ptr)
	{																			
		unsigned char i;
		for(i=0;i<7;i++)
			{
				ptr[i]=hex_convert_bcd(ptr[i]);
			}
		i2c_write_byte(PCF8563_SLA,0x00,0x20);			//	PCF8563时钟芯停止运行
		i2c_write_nbyte(PCF8563_SLA,0x02,ptr,7);		//	将设置时间写入PCF8563
		i2c_write_byte(PCF8563_SLA,0x00,0x00);			//	PCF8563时钟芯开始运行
	}
//***************************************************************************************
//*名称:  void pcf8563_init(void)														* 
//*功能:  如果时钟数据不对则写入初始值													*
//*参数:  																				*
//*返回:                  																*
//***************************************************************************************
void pcf8563_init(void)
	{
		unsigned char time_temp[7];
		pcf8563_get_time(time_temp);
		if(time_temp[0]>=60||time_temp[1]>=60||time_temp[2]>=24||time_temp[3]>=32
		  ||time_temp[5]>=13||time_temp[6]>=100)
		  {
		  	pcf8563_rewirte_time(init_time);
		  }
	}
#endif

⌨️ 快捷键说明

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