#define RTC_OK1//操作成功
#define RTC_FAIL0//操作失败
#define SECOND_INVALID40//秒值无效
#define MINUTE_INVALID41//分钟值无效
#define HOUR_INVALID42//小时值无效
#define WEEK_INVALID43//星期值无效
#define DAY_INVALID44//日期值无效
#define MONTH_INVALID45//月份值无效
#define YEAR_INVALID46//年份值无效
#define CENTURY_INVALID47//世纪值无效
//时钟制式参数定义(暂没开启,全用24小时制)
#define MODE_12_HOUR0x80//12小时制,即有AM/PM
#define MODE_24_HOUR0x00//24小时制
//充电参数定义
#define CHARGE_ENABLE0xA0//开启充电,此时,若限流电阻选择SELECT_NONE,还是不能充电
#define CHARGE_DISABLE0x00//关闭充电
//充电二极管参数
#define SELECT_ONE_DIODE0x04//选择一个二极管
#define SELECT_TWO_DIODE0x08//选择二个二极管
//充电限流电阻参数
#define SELECT_NONE0x00//不用连,即断开充电电路
#define SELECT_R1_2k0x01//选择电阻1,2K
#define SELECT_R2_4k0x02//选择电阻2,4K
#define SELECT_R3_8k0x03//选择电阻3,8K
#define RAM_MAXSIZE31//时钟内部用户可自定义的RAM最大空间
typedef struct _DATETIME
{
unsigned char Century;
unsigned char Year;
unsigned char Month;
unsigned char Day;
unsigned char Hour;
unsigned char Min;
unsigned char Sec;
unsigned char Week;
}sttDateTime;
sbitRST=P1^5;
sbitSCL=P1^6;
sbitSDA=P1^7;
#define SEC_ADDR0x80//1000 0000
#define MIN_ADDR0x82//1000 0010
#define HOUR_ADDR0x84//1000 0100
#define DAY_ADDR0x86//1000 0110
#define MOM_ADDR0x88//1000 1000
#define WEEK_ADDR0x8A//1000 1010
#define YEAR_ADDR0x8C//1000 1100
#define CENTURY_ADDR0xC0//1100 0000用其RTC内存一字节作为年世纪字节
#define CONTROL_ADDR0x8E//1000 1110
#define TRCH_ADDR0x90//1001 0000
#define RAM_BASE_ADDR0xC0
#define READ1
#define WRITE0
void DS1302_SendByte(unsigned char Data)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 0;
SDA = (bit)(Data & 0x01);
SCL = 1;
Data >>= 1;
}
return;
}
unsigned char DS1302_ReadByte(void)
{
unsigned char i,Data;
SDA = 1;
for(i=0;i<8;i++)
{
Data >>= 1;
SCL = 0;
if(SDA==1) Data |= 0x80;
SCL = 1;
}
return(Data);
}
voidDS1302WriteByte(unsigned char Addr, unsigned char Data)
{
SCL = 0;
RST = 1;
DS1302_SendByte(Addr);
DS1302_SendByte(Data);
RST = 0;
return ;
}
unsigned char DS1302ReadByte(unsigned char Addr)
{
unsigned char Data;
SCL = 0;
RST = 1;
DS1302_SendByte(Addr | READ);
Data = DS1302_ReadByte();
RST = 0;
return Data;
}
//初始化时钟
void InitDS1302(unsigned char ChargeMode)
{
unsigned char i;
SCL = 0;
RST = 0;
DS1302WriteByte(CONTROL_ADDR,0x00);//WP=0;
i = DS1302ReadByte(SEC_ADDR);
if((i & 0x80) == 0x80)
{//若时钟丢失,即锂电池没电时,自动复位为2005-1-1 00:00:00 星期六
DS1302WriteByte(CENTURY_ADDR,0x20);
DS1302WriteByte(YEAR_ADDR,0x06);
DS1302WriteByte(MOM_ADDR,0x01);
DS1302WriteByte(DAY_ADDR,0x01);
DS1302WriteByte(HOUR_ADDR,0x00);
DS1302WriteByte(MIN_ADDR,0x00);
DS1302WriteByte(SEC_ADDR,0x00);
DS1302WriteByte(WEEK_ADDR,0x00);
}
DS1302WriteByte(TRCH_ADDR,ChargeMode);//设置充电模式
DS1302WriteByte(CONTROL_ADDR,0x80);//WP=1;
}
//读时钟
void ReadClock(sttDateTime *pstDateTime)
{
//pstDateTime->Century = DS1302ReadByte(CENTURY_ADDR);
pstDateTime->Year= DS1302ReadByte(YEAR_ADDR);
if(pstDateTime->Year < 0x80)
pstDateTime->Century = 0x20;
else
pstDateTime->Century = 0x19;
pstDateTime->Month= DS1302ReadByte(MOM_ADDR);
pstDateTime->Day= DS1302ReadByte(DAY_ADDR);
pstDateTime->Hour= DS1302ReadByte(HOUR_ADDR);
pstDateTime->Min= DS1302ReadByte(MIN_ADDR);
pstDateTime->Sec= DS1302ReadByte(SEC_ADDR);
pstDateTime->Week= DS1302ReadByte(WEEK_ADDR);
}
//写时钟
void WriteClock(sttDateTime *pstDateTime)
{
DS1302WriteByte(CONTROL_ADDR,0x00);//WP=0;
DS1302WriteByte(CENTURY_ADDR,pstDateTime->Century);
DS1302WriteByte(YEAR_ADDR,pstDateTime->Year);
DS1302WriteByte(MOM_ADDR,pstDateTime->Month);
DS1302WriteByte(DAY_ADDR,pstDateTime->Day);
DS1302WriteByte(HOUR_ADDR,pstDateTime->Hour);
DS1302WriteByte(MIN_ADDR,pstDateTime->Min);
DS1302WriteByte(SEC_ADDR,pstDateTime->Sec);
DS1302WriteByte(WEEK_ADDR,pstDateTime->Week);
DS1302WriteByte(CONTROL_ADDR,0x80);//WP=1;
}