可以把DS1302理解为一个一个电子手表,里面带有一个31字节的RAM内存
DS1302里面有两块存储器,日历时钟寄存器(记录实时时间)和静态RAM存储器(记录其他数据)
控制字格式与数据定义
控制字的格式
BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0
1 1:RAM A4 A4 A4 A4 A4 1:RD
0:CK 0:WR
BIT7必须为1,否则不能写
BIT6,0表示存取日历时钟数据,1表示存取RAM数据
5~1为操作单元的地址
BIT0:0表示写,1表示读
unsigned char code WRITE_RTC_ADDR[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//写操作
unsigned char code READ_RTC_ADDR[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};//读操作
unsigned char TIME[7]={0x30,0x50,0x23,0x17,0x02,0x06,0x18};//日历时钟的7个配置参数,18年2月17日23时50分30秒
//日历时钟参数配置参数
void DS1302 _config()
{
unsigned char n;
DS1302_WriteByte(0x8e,0x00);
for(n=0;n<7;n++)
{
DS1302_WriteByte(WRITE_RTC_ADDR[n],TIME[n]);
}
DS1302_WriteByte(0x8e,0x80);
}
//日历时钟数据读取参数
void DS1302 _Readtime()
{
unsigned char n;
for(n=0;n<7;n++)
{
TIME[n]=DS1302_ReadByte(READ_RTC_ADDR[n]);
}
}
把官方给的DS1302.c和DS1302.h复制到文件夹中,并且把.c添加到项目里面
同时把上一篇文章里面定义了的smg.c和smg.h拿来用了,方法同上
#include"reg52.h"
#include"ds1302.h"
#include"smg.h"
unsigned char code WRITE_RTC_ADDR[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//写操作
unsigned char code READ_RTC_ADDR[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};//读操作
unsigned char TIME[7]={0x30,0x50,0x23,0x17,0x02,0x06,0x18};//日历时钟的7个配置参数,18年2月17日23时50分30秒
//日历时钟参数配置参数 //
void DS1302_config()
{
unsigned char n;
Write_Ds1302(0x8e,0x00);
for(n=0;n<7;n++)
{
Write_Ds1302(WRITE_RTC_ADDR[n],TIME[n] ); //写函数必须从.h文件里拿,必须保持一致
}
Write_Ds1302(0x8e,0x80);
}
//日历时钟数据读取参数
void DS1302_Readtime()
{
unsigned char n;
for(n=0;n<7;n++)
{
TIME[n]=Read_Ds1302 (READ_RTC_ADDR[n]); //前面的unsigned char去掉不要复制
}
}
void display_DS1302()
{
displaySMGbit(0,SMGNoDot_CA[TIME[2]/16]); //小时的十位
delaySMG(100) ;
displaySMGbit(1,SMGNoDot_CA[TIME[2]%16]); //小时的个位
delaySMG(100) ;
displaySMGbit(2,0xbf); //--
delaySMG(100) ;
displaySMGbit(3,SMGNoDot_CA[TIME[1]/16]); //分钟的十位
delaySMG(100) ;
displaySMGbit(4,SMGNoDot_CA[TIME[1]%16]); //分钟的个位
delaySMG(100) ;
displaySMGbit(5,0xbf); //--
delaySMG(100) ;
displaySMGbit(6,SMGNoDot_CA[TIME[0]/16]); //秒的十位
delaySMG(100) ;
displaySMGbit(7,SMGNoDot_CA[TIME[0]%16]); //秒的个位
delaySMG(100) ;
displayall(0xff);
}
void main()
{
DS1302_config();
while(1)
{
DS1302_Readtime();
display_DS1302();
}
}