5脚多位数码管显示

5脚多位数码管显示

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

市面上大多数的8.8.数码管都是8脚或者10脚,5脚比较少见,实际上对于5脚的操作也比较简单,这里分享一下5脚数据管显示程序编写的大致流程,希望这对同为电子行业的朋友有所帮助!


一、脚位表

数码管规格书应该能找到数码管的引脚位置图,为了便于编程,可以根据点亮转换成脚位表

在这里插入图片描述在这里插入图片描述
5脚对应5个IO脚控制,如上脚位图所示,3脚低电平,2脚高电平,A1段亮;2脚低电平,3脚高电平,B1亮。转换为显示脚位图如下在这里插入图片描述
可以看到PIN5行还是空的,说明5脚控制8.8.的数码管绰绰有余,乃至1.8.8的数码管依然有余量

二、LED正反扫

1.头文件LED.h

定义2个8位的数据,对应数码管的每一段

#ifndef _LED_H_
#define _LED_H_

typedef union
{
	unsigned char byte;
	struct{
		unsigned char bit0:1;
		unsigned char bit1:1;
		unsigned char bit2:1;
		unsigned char bit3:1;
		unsigned char bit4:1;
		unsigned char bit5:1;
		unsigned char bit6:1;
		unsigned char bit7:1;
	}Bits;
}ByteBit_Union;

#define  led_io_5  P12   //led io口
#define  led_io_4  P13   //led io口
#define  led_io_3  P14   //led io口
#define  led_io_2  P00   //led io口
#define  led_io_1  P01   //led io口

#define  led_io_5_in  P1IO = P1IO & 0xfb	//io5置为输入
#define  led_io_4_in  P1IO = P1IO & 0xf7	//io4置为输入
#define  led_io_3_in  P1IO = P1IO & 0xef	//io3置为输入
#define  led_io_2_in  P0IO = P0IO & 0xfe	//io2置为输入
#define  led_io_1_in  P0IO = P0IO & 0xfd	//io1置为输入

#define  led_io_5_out  P1IO = P1IO | 0x04   //io5置为输出
#define  led_io_4_out  P1IO = P1IO | 0x08   //io4置为输出
#define  led_io_3_out  P1IO = P1IO | 0x10   //io3置为输出
#define  led_io_2_out  P0IO = P0IO | 0x01   //io2置为输出
#define  led_io_1_out  P0IO = P0IO | 0x02   //io1置为输出

/*数码管段显*/
extern ByteBit_Union DG1_Flag;
#define DG1_A DG1_Flag.Bits.bit0
#define DG1_B DG1_Flag.Bits.bit1
#define DG1_C DG1_Flag.Bits.bit2
#define DG1_D DG1_Flag.Bits.bit3
#define DG1_E DG1_Flag.Bits.bit4
#define DG1_F DG1_Flag.Bits.bit5
#define DG1_G DG1_Flag.Bits.bit6
#define DG1_DP DG1_Flag.Bits.bit7

extern ByteBit_Union DG2_Flag;
#define DG2_A DG2_Flag.Bits.bit0
#define DG2_B DG2_Flag.Bits.bit1
#define DG2_C DG2_Flag.Bits.bit2
#define DG2_D DG2_Flag.Bits.bit3
#define DG2_E DG2_Flag.Bits.bit4
#define DG2_F DG2_Flag.Bits.bit5
#define DG2_G DG2_Flag.Bits.bit6
#define DG2_DP DG2_Flag.Bits.bit7

void Led_under_deal(void);	//数码管灯阵扫描函数
void DG_ALL(bit EN_DIS);	//数码全显或全关
void DG_Display(unsigned int num1,unsigned int num2);	//数码管,显示0-9

2.LED正反扫

根据脚位表,设置每个端口需要扫描的段:

ByteBit_Union DG1_Flag = {0};
ByteBit_Union DG2_Flag = {0};

/*********************************************************
函  数:Led_under_deal
描  述:	LED底层处理(正反扫)(100us 中断)
输入值: 无
输出值: 无
返回值: 无 
**********************************************************/
void Led_under_deal(void)
{
	led_io_1_in;
	led_io_2_in;
	led_io_3_in;
	led_io_4_in;
	led_io_5_in;

	led_io_1=0;
	led_io_2=0;
	led_io_3=0;	
	led_io_4=0;	
	led_io_5=0;	
	
	if(led_disp_step==0)
	{	    			
		if(DG2_B)
		{
			led_io_2_out;
			led_io_2=1;
		}	
		if(DG2_D)
		{
			led_io_3_out;
			led_io_3=1;
		}		
		if(DG2_F)
		{
			led_io_4_out;
			led_io_4=1;
		}
		if(DG2_G)
		{
			led_io_5_out;
			led_io_5=1;
		}		
		led_io_1_out;			
		led_io_1=0;	 		
	}	
	else if(led_disp_step==1)
	{	   				
		if(DG2_A)
		{
			led_io_1_out;
			led_io_1=1;
		}
		if(DG1_B)
		{
			led_io_3_out;
			led_io_3=1;
		}
		if(DG1_C)
		{
			led_io_4_out;
			led_io_4=1;
		}
		if(DG1_E)
		{
			led_io_5_out;
			led_io_5=1;
		}			
		led_io_2_out;			
		led_io_2=0;	      
	}	
	else if(led_disp_step==2)
	{	   				
		if(DG2_C)
		{
			led_io_1_out;
			led_io_1=1;
		}
		if(DG1_A)
		{
			led_io_2_out;
			led_io_2=1;
		}
		if(DG1_D)
		{
			led_io_4_out;
			led_io_4=1;
		}
		if(DG1_F)
		{
			led_io_5_out;
			led_io_5=1;
		}		
		led_io_3_out;			
		led_io_3=0;	      
	}
	else if(led_disp_step==3)
	{	   				
		if(DG2_E)
		{
			led_io_1_out;
			led_io_1=1;
		}
		if(DG2_DP)
		{
			led_io_2_out;
			led_io_2=1;
		}	
		if(DG1_DP)
		{
			led_io_3_out;
			led_io_3=1;
		}	
		if(DG1_G)
		{
			led_io_5_out;
			led_io_5=1;
		}			
		led_io_4_out;			
		led_io_4=0;	      
	}	
	led_disp_step++;
	if(led_disp_step>=4)led_disp_step=0;
}
/*数码全显或全关*/
void DG_ALL(bit EN_DIS)
{
	if(EN_DIS)	//全开
	{
		DG1_Flag.byte = 0xff;
		DG2_Flag.byte = 0xff;	
	}
	else	//全关
	{
		DG1_Flag.byte = 0;
		DG2_Flag.byte = 0;				
	}
}

/*数码管,显示0-9*/
void DG_Display(unsigned int num1,unsigned int num2)
{	
	DG_ALL(0);//全写0
	switch(num1)
	{
		case 0:	//A.B.C.D.E.F
			DG1_A = 1;DG1_B = 1;
			DG1_C = 1;DG1_D = 1;
			DG1_E = 1;DG1_F = 1;
			break;
		case 1:	//B.C
			DG1_B = 1;
			DG1_C = 1;	
			break;
		case 2:	//A.B.D.E.G
			DG1_A = 1;DG1_B = 1;
			DG1_D = 1;
			DG1_E = 1;
			DG1_G = 1;
			break;		
		case 3:	//A.B.C.D.G
			DG1_A = 1;DG1_B = 1;
			DG1_C = 1;DG1_D = 1;
			DG1_G = 1;	
			break;
		case 4:	//B.C.F.G
			DG1_B = 1;
			DG1_C = 1;
			DG1_F = 1;
			DG1_G = 1;	
			break;
		case 5:	//A.C.D.F.G
			DG1_A = 1;
			DG1_C = 1;DG1_D = 1;
			DG1_F = 1;
			DG1_G = 1;	
			break;	
		case 6:	//A.C.D.E.F.G
			DG1_A = 1;
			DG1_C = 1;DG1_D = 1;
			DG1_E = 1;DG1_F = 1;
			DG1_G = 1;	
			break;		
		case 7:	//A.B.C
			DG1_A = 1;DG1_B = 1;
			DG1_C = 1;	
			break;
		case 8:	//A.B.C.D.E.F.G
			DG1_A = 1;DG1_B = 1;
			DG1_C = 1;DG1_D = 1;
			DG1_E = 1;DG1_F = 1;
			DG1_G = 1;	
			break;
		case 9:	//A.B.C.D.F.G
			DG1_A = 1;DG1_B = 1;
			DG1_C = 1;DG1_D = 1;
			DG1_F = 1;
			DG1_G = 1;		
			break;			
	}
	
	switch(num2)
	{
		case 0:	//A.B.C.D.E.F
			DG2_A = 1;DG2_B = 1;
			DG2_C = 1;DG2_D = 1;
			DG2_E = 1;DG2_F = 1;
			break;
		case 1:	//B.C
			DG2_B = 1;
			DG2_C = 1;
			break;
		case 2:	//A.B.D.E.G
			DG2_A = 1;DG2_B = 1;
			DG2_D = 1;
			DG2_E = 1;
			DG2_G = 1;
			break;		
		case 3:	//A.B.C.D.G
			DG2_A = 1;DG2_B = 1;
			DG2_C = 1;DG2_D = 1;
			DG2_G = 1;
			break;
		case 4:	//B.C.F.G
			DG2_B = 1;
			DG2_C = 1;
			DG2_F = 1;
			DG2_G = 1;
			break;
		case 5:	//A.C.D.F.G
			DG2_A = 1;
			DG2_C = 1;DG2_D = 1;
			DG2_F = 1;
			DG2_G = 1;
			break;	
		case 6:	//A.C.D.E.F.G
			DG2_A = 1;
			DG2_C = 1;DG2_D = 1;
			DG2_E = 1;DG2_F = 1;
			DG2_G = 1;
			break;		
		case 7:	//A.B.C
			DG2_A = 1;DG2_B = 1;
			DG2_C = 1;
			break;
		case 8:	//A.B.C.D.E.F.G
			DG2_A = 1;DG2_B = 1;
			DG2_C = 1;DG2_D = 1;
			DG2_E = 1;DG2_F = 1;
			DG2_G = 1;
			break;
		case 9:	//A.B.C.D.F.G
			DG2_A = 1;DG2_B = 1;
			DG2_C = 1;DG2_D = 1;
			DG2_F = 1;
			DG2_G = 1;
			break;			
	}
}

最后调用DG_Display函数就可以显示任意数字了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值