动态显示。就是先单独显示完一位后,然后关闭这一位(也就是熄灭这一位的led),然后再单独显示下一位。由于人眼反应慢,随意看起来就像4位数码管都在同时显示
a[]={0,9,8,7};
sencode[10]={共阳极七段码};
char One_second=0; //通过设置一秒钟定时器中断来让 One_second0
void display() //P2口输出七段码,P1.0,P1.1,P1.2,P1.3,代表由高到低位驱动信号,低电平有效;
{ char k,m,w;
w=0xfe
for(k=0;k4;k++){ m=a[k]; P2=sencode[m]; P1=w; w=w1; w++;delay(); }
}
void main()
{ char k,m;
while(1)
{ if (One_second) //通过设置一秒钟定时器中断来让 One_second0
{ One_second=0; for(k=0;k4;k++){ m=a[k]; m++; a[k]=m; } }
display();
}
}
程序如下(用的是STC89C52芯片):
#includereg52.h//52系列单片机头文件
#define uchar unsigned char
#define uint unsigned int
uint x,y;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};//共阴极数码管编码
void display(uchar,uchar,uchar,uchar);//声明子函数
void delay(int);//声明子函数
void main()
{
while(1)
{
display(1,2,3,4);//主程序始终调用数码管显示子程序
}
}
void display(uchar a,uchar b,uchar c,uchar d)
{
P2=0xef;\t
P0=table[a];//给第一个数码管送"a"
delay(1);//延时1ms
P2=0xdf;
P0=table[b];//给第二个数码管送"b"
delay(1);//延时1ms
P2=0xbf;
P0=table[c];//给第三个数码管送"c"
delay(1);//延时1ms
P2=0x7f;
P0=table[d];//给第三个数码管送"d"
delay(1);//延时1ms
}
void delay(uint z)//延时子函数
{
uint x,y;
for(x=z;x0;x--)
for(y=110;y0;y--);
}
扩展资料
led数码管是由多个发光二极管封装在一起组成“8”字型的器件,引线已在内部连接完成,只需引出它们的各个笔划,公共电极。led数码管常用段数一般为7段有的另加一个小数点,还有一种是类似于3位“+1”型。
位数有半位,1,2,3,4,5,6,8,10位等等,led数码管根据LED的接法不同分为共阴和共阳两类,了解LED的这些特性,对编程是很重要的,因为不同类型的数码管,除了它们的硬件电路有差异外,编程方法也是不同的。
参考资料来源:百度百科-数码管
每一次只能让一个数码管位选,其他的关闭,给数码管赋值。重复4次就可以了。
假设要显示“1234”:
#includereg52.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 );
}
}
单片机c51数码管显示四位的介绍就聊到这里吧,感谢您花时间阅读,谢谢。
本文标签:单片机c51数码管显示四位