基于51單片機(jī)開(kāi)發(fā)板的應(yīng)用(數(shù)碼管)
在對(duì)LED燈的應(yīng)用有了一定的了解之后,我開(kāi)始學(xué)習(xí)了一些關(guān)于數(shù)碼管的應(yīng)用。
在我的開(kāi)發(fā)板上,有獨(dú)立共陽(yáng)管和八位共陰管 。數(shù)碼管從高位到低位的段碼依次是h(dp),g,f,e,d,c,b,a共八位。共陰管是“1”表示亮,“0”表示滅,而共陽(yáng)管則是相反的。順便提一句,若是要檢測(cè)數(shù)碼管是否完好,可以用數(shù)碼管“8”來(lái)檢測(cè)。
若是要在數(shù)碼管上顯示0~F,則可以用一套固定的十六進(jìn)制數(shù)表示,可以放在數(shù)組中,為{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}。這一個(gè)數(shù)組是用來(lái)表示共陰管的亮的,而若是共陽(yáng)管的時(shí)候,需要在前面加上“~”。
獨(dú)立共陽(yáng)管顯示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位共陰管顯示有靜態(tài)掃描和動(dòng)態(tài)掃描兩種方式。
1、8個(gè)同時(shí)顯示0-F 靜態(tài)掃描
#include
#define DataPort P0 //數(shù)據(jù)端口
sbit Seg_latch=P2^2; //段鎖存
sbit Bit_latch=P2^3; //位鎖存 兩者必須是取反,只能有一個(gè)成立
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; //開(kāi)段鎖存
Seg_latch=0; //關(guān)段鎖存 值進(jìn)來(lái)了
DataPort=0x00; //控制位鎖存(低電平有效),8個(gè)管同時(shí)亮
Bit_latch=1; //開(kāi)位鎖存
Bit_latch=0; //關(guān)位鎖存
DelayMs(200);
}
}
}
2、顯示0-F:先是顯示0-7,然后顯示8-F 位:第1-8位,第1-8位 動(dòng)態(tài)掃描
#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);
}
}
}