51单片机学习路程(三)数码管点亮

文章最后面附有:数码管一些特殊运用。

 

数码管电路图

51单片机学习路程(三)数码管点亮_第1张图片

数码管原理和结构图

51单片机学习路程(三)数码管点亮_第2张图片

真值表,编程时用的到,这边先列出来。

 

51单片机学习路程(三)数码管点亮_第3张图片

下面就是点亮大数码管(共阳级)的代码:

//七段数码管显示实验 
//2018.7.22

#include 
#include 

#define DataPort P1 // 换接口,只需修改这里

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};
//段码,位码

void main()
{
	int i;
	
	while(1)
	{
		for(i = 0;i < 16;i++)
		{	
			P1 = ~Seg_Code[i];			
			DelayMs(255);				
		}	
	}
}

 

点亮大数码管(共阴级)的代码:

//七段数码管显示实验 
//2018.7.22

#include 
#include 

#define DataPort P1 // 换接口,只需修改这里

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};
//段码,位码

void main()
{
	unsigned char i;
	unsigned char j;
	while(1)
	{
		
			DataPort = 0;		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			for(j = 0;j < 8;j++)
			{
			DataPort = Bit_Code[j];		//送位码,选择哪个灯亮
			Bit_Latch = 1;		//开门
			Bit_Latch = 0;		//关门
			//DelayMs(255);
			DataPort = Seg_Code[j];		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;
			DelayMs(255);
			}
			for(j = 0;j < 8;j++)
			{
			DataPort = 0;		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			
			DataPort = Bit_Code[j];		//送位码,选择哪个灯亮
			Bit_Latch = 1;		//开门
			Bit_Latch = 0;		//关门
			DataPort = Seg_Code[j + 8];		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			
			DelayMs(255);
			}				
		
	}
}

//七段数码管显示实验 
//2018.7.22

#include 
#include 

#define DataPort P1 // 换接口,只需修改这里

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,0x40};
//unsigned char code Seg_Code[] = {0x30,0x38,0x3f,0x3e,0x79,0x6e,0x3f,0x3e};
unsigned char code Bit_Code[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//段码,位码


void display1()				//0-F,使用共阳极数码管显示
{
	int i;
	
	while(1)
	{
		for(i = 0;i < 16;i++)
		{	
			P1 = ~Seg_Code[i];			
			DelayMs(255);				
		}	
	}
}


void display2()				//0-7(跑马灯),使用共阴极数码管显示
{
	unsigned char i;
	while(1)
	{
		for(i = 0;i < 8;i++)
		{
			DataPort = 0;		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			
			DataPort = Bit_Code[i];		//送位码,选择哪个灯亮
			Bit_Latch = 1;		//开门
			Bit_Latch = 0;		//关门
			//DelayMs(255);
			
			DataPort = Seg_Code[i];		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			
			DelayMs(255);			
		}
	}
}


void display3()				//0-F(跑马灯形式)
{
	unsigned char i;
	unsigned char j;
	while(1)
	{	
			DataPort = 0;		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			for(j = 0;j < 8;j++)
			{
				DataPort = Bit_Code[j];		//送位码,选择哪个灯亮
				Bit_Latch = 1;		//开门
				Bit_Latch = 0;		//关门
				//DelayMs(255);
				DataPort = Seg_Code[j];		//送段码,
				Seg_Latch = 1;		//开门
				Seg_Latch = 0;
				DelayMs(255);
			}
			for(j = 0;j < 8;j++)
			{
				DataPort = 0;		//送段码,
				Seg_Latch = 1;		//开门
				Seg_Latch = 0;		//关门
			
				DataPort = Bit_Code[j];		//送位码,选择哪个灯亮
				Bit_Latch = 1;		//开门
				Bit_Latch = 0;		//关门
				DataPort = Seg_Code[j + 8];		//送段码,
				Seg_Latch = 1;		//开门
				Seg_Latch = 0;		//关门
			
				DelayMs(255);
			}					
	}
}  

void display4(unsigned char x,unsigned char num)				//0—99//参数选两个数码管(0-8随机),参数X控制速度//精确版
{
	unsigned char i,j,Data[8];
	
	Data[6] = num / 10;
	Data[7] = num % 10;
		
	for(i = 0;i < x;i++)
	{
		for(j = 6;j < 8;j++)	
		{
			DataPort = 0;		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			
			DataPort = Bit_Code[j];		//送位码,选择哪个灯亮
			Bit_Latch = 1;		//开门
			Bit_Latch = 0;		//关门
			
			DataPort = Seg_Code[Data[j]];		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;
			DelayMs(3);
		}
	}						
}

void display5(unsigned char x,unsigned char y,unsigned char z)	//0—999//参数选两个数码管(0-8随机),简陋版(个位过快)
{
	unsigned char i,j,k;

	while(1)
	{			
		for(i = 0;i < 10;i++)
		{
				
			for(j = 0;j < 10;j++)
			{
				for(k = 0;k < 10;k++)
				{
				DataPort = 0;		//送段码,
				Seg_Latch = 1;		//开门
				Seg_Latch = 0;		//关门
				DataPort = Bit_Code[x];		//送位码,选择哪个灯亮
				Bit_Latch = 1;		//开门
				Bit_Latch = 0;		//关门
					
				DataPort = Seg_Code[i];		//送段码,
				Seg_Latch = 1;		//开门
				Seg_Latch = 0;
				DelayMs(5);
				
				DataPort = Bit_Code[y];		//送位码,选择哪个灯亮
				Bit_Latch = 1;		//开门
				Bit_Latch = 0;		//关门
			
				DataPort = Seg_Code[j];		//送段码
				Seg_Latch = 1;		//开门
				Seg_Latch = 0;
				DelayMs(5);
				
				DataPort = Bit_Code[z];		//送位码,选择哪个灯亮
				Bit_Latch = 1;		//开门
				Bit_Latch = 0;		//关门
			
				DataPort = Seg_Code[k];		//送段码
				Seg_Latch = 1;		//开门
				Seg_Latch = 0;
				DelayMs(5);
			}
			}
		}						
	}
}  

 void display6(unsigned char x,unsigned int num)				//0—999//参数选两个数码管(0-8随机),精确版
{
	unsigned int i,j,Data[8];
	Data[5] = num / 100;
	Data[6] = num / 10 % 10;
	Data[7] = num % 10;
		
	for(i = 0;i < x;i++)
	{
		for(j = 5;j < 8;j++)	
		{
			
			DataPort = 0;		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			
			DataPort = Bit_Code[j];		//送位码,选择哪个灯亮
			Bit_Latch = 1;		//开门
			Bit_Latch = 0;		//关门
			
			DataPort = Seg_Code[Data[j]];		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;
			DelayMs(3);
		}
	}						
} 

void display7()				// I LOVE YOU 显示
{
	unsigned char i,j;
	while(1)
	{	
			DataPort = 0;		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			for(j = 0;j < 8;j++)
			{
				DataPort = Bit_Code[j];		//送位码,选择哪个灯亮
				Bit_Latch = 1;		//开门
				Bit_Latch = 0;		//关门
			
				DataPort = Seg_Code[j];		//送段码,
				Seg_Latch = 1;		//开门
				Seg_Latch = 0;
				DelayMs(3);
			}						
	}
}  

void display8(unsigned char x,unsigned int hour,unsigned int minutes,unsigned int second)    //时刻表(时 :分 :秒)显示
{
	unsigned int i,j,Data[8];
	Data[0] = hour / 10;
	Data[1] = hour % 10;
	Data[2] = 16;
	Data[3] = minutes / 10;
	Data[4] = minutes % 10;
	Data[5] = 16;
	Data[6] = second / 10;
	Data[7] = second % 10;
		
	for(i = 0;i < x;i++)
	{
		for(j = 0;j < 8;j++)	
		{			
			DataPort = 0;		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;		//关门
			
			DataPort = Bit_Code[j];		//送位码,选择哪个灯亮
			Bit_Latch = 1;		//开门
			Bit_Latch = 0;		//关门
			
			DataPort = Seg_Code[Data[j]];		//送段码,
			Seg_Latch = 1;		//开门
			Seg_Latch = 0;
			DelayMs(3);
		}
	}
}

void main()
{
	int i,j,k;
	//display1();
	//display2();
	//display3();
	
	/*
	
	for(i = 1000;i > 0;i--)		//自增
	{
		display6(150,i);
	}
	for(i = 0;i < 1000;i++)		//自减
	{
		display6(150,i);
	}
	*/
	
	//display5(1,2,3);
	
	//display7();
	
	for(i = 0;i <= 12;i++)
	{
		for(j = 0;j < 60;j++)
		{
			for(k = 0;k < 60;k++)
			{
				display8(50,i,j,k);
			}
		}
	}
	
}


今天就先到这里,硬件学习要用单片机多调试,才能更好的运用欧。

 

你可能感兴趣的:(51单片机)