基于51单片机开发板的应用(数码管)
扫描二维码
随时随地手机看文章
在对LED灯的应用有了一定的了解之后,我开始学习了一些关于数码管的应用。
在我的开发板上,有独立共阳管和八位共阴管 。数码管从高位到低位的段码依次是h(dp),g,f,e,d,c,b,a共八位。共阴管是“1”表示亮,“0”表示灭,而共阳管则是相反的。顺便提一句,若是要检测数码管是否完好,可以用数码管“8”来检测。
若是要在数码管上显示0~F,则可以用一套固定的十六进制数表示,可以放在数组中,为{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}。这一个数组是用来表示共阴管的亮的,而若是共阳管的时候,需要在前面加上“~”。
独立共阳管显示0-F
//显示0-F
#include
unsigned char code LED[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
void main(void)
{
unsigned char i;
while(1)
{
for(i=0; i<16; i++)
{
P1=~LED[i]; //取反
DelayMs(200); //大约延迟200ms
}
}
}
8位共阴管显示有静态扫描和动态扫描两种方式。
1、8个同时显示0-F 静态扫描
#include
#define DataPort P0 //数据端口
sbit Seg_latch=P2^2; //段锁存
sbit Bit_latch=P2^3; //位锁存 两者必须是取反,只能有一个成立
unsigned char code Seg_code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
void main(void)
{
unsigned char i;
while(1)
{
for(i=0; i<16; i++)
{
DataPort=Seg_code[i]; //控制段锁存,显示0-F
Seg_latch=1; //开段锁存
Seg_latch=0; //关段锁存 值进来了
DataPort=0x00; //控制位锁存(低电平有效),8个管同时亮
Bit_latch=1; //开位锁存
Bit_latch=0; //关位锁存
DelayMs(200);
}
}
}
2、显示0-F:先是显示0-7,然后显示8-F 位:第1-8位,第1-8位 动态扫描
#include
#define DataPort P0
sbit Seg_latch=P2^2;
sbit Bit_latch=P2^3;
unsigned char code Seg_code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code Bit_code[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(unsigned char i)
{
while(i--)
;
}
void main(void)
{
unsigned char i;
while(1)
{
for(i=0; i<16; i++)
{
DataPort=0x00; //消除重影
Seg_latch=1;
Seg_latch=0;
DataPort=Bit_code[i]; //位码
Bit_latch=1;
Bit_latch=0;
DataPort=Seg_code[i]; //段码
Seg_latch=1;
Seg_latch=0;
delay(100000);
}
}
}