51单片机控制3位7段共阴数码管

2024-11-22 06:45:00
推荐回答(2个)
回答(1):

//51单片机控制3位7段共阴数码管 p0口控制 abcdefg dp ,
//p2.0-p2.2控制123位,----注意:没有用P2.3。
//要使其间隔1S分别显示123,231,321,213...........无限循环

//下列程序,经过仿真,符合要求。

#include
#define uchar unsigned char
sbit bit1 = P2^0;
sbit bit2 = P2^1;
sbit bit3 = P2^2;
uchar code dulatab[] = //数字编码0-9
{0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
//----------------------------------------------------------------
void delay(uchar time)
{
uchar i, j;
for(i = 0; i < time; i++) for(j = 0; j < 110; j++);
}
//----------------------------------------------------------------
void disp_num(uchar a , b, c)
{
uchar i;
for(i = 1; i < 250; i++) {
P2 = 255; //关显示.
P0 = dulatab[a];
bit1 = 0; delay(4);
P2 = 255; //关显示.
P0 = dulatab[b];
bit2 = 0; delay(4);
P2 = 255; //关显示.
P0 = dulatab[c];
bit3 = 0; delay(4);
}
}
//----------------------------------------------------------------
void main()
{
while(1) {
//要使其间隔1S分别显示123,231,321,213...........无限循环.
disp_num(1, 2, 3);
disp_num(2, 3, 1);
disp_num(3, 2, 1);
disp_num(2 , 1, 3);
// disp_num(? , ?, ?); 还想显示?内容,楼主可自行编写.
// disp_num(? , ?, ?);
}
}
//----------------------------------------------------------------

回答(2):

//51单片机控制3位7段共阴数码管 p0口控制 abcdefg dp ,
//p2.0-p2.3控制123位,
//要使其间隔1S分别显示123,231,321,213...........无限循环

#include
#define uchar unsigned char
sbit bit1=P2^0;
sbit bit2=P2^1;
sbit bit3=P2^2;
uchar code dulatab[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f}; //数字编码0-9

void delay(uchar time)
{
uchar i,j;
for(i=0;i {
for(j=0;j<110;j++)
;
}
}
void write_bit1(uchar num)
{
//关显示
bit1=1;

//送数据
P0=dulatab[num];

//开显示
bit1=0;
delay(4);
}
void write_bit2(uchar num)
{
//关显示
bit2=1;

//送数据
P0=dulatab[num];

//开显示
bit2=0;
delay(4);
}
void write_bit3(uchar num)
{
//关显示
bit3=1;

//送数据
P0=dulatab[num];

//开显示
bit1=0;
delay(4);
}
void main()
{
uchar i,j,k;
uchar waiti,waitj,waitk;
while(1)
{

for(i=1;i<4;i++)
{
for(j=1;j<4;j++)
{
for(k=1;k<4;k++)
{
if(i!=j&&j!=k&&i!=k)
{
waiti=i;
waitj=j;
waitk=k;

write_bit1(i);
write_bit2(j);
write_bit3(k);
}
else
{
write_bit1(waiti);
write_bit2(waitj);
write_bit3(waitk);
}
}
}
}
}
}
//顺序是这样的123 132 213 231 312 321没有完全按照你给的顺序