51数码管显示整数0-9999

51数码管显示整数0-9999_第1张图片
#include
sbit du=P2^6;
sbit we=P2^7;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(unsigned char a)
{unsigned char x,y;
for(x=0;x for(y=0;y<114;y++);
}
void display(unsigned int a)
{ P0=0xff; //清除乱码
we=1;
P0=0xfe; //第1位数码管显示
we=0;
P0=0; //擦除第1位遗留的值0xfe
du=1;
if(a>=1000)
P0=table[a/1000];
else
{P0=0;//第1位数码管不显示
}
du=0;
delay(10);

P0=0xff;
we=1;
P0=0xfd;
we=0;
P0=0;
du=1;
if(a>=100)
P0=table[a%1000/100];
else 
P0=0;
du=0;
delay(10);

P0=0xff;
we=1;
P0=0xfb;
we=0;
P0=0;
du=1;
if(a>=10)
P0=table[a%100/10];
else
P0=0;
du=0;
delay(10);

P0=0xff;
we=1;
P0=0xf7;
we=0;
P0=0;
du=1;
P0=table[a%10];
du=0;
delay(10);

}

void main()
{ while(1)
{display(1206);
}

}

你可能感兴趣的:(51单片机)