基于89C52的串口调时时钟程序
时间:10-02
整理:3721RD
点击:
先总结之前出错原因:在串口通讯的时候有更高优先级的定时器0中断产生导致出错。加了一句PS=1;将串口通信中断服务程序优先级调高,终于稳定了。现分享给大家。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[]="12-05-14";
uchar xingqi[7][3]={"MON","TUE","WED","TUR","FRI","SAT","SUN"};
uchar table1[]=" 13:34:56 ";
uchar receve[13];
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit rw=P3^6;
uchar num;
uchar i;
uchar sec,min,hour,day,xq,month;
uchar aa=0;
uchar flag,flag1;
void datachange();
void send_data();
void recepros();
void write_data(uchar date);
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_sfm(uchar add,uchar sfm)
{
write_com(add);
write_data(sfm/10+0x30);
write_data(sfm%10+0x30);
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
rw=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
sec=50;
min=59;
hour=23;
day=29;
month=5;
xq=0;
TMOD=0X21;
TH1=0xFD;
TL1=0xFD;
SCON=0X50;
ES=1;
TR1=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
PS=1;
}
void main()
{
// uchar sentnum;
init();
while(1)
{
datachange();
send_data();
write_com(0x80);
for(num=0;num<8;num++)
{
write_data(table[num]);
delay(5);
//delay(50);
}
write_com(0x80+0x0d);
for(num=0;num<3;num++)
{
write_data(xingqi[xq][num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_data(table1[num]);
delay(5);
}
if(flag==1)
{
flag=0;
/*for(sentnum=0;sentnum<13;sentnum++)
{
SBUF=receve[sentnum];
while(!TI);
TI=0;
}*/
recepros();
}
if(flag1)
{
for(i=0;i<13;i++)
{
receve[aa]=0;
}
flag1=0;
}
}
}
void timer0(void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
i++;
if(i==20)
{
i=0;
sec++;
}
}
void datachange()
{
if(sec==60)
{
sec=0;
min++;
}
if(min==60)
{
min=0;
hour++;
}
if(hour==24)
{
hour=0;
xq++;
day++;
}
if(xq==6)
{
xq=0;
}
if(day==30)
{
day=0;
month++;
}
}
void send_data()
{
table1[11]=(sec%10)+0x30;
table1[10]=(sec/10)+0x30;
table1[8]=(min%10)+0x30;
table1[7]=(min/10)+0x30;
table1[5]=(hour%10)+0x30;
table1[4]=(hour/10)+0x30;
table[7]=(day%10)+0x30;
table[6]=(day/10)+0x30;
table[4]=(month%10)+0x30;
table[3]=(month/10)+0x30;
}
void recepros()
{
sec=receve[11]*10+receve[12];
write_sfm(0x80+0x40+0x0a,sec);
min=receve[9]*10+receve[10];
write_sfm(0x80+0x40+0x07,min);
hour=receve[8]+receve[7]*10;
write_sfm(0x80+0x40+0x04,hour);
xq=receve[6]-1;
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_data(table1[num]);
delay(5);
}
day=receve[5]+receve[4]*10;
write_sfm(0x80+0x06,day);
month=receve[3]+receve[2]*10;
write_sfm(0x80+0x03,month);
/*table1[10]=receve[11];
table[11]=receve[12];
table1[7]=receve[9];
table1[8]=receve[10];
table1[4]=receve[7];
table1[5]=receve[8];
xq=receve[6]-1;
table[6]=receve[4];
table[7]=receve[5];
table[3]=receve[2];
table[4]=receve[3];*/
send_data();
}
void rxd(void) interrupt 4
{
uchar abc=0;
RI=0;
abc=SBUF;
receve[aa]=abc-0x30;
aa++;
if(aa==13)
{
aa=0;
flag=1;
flag1=1;
}
}
支持分享