|
#ifndef _1302_H
#define _1302_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
#include<reg52.h>
#include<1302.h>
//---DS1302 寫入和讀取時(shí)分秒的地址命令---//
//---秒分時(shí)日月周年 最低位讀寫位;-------//
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í)鐘初始化 2020 年 7 月 29 日星期三 12 點(diǎn) 00 分 00 秒。---//
//---存儲(chǔ)順序是秒分時(shí)日月周年,存儲(chǔ)格式是用 BCD 碼---//
uchar TIME[7] = {0x00, 0x00, 0x12,0x92 ,0x07,0x03, 0x20};
/****************************************************************
***************
* 函 數(shù) 名 : Ds1302Write
* 函數(shù)功能 : 向 DS1302 命令(地址+數(shù)據(jù))
* 輸 入 : addr,dat
* 輸 出 : 無(wú)
*****************************************************************
**************/
void Ds1302Write(uchar addr,uchar dat)
{
uint n;
RST=0;
_nop_();
SCLK=0;
RST=1;
_nop_();
for(n=0;n<8;n++)
{
DSIO=addr&0x01;
addr>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
for(n=0;n<8;n++)
{
DSIO=dat&0x01;
dat>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
RST=0;
_nop_();
}
/****************************************************************
***************
* 函 數(shù) 名 : Ds1302Read
* 函數(shù)功能 : 讀取一個(gè)地址的數(shù)據(jù)
* 輸 入 : addr
* 輸 出 : dat
*****************************************************************
**************/
uchar Ds1302Read(uchar addr)
{
uchar dat,dat1,i;
RST=0;
_nop_();
SCLK=0;
_nop_();
RST=1;
_nop_();
for(i=0;i<8;i++)
{
DSIO=addr&0x01;
addr>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
for(i=0;i<8;i++)
{
dat1=DSIO;
dat=(dat>>1)|(dat1<<7);
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
RST=0; //復(fù)位穩(wěn)定程序
_nop_();
SCLK = 1;
_nop_();
DSIO = 0;
_nop_();
DSIO = 1;
_nop_();
return dat;
}
/****************************************************************
***************
* 函 數(shù) 名 : Ds1302Init
* 函數(shù)功能 : 初始化 DS1302.
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*****************************************************************
**************/
void Ds1302Init()
{ uint i;
Ds1302Write(0x80,0x00);//關(guān)閉寫保護(hù)功能
for(i=0;i<7;i++)
{
Ds1302Write(READ_RTC_ADDR[i],TIME[i]);
}
Ds1302Write(0x8e,0x80); //打開(kāi)寫保護(hù)位;
}
/****************************************************************
***************
* 函 數(shù) 名 : Ds1302ReadTime
* 函數(shù)功能 : 讀取時(shí)鐘信息
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*****************************************************************
**************/
void Ds1302ReadTime()
{
uchar n;
for (n=0; n<7; n++)//讀取 7 個(gè)字節(jié)的時(shí)鐘信號(hào):分秒時(shí)日月周年
{
TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
}
}
#include<reg52,h>
#include<1302.h>
typedef unsigned int u16; //對(duì)數(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í)函數(shù),i=1 時(shí),大約延時(shí) 10us
*****************************************************************
**************/
void delay(u16 i)
{
while(i--);
}
/****************************************************************
***************
* 函 數(shù) 名 : datapros()
* 函數(shù)功能 : 時(shí)間讀取處理轉(zhuǎn)換函數(shù)
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
***/
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ù)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*****************************************************************
**************/
void DigDisplay()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管,
{
case(0):
LSA=1;LSB=1;LSC=1; break;//顯示第 0 位
case(1):
LSA=0;LSB=1;LSC=1; break;//顯示第 1 位
case(2):
LSA=1;LSB=0;LSC=1; break;//顯示第 2 位
case(3):
LSA=0;LSB=0;LSC=1; break;//顯示第 3 位
case(4):
LSA=1;LSB=1;LSC=0; break;//顯示第 4 位
case(5):
LSA=0;LSB=1;LSC=0; break;//顯示第 5 位
case(6):
LSA=1;LSB=0;LSC=0; break;//顯示第 6 位
case(7):
LSA=0;LSB=0;LSC=0; break;//顯示第 7 位
}
P0=DisplayData[i];//發(fā)送數(shù)據(jù)
delay(100); //間隔一段時(shí)間掃描
P0=0x00;//消隱
}
}
void main()
{
//Ds1302Init();
while(1)
{
datapros(); //數(shù)據(jù)處理函數(shù)
DigDisplay();//數(shù)碼管顯示函數(shù)
}
}
|
|