#define uint unsigned int
#define uchar unsigned char
//------共阳极数码管段码---------
uchar code table[]={0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90};
uchar dat[]={0,0,0xbf,0,0,0xbf,0,0};
uchar h,m,s,ms;
uchar scan_which;
uchar index;
uchar key_state;
//----------延时----------
void delay(uint t)
{
uint i;
while(t--)
for(i=0;i<120;i++);
}
void hour()
{
if(++h>23)
h=0;
dat[0]=table[h/10];
dat[1]=table[h%10];
}
void min()
{
if(++m>59)
{
m=0;
hour();
}
dat[3]=table[m/10];
dat[4]=table[m%10];
}
void second()
{
if(++s>59)
{
s=0;
min();
}
dat[6]=table[s/10];
dat[7]=table[s%10];
}
void main()
{
IE=0x8a;
TMOD=0x11;
TCON=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
h=17;
m=50;
s=ms=0;
dat[0]=table[h/10];
dat[1]=table[h%10];
dat[3]=table[m/10];
dat[4]=table[m%10];
dat[6]=table[s/10];
dat[7]=table[s%10];
scan_which=0xfe;
index=0;
TR0=1;
TR1=1;
key_state=0xfe;
while(1)
{
if(P1^key_state)
{
delay(10);
if(P1^key_state)
{
key_state=P1;
EA=0;
if((key_state & 0x01)==0)
hour();
else if((key_state & 0x02)==0)
{
m=(m+1)%60;
dat[3]=table[m/10];
dat[4]=table[m%10];
}
EA=1;
}
}
}
}
void T0_int() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
P3=scan_which;
P0=~dat[index];
scan_which=_crol_(scan_which,1);
index=(index+1)%8;
}
void T1_int() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
if(++ms==20)
{
ms=0;
second();
}
}