数码管中有8位数字,从右往左分别为LED1、LED2、...、LED8,如下图所示
如何实现点亮单个数字,用下图中的ABC来实现
P2.2管脚控制A,P2.3管脚控制B,P2.4管脚控制C
//定义数码管位选管脚
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;for(i=0;i<8;i++){
//位选
switch(i){
case 0:
LSC=1,LSB=1,LSA=1; //7:LED8
break;
case 1:
LSC=1,LSB=1,LSA=0; //6:LED7
break;
case 2:
LSC=1,LSB=0,LSA=1; //5:LED6
break;
case 3:
LSC=1,LSB=0,LSA=0; //4:LED5
break;
case 4:
LSC=0,LSB=1,LSA=1; //3:LED4
break;
case 5:
LSC=0,LSB=1,LSA=0; //2:LED3
break;
case 6:
LSC=0,LSB=0,LSA=1; //1:LED2
break;
case 7:
LSC=0,LSB=0,LSA=0; //0:LED1
break;
}
}
每一位有8段,如下图所示
eg显示0:0 0 1 1 1 1 1 1
------------------------------------------------------
DP(小数点) G F E D C B A
//段码数据
u8 gsmg_code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0 1 2 3 4 5 6 7 8 9 A B C D E F
P0端控制数码管
1、静态数码管实验
#include "reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
//数码管显示数字的数组(共阴数码管)
u8 gsmg_code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0-F
#define SMG_A_DP_PORT P0
//控制静态数码管显示数字0,即让P0端口输出数字0的段码0x3f(共阴)
void main(){
while(1){
//显示数字0
SMG_A_DP_PORT=gsmg_code[0];
}
}
2、动态数码管实验
#include "reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
//段码数据
u8 gsmg_code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
#define SMG_A_DP_PORT P0
//定义数码管位选管脚
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
void delay(u16 time){
while(time--);
}
//动态数码管
void smg_display(){
u16 i=0;
for(i=0;i<8;i++){
//位选
switch(i){
case 0:
LSC=1,LSB=1,LSA=1; //7
break;
case 1:
LSC=1,LSB=1,LSA=0; //6
break;
case 2:
LSC=1,LSB=0,LSA=1; //5
break;
case 3:
LSC=1,LSB=0,LSA=0; //4
break;
case 4:
LSC=0,LSB=1,LSA=1; //3
break;
case 5:
LSC=0,LSB=1,LSA=0; //2
break;
case 6:
LSC=0,LSB=0,LSA=1; //1
break;
case 7:
LSC=0,LSB=0,LSA=0; //0
break;
}
SMG_A_DP_PORT=gsmg_code[i];
delay(5000);
SMG_A_DP_PORT=0x00; //消隐
}
}
void main(){
while(1){
smg_display();
}
}