数码管显示

这篇博客介绍了单片机控制数码管显示的方法,包括静态显示和动态显示。在静态显示中,展示了如何通过位选和段选控制数码管显示特定字符,并给出了示例代码。动态显示部分则演示了如何利用延时函数实现多位数码管连续快速切换来显示多位数字,同样附带了相应的C语言代码实现。
摘要由CSDN通过智能技术生成

单片机学习 1月8日

数码管静态显示

0x3f , 0x06 , 0x5b , 0x4f , 0x66 , 0x6d ,
0 1 2 3 4 5
0x7d , 0x07 , 0x7f , 0x6f , 0x77 , 0x7c ,
6 7 8 9 A B
0x39 , 0x5e , 0x79 , 0x71 , 0x00
C D E F 不显示

位选

sbit weal = P2^7;

低电流通过时工作

段选

sbit dula = P2^6;

共阴极 高电流通过时工作

例程

#include<reg52.h>

sbit dula = P2^6;     //申明U1锁存器锁存端
sbit wela = P2^7;     //申明U2锁存器锁存端      
 
void main()
{
     wela = 1;           //打开锁存器      
	 P0 = 0xfc;          //1111 1100      低电流通过
	 wela = 0;           //关闭锁存器       
	 
	 dula = 1; 
	 P0 = 0x6;           //1100  0000   段选  共阴极    高电流通过
	 dula = 0;
	
	while(1);             //工作停止
	
}
//数码管   第1,2个显示1
#include<reg52.h>

sbit du = P2^6;
sbit we = P2^7;

void main()
{
    we = 1;
	P0 = 0x15;      //0001  0101
	we = 0;
	
	du = 1;
	P0 = 0x7f;      //0011 1111
	du = 0;
	
	while(1);
}
//数码管  1  3  5 位显示8
#include<reg52.h>

sbit du = P2^6;
sbit we = P2^7;

void main()
{
    we = 1;
	P0 = 0xea;      //1110  1010
	we = 0;
	
	du = 1;
	P0 = 0x7f;      //0011 1111
	du = 0;
	
	while(1);
}
//数码管  1  3  5 位显示8

数组实现方式

数组

uchar code table[]={0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
#include<reg52.h>
#define uchar unsigned char
	
uchar code table[]={0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};

sbit dula = P2^6;
sbit wela = P2^7;

void main()
{
     wela = 1;
	 P0 = 0xfa;                  //  1111 1010
     wela = 0;
	
	 dula = 1;
	 P0 = table[6];
	 dula = 0;
	
	while(1);
}
//数码管 1 3 位实现6

数码管动态显示

#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
	
uchar code table[]={0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};

sbit dula = P2^6;
sbit weal = P2^7;


void delay(uint x)
{
	
  uint i,j;
  for(i=x;i>0;i--)
	for(j=110;j>0;j--);
	
}

void main()
{
	 P0 = 0xff;             //1111 1111
   weal = 1;
	 P0 = 0xfe;             //1111 1110
	 weal = 0;
	 dula = 1;
	 P0 = table[0];
	 delay(1);
	
	 P0 = 0xff;             //1111 1111
   weal = 1;
	 P0 = 0xfd;             //1111 1101
	 weal = 0;
	 dula = 1;
	 P0 = table[1];
	 delay(1);
	
 	 P0 = 0xff;             //1111 1111
   weal = 1;
	 P0 = 0xfb;             //1111 1011
	 weal = 0;
	 dula = 1;
	 P0 = table[2];
	 delay(1);
	 
	 P0 = 0xff;             //1111 1111
   weal = 1;
	 P0 = 0xf7;             //1111 0111
	 weal = 0;
	 dula = 1;
	 P0 = table[3];
	 delay(1);
	 
	 P0 = 0xff;             //1111 1111
   weal = 1;
	 P0 = 0xef;             //1110 1111
	 weal = 0;
	 dula = 1;
	 P0 = table[4];
	 delay(1);
	 
	 P0 = 0xff;             //1111 1111
   weal = 1;
	 P0 = 0xdf;             //1101 1111
	 weal = 0;
	 dula = 1;
	 P0 = table[5];
	 delay(1);

}
//数码管同时显示 0 1 2 3 4 5 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值