采用SPI三线接口与MCU进行同步通信,并可采用突发方式一次传送多个字节的时钟参数和RAM数据。
日历时钟寄存器有8个存储单元:
所以,我们需要三个数组:
定义DS1302读操作的日历时钟存储器地址
r_add[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
定义DS1302写操作的日历时钟存储器地址
w_add[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
定义DS1302日历时钟的7个配置参数:
如22年2月6日21时56分30秒
nowTime[7]={0x30,0x34,0x21,0x06,0x02,0x07,0x22};
我们可以进行基本操作:设定时间参数,读取实时时间。
日历时钟参数配置函数:
void DS1302_Config()
{
char i;
Write_Ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302(w_add[i],nowTime[i]);
}
Write_Ds1302(0x8e,0x80);
}
日历时钟数据读取函数:
void Get_Time(void)
{
unsigned char i;
EA = 0;
for(i=0;i<7;i++){
nowTime[i] = Read_Ds1302(r_add[i]);
}
EA = 1;
}
下面是完整代码:
SMG.h
#ifndef __SMG_H
#define __SMG_H
#include "reg52.h"
void DelaySMG(unsigned char t);
void DisPlaySMG_Bit(unsigned char pos,unsigned char dat);
void DisPlay_All(unsigned char dat);
#endif
SMG.c:
#include "SMG.h"
void DelaySMG(unsigned char t)
{
while(t--);
}
void DisPlaySMG_Bit(unsigned char pos,unsigned char dat)
{
P0=0xff;
P2=P2&0x1f|0xe0;
P2=P2&0x1f;
P0=0x01<<pos;
P2=P2&0x1f|0xc0;
P2=P2&0x1f;
P0=dat;
P2=P2&0x1f|0xe0;
P2=P2&0x1f;
}
void DisPlay_All(unsigned char dat)
{
P0=0xff;
P2=P2&0x1f|0xc0;
P2=P2&0x1f;
P0=dat;
P2=P2&0x1f|0xe0;
P2=P2&0x1f;
}
ds1302.h:
#ifndef __DS1302_H
#define __DS1302_H
#define REG_SECOND 0x80
#define REG_MINUTES 0x82
#define REG_HOUR 0x84
#define REG_DATE 0x86
#define REG_MONTH 0x88
#define REG_WEEK 0x8A
#define REG_YEAR 0x8C
#define REG_PROTECT 0x8E
void Write_Ds1302_Byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void DS1302_Init(void);
void Get_Time(void);
#endif
ds1302.c:
#include <intrins.h>
#include "ds1302.h"
#include "reg52.h"
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3; // DS1302¸´Î»
extern unsigned char nowTime[7];//Ãë·Öʱ13:30:00
code unsigned char r_add[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
/**
* @brief Send a byte to the DS1302.
* @param temp: value to write to DS1302.
* @retval None
*/
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
/**
* @brief Writes to the selected DS1302 register.
* @param address: address of the selected register.
* @param dat: value to write to the selected register.
* @retval None
*/
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=0;
}
/**
* @brief Reads the selected DS1302 Register.
* @param address: address of the selected register.
* @retval DS1302 Register Value.
*/
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;
_nop_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp);
}
void DS1302_Init(void)
{
Write_Ds1302(REG_PROTECT,0x00);
Write_Ds1302(REG_SECOND,0);
Write_Ds1302(REG_MINUTES,0);
Write_Ds1302(REG_HOUR,0);
Write_Ds1302(REG_PROTECT,0x80);
}
void Get_Time(void)
{
unsigned char i;
EA = 0;
for(i=0;i<7;i++){
nowTime[i] = Read_Ds1302(r_add[i]);
}
EA = 1;
}
main.c:
#include "ds1302.h"
#include "SMG.h"
#include "reg52.h"
code unsigned char w_add[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
code unsigned char SMG_duanma[10]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90};
//2022.2.6 7 21.33.30
unsigned char nowTime[7]={0x30,0x34,0x21,0x06,0x02,0x07,0x22};
void DS1302_Config()
{
char i;
Write_Ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302(w_add[i],nowTime[i]);
}
Write_Ds1302(0x8e,0x80);
}
void DisPlay_DS1302()
{
//h
DisPlaySMG_Bit(0,SMG_duanma[nowTime[2]/16]);
DelaySMG(100);
DisPlaySMG_Bit(1,SMG_duanma[nowTime[2]%16]);
DelaySMG(100);
DisPlaySMG_Bit(2,0xbf);
DelaySMG(100);
//min
DisPlaySMG_Bit(3,SMG_duanma[nowTime[1]/16]);
DelaySMG(100);
DisPlaySMG_Bit(4,SMG_duanma[nowTime[1]%16]);
DelaySMG(100);
DisPlaySMG_Bit(5,0xbf);
DelaySMG(100);
//s
DisPlaySMG_Bit(6,SMG_duanma[nowTime[0]/16]);
DelaySMG(100);
DisPlaySMG_Bit(7,SMG_duanma[nowTime[0]%16]);
DelaySMG(100);
DisPlay_All(0xff);
}
void main()
{
DS1302_Init();
DS1302_Config();
P0=0;
P2=P2&0x1f|0xa0;
P2=P2&0x1f;
while(1)
{
Get_Time();
DisPlay_DS1302();
}
}