我只能显示最右边一位显示0~9。 P2^4~P2^7位选,P0段选,共阳4位数码管
程序如下:
#include<at89x52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar dat[]={0,0,0,0};
uchar ge=0,shi=0,bai=0,qian=0;
void delay1s(void)
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
}
void main(void)
{
uchar i;
while(1)
{
for(ge=0;ge<10;ge++)
{
P2=0xef;
P0=tab[i];
delay1s();
if(ge>=9)
{
ge=0;
shi++;
P0=tab[i];
delay1s();
if(shi>=9)
{
shi=0;
bai++;
delay1s();
if(bai>=9)
{
bai=0;
qian++;
delay1s();
if(qian>=9)
{
qian=0;
}
}
}
}
}
dat[0]=ge%10;
dat[1]=shi%10/10;
dat[2]=bai/100;
dat[3]=qian/1000;
}
} |