一、DS1302時(shí)鐘模塊
現(xiàn)在流行的串行時(shí)鐘電路很多,如DS1302、 DS1307、PCF8485等。這些電路的接口簡(jiǎn)單、價(jià)格低廉、使用方便,被廣泛地采用。
DS1302 是美國(guó)DALLAS公司推出的一種高性能、低功耗、帶RAM的實(shí)時(shí)時(shí)鐘電路,它可以對(duì)年、月、日、周、時(shí)、分、秒進(jìn)行計(jì)時(shí),具有閏年補(bǔ)償功能,工作電壓為2.0V~5.5V。采用三線接口與CPU進(jìn)行同步通信,并可采用突發(fā)方式一次傳送多個(gè)字節(jié)的時(shí)鐘信號(hào)或RAM數(shù)據(jù)。
DS1302內(nèi)部有一個(gè)31×8的用于臨時(shí)性存放數(shù)據(jù)的RAM寄存器。DS1302是DS1202的升級(jí)產(chǎn)品,與DS1202兼容,但增加了主電源/后備電源雙電源引腳,同時(shí)提供了對(duì)后備電源進(jìn)行涓細(xì)電流充電的能力,該芯片采用普通32.768kHz晶振,DS1302 工作時(shí)功耗很低保持?jǐn)?shù)據(jù)和時(shí)鐘信息時(shí)功率小于1mW。
圖1 DS1302時(shí)鐘模塊實(shí)物圖
圖2 DS1302時(shí)鐘模塊封裝
DS1302時(shí)鐘模塊的引腳功能介紹如表1所示,而時(shí)序不再做陳述,需要再自行查找資料。
二、DS1302時(shí)鐘模塊驅(qū)動(dòng)代碼
1.頭文件
#ifndef DS1302_H
#define DS1302_H
#include"STC15F2K60S2.h"
#ifndef UINT8
#define UINT8unsigned char
#endif
#defineDS1302_READ_BURST 0xBF
#endif
#ifndefDS1302_WRITE_BURST
#defineDS1302_WRITE_BURST 0xBE
#endif
sbitDS1302_IO = P1^4;
sbitDS1302_RST = P1^5;
sbit DS1302_SCLK =P1^3;
extern UINT8 xdatatime[9];
extern UINT8 xdatadate[11];
extern UINT8 xdatacurrent_day[2];
//聲明全局變量
voidDS1302_WriteByte(UINT8 data_byte);//向ds1302寫一個(gè)字節(jié)
voidDS1302_ReadByte(UINT8 *data_byte);//從ds1302讀一個(gè)字節(jié)
voidDS1302_Start();//操作起始信號(hào)
voidDS1302_Over();//操作結(jié)束信號(hào)
voidDS1302_ClearWriteProtection();//清除寫保護(hù)
voidDS1302_SetWriteProtection();//設(shè)置寫保護(hù)
voidDS1302_SetTime(UINT8 *ds1302_set_buffer);//設(shè)置ds1302的時(shí)間
voidDS1302_ReadTime(UINT8 *ds1302_build_buffer);//讀取ds1302的時(shí)間
voidTime_Build();//系統(tǒng)從ds1302讀取時(shí)間
void Time_Set();//系統(tǒng)向ds1302設(shè)置時(shí)間
voidTime_Init();//系統(tǒng)時(shí)間初始化
#endif
2.主程序
#include"ds1302.h"
voidDS1302_WriteByte(UINT8 data_byte)//向ds1302寫一個(gè)字節(jié)
{
UINT8 i;
for (i=0;i< 8;i++)
{
DS1302_IO = data_byte &0x01;
DS1302_SCLK = 1;
data_byte > >= 1;
DS1302_SCLK = 0;
}
}
voidDS1302_ReadByte(UINT8 *data_byte) //從ds1302讀一個(gè)字節(jié)
{
UINT8 i;
for (i=0;i< 8;i++)
{
*data_byte > >= 1;
if (DS1302_IO){*data_byte |=0x80;}
DS1302_SCLK = 1;
DS1302_SCLK = 0;
}
}
voidDS1302_Start()//操作起始信號(hào)
{
DS1302_RST = 0;
DS1302_SCLK = 0;
DS1302_RST = 1;
}
voidDS1302_Over()//操作結(jié)束信號(hào)
{
DS1302_IO = 0;
DS1302_RST = 0;
}
voidDS1302_ClearWriteProtection()//清除寫保護(hù)
{
DS1302_Start();
DS1302_WriteByte(0x8E);
DS1302_WriteByte(0x00);
DS1302_Over();
}
voidDS1302_SetWriteProtection()//設(shè)置寫保護(hù)
{
DS1302_Start();
DS1302_WriteByte(0x8E);
DS1302_WriteByte(0x80);
DS1302_Over();
}
voidDS1302_SetTime(UINT8 *ds1302_set_buffer)//突發(fā)模式下設(shè)置時(shí)間
{
UINT8 i;
DS1302_ClearWriteProtection();
DS1302_Start();
DS1302_WriteByte(DS1302_WRITE_BURST);
for (i=0; i< 7; i++)
{
DS1302_WriteByte(ds1302_set_buffer[i]);
}
DS1302_WriteByte(0x80);//突發(fā)模式一次要寫8個(gè)字節(jié),第八個(gè)字節(jié)是寫保護(hù)字節(jié)
DS1302_Over();
}
void DS1302_ReadTime(UINT8*ds1302_read_buffer)//突發(fā)模式下讀取時(shí)間
{
UINT8 i,Temp;
DS1302_ClearWriteProtection();
DS1302_Start();
DS1302_WriteByte(DS1302_READ_BURST);
for (i=0; i< 7; i++)
{
DS1302_ReadByte(ds1302_read_buffer+i);
}
DS1302_ReadByte(&Temp);//突發(fā)模式一次讀8個(gè)字節(jié),最后一字節(jié)讀出來(lái)沒(méi)用
DS1302_Over();
DS1302_SetWriteProtection();
}
voidTime_Build()//讀取時(shí)間后轉(zhuǎn)換成需要的格式
{
UINT8 xdata ds1302_build_buffer[7];
DS1302_ReadTime(ds1302_build_buffer);
time[7] =(ds1302_build_buffer[0]&0x0f)+'0';
time[6] =((ds1302_build_buffer[0]&0x70) >>4)+'0';
time[4] =(ds1302_build_buffer[1]&0x0f)+'0';
time[3] =((ds1302_build_buffer[1]&0x70) >>4)+'0';
time[1] =(ds1302_build_buffer[2]&0x0f)+'0';
time[0] =((ds1302_build_buffer[2]&0x30) >>4)+'0';
date[9] =(ds1302_build_buffer[3]&0x0f)+'0';
date[8] =((ds1302_build_buffer[3]&0x30) >>4)+'0';
date[6] =(ds1302_build_buffer[4]&0x0f)+'0';
date[5] =((ds1302_build_buffer[4]&0x10) >>4)+'0';
date[3] =(ds1302_build_buffer[6]&0x0f)+'0';
date[2] = ((ds1302_build_buffer[6]&0xf0) >>4)+'0';
}
void Time_Set()//將時(shí)間轉(zhuǎn)化為對(duì)應(yīng)格式存入ds1302
{
UINT8 xdata ds1302_set_buffer[7];
ds1302_set_buffer[0] = time[7]-'0';
ds1302_set_buffer[0] |=((time[6]-'0')&0x07)< 4;
ds1302_set_buffer[1] = time[4]-'0';
ds1302_set_buffer[1] |=((time[3]-'0')&0x07)< 4;
ds1302_set_buffer[2] = time[1]-'0';
ds1302_set_buffer[2] |=((time[0]-'0')&0x03)< 4;
ds1302_set_buffer[3] = date[9]-'0';
ds1302_set_buffer[3] |=((date[8]-'0')&0x03)< 4;
ds1302_set_buffer[4] = date[6]-'0';
ds1302_set_buffer[4] |=((date[5]-'0')&0x01)< 4;
ds1302_set_buffer[6] = date[3]-'0';
ds1302_set_buffer[6] |=((date[2]-'0')&0x0f)< 4;
ds1302_set_buffer[5] = 0x01;
DS1302_SetTime(ds1302_set_buffer);
}
void Time_Init()//開機(jī)時(shí)間初始化
{
Time_Build();
current_day[0] = date[8];
current_day[1] = date[9];//保存當(dāng)前日期,用于檢測(cè)日期變化
}
三、 DS1302時(shí)鐘模塊與USRAT HMI通信
uchara=0,b=0,c=0,d=0,e=0,f=0;
/**********顯示時(shí)間**********/
a = date[2]; //發(fā)送年數(shù)據(jù)
b = date[3];
c = date[5]; //發(fā)送月數(shù)據(jù)
d = date[6];
e = date[8]; //發(fā)送日數(shù)據(jù)
f = date[9];
write_txt("t0.txt="); //發(fā)送文本
write_COM(34); //雙引號(hào)
write_COM(a);
write_COM(b);
write_COM(34);
write_END(); //結(jié)束符
write_txt("t1.txt="); //發(fā)送文本
write_COM(34); //雙引號(hào)
write_COM(c);
write_COM(d);
write_COM(34);
write_END(); //結(jié)束符
write_txt("t2.txt="); //發(fā)送文本
write_COM(34); //雙引號(hào)
write_COM(e);
write_COM(f);
write_COM(34);
write_END(); //結(jié)束符
a = time[0]; //發(fā)送時(shí)數(shù)據(jù)
b = time[1];
c = time[3]; //發(fā)送分鐘數(shù)據(jù)
d = time[4];
e = time[6]; //發(fā)送秒鐘數(shù)據(jù)
f = time[7];
write_txt("t3.txt="); //發(fā)送文本
write_COM(34); //雙引號(hào)
write_COM(a);
write_COM(b);
write_COM(34);
write_END(); //結(jié)束符
write_txt("t4.txt="); //發(fā)送文本
write_COM(34); //雙引號(hào)
write_COM(c);
write_COM(d);
write_COM(34);
write_END(); //結(jié)束符
write_txt("t5.txt="); //發(fā)送文本
write_COM(34); //雙引號(hào)
write_COM(e);
write_COM(f);
write_COM(34);
write_END(); //結(jié)束符
write_txt("t9.txt="); //發(fā)送文本
write_COM(34);
if(week==1) //發(fā)送星期數(shù)據(jù)
write_txt("一");
if(week==2)
write_txt("二");
if(week==3)
write_txt("三");
if(week==4)
write_txt("四");
if(week==5)
write_txt("五");
if(week==6)
write_txt("六");
if(week==7)
write_txt("日");
write_COM(34);
write_END();
-
寄存器
+關(guān)注
關(guān)注
31文章
5394瀏覽量
122354 -
實(shí)時(shí)時(shí)鐘
+關(guān)注
關(guān)注
4文章
292瀏覽量
66459 -
DS1302
+關(guān)注
關(guān)注
8文章
450瀏覽量
51211 -
PCF
+關(guān)注
關(guān)注
0文章
32瀏覽量
21018 -
時(shí)鐘模塊
+關(guān)注
關(guān)注
1文章
58瀏覽量
14505
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
DS1302時(shí)鐘模塊簡(jiǎn)介
DS1302基本介紹
DS1302驅(qū)動(dòng)代碼分享
時(shí)鐘芯片DS1302及其在數(shù)據(jù)記錄中的應(yīng)用
實(shí)時(shí)時(shí)鐘模DS1302程序列子
基于實(shí)時(shí)時(shí)鐘模塊 時(shí)鐘芯片DS1302

51單片機(jī)DS1302實(shí)時(shí)時(shí)鐘驅(qū)動(dòng)程序

DS1302時(shí)鐘芯片的使用源代碼免費(fèi)下載

微雪電子RTC 時(shí)鐘模塊 DS1302簡(jiǎn)介

DS1302時(shí)鐘芯片的Keil代碼免費(fèi)下載

畢業(yè)設(shè)計(jì)之時(shí)鐘模塊設(shè)計(jì)DS1302

基于STM32的DS1302時(shí)鐘模塊驅(qū)動(dòng)程序(詳細(xì))

評(píng)論