示例:分秒表
原始方法:
查看代码
unsigned char SMG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void disp(unsigned int j,k)
{
P2=0XEF; //P24数码管
P0=SMG[j/10];
delay(20);
P0=0X00;
P2=0XDF; //P25数码管
P0=SMG[j%10];
delay(20);
P0=0X00;
P2=0XBF; //P26数码管
P0=SMG[k/10];
delay(20);
P0=0X00;
P2=0X7F; //P27数码管
P0=SMG[k%10];
delay(20);
P0=0X00;
}
精简方法:
查看代码
char Tube[]={0xEF,0XDF,0XBF,0x7F}; //P24 25 26 27
unsigned char SMG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//分秒数码管上显示出来
void dispTime(char j,k)
{
char t=0;
char Time[4]={0};
Time[0]=j/10;Time[1]=j%10;
Time[0]=k/10;Time[1]=k%10;
for(t=0;t<4;t++){
P2=Tube[t];
P0=SMG[Time[t]];
delay(40);
P0=0X00;
}
}
对比我们可以发现 精简方法明显少了很多代码量 写熟了就好了。
示例:显示1234
原始方法:
查看代码
#define UC unsigned char
UC SMG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//函数3:数字1234
void num1234()
{
P2=0XEF; //P24显示
P0=SMG[1];
delay(40);
P0=0X00;
P2=0XDF; //P25显示
P0=SMG[2];
delay(40);
P0=0X00;
P2=0XBF; //P26显示
P0=SMG[3];
delay(40);
P0=0X00;
P2=0X7F; //P27显示
P0=SMG[4];
delay(40);
P0=0X00;
}
精简方法:
查看代码
#define UC unsigned char
UC Tube[]={0XEF,0XDF,0XBF,0X7F};
UC Num[]={0x06,0x5b,0x4f,0x66}; //1234
//函数3:数字234
void num1234()
{
UC N=0;
for(N=0;N<4;N++){
P2=Tube[N];
P0=Num[N];
delay(20);
P0=0X00;
}
}
标签:P2,动态显示,P0,0X00,char,数码管,delay,SMG,模板 From: https://www.cnblogs.com/YYZYCS/p/16893543.html