# Include <at89x51. h>
Unsigned char code dispcode [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40 }; // 0123456789 off
Unsigned char dispbitcode [] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f}; // select different bits for Illumination
Unsigned char dispbuf [8] = {0, 10, 0,}; // Initial Value
Unsigned char dispbitcnt;
Unsigned char second;
Unsigned char Minite;
Unsigned char hour;
Unsigned int tcnt;
Unsigned char mstcnt;
Unsigned char I, J;
Void main (void)
{
Tmod = 0x02;
Th0 = 0x06;
Tl0 = 0x06;
Tr0 = 1;
Et0 = 1; // t0 interrupt control bit
Ea = 1; // count 250 times
While (1)
{
If (p0_0 = 0)
{
For (I = 5; I> 0; I --)
For (j = 248; j> 0; j --);
If (p0_0 = 0)
{
Second ++;
If (second = 60)
{
Second = 0;
}
Dispbuf [0] = Second % 10;
Dispbuf [1] = Second/10; // display seconds
While (p0_0 = 0); // indicates that p0_0 is pressed to continue execution. Otherwise, skip
}}
If (p0_1 = 0)
{
For (I = 5; I> 0; I --)
For (j = 248; j> 0; j --);
If (p0_1 = 0)
{
Minite ++;
If (Minite = 60)
{
Minite = 0;
}
Dispbuf [3] = Minite % 10;
Dispbuf [4] = Minite/10;
While (p0_1 = 0 );
}
}
If (p0_2 = 0)
{
For (I = 5; I> 0; I --)
For (j = 248; j> 0; j --);
If (p0_2 = 0)
{
Hour ++;
If (hour = 24)
{Hour = 0 ;}
Dispbuf [6] = hour % 10;
Dispbuf [7] = hour/10;
While (p0_2 = 0 );
}}}}
Void T0 (void) interrupt 1 using 0 // interrupt entry
{
Mstcnt ++ ;,
If (mstcnt = 8) // 2 ms, refresh rate
{
Mstcnt = 0;
P1 = dispcode [dispbuf [dispbitcnt];
P3 = dispbitcode [dispbitcnt];
Dispbitcnt ++;
If (dispbitcnt = 8) // 2 ms
{
Dispbitcnt = 0;
}
}
Tcnt ++;
If (tcnt = 4000) // one second
{
Tcnt = 0;
Second ++;
If (second = 60)
{
Second = 0;
Minite ++;
If (Minite = 60)
{
Minite = 0;
Hour ++;
If (hour = 24)
{
Hour = 0;
}
}
}
Dispbuf [0] = Second % 10;
Dispbuf [1] = Second/10;
Dispbuf [3] = Minite % 10;
Dispbuf [4] = Minite/10;
Dispbuf [6] = hour % 10;
Dispbuf [7] = hour/10; // display various types of data
}
}