|
最近用了款DS1302 RTC實(shí)時(shí)時(shí)鐘,自己動(dòng)手做個(gè)萬(wàn)年歷時(shí)鐘,加上鬧鐘,再也不用擔(dān)心午睡超時(shí)了。
按鍵說(shuō)明:S1 時(shí)間設(shè)置;S2 鬧鈴設(shè)置;S3 移動(dòng)模式;S4 時(shí)間減;S5 時(shí)間加;S6 確認(rèn); S7 取消。
制作出來(lái)的實(shí)物圖如下:
萬(wàn)年歷點(diǎn)亮啦
萬(wàn)年歷組裝好啦
電路原理圖如下:
單片機(jī)源程序如下:
先來(lái)一段1302驅(qū)動(dòng)程序:
/*
*******************************************************************************
*
* 文 件 名:DS1302.c
* 描 述:實(shí)時(shí)時(shí)鐘芯片DS1302驅(qū)動(dòng)模塊
* 功 能:
*******************************************************************************
*/
#include "Config.h"
#include "DS1302.h"
/********LCD1302的讀/寫函數(shù)編輯寫程序********/
/**********************************************
函 數(shù) 名: RTInputByte()
功 能: 實(shí)時(shí)時(shí)鐘寫入一字節(jié)
說(shuō) 明: 往DS1302寫入1Byte數(shù)據(jù)(內(nèi)部函數(shù))
入口參數(shù): d 寫放的數(shù)據(jù)
返 回 值: 無(wú)
**********************************************/
void RTInputByte(uchar d)
{
uchar i;
T_IO = 0; //把此雙向口清零,不用在此腳加上拉電阻
for(i=8; i>0; i--)
{
T_IO=d&0x01; //取最低位
T_CLK=0;
T_CLK=1;
d=d>>1;
}
}
/******************************************************************************/
/* 函數(shù)名稱 : DS1302_OutputByte */
/* 函數(shù)描述 : 從DS1302讀取1Byte數(shù)據(jù)函數(shù) */
/* 輸入?yún)?shù) : 無(wú) */
/* 參數(shù)描述 : 無(wú) */
/* 返回值 : 無(wú) */
/******************************************************************************/
uchar RTOutputByte(void) //換此函數(shù)正確
{
uchar i;
uchar temp;
temp = 0;
for(i = 0; i < 8; i++)
{
T_CLK = 0;
if(T_IO != 0)
temp |= (1 << i);
T_CLK = 1;
}
return(temp);
}
void reset_ds1302()
{
T_RST = 0;
T_CLK = 0;
T_RST = 1;
}
void clear_ds1302_WP()
{
reset_ds1302();
T_RST = 1;
RTInputByte(0x8E);
RTInputByte(0);
T_IO = 0;
T_RST = 0;
}
/*設(shè)置寫保護(hù)*/
void set_ds1302_WP()
{
reset_ds1302();
T_RST = 1;
RTInputByte(0x8E);
RTInputByte(0x80);
T_IO = 0;
T_RST = 0;
}
/**********************************************
函 數(shù) 名: Write1302()
功 能: 往DS1302寫入數(shù)據(jù)
說(shuō) 明: 先寫地址,后寫命令/數(shù)據(jù)(內(nèi)部函數(shù))
調(diào) 用: RTInputByte(), RTOutputByte()
入口參數(shù): ucAddr:DS1302地址,ucData:要寫的數(shù)據(jù)
返 回 值: 無(wú)
**********************************************/
void Write1302(uchar ucAddr,uchar ucDa)
{
clear_ds1302_WP();
T_RST = 0;
T_CLK = 0;
T_RST = 1;
RTInputByte(ucAddr); //地址,寫地址
RTInputByte(ucDa); //寫1Byte數(shù)據(jù)
T_CLK = 1;
T_RST = 0;
set_ds1302_WP();
}
/**********************************************
函 數(shù) 名: Read1302()
功 能: 讀取DS1302某地址的數(shù)據(jù)
說(shuō) 明: 先寫地址,后讀命令/數(shù)據(jù)(內(nèi)部函數(shù))
調(diào) 用: RTInputByte(), RTOutputByte()
入口參數(shù): ucAddr:DS1302地址
返 回 值: ucData:讀取的數(shù)據(jù)
**********************************************/
uchar Read1302(uchar ucAddr)
{
uchar ucData;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
RTInputByte(ucAddr); //地址,命令
ucData = RTOutputByte(); //讀1Byte數(shù)據(jù)
T_CLK = 1;
T_RST = 0;
return(ucData);
}
/**********************************************
函 數(shù) 名: Set1302()
功 能: 設(shè)置初始時(shí)間
說(shuō) 明: 先寫地址,后讀命令/數(shù)據(jù)(寄存器多字節(jié)方式)
調(diào) 用: Write1302()
入口參數(shù): pClock:設(shè)置時(shí)鐘數(shù)據(jù)地址 格式為: 秒 分 時(shí) 日 月 星期 年
7Byte(BCD碼) 1B 1B 1B 1B 1B 1B 1B
返 回 值: 無(wú)
**********************************************/
void Set1302(uchar *pClock)
{
uchar i;
uchar ucAddr = 0x80;
Write1302(0x8e,0x00); //控制命令,WP=0, 寫操作
for(i=7; i>0; i--)
{
Write1302(ucAddr, *pClock); //秒 分 時(shí) 日 月 星期 年
pClock++;
ucAddr += 2;
}
Write1302(0x8e,0x80); //控制命令,WP=1,寫保護(hù)
}
/**********************************************
函 數(shù) 名: get_time()
功 能: 獲取DS1302時(shí)間數(shù)據(jù)
說(shuō) 明: 先寫地址,后讀命令/數(shù)據(jù)
調(diào) 用: Read1302()
入口參數(shù): 無(wú)
返 回 值: *p: 把讀取到的數(shù)據(jù)保存到 p 指向的數(shù)組里
**********************************************/
void get_time(uchar *p)
{
uchar i,n;
n = 0x81;
for(i=7; i>0; i--)
{
*p = Read1302(n);
p++;
n+=2;
}
}
詳見(jiàn)附件哦~~
全部資料51hei下載地址:
DS1302.rar
(297.68 KB, 下載次數(shù): 187)
2019-9-26 09:32 上傳
點(diǎn)擊文件名下載附件
原理圖源代碼
|
評(píng)分
-
查看全部評(píng)分
|