51单片机程序:
#include<reg52.h>
#define UCHAR unsigned char
#define UINT unsigned int
//sbit KEY=P3^2;
UCHAR table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
UINT conter;
UCHAR timer;
void inittime(void)
{
timer=0;
conter=0;
RCAP2L=0x00;
RCAP2H=0x4c;
TH0=0X4c;
TL0=0X00;
EA=1;
ET2=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 = 4; //第7位数码管显示
P0 = table[(conter / 10000)%10]; //在这里取 i 的十位数
Delay_1ms(5); //延时0.5秒后显示下一个数
P0 = 0; //消隐
P2 = 5; //第8位数码管显示
P0 = table[conter /100% 10]; //在这里取 i 的个位数
Delay_1ms(5);
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()
{
inittime();
while(1)
segdisplay();
}
void timer_t2(void)interrupt 5
{
TF2=0;
timer++;
if(timer==20)
{
timer=0;
conter++;
}
if(conter==10000)
conter=0;
}