📄 ds1302.c
字号:
/******************************************************************
本程序只供学习使用,未经作者许可,不得用于其它任何用途
欢迎访问我的USB专区:http://group.ednchina.com/93/
欢迎访问我的blog: http://www.ednchina.com/blog/computer00
http://computer00.21ic.org
DS1302.C file
作者:Computer-lov
建立日期: 2008.05.19
修改日期: 2008.05.19
版本:V1.0
版权所有,盗版必究。
Copyright(C) Computer-lov 2008-2018
All rights reserved
*******************************************************************/
#include <intrins.h>
#include <at89x52.h>
#include "ds1302.h"
#include "MyType.h"
/********************************************************************
函数功能:DS1302写一字节函数。
入口参数:一字节数据。
返 回:无。
备 注:RST脚要先设置好。
********************************************************************/
void DS1302WriteByte(uint8 d)
{
uint8 i;
for(i=0;i<8;i++)
{
if(d&0x01)
{
DS_IO=1;
}
else
{
DS_IO=0;
}
DS_SCLK=1;
_nop_();
DS_SCLK=0;
d>>=1;
}
DS_IO=1;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:DS1302读一字节函数。
入口参数:无。
返 回:一字节数据。
备 注:RST要先设置好。
********************************************************************/
uint8 DS1302ReadByte(void)
{
uint8 i,d;
for(i=0;i<8;i++)
{
d>>=1;
if(DS_IO)
{
d|=0x80;
}
DS_SCLK=1;
_nop_();
DS_SCLK=0;
}
return(d);
}
////////////////////////End of function//////////////////////////////
uint8 Time[7];
/********************************************************************
函数功能:获取时间函数。
入口参数:保存时间的指针。
返 回:无。
备 注:无。
********************************************************************/
void GetTime(uint8 *p)
{
uint8 i;
DS_SCLK=0;
DS_RST=1;
DS1302WriteByte(0xBF); //Burst read ??只写一次不行,返回全1。
DS_RST=0;
DS_SCLK=0;
DS_RST=1;
DS1302WriteByte(0xBF); //Burst read
for(i=0;i<7;i++)
{
p[i]=DS1302ReadByte();
}
DS_RST=0;
DS_SCLK=0;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:设置时间函数。
入口参数:设置时间的数据指针。
返 回:无。
备 注:。
********************************************************************/
void SetTime(uint8 *p)
{
uint8 i;
DS_SCLK=0;
DS_RST=1;
p[0]&=0x7F; //启动
p[2]&=0x7F; //24小时制
DS1302WriteByte(0xBE); //Burst write
for(i=0;i<7;i++)
{
DS1302WriteByte(p[i]);
}
DS1302WriteByte(0x00);
DS_RST=0;
DS_SCLK=0;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:DS1302初始化函数。
入口参数:无。
返 回:无。
备 注:无。
********************************************************************/
void DS1302Init(void)
{
uint8 x;
DS_RST=0;
DS_SCLK=0;
DS_RST=1;
DS1302WriteByte(0x8E); //写WP
DS1302WriteByte(0x00);
DS_RST=0;
DS_SCLK=0;
DS_RST=1;
DS1302WriteByte(0x81); //读地址0
x=DS1302ReadByte();
DS_RST=0;
DS_SCLK=0;
x&=0x7F;
DS_RST=1;
DS1302WriteByte(0x80); //写地址0
DS1302WriteByte(x);
DS_RST=0;
DS_SCLK=0;
DS_RST=1;
DS1302WriteByte(0x90); //充电控制
DS1302WriteByte(0xA5);
DS_RST=0;
DS_SCLK=0;
}
////////////////////////End of function//////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -