#include<AT89X51.h>
#define uchar unsigned char
uchar code table[]={0xc0,0xf9,0xc4,0xb0,0x99
,0x92,0x82,0xf8,0x80,0x90}; //共阳数码管
uchar time,num,hour,min,sec;
void delay(uchar x)
{
TMOD=0x01;
TR0=1;
while(x--)
{
TH0=0x1c;
TL0=0x18;
while(!TF);
TF0=0;
time++;
}
TR0=0;
}
void write(uchar num,chose)
{
uchar i;
for(i=0;i<2;i++)
{
P0=0xff;
P2=~chose;//位选
if(i==0)
{
P0=table[num%10];//段选
}
else
P0=table[num/10];//段选
chose=chose<<1;
delay(5);
}
}
void display()
{
P0=0xff;
P2=0xdb;
P0=0xbf;
delay(5);
}//-隔开时,分,秒显示
void time_add()
{
if(time==1000)
{
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
}
void main()
{
hour=23;
min=37;
sec=50;
while(1)
{
write(sec,0x01);
write(min,0x08);
write(hour,0x40);
display();
time_add();
}
} |