1. 程式人生 > >基於51微控制器開發板的應用(數碼管)

基於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<reg51.h>
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<reg51.h>
#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<reg51.h>
#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);	
		}		 

	}
}