4位共阴极数码管单片机驱动
的有关信息介绍如下:宏晶12系列单片机单片机驱动4位数码管计数显示。
1、共阴极数码管引脚分析迅倘
共阴极数码管 是指每一位数码管的阴极,连接到电源的负极上。
也就是图中所指1,2,3,4公共端。
如果不知道是共阴极和择脂祝共阳极可以用万用表,或者直接找一个电源试一试。
电源测试的话最好串一个限流电阻,否则容易烧坏数排蚂码管。
2、电路原理图
P0:控制显示段
P2:控制显示相应的位
由于单片机的I/O驱动电流有限所以P0口需要加上拉电阻。
P2口直接连接数码管的选择位即可。
1、编写的方式:
(1)、驱动方式P2口相应的位置0 即选择要显示的数码管位
P0口相应的位置1即可显示相应的数码管段
(2)、 采用动态扫描的方式进行显示4位即每隔一段时间显示一位,循环扫 描,由于辉光效应,肉眼观察到的是4位同时显示。
下面编写相应的函数。
2、数码管显示函数
void Dig_OutPut(int i)
{
unsigned char a , b ,c ,d;
a = i%10; //提取个位
b = (i/10)%10; //提取十位
c = (i/100)%10; //提取百位
d = (i/1000)%10; //提取千位
if(i<=9)
{
P2 = 0x07;
P0 = DIG4_TABLE[a];
Delay1ms();
Delay1ms();
Delay1ms();
}
if((i<=99)&&(i>9))
{
P2 = 0x07;
P0 = DIG4_TABLE[a];
Delay1ms();
Delay1ms();
P2 = 0x0b;
P0 = DIG4_TABLE[b];
Delay1ms();
Delay1ms();
}
if((i<=999)&&(i>99))
{
P2 = 0x07;
P0 = DIG4_TABLE[a];
Delay1ms();
Delay334us();
P2 = 0x0b;
P0 = DIG4_TABLE[b];
Delay1ms();
Delay334us();
P2 = 0x0d;
P0 = DIG4_TABLE[c];
Delay1ms();
Delay334us();
}
if((i<=9999)&&(i>999))
{
P2 = 0x07;
P0 = DIG4_TABLE[a];
Delay1ms();
P2 = 0x0b;
P0 = DIG4_TABLE[b];
Delay1ms();
P2 = 0x0d;
P0 = DIG4_TABLE[c];
Delay1ms();
P2 = 0x0e;
P0 = DIG4_TABLE[d];
Delay1ms();
}
}
函数分析:
(1)、首先定义了4无符号类型的char数据,用于保存要显示数据的相应位。
(2)、判断数据的大小来判断需要点亮的数码管位数。
(3)、观察每个每个if判断语句里的延时都是4ms,这样做的原因是 保证再循环显示的过程中,每一位的数码管亮度一样。
注意事项:延时的选择不宜过大也不宜过小,过小循环的太快,数据显示不正确;过大显示闪频,看不到4位同时显示的效果
3、主函数
void main()
{
int i,j;
P0M0 = 0xff;
P0M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
while(1)
{
i++;
j = 99;
while(j--)
{
Dig_OutPut(i);
}
}
函数分析:
(1)、定义两个整数型变量,i作为要显示的数,j延时技术。
(2)、STC12系列单片机的使用IO口的时候需要进行设置io的工作模式,
上面设置位传统51工作模式,即若上拉。
(3)、while(1)重复执行这个函数里的内容,也就是动态显示。
(4)、while(j--)的作用是间隔性的对i增加,如果直接执行的话,数字跑的太快我们根本就看不清。
现在程序到单片机后:数码管会从0开始计数,间隔略小于1s。最大值是9999
过了9999后就会显示乱码,可以添加函数对于大于9999的数进行处理。
源代码
#include
unsigned char DIG4_TABLE[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71};
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void Delay334us() //@11.0592MHz
{
unsigned char i, j;
i = 4;
j = 148;
do
{
while (--j);
} while (--i);
}
void Dig_OutPut(int i)
{
unsigned char a , b ,c ,d;
a = i%10;
b = (i/10)%10;
c = (i/100)%10;
d = (i/1000)%10;
if(i<=9)
{
P2 = 0x07;
P0 = DIG4_TABLE[a];
Delay1ms();
Delay1ms();
Delay1ms();
}
if((i<=99)&&(i>9))
{
P2 = 0x07;
P0 = DIG4_TABLE[a];
Delay1ms();
Delay1ms();
P2 = 0x0b;
P0 = DIG4_TABLE[b];
Delay1ms();
Delay1ms();
}
if((i<=999)&&(i>99))
{
P2 = 0x07;
P0 = DIG4_TABLE[a];
Delay1ms();
Delay334us();
P2 = 0x0b;
P0 = DIG4_TABLE[b];
Delay1ms();
Delay334us();
P2 = 0x0d;
P0 = DIG4_TABLE[c];
Delay1ms();
Delay334us();
}
if((i<=9999)&&(i>999))
{
P2 = 0x07;
P0 = DIG4_TABLE[a];
Delay1ms();
P2 = 0x0b;
P0 = DIG4_TABLE[b];
Delay1ms();
P2 = 0x0d;
P0 = DIG4_TABLE[c];
Delay1ms();
P2 = 0x0e;
P0 = DIG4_TABLE[d];
Delay1ms();
}
}
void main()
{
int i,j;
P0M0 = 0xff;
P0M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
while(1)
{
i++;
j = 99;
while(j--)
{
Dig_OutPut(i);
}
}
没有找到添加源代码的位置,所以将整个代码放在了一个文件内。实际操作中,可以分成多个文件便于管理调用。
实际的执行效果。
后面的跳线有点乱。