#include<reg51.h>
//不考虑小数点,单个数字0~F的编码:实际对应a~g数码管段
unsigned char code codings[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//8位数码管,对应一个完整数码管的位置二进制值;
//例如0x7F:0b01111111,CC共阴极数码管低电平亮,第八位按物理元件就是右起第一个数码管
unsigned char code pos[] = {0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE};
void segDis(unsigned int val, unsigned char dec_hex) {
unsigned char i=2,dis_data;//8位数码管从0开始,我用6位所以是2
while(i < 8) {
dis_data = val % dec_hex;
val /= dec_hex;
P2 = 0x00;//去除残像,很重要
P2 = codings[dis_data];
P3 = pos;
i++;
if(val < 1){
break;
}
}
}