P0控制数码管 ,P0低电平 P1,P2,P3高电平
1、静态数码管
需求:数码管显示0,即让p0端口输出数字0的段码0x3f(共阴)
#include "reg52.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-9 A-F#define SMG_A_OP_PORT P0 //宏定义P0
void main(){
while(1){
//显示数字0 需显示数字几索引号就是几
SMG_A_OP_PORT =gsmg_code[0];
}
}
2、动态数码管
#include "reg52.h"
typedef unsigned int u16;
typedef unsigned char u8;
#define SMG_A_OP_PORT P0
void delay_10us(u16 ten_us)
{
while(ten_us--);
}//数码管显示数字的数组 共阴极 显示段选数据
u8 gsmg[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e, 0x79,0x71};//定义数码管位选的控制管脚
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;void smg_xianshi(){
u8 i=0;
for(i=0;i<8;i++){
//位选操作
switch(i){
case 0:
LSC=1,LSB=1,LSA=1; //Y7亮(第1个数码管亮)
break;
case 1:
LSC=1,LSB=1,LSA=0; //Y6亮(第2个数码管亮)
break;
case 2:
LSC=1,LSB=0,LSA=1;
break;
case 3:
LSC=1,LSB=0,LSA=0;
break;
case 4:
LSC=0,LSB=1,LSA=1;
break;
case 5:
LSC=0,LSB=1,LSA=0;
break;
case 6:
LSC=0,LSB=0,LSA=1;
break;
case 7:
LSC=0,LSB=0,LSA=0;//Y0亮(第7个数码管亮)
break;
}
SMG_A_OP_PORT=gsmg[i];
delay_10us(100);
SMG_A_OP_PORT=0x00;//消隐(消除重影)
}
}void main(){
while(1){
smg_xianshi();
}
}