基于DS12C887的日歷時(shí)鐘顯示系統(tǒng)設(shè)計(jì)
主要器件:
1、AT89C52單片機(jī)芯片,用于對(duì)時(shí)鐘芯片的控制和初始化,并控制數(shù)碼管顯示。
2、日歷時(shí)鐘芯片DS12C887。
試驗(yàn)流程圖;
試驗(yàn)電路圖:
試驗(yàn)程序代碼:
//CalendarClk.h程序
#ifndef_CALENDARCLK_H//防止CalendarClk.h被重復(fù)引用
#define_CALENDARCLK_H
#include
#include
#define uchar unsigned char
#define uint unsigned int
/* DS12C887內(nèi)部專(zhuān)用寄存器宏定義*/
#define MIN XBYTE[0x0102]
#define HOUR XBYTE[0x0104]
#define DAYOFWEEK XBYTE[0x0106]
#define DAYOFMONTH XBYTE[0x0107]
#define MONTH XBYTE[0x0108]
#define YEAR XBYTE[0x0109]
#define REG_A XBYTE[0x010a]
#define REG_B XBYTE[0x010b]
/*由串口獲得的日歷時(shí)鐘信息變量,用于對(duì)芯片時(shí)間的設(shè)置*/
uchar year1,month1,dayofweek1,dayofmonth1,hour1,min1;
/*芯片DS12C887提供的日歷時(shí)鐘信息變量*/
uchar year2,month2,dayofweek2,dayofmonth2,hour2,min2;
#endif
//CalendarClk.c程序
#include "CalendarClk.h"
/*從串行口獲取數(shù)據(jù)函數(shù),數(shù)據(jù)包括:year1,month1,dayofweek1,
dayofmonth1,hour1,min1。如果獲取到正確數(shù)據(jù)則返回1,否則返回0 */
uchar getdata()
{
//略去
}
/*設(shè)置日歷和時(shí)鐘函數(shù)*/
void settime()
{
REG_B = REG_B|0x80;// SET=1,芯片DS12C887處于設(shè)置狀態(tài)
MIN = min1;
HOUR = hour1;
DAYOFWEEK = dayofweek1;
DAYOFMONTH = dayofmonth1;
MONTH = month1;
YEAR = year1;
REG_B = REG_B&0x7f;// SET=0,芯片DS12C887恢復(fù)正常數(shù)據(jù)更新?tīng)顟B(tài)
}
/*獲取日歷時(shí)鐘函數(shù)*/
void gettime()
{
while (REG_A&0x80==0x00)//直到UIP=0時(shí),才能讀取日歷時(shí)鐘信息{
{
min2 = MIN ;
hour2 = HOUR;
dayofweek2 = DAYOFWEEK;
dayofmonth2 = DAYOFMONTH;
month2 = MONTH;
year2 = YEAR;
}
}
/* 13位數(shù)碼管顯示年、月、日、星期、時(shí)、分*/
void display()
{
//略去
}
/*串口初始化函數(shù)*/
void init_serial()
{
TMOD = 0x20;//定時(shí)器T1使用工作方式2
TH1 = 250;
TL1 = 250;
TR1 = 1;//開(kāi)始計(jì)時(shí)
PCON = 0x80;// SMOD = 1
SCON = 0x50;//工作方式1,波特率9600kbit/s,允許接收
}
void main(void)
{
uchar setflag;
init_serial();
/*設(shè)置DV2、DV1、DV0為010,打開(kāi)芯片DS12C877內(nèi)部晶振*/
REG_A = REG_A&0xaf;// DV2=DV0=0
REG_A = REG_A|0x20;// DV1=0
REG_B = REG_B&0x7b;// SET=0,時(shí)間數(shù)據(jù)正常更新;DM=0,二進(jìn)制數(shù)據(jù)模式
REG_B = REG_B|0x02;//寄存器B的24/12位置1,24小時(shí)時(shí)間模式
while (1)
{
setflag = getdata();//通過(guò)查詢(xún)方式獲取串口數(shù)據(jù)
/*如果獲取到正確數(shù)據(jù),則設(shè)置時(shí)間*/
if (setflag==0x01)
settime();
/*調(diào)用獲取日歷時(shí)鐘函數(shù)*/
gettime();
/* 13位數(shù)碼管顯示日歷、星期和時(shí)間信息*/
display();
}
}
評(píng)論