程序:
#include<reg52.h>
#define UCHAR unsigned char
#define UINT unsigned int
sfr WDT_CONTR = 0xe1;
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;
TMOD=0X20;
SCON=0X50;
TL1=0xFD;
TH1=0xFD;
EA=1;
ES=1;
TR1=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]; //在这里取 i 的十位数
Delay_1ms(5); //延时0.5秒后显示下一个数
P0 = 0; //消隐
P2 = 7; //第8位数码管显示
P0 = table[conter % 10]; //在这里取 i 的个位数
Delay_1ms(5);
P0=0;
}
void main()
{
initserial();
WDT_CONTR=0x3c;
while(1)
{
conter=timer-'0';
segdisplay();
WDT_CONTR=0x3c;
}
}
void serial_t2(void)interrupt 4
{
if(RI)
{
RI=0;
timer=SBUF;
SBUF=timer;
}
if(TI)
{
TI=0;
}
}