51单片机学习--数码管显示

首先实现静态数码管显示

SMG表示共阴极 ,要让第三位显示数字6,需要LED6端口接0,即Y5=0,5的二进制为101,按P2_4 到 P2_2 的顺序接,则CBA分别接101

然后在数码管另一端,需要点亮的接上1,根据原理图一一对应即可

得到P0_0 到 P0_7 依次为1011 1110

所以P0的十六进制为0111 1101 == 7D

51单片机学习--数码管显示_第1张图片

51单片机学习--数码管显示_第2张图片

 


 功能:使第三位显示数字6

#include 
#include 



void main()
{
	P2_4 = 1; //C
	P2_3 = 0; //B
	P2_2 = 1; //A
	
	P0 = 0x7D;

	while(1)
	{
		
	}
}

51单片机学习--数码管显示_第3张图片



 51单片机学习--数码管显示_第4张图片

将这个功能封装成函数,指定位置显示指定数字 

#include 
#include 

unsigned char NixieTable[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

void Nixie(unsigned char Location, Number)
{
	switch(Location)
	{
		case 1: P2_4 = 1; P2_3 = 1; P2_2 = 1; break;
		case 2: P2_4 = 1; P2_3 = 1; P2_2 = 0; break;
		case 3: P2_4 = 1; P2_3 = 0; P2_2 = 1; break;
		case 4: P2_4 = 1; P2_3 = 0; P2_2 = 0; break;
		case 5: P2_4 = 0; P2_3 = 1; P2_2 = 1; break;
		case 6: P2_4 = 0; P2_3 = 1; P2_2 = 0; break;
		case 7: P2_4 = 0; P2_3 = 0; P2_2 = 1; break;
		case 8: P2_4 = 0; P2_3 = 0; P2_2 = 0; break;
	}
	
	P0 = NixieTable[Number];
}

void main()
{
	Nixie(7, 2);

	while(1)
	{
		
	}
}

51单片机学习--数码管显示_第5张图片 



 在点亮某位数码管后,因为切换的太快会产生错位重影,所以每次都要将P0归零来消影,在极快的速度下依次显示123,视觉上得到同时亮起的123,这就是动态数码管显示

#include 
#include 

unsigned char NixieTable[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

void Delay(unsigned int t)		//@11.0592MHz
{
	unsigned char i, j;

	while(t --)
	{
		_nop_();
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);
	}
}


void Nixie(unsigned char Location, Number)
{
	switch(Location)
	{
		case 1: P2_4 = 1; P2_3 = 1; P2_2 = 1; break;
		case 2: P2_4 = 1; P2_3 = 1; P2_2 = 0; break;
		case 3: P2_4 = 1; P2_3 = 0; P2_2 = 1; break;
		case 4: P2_4 = 1; P2_3 = 0; P2_2 = 0; break;
		case 5: P2_4 = 0; P2_3 = 1; P2_2 = 1; break;
		case 6: P2_4 = 0; P2_3 = 1; P2_2 = 0; break;
		case 7: P2_4 = 0; P2_3 = 0; P2_2 = 1; break;
		case 8: P2_4 = 0; P2_3 = 0; P2_2 = 0; break;
	}
	
	P0 = NixieTable[Number];
	Delay(1);
	P0 = 0x00; //清零消影
}

void main()
{
	
	while(1) //依次显示123,因为太快所以看起来是同时亮的
	{
		Nixie(1, 1);
		Delay(2);
		Nixie(2, 2);
		Delay(2);
		Nixie(3, 3);
		Delay(2);
	}
}

51单片机学习--数码管显示_第6张图片

 

你可能感兴趣的:(51单片机,51单片机,学习,嵌入式硬件)