一、DS1302时钟模块简介
二、绘制Proteus 仿真电路图
三、编写51单片机代码
#include "DS1302.h"
// 位定义
sbit DS1302_DATA = P3^3;
sbit SCLK = P3^2;
sbit RST = P3^1;
// 向DS1302写一个字节
void DS1302_Write_Byte(unsigned char addrOrData)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
DS1302_DATA = addrOrData&(0x01<<i);
SCLK = 1;
SCLK = 0;
}
}
// 向DS1302寄存器写数据
void DS1302_Write(unsigned char addr, unsigned char dat)
{
RST = 0;
SCLK = 0;
RST = 1;
DS1302_Write_Byte(addr); // 写入寄存器地址
DS1302_Write_Byte(dat); // 写入数据
RST = 0;
}
// 读取数据
unsigned char ds1302_read_byte()
{
unsigned char dat = 0x00;
unsigned char i;
for (i = 0; i < 8; i++)
{
if(DS1302_DATA)
{
dat|=(0x01<<i);
}
SCLK = 1;
SCLK = 0;
}
return dat;
}
unsigned char ds1302_read(unsigned char addr)
{
unsigned char dat;
RST = 0;
SCLK = 0;
RST = 1;
DS1302_Write_Byte(addr);
dat = ds1302_read_byte();
RST = 0;
return dat;
}
void DS1302_Init()
{
DS1302_Write(0x8E, 0x00);
DS1302_Write(0x84, 0x19); // 写入时
DS1302_Write(0x82, 0x06); // 写入分
DS1302_Write(0x80, 0x30); // 写入秒
DS1302_Write(0x8E, 0x80);
}
四、仿真效果
五、完整源码和Proteus仿真电路图下载
链接:https://pan.baidu.com/s/1b0QVVlKWSNABu2LsQcFE0A?pwd=pvjs
提取码:pvjs