STC89C52单片机,用数码管显示时间,请看程序:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar num,a,a1,b,b1,c,c1;
uchar shi,fen,miao;
sbit fm=P3^4;
void delay(uchar time)
{
uchar i,j;
for(i=time;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
}
void display(char a,a1,b,b1,c,c1)
{
P1=0x00;
P2=table[a];
delay(5);
P1=0x01;
P2=table[a1];
delay(5);
P1=0x02;
P2=0x40;
delay(5);
P1=0x03;
P2=table;
delay(5);
P1=0x04;
P2=table[b1];
delay(5);
P1=0x05;
P2=0x40;
delay(5);
P1=0x06;
P2=table[c];
delay(5);
P1=0x07;
P2=table[c1];
delay(5);
}
void t0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
}
void main()
{
init();
shi=15;
fen=31;
miao=00;
while(1)
{
if(num==20)
{
num=0;
if(miao==59) //问题一:为什么miao==59而不等于60
{
miao=-1; //问题二:为什么不是miao=0
if(fen==59)
{
fen=-1;
if(shi==24)
shi=-1;
shi++;
}
fen++;
if(fen==50){fm=0;delay(80);fm=1;}
}
miao++;
}
a=shi/10;
a1=shi%10;
b=fen/10;
b1=fen%10;
c=miao/10;
c1=miao%10;
display(a,a1,b,b1,c,c1);
}
}
|