Digital Display 00 ~ 59. Press key 1 to add one to each display value, press Key 2 to subtract one from each display value, press key 3 to return to zero, and press Key 4 to start or stop the timer 0.
# Include <reg52.h>
# Define uchar unsigned char
# Define uint unsigned int
Sbit key1 = P3 ^ 4;
Sbit key2 = P3 ^ 5;
Sbit key3 = P3 ^ 6;
Sbit key4 = P3 ^ 7;
Sbit Dula = P2 ^ 6;
Sbit wela = P2 ^ 7;
Uchar code table [] = {
0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79,0x71
};
Void delayms (uint );
Uchar numt0, num;
Void display (uchar numdis)
{
Uchar Shi, GE;
Shi = numdis/10;
GE = numdis % 10;
Dula = 1;
P0 = table [Shi];
Dula = 0;
P0 = 0xff;
Wela = 1;
P0 = 0xfe;
Wela = 0;
Delayms (5 );
Dula = 1;
P0 = table [Ge];
Dula = 0;
P0 = 0xff;
Wela = 1;
P0 = 0xfd;
Wela = 0;
Delayms (5 );
}
Void delayms (uint XMS)
{
Uint I, J;
For (I = XMS; I> 0; I --)
For (j = 110; j> 0; j --);
}
Void Init ()
{
Tmod = 0x01;
Th0 = (65535-45872)/256;
Tl0 = (65536-45872) % 256;
Ea = 1;
Et0 = 1;
}
Void keyscan ()
{
If (key1 = 0)
{
Delayms (10 );
If (key1 = 0)
{
Num ++;
If (num = 60)
Num = 0;
While (! Key1 );
}
}
If (key2 = 0)
{
Delayms (10 );
If (key2 = 0)
{
If (num = 0)
Num = 60;
Num --;
While (! Key2 );
}
}
If (key3 = 0)
{
Delayms (10 );
If (key3 = 0)
{
Num = 0;
While (! Key3 );
}
}
If (key4 = 0)
{
Delayms (10 );
If (key4 = 0)
{
While (! Key4 );
Tr0 = ~ Tr0;
}
}
}
Void main ()
{
Init ();
While (1)
{
Keyscan ();
Display (Num );
}
}
Void t0_timer () interrupt 1
{
Th0 = (65536-45872)/256;
// Reinstall the Initial Value
Tl0 = (65536-45872) % 256;
Numt0 ++;
If (numt0 = 20)
{
Numt0 = 0;
Num ++;
If (num = 60)
Num = 0;
}
}