# Include <reg52.h> // contains the header file, which generally does not need to be changed. The header file contains the definition of special function registers.
# Define uchar unsigned char
# Define uint unsigned int
Static unsigned char count;
Code unsigned char tab [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
// Common overcast digital tube 0-9
Uchar smg [8]; // defines the buffer.
Uint minute = 30, hour = 12, second; // defined and initialized at 12:30:00
Void delay (unsigned int cnt)
{
While (-- cnt );
}
Void display ()
{
Smg [0] = tab [hour/10]; // displays the hour
Smg [1] = tab [hour % 10];
Smg [2] = 0x40; // display "-"
Smg [3] = tab [minute/10]; // display minutes
Smg [4] = tab [minute % 10];
Smg [5] = 0x40; // display "-"
Smg [6] = tab [second/10]; // display seconds
Smg [7] = tab [second % 10];
}
Void main ()
{
Uchar I;
TMOD | = 0x01; // timer 0 10 ms in 12 M crystal for timing
TH0 = 0xd8; // Initial Value
TL0 = 0xf0;
ET0 = 1;
TR0 = 1;
EA = 1;
Display ();
While (1)
{
For (I = 0; I <8; I ++) // display time
{
P0 = smg [I];
P2 = I;
Delay (100 );
}
If (count = 100)
{
Count = 0;
Second ++; display (); // second plus 1
If (second = 60)
{
Second = 0;
Minute ++; // Add 1
If (minute = 60)
{
Minute = 0;
Hour ++; // Add 1
If (hour = 24)
Hour = 0;
}
}
}
Display ();
}
}
Void timer () interrupt 1
{
TH0 = 0xd8; // assign a value again
TL0 = 0xf0;
Count ++;
}