資源簡介
用51單片機連接DS1302時鐘芯片,即可在串口輸出對應的時間,看到時間一直在走,有年與日時分秒星期和農歷,其中星期和農歷程序自動計算,下載解壓即可用keil打開,編譯0錯誤0警告,代碼全部上備注,方便查閱,方便移植,是使用DS1302的友友們必選好文!

代碼片段和文件信息
#include?“STC15W204S.h“
#include? ?
#define?uint?unsigned?int
#define?u32?unsigned?long?int
#define?uchar?unsigned?char
?
void?Send_Byte_UART1(uchar?dat) ;
#include?“DS1302.h“
uint?aa;
?
struct?sTime?Time;??//日期時間緩沖區
void?Init_MCU()
{
EA??=?1; //開總中斷?
// TMOD?|=?0x22; //設定定時器1為16位自動重裝方式
// TL1?=?0xfd; //設定定時初值
// TH1?=?0xfd; //設定定時初值
//
// ET1?=?0; //禁止定時器1中斷?==1允許
// TR1?=?1; //啟動定時器1
//
// REN=1;
// SM0=0;
// SM1=1;
SCON?=?0x50; //8位數據可變波特率
AUXR?|=?0x04; //定時器2時鐘為Fosc即1T
T2L?=?0xE0; //設定定時初值
T2H?=?0xFE; //設定定時初值
AUXR?|=?0x01; //串口1選擇定時器2為波特率發生器
AUXR?|=?0x10; //啟動定時器2
// TMOD|=0x01; //定時器0為模式1
// TH0=(65536-10000)/256; //裝高八位
// TL0=(65536-10000)%256; //裝低八位
// EA=1; //開總中斷
// ET0=1; //開定時器中斷
// TR0=1; //啟動定時器
// //100微秒@11.0592MHz
// AUXR?|=?0x80; //定時器時鐘1T模式
// TMOD?&=?0xF0; //設置定時器模式
// TL0?=?0xAE; //設置定時初值
// TH0?=?0xFB; //設置定時初值
// TF0?=?0; //清除TF0標志
// TR0?=?1; //定時器0開始計時
//5毫秒@11.0592MHz
AUXR?|=?0x80; //定時器時鐘1T模式
TMOD?&=?0xF0; //設置定時器模式
TL0?=?0x00; //設置定時初值
TH0?=?0x28; //設置定時初值
TF0?=?0; //清除TF0標志
TR0?=?1; //定時器0開始計時
ET0?=?1;
P3M0=0;
P3M1=0;
}
/*串口發送數據*/
void?Send_Byte_UART1(uchar?dat)
{
ES=0;
SBUF=dat;
while(!TI);
TI=0;
// ES?=1;
}
void?main()
{
uchar?sec;
struct?sTime?buftime;
Init_MCU();
Init_DS1302?();
buftime.year=95;?
buftime.mon=1; ?
buftime.day=6; ??
buftime.hour=23;
buftime.min=59; ??
buftime.sec=55;
Set_Time(&buftime);
while(1)
{ ??? ???
Get_Time(?&Time?);
if(Time.sec!=sec)
{
sec=Time.sec;
Send_Byte_UART1(Time.year/10+0x30);?
Send_Byte_UART1(Time.year%10+0x30);
Send_Byte_UART1(‘?‘);
Send_Byte_UART1(Time.mon/10+0x30);?
Send_Byte_UART1(Time.mon%10+0x30); ???
Send_Byte_UART1(‘?‘);
?
Send_Byte_UART1(Time.day/10+0x30);?
Send_Byte_UART1(Time.day%10+0x30); ???
Send_Byte_UART1(‘?‘);?
Send_Byte_UART1(Time.hour/10+0x30);?
Send_Byte_UART1(Time.hour%10+0x30); ???
Send_Byte_UART1(‘?‘);
Send_Byte_UART1(Time.min/10+0x30);?
Send_Byte_UART1(Time.min%10+0x30); ???
Send_Byte_UART1(‘?‘);?
Send_Byte_UART1(Time.sec/10+0x30);?
Send_Byte_UART1(Time.sec%10+0x30); ???
Send_Byte_UART1(‘?‘);
Send_Byte_UART1(Time.week/10+0x30);?
Send_Byte_UART1(Time.week%10+0x30); ???
Send_Byte_UART1(‘?‘); ??
Send_Byte_UART1(Time.mon_CTC/10+0x30);?
Send_Byte_UART1(Time.mon_CTC%10+0x30); ???
Send_Byte_UART1(‘?‘);
Send_Byte_UART1(Time.day_CTC/10+0x30);?
Send_Byte_UART1(Time.day_CTC%10+0x30);
?
Send_Byte_UART1(‘\n‘);
}
}
}
/*定時器0中斷服務程序*/
void?Timer_0()?interrupt?1
{??
if((aa++)>=10000){aa=0;} //定時器記數
}
?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\
?????文件???????17187??2015-09-26?15:51??基于STC15W408AS的串口DS1302時鐘\DS1302.h
?????文件???????12805??2015-07-09?20:47??基于STC15W408AS的串口DS1302時鐘\STC15W204S.h
?????文件???????26684??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx
?????文件????????1003??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.build_log.htm
?????文件????????3086??2015-09-26?15:48??基于STC15W408AS的串口DS1302時鐘\xxxx.c
?????文件????????8375??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.hex
?????文件??????????40??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.lnp
?????文件????????8162??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.LST
?????文件???????26279??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.M51
?????文件???????30825??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.OBJ
?????文件?????????190??2015-11-19?11:29??基于STC15W408AS的串口DS1302時鐘\xxxx.plg
?????文件???????88506??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.uvgui.Administrator
?????文件???????81845??2015-09-26?15:26??基于STC15W408AS的串口DS1302時鐘\xxxx.uvgui_Administrator.bak
?????文件????????6046??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.uvopt
?????文件???????14198??2019-01-22?21:49??基于STC15W408AS的串口DS1302時鐘\xxxx.uvproj
?????文件????????6066??2015-09-26?15:26??基于STC15W408AS的串口DS1302時鐘\xxxx_uvopt.bak
?????文件???????????0??2015-09-25?14:43??基于STC15W408AS的串口DS1302時鐘\xxxx_uvproj.bak
- 上一篇:混沌粒子群優化算法代碼
- 下一篇:高壓直流輸電
評論
共有 條評論