|
#include"ds1302.h"
//---DS1302寫入和讀取時(shí)分秒的地址命令---//
//---秒分時(shí)日月周年 最低位讀寫位;-------//</font>
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
這兩行地址和最低讀寫位是怎么來的,我可以隨便寫幾個(gè)上去嗎?
- #ifndef __DS1302_H_
- #define __DS1302_H_
- //---包含頭文件---//
- #include<reg52.h>
- #include<intrins.h>
- //---重定義關(guān)鍵詞---//
- #ifndef uchar
- #define uchar unsigned char
- #endif
- #ifndef uint
- #define uint unsigned int
- #endif
- //---定義ds1302使用的IO口---//
- sbit DSIO=P3^4;
- sbit RST=P3^5;
- sbit SCLK=P3^6;
- //---定義全局函數(shù)---//
- void Ds1302Write(uchar addr, uchar dat);
- uchar Ds1302Read(uchar addr);
- void Ds1302Init();
- void Ds1302ReadTime();
- //---加入全局變量--//
- extern uchar TIME[7]; //加入全局變量
- #endif
復(fù)制代碼- #include"ds1302.h"
- //---DS1302寫入和讀取時(shí)分秒的地址命令---//
- //---秒分時(shí)日月周年 最低位讀寫位;-------//</font>
- uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
- uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
- //---DS1302時(shí)鐘初始化2016年5月7日星期六12點(diǎn)00分00秒。---//
- //---存儲(chǔ)順序是秒分時(shí)日月周年,存儲(chǔ)格式是用BCD碼---//
- uchar TIME[7] = {0, 0, 0x12, 0x07, 0x05, 0x06, 0x16};
- /*******************************************************************************
- * 函 數(shù) 名 : Ds1302Write
- * 函數(shù)功能 : 向DS1302命令(地址+數(shù)據(jù))
- * 輸 入 : addr,dat
- * 輸 出 : 無
- *******************************************************************************/
- void Ds1302Write(uchar addr, uchar dat)
- {
- uchar n;
- RST = 0;
- _nop_();
- SCLK = 0;//先將SCLK置低電平。
- _nop_();
- RST = 1; //然后將RST(CE)置高電平。
- _nop_();
- for (n=0; n<8; n++)//開始傳送八位地址命令
- {
- DSIO = addr & 0x01;//數(shù)據(jù)從低位開始傳送
- addr >>= 1;
- SCLK = 1;//數(shù)據(jù)在上升沿時(shí),DS1302讀取數(shù)據(jù)
- _nop_();
- SCLK = 0;
- _nop_();
- }
- for (n=0; n<8; n++)//寫入8位數(shù)據(jù)
- {
- DSIO = dat & 0x01;
- dat >>= 1;
- SCLK = 1;//數(shù)據(jù)在上升沿時(shí),DS1302讀取數(shù)據(jù)
- _nop_();
- SCLK = 0;
- _nop_();
- }
-
- RST = 0;//傳送數(shù)據(jù)結(jié)束
- _nop_();
- }
- /*******************************************************************************
- * 函 數(shù) 名 : Ds1302Read
- * 函數(shù)功能 : 讀取一個(gè)地址的數(shù)據(jù)
- * 輸 入 : addr
- * 輸 出 : dat
- *******************************************************************************/
- uchar Ds1302Read(uchar addr)
- {
- uchar n,dat,dat1;
- RST = 0;
- _nop_();
- SCLK = 0;//先將SCLK置低電平。
- _nop_();
- RST = 1;//然后將RST(CE)置高電平。
- _nop_();
- for(n=0; n<8; n++)//開始傳送八位地址命令
- {
- DSIO = addr & 0x01;//數(shù)據(jù)從低位開始傳送
- addr >>= 1;
- SCLK = 1;//數(shù)據(jù)在上升沿時(shí),DS1302讀取數(shù)據(jù)
- _nop_();
- SCLK = 0;//DS1302下降沿時(shí),放置數(shù)據(jù)
- _nop_();
- }
- _nop_();
- for(n=0; n<8; n++)//讀取8位數(shù)據(jù)
- {
- dat1 = DSIO;//從最低位開始接收
- dat = (dat>>1) | (dat1<<7);
- SCLK = 1;
- _nop_();
- SCLK = 0;//DS1302下降沿時(shí),放置數(shù)據(jù)
- _nop_();
- }
- RST = 0;
- _nop_(); //以下為DS1302復(fù)位的穩(wěn)定時(shí)間,必須的。
- SCLK = 1;
- _nop_();
- DSIO = 0;
- _nop_();
- DSIO = 1;
- _nop_();
- return dat;
- }
- /*******************************************************************************
- * 函 數(shù) 名 : Ds1302Init
- * 函數(shù)功能 : 初始化DS1302.
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void Ds1302Init()
- {
- uchar n;
- Ds1302Write(0x8E,0X00); //禁止寫保護(hù),就是關(guān)閉寫保護(hù)功能
- for (n=0; n<7; n++)//寫入7個(gè)字節(jié)的時(shí)鐘信號(hào):分秒時(shí)日月周年
- {
- Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
- }
- Ds1302Write(0x8E,0x80); //打開寫保護(hù)功能
- }
- /*******************************************************************************
- * 函 數(shù) 名 : Ds1302ReadTime
- * 函數(shù)功能 : 讀取時(shí)鐘信息
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void Ds1302ReadTime()
- {
- uchar n;
- for (n=0; n<7; n++)//讀取7個(gè)字節(jié)的時(shí)鐘信號(hào):分秒時(shí)日月周年
- {
- TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
- }
-
- }
復(fù)制代碼
|
|