C51单片机如何让四位数码管分别显示四位不同的数字?

是在位选时同时选中全部四位数码管然后赋予不同段选么?需要用什么函数实现?

每一次只能让一个数码管位选,其他的关闭,给数码管赋值。重复4次就可以了。
假设要显示“1234”:

#include<reg52.h>
sbit k1 = P1^1;
sbit k2 = P1^2;
sbit k3 = P1^3;
sbit k4 = P1^4;//k1-k4 4个位选开关,我假设你的电路是低电平有效

uchar code table[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8;
0x80,0x98};//共阳极1-9
void delay( char t )//延时函数
{
char x,y;

for( x = t;x > 0;x-- )

{
for( y = 100;y > 0;y-- );

}
}

void main()//主函数
{
int i = 1234;

int temp;

while(1)
{
temp = i;//temp = 1234;
//显示第4位
k1 = k2 = k3 = k4 = 1;//关闭所有未选
k4 = 0;//打开第4位位选
P2 = table( temp%10 );//假设P2口控制数码管
temp = temp / 10;//temp = 123

delay( 5 );

//显示第3位
k1 = k2 = k3 = k4 = 1;//关闭所有未选
k3 = 0;//打开第3位位选
P2 = table( temp%10 );//,假设P2口控制数码管
temp = temp / 10;//temp = 12
delay( 5 );

//显示第2位
k1 = k2 = k3 = k4 = 1;//关闭所有未选
k4 = 0;//打开第4位位选
P2 = table( temp%10 );//,假设P2口控制数码管
temp = temp / 10;//temp = 1;
delay( 5 );
//显示第1位
k1 = k2 = k3 = k4 = 1;//关闭所有未选
k4 = 0;//打开第1位位选
P2 = table( temp%10 );//假设P2口控制数码管
delay( 5 );

}

}
温馨提示:答案为网友推荐,仅供参考
相似回答