解读定义DS1302的头文件中的函数问题

liubingbing 2008-05-22 05:01:37
void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数)
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0; //相当于汇编中的 RRC
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC = ACC >> 1;
}
}

unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; //相当于汇编中的 RRC
ACC7 = DS1302_IO;
DS1302_CLK = 1;
DS1302_CLK = 0;
}
return(ACC);
}

void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // 地址,命令
DS1302InputByte(ucDa); // 写1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
}

unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); // 地址,命令
ucData = DS1302OutputByte(); // 读1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
return(ucData);
}

void DS1302_SetProtect(bit flag) //是否写保护
{
if(flag)
Write1302(0x8E,0x10); //??0x80
else
Write1302(0x8E,0x00);
}

void DS1302_SetTime(unsigned char Address, unsigned char Value) // 设置时间函数
{
DS1302_SetProtect(0);
Write1302(Address, ((Value/10)<<4 | (Value%10)));
}

void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue;
ReadValue = Read1302(DS1302_SECOND);
Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_MINUTE);
Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_HOUR);
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_DAY);
Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_WEEK);
Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_MONTH);
Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_YEAR);
Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
}

void DateToStr(SYSTEMTIME *Time)
{
Time->DateString[0] = Time->Year/10 + '0';
Time->DateString[1] = Time->Year%10 + '0';
Time->DateString[2] = '-';
Time->DateString[3] = Time->Month/10 + '0';
Time->DateString[4] = Time->Month%10 + '0';
Time->DateString[5] = '-';
Time->DateString[6] = Time->Day/10 + '0';
Time->DateString[7] = Time->Day%10 + '0';
Time->DateString[8] = '\0';
}

void TimeToStr(SYSTEMTIME *Time)
{
Time->TimeString[0] = Time->Hour/10 + '0';
Time->TimeString[1] = Time->Hour%10 + '0';
Time->TimeString[2] = ':';
Time->TimeString[3] = Time->Minute/10 + '0';
Time->TimeString[4] = Time->Minute%10 + '0';
Time->TimeString[5] = ':';
Time->TimeString[6] = Time->Second/10 + '0';
Time->TimeString[7] = Time->Second%10 + '0';
Time->DateString[8] = '\0';
}

void Initial_DS1302(void)
{
unsigned char Second=Read1302(DS1302_SECOND);
if(Second&0x80)
DS1302_SetTime(DS1302_SECOND,0);
}

/********************************************************************************
void BurstWrite1302(unsigned char *pWClock) //往DS1302写入时钟数据(多字节方式)
{
unsigned char i;
Write1302(0x8e,0x00); // 控制命令,WP=0,写操作?
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(0xbe); // 0xbe:时钟多字节写命令
for (i = 8; i>0; i--) //8Byte = 7Byte 时钟数据 + 1Byte 控制
{
DS1302InputByte(*pWClock); // 写1Byte数据
pWClock++;
}
DS1302_CLK = 1;
DS1302_RST = 0;
}

void BurstRead1302(unsigned char *pRClock) //读取DS1302时钟数据(时钟多字节方式)
{
unsigned char i;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(0xbf); // 0xbf:时钟多字节读命令
for (i=8; i>0; i--)
{
*pRClock = DS1302OutputByte(); // 读1Byte数据
pRClock++;
}
DS1302_CLK = 1;
DS1302_RST = 0;
}
********************************************************************************/
#endif

能把各个函数的功能详细的解说下。(尽量详细)
...全文
573 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
空影 2009-08-24
  • 打赏
  • 举报
回复
楼主你可以去下个ds1302的中文说明书,有介绍具体的读写时序和内部寄存器,看了自然就明白了,这个时钟芯片我也使用过的
dceacho 2009-07-17
  • 打赏
  • 举报
回复
程序用了几个全局变量
关键函数
void DS1302_GetTime(SYSTEMTIME *Time)

void BurstWrite1302(unsigned char *pWClock) //往DS1302写入时钟数据(多字节方式)

void BurstRead1302(unsigned char *pRClock) //读取DS1302时钟数据(时钟多字节方式)

例如你有uchar StringT[],里面存放的时间日期,格式参考void DateToStr(SYSTEMTIME *Time) ,void DateToStr(SYSTEMTIME *Time)

....
总的来说,你在主程序中试下每隔一段距离试下执行BurstRead1302(StringT),看看结果就知道了
dgw6698555 2009-07-15
  • 打赏
  • 举报
回复
The true ,the good and the beautiful
gaojunbo 2009-07-13
  • 打赏
  • 举报
回复
good!
爪哇鹅 2009-02-03
  • 打赏
  • 举报
回复
此程序是使用了DS1302的全部功能,如果只想读取时间的话只需用到其中的几个函数,其它函数可以暂不考虑
sdqd321 2009-02-02
  • 打赏
  • 举报
回复
帮顶
noenoughmemory 2009-02-02
  • 打赏
  • 举报
回复
帮顶
gaojunbo 2009-02-02
  • 打赏
  • 举报
回复
不错!
shuiyan 2008-05-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 langhua0001 的回复:]
DS1302_SetTime(0x80,0x01) //写入秒
DS1302_SetTime(0x82,0x01) //写入分

其他的类似
[/Quote]

hoho。。。估计lz的板子连初始化都没过呢。不然哪会这么麻烦。
liubingbing 2008-05-24
  • 打赏
  • 举报
回复
那我一点一点找吧!

它的电源怎么个接法啊? 它不是有两个电源接口吗? 我如果只接一个电源,是不是接在VCC1上啊?
shuiyan 2008-05-24
  • 打赏
  • 举报
回复
既然都对的,有什么疑问呢?如果能读出时间,那就万事大吉。
如果读不出时间,千万不要说“读不出时间”,因为这太笼统了。
是通讯不成功?还是通讯成功但是读不出数据?波形看过没有?时序对吗?
liubingbing 2008-05-24
  • 打赏
  • 举报
回复
程序是对的,在仿真Proteus上就可以显示的。
主程序是这样的:
#include <REG52.H>
#include "LCD1602.h"
#include "DS1302.h"
main()
{
SYSTEMTIME CurrentTime;
LCD_Initial();
Initial_DS1302();
GotoXY(0,0);
Print("Date: ");
GotoXY(0,1);
Print("Time: ");
while(1)
{
DS1302_GetTime(¤tTime);
DateToStr(¤tTime);
TimeToStr(¤tTime);
GotoXY(6,0);
Print(CurrentTime.DateString);
GotoXY(6,1);
Print(CurrentTime.TimeString);
}
}
shuiyan 2008-05-24
  • 打赏
  • 举报
回复
如果程序正确,是的,可以直接读取。当然,你必须懂得怎么调用相关的函数。
liubingbing 2008-05-24
  • 打赏
  • 举报
回复
刚买回来的DS1302中有时间信息,就是说我可以直接读取其中的时间吗?
langhua0001 2008-05-24
  • 打赏
  • 举报
回复
DS1302_SetTime(0x80,0x01) //写入秒
DS1302_SetTime(0x82,0x01) //写入分

其他的类似
shuiyan 2008-05-24
  • 打赏
  • 举报
回复
调试是很麻烦的事,不要指望瞬间就全部ok。慢慢来,要有耐心。做技术的就是要求能静下心。

DS1302 Vcc1为后备电源,VCC2为主电源。在主电源关闭的情况下,也能保持时钟的连续运行。DS1302由Vcc1或Vcc2两者中的较大者供电。当Vcc2大于Vcc1+0.2V时,Vcc2给DS1302供电。当Vcc2小于Vcc1时,DS1302由Vcc1供电。
shuiyan 2008-05-23
  • 打赏
  • 举报
回复
程序注释比较多,建议你静下心来看,应该能看懂大部分的。

在看的过程中碰到不清楚的地方,再发帖问,这样才有针对性,才能更快解决问题。
langhua0001 2008-05-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liubingbing 的回复:]
谢了,但现在我急用呢,没时间看了,那位能解释出来,谢谢!
[/Quote]

呵呵 用你等待的时间去看看资料足够了

论坛上很难说清楚,最后你还得去看资料,

你最好先看看资料,有具体问题再过来问

你这种提问法别人很难帮你
liubingbing 2008-05-22
  • 打赏
  • 举报
回复
谢了,但现在我急用呢,没时间看了,那位能解释出来,谢谢!
langhua0001 2008-05-22
  • 打赏
  • 举报
回复
程序注释已经可以了
你缺乏的是1302的理论知识
建议你看看理论,一般单片机书里都有

http://www.infoxa.com

这里就有

27,382

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧