#include<reg52.h>
#define UCHAR unsigned char
#define UINT unsigned int
UCHAR table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
UCHAR timer;
UCHAR conter;
void initserial(void)
{
timer='0';
conter=0;
SCON=0X50;
RCLK=1;
TCLK=1;
RCAP2L=0xdc;
RCAP2H=0xff;
EA=1;
ES=1;
TR2=1;
}
void Delay_1ms(UCHAR i)//1ms延时
{
UCHAR x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void segdisplay(void)
{
P0 = 0; //消隐
P2 = 6; //第7位数码管显示
P0 = table[(conter / 10)%10]; //在这里取 i 的十位数
Delay_1ms(5); //延时0.5秒后显示下一个数
P0 = 0; //消隐
P2 = 7; //第8位数码管显示
P0 = table[conter % 10]; //在这里取 i 的个位数
Delay_1ms(5);
P0=0;
}
void main()
{
initserial();
while(1)
{
conter=timer-'0';
segdisplay();
}
}
void serial_t2(void)interrupt 4
{
if(RI)
{
RI=0;
timer=SBUF;
SBUF=timer;
}
if(TI)
{
TI=0;
}