原理图:
c51单片机程序如下:
#include<reg51.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 conter;
UCHAR timer;
void inittime(void)
{
timer=0;
conter=0;
TMOD=0X01;
TH0=0X4C;
TL0=0X00;
EA=1;
ET0=1;
TR0=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()
{
inittime();
while(1)
segdisplay();
}
void timer_t0(void) interrupt 1
{
TH0=0X4C;
TL0=0X00;
timer++;
if(timer==20)
{
timer=0;
conter++;
}
if(conter==100)
conter=0;
}