设计要求
调用DS1302芯片驱动程序,读取DS1302中的实时时分秒数据,并显示在数码管上。

23时59分59秒
通过串口发送时间作为定时时间,定时时间到则蜂鸣器响2s后静音。
串口发送格式:"12:35:66"。
备注:
单片机IRC振荡器频率设置为12MHz。
键盘模式:BTN;扩展模式:IO模式
串口通信波特率:9600bps。
上电数码管显示时间:23时59分59秒,蜂鸣器静音。
代码
sys.h
cpp
#ifndef __SYS_H__
#define __SYS_H__
#include <STC12C5A60S2.H>
extern unsigned char time[3];
extern unsigned char Seg_Buff[8];
//sys.c
void init74hc138();
void init();
void Seg_Loop();
void seg_time();
void buzz(bit flag);
//ds1302.c
void w_ds1302();
void r_ds1302();
#endif
main.c
cpp
#include "sys.h"
unsigned char str[8]={1,2,':',3,5,':',6,6};
bit flag_seg=0;
bit flag_time=0;
bit flag_buzz=0;
void Timer0_Init(void) //100微秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x50; //设置定时初始值
TH0 = 0xFB; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //使能定时器0中断
EA = 1;
}
void UartInit(void) //[email protected]
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xC7; //设置定时初始值
TH1 = 0xFE; //设置定时初始值
ET1 = 0; //禁止定时器中断
TR1 = 1; //定时器1开始计时
ES = 1;
EA = 1;
}
void time_clock(){
if(Seg_Buff[0]==str[0]&&Seg_Buff[1]==str[1]&&
Seg_Buff[3]==str[3]&&Seg_Buff[4]==str[4]&&
Seg_Buff[6]==str[6]&&Seg_Buff[7]==str[7])
{
buzz(1);
flag_buzz=1;
}
}
void main(){
init();
Timer0_Init();
UartInit();
w_ds1302();
while(1){
if(flag_seg){
flag_seg=0;
Seg_Loop();
}
if(flag_time){
flag_time=0;
r_ds1302();
seg_time();
time_clock();
}
}
}
void uart() interrupt 4
{
static unsigned char i=0;
if(RI){
RI=0;
str[i++]=SBUF-'0';
if(i==8)i=0;
}
}
void Timer0_Isr(void) interrupt 1
{
static unsigned char count1=0;
static unsigned int count2=0;
static unsigned int count_buzz=0;
count1++;count2++;
if(count1==2){
count1=0;
flag_seg=1;
}
if(count2==5000){
count2=0;
flag_time=1;
}
if(flag_buzz){
count_buzz++;
if(count_buzz==20000){
count_buzz=0;
flag_buzz=0;
buzz(0);
}
}
}
sys.c
cpp
#include "sys.h"
code unsigned char Seg_Table[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0xbf, //- 10
0xff //熄灭 11
};
unsigned char Seg_Buff[8]={11,11,11,11,11,11,11,11};
void init74hc138(unsigned char n){
P2=(P2&0x1f)|(n<<5);
P2&=0x1f;
}
void init(){
P0=0x00;
init74hc138(5);
P0=0xff;
init74hc138(4);
}
void seg(unsigned char addr,num){
P0=0xff;
init74hc138(7);
P0=0x01<<addr;
init74hc138(6);
P0=Seg_Table[num];
init74hc138(7);
}
void Seg_Loop(){
static unsigned char i=0;
seg(i,Seg_Buff[i]);
i++;
if(i==8)i=0;
}
void seg_time(){
Seg_Buff[7]=time[0]%10;
Seg_Buff[6]=time[0]/10;
Seg_Buff[5]=10;
Seg_Buff[4]=time[1]%10;
Seg_Buff[3]=time[1]/10;
Seg_Buff[2]=10;
Seg_Buff[1]=time[2]%10;
Seg_Buff[0]=time[2]/10;
}
void buzz(bit flag){
if(flag)
P0=0x40;
else
P0=0x00;
init74hc138(5);
}
ds1302.c
cpp
#include "sys.h"
#include "intrins.h"
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST=P1^3;
unsigned char time[3]={59,59,23};
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA = temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat/10*16|dat%10);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
unsigned char dat1,dat2;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
dat1=temp/16;
dat2=temp%16;
temp=dat1*10+dat2;
return (temp);
}
void w_ds1302(){
unsigned char i,addr=0x80;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<3;i++){
Write_Ds1302_Byte(addr,time[i]);
addr+=2;
}
Write_Ds1302_Byte(0x8e,0x80);
}
void r_ds1302(){
unsigned char i,addr=0x81;
for(i=0;i<3;i++){
time[i]=Read_Ds1302_Byte(addr);
addr+=2;
}
}