基于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);	
		}		 

	}
}



     

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值