|
include "reg52.h" //此文件中定義了單片機(jī)的一些特殊功能寄存器
#include"ds1302.h"
typedef unsigned int u16; //對數(shù)據(jù)類型進(jìn)行聲明定義
typedef unsigned char u8;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
char num=0;
u8 DisplayData[8];
u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/*******************************************************************************
* 函 數(shù) 名 : delay
* 函數(shù)功能 : 延時函數(shù),i=1時,大約延時10us
*******************************************************************************/
void delay(u16 i)
{
while(i--);
}
/*******************************************************************************
* 函 數(shù) 名 : datapros()
* 函數(shù)功能 : 時間讀取處理轉(zhuǎn)換函數(shù)
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void datapros()
{
Ds1302ReadTime();
DisplayData[0] = smgduan[TIME[2]/16]; //時
DisplayData[1] = smgduan[TIME[2]&0x0f];
DisplayData[2] = 0x40;
DisplayData[3] = smgduan[TIME[1]/16]; //分
DisplayData[4] = smgduan[TIME[1]&0x0f];
DisplayData[5] = 0x40;
DisplayData[6] = smgduan[TIME[0]/16]; //秒
DisplayData[7] = smgduan[TIME[0]&0x0f];
}
/*******************************************************************************
* 函數(shù)名 :DigDisplay()
* 函數(shù)功能 :數(shù)碼管顯示函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void DigDisplay()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//顯示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//顯示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//顯示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//顯示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//顯示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//顯示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//顯示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//顯示第7位
}
P0=DisplayData[7-i];//發(fā)送數(shù)據(jù)
delay(100); //間隔一段時間掃描
P0=0x00;//消隱
}
}
/*******************************************************************************
* 函 數(shù) 名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void main()
{
Ds1302Init();
while(1)
{
datapros(); //數(shù)據(jù)處理函數(shù)
DigDisplay();//數(shù)碼管顯示函數(shù)
}
}
#include"ds1302.h"
//---DS1302寫入和讀取時分秒的地址命令---//
//---秒分時日月周年 最低位讀寫位;-------//
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時鐘初始化2016年5月7日星期六12點(diǎn)00分00秒。---//
//---存儲順序是秒分時日月周年,存儲格式是用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ù)在上升沿時,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ù)在上升沿時,DS1302讀取數(shù)據(jù)
_nop_();
SCLK = 0;
_nop_();
}
RST = 0;//傳送數(shù)據(jù)結(jié)束
_nop_();
}
/*******************************************************************************
* 函 數(shù) 名 : Ds1302Read
* 函數(shù)功能 : 讀取一個地址的數(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ù)在上升沿時,DS1302讀取數(shù)據(jù)
_nop_();
SCLK = 0;//DS1302下降沿時,放置數(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ù)據(jù)
_nop_();
}
RST = 0;
_nop_(); //以下為DS1302復(fù)位的穩(wěn)定時間,必須的。
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個字節(jié)的時鐘信號:分秒時日月周年
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //打開寫保護(hù)功能
}
/*******************************************************************************
* 函 數(shù) 名 : Ds1302ReadTime
* 函數(shù)功能 : 讀取時鐘信息
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void Ds1302ReadTime()
{
uchar n;
for (n=0; n<7; n++)//讀取7個字節(jié)的時鐘信號:分秒時日月周年
{
TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
}
}
|
|