51单片机实现秒表计时完整代码
#include <at89x52.h>
#include <intrins.h>
unsigned int count;
unsigned char cntr = 0;
unsigned char tab[]= {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x76};
unsigned char sj[2];
void bcdconvers()
{
sj[0] = count / 10;
sj[1] = count % 10;
}
void delay()
{
unsigned int y ;
for (y = 0; y < 10000; y++);
}
void display()
{
unsigned char i, wei;
wei = 0xfe;
for(i = 0; i < 2; i++)
{
P2 = 0xff;
P0 = tab[sj[i]];
P2 = wei;
delay();
wei = (wei << 1) + 1;
}
}
void main()
{
TMOD = 0x01;
TH0 = -50000 >> 8;
TL0 = -50000;
EA = 1;
IT0=1;
IT1=1;
ET0 = 1;
TR0 = 1;
EX0=1;
while (1)
{
bcdconvers();
display();
}
}
void T1PRG() interrupt 1
{
TH0 = -50000 >> 8;
TL0 = -50000;
if (cntr == 20)
{
count++;
cntr = 0;
}
else
{
cntr++;
}
if (count >= 60)
{
count = 0;
}
}
void key_0() interrupt 0
{
count = count + 10;
if (count >= 60)
{
count = count - 60;
}
}
void key_1() interrupt 2
{
count++;
if (count >= 60)
{
count = count - 60;
}
} |