51单片机(V51)————静态数码管和动态数码管
- 一、基本知识
-
- (1)锁存
- (2)电路图看段选和位选
- (3)共阳极数码管与共阴极数码管
- 二、静态数码管
- 三、动态数码管
一、基本知识
(1)锁存
锁存芯片的作用:保存上一状态值先给他赋值1,【使能端为高】,让我们要显示的数值通过,再给他赋值0,【使能端为低】,就可以锁住数值
(2)电路图看段选和位选
在BSTV51单片机中 P2^6 这个串口是段选,控制那一个数字的具体显示,可根据字形表对应输出 P2^7 是位选,选择控制要哪一位亮灯
(3)共阳极数码管与共阴极数码管
- 共阴极–输出低电平0,点亮
- 共阳极–输出高电平点亮
- 共有十个引脚,2个接地GND
二、静态数码管
- 静态显示的连接方式、静态显示的优缺点(优点:不需要动态刷新;缺点:占用IO口线多)
- 动态显示的连接方式(所有位数码管的段选线并联在一起,由 位选线控制是哪一位数码管有效)、动态显示的优缺点(缺点:需要动态刷新;优点:占用IO口线少)
#include<reg52.h>
sbit DUAN = P2^6;
sbit WEI = P2^7;
void main()
{
P0 = 0x7d; // 1111 1110
DUAN = 1;
DUAN = 0;
P0 = 0x00;
WEI = 1;
WEI = 0;
}
三、动态数码管
用延迟函数
#include<reg52.h>
sbit DUAN = P2^6;
sbit WEI = P2^7;
void delay(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--);
}
unsigned code sz[17] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00
};
void main()
{
while(1)
{
P0 = sz[1]; // 数字1
DUAN = 1; // 一端上电
DUAN = 0; // 断开
P0 = 0xfe;
WEI = 1; // 另一端上高电平
WEI = 0; // 另一端断开
delay(200);
P0 = sz[2]; // 数字2
DUAN = 1;
DUAN = 0;
P0 = 0xfd;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[3]; // 数字3
DUAN = 1;
DUAN = 0;
P0 = 0xfb;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[4]; // 数字4
DUAN = 1;
DUAN = 0;
P0 = 0xf7;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[5]; // 数字5
DUAN = 1;
DUAN = 0;
P0 = 0xef;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[6]; // 数字6
DUAN = 1;
DUAN = 0;
P0 = 0xdf;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[7]; // 数字7
DUAN = 1;
DUAN = 0;
P0 = 0xbf;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[8]; // 数字8
DUAN = 1;
DUAN = 0;
P0 = 0x7f;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[8]; // 数字8
DUAN = 1;
DUAN = 0;
P0 = 0x7f;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[7]; // 数字7
DUAN = 1;
DUAN = 0;
P0 = 0xbf;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[6]; // 数字6
DUAN = 1;
DUAN = 0;
P0 = 0xdf;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[5]; // 数字5
DUAN = 1;
DUAN = 0;
P0 = 0xef;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[4]; // 数字4
DUAN = 1;
DUAN = 0;
P0 = 0xf7;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[3]; // 数字3
DUAN = 1;
DUAN = 0;
P0 = 0xfb;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[2]; // 数字2
DUAN = 1;
DUAN = 0;
P0 = 0xfd;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[1]; // 数字1
DUAN = 1;
DUAN = 0;
P0 = 0xfe;
WEI = 1;
WEI = 0;
delay(200);
}
}
不用延迟函数
#include<reg52.h>
sbit DUAN = P2^6;
sbit WEI = P2^7;
unsigned code sz[17] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00
};
void main()
{
while(1)
{
P0 = sz[1]; // 数字1
DUAN = 1; // 一端上电
DUAN = 0; // 断开
P0 = 0xfe;
WEI = 1; // 另一端上高电平
WEI = 0; // 另一端断开
P0 = 0xff; // 不用延迟函数需要开一个关一个才能保证不乱码
WEI = 1;
WEI = 0;
P0 = sz[2]; // 数字2
DUAN = 1;
DUAN = 0;
P0 = 0xfd;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[3]; // 数字3
DUAN = 1;
DUAN = 0;
P0 = 0xfb;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[4]; // 数字4
DUAN = 1;
DUAN = 0;
P0 = 0xf7;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[5]; // 数字5
DUAN = 1;
DUAN = 0;
P0 = 0xef;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[6]; // 数字6
DUAN = 1;
DUAN = 0;
P0 = 0xdf;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[7]; // 数字7
DUAN = 1;
DUAN = 0;
P0 = 0xbf;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[8]; // 数字8
DUAN = 1;
DUAN = 0;
P0 = 0x7f;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
}
}