📄 ds1302.c
字号:
#include"init.h"
#include"delay.h"
#include"ds1302.h"
/**********************************************************
//开始时的定义
**********************************************************/
void condition()
{
RST=0;
SCK=0;
RST=1;
}
/**********************************************************
//发送一个字节
**********************************************************/
void sendbyte(uchar command,uchar dat)
{
uchar i;
condition();
for(i=0;i<8;i++)
{
SDA=command&0x01;
SCK=0;
SCK=1;
command>>=1;
}
for(i=0;i<8;i++)
{
SDA=dat&0x01;
SCK=0;
SCK=1;
dat>>=1;
}
RST=0;
}
/**********************************************************
//读一个字节
**********************************************************/
uchar readbyte(uchar command)
{
uchar i,dat=0;
condition();
for(i=0;i<8;i++)
{
SDA=command&0x01;
SCK=0;
SCK=1;
command>>=1;
}
for(i=0;i<8;i++)
{
SCK=1;
SCK=0;
if(SDA)
dat|=0x01<<i;
}
RST=0;
return (dat);
}
/**********************************************************
//1时准许写入;0时为写保护
**********************************************************/
void kaiguan(bit dat)
{
if(dat)
sendbyte(0x8e,0x00);
else
sendbyte(0x8e,0x80);
}
/**********************************************************
ds1302初始化
**********************************************************/
void init_ds1302()
{
kaiguan(1);
sendbyte(0x80,0x45); // 将45写入秒寄存器;
sendbyte(0x82,0x59); // 将59写入分寄存器;
sendbyte(0x84,0x23); // 将23写入小时寄存器;
sendbyte(0x86,0x31); // 将31日写入日寄存器;
sendbyte(0x88,0x12); // 将12月写入月寄存器;
sendbyte(0x8a,0x03); // 将05星期写入寄存器; sendbyte(0x8c,0x11); // 将12年写入年寄存器;}
/**********************************************************
ds1302时钟读取
**********************************************************/
void clock_reda()
{
week =readbyte(0x8b); //读星期
year =readbyte(0x8d); //读年
mouth =readbyte(0x89); //读月
day =readbyte(0x87); //读日
hour =readbyte(0x85); //读小时
minute=readbyte(0x83); //读分钟
sec =readbyte(0x81); //读秒
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -