目的是用在单片机数码管显示,高位清零,本来的代码能用,但是一个数如果是980,转换后是00000980,就想把高位是零的消掉.这是很笨的办法.所以请教高手能否简化写法.
uint32 定义为 无符号32位整数.
void BCDDataConversion(uint32 Bign)
{
if (0<=Bign<10){//
ledcache[7]= leddatacl[Bign%10];
ledcache[6]= 0x00;
ledcache[5]= 0x00;
ledcache[4]= 0x00;
ledcache[3]= 0x00;
ledcache[2]= 0x00;
ledcache[1]= 0x00;
ledcache[0]= 0x00;
}
else if (10<=Bign<100){
ledcache[7]= leddatacl[Bign%10];
ledcache[6]= leddatacl[Bign/10%10];
ledcache[5]= 0x00;
ledcache[4]= 0x00;
ledcache[3]= 0x00;
ledcache[2]= 0x00;
ledcache[1]= 0x00;
ledcache[0]= 0x00;
} else if (100<=Bign<1000){
ledcache[7]= leddatacl[Bign%10];
ledcache[6]= leddatacl[Bign/10%10];
ledcache[5]= leddatacl[Bign/100%10];
ledcache[4]= 0x00;
ledcache[3]= 0x00;
ledcache[2]= 0x00;
ledcache[1]= 0x00;
ledcache[0]= 0x00;
}else if (1000<=Bign<10000){
ledcache[7]= leddatacl[Bign%10];
ledcache[6]= leddatacl[Bign/10%10];
ledcache[5]= leddatacl[Bign/100%10];
ledcache[4]= leddatacl[Bign/1000%10];
ledcache[3]= 0x00;
ledcache[2]= 0x00;
ledcache[1]= 0x00;
ledcache[0]= 0x00;
}else if (10000<=Bign<100000){
ledcache[7]= leddatacl[Bign%10];
ledcache[6]= leddatacl[Bign/10%10];
ledcache[5]= leddatacl[Bign/100%10];
ledcache[4]= leddatacl[Bign/1000%10];
ledcache[3]= leddatacl[Bign/10000%10];
ledcache[2]= 0x00;
ledcache[1]= 0x00;
ledcache[0]= 0x00;
}else if (100000<=Bign<1000000){
ledcache[7]= leddatacl[Bign%10];
ledcache[6]= leddatacl[Bign/10%10];
ledcache[5]= leddatacl[Bign/100%10];
ledcache[4]= leddatacl[Bign/1000%10];
ledcache[3]= leddatacl[Bign/10000%10];
ledcache[2]= leddatacl[Bign/100000%10];
ledcache[1]= 0x00;
ledcache[0]= 0x00;
}else if (1000000<=Bign<10000000){
ledcache[7]= leddatacl[Bign%10];
ledcache[6]= leddatacl[Bign/10%10];
ledcache[5]= leddatacl[Bign/100%10];
ledcache[4]= leddatacl[Bign/1000%10];
ledcache[3]= leddatacl[Bign/10000%10];
ledcache[2]= leddatacl[Bign/100000%10];
ledcache[1]= leddatacl[Bign/1000000%10];
ledcache[0]= leddatacl[Bign/10000000%10];
ledcache[0]= 0x00;
}else if (10000000<=Bign<100000000){
ledcache[7]= leddatacl[Bign%10];
ledcache[6]= leddatacl[Bign/10%10];
ledcache[5]= leddatacl[Bign/100%10];
ledcache[4]= leddatacl[Bign/1000%10];
ledcache[3]= leddatacl[Bign/10000%10];
ledcache[2]= leddatacl[Bign/100000%10];
ledcache[1]= leddatacl[Bign/1000000%10];
ledcache[0]= leddatacl[Bign/10000000%10];
}
else Bign=0;
}
这个函数能有简单点的写法吗?想蒙住了. 求指点.
补上开发板图片
IMG_20121107_004248_0.jpg (740.3 KB, 下载次数: 0)
下载附件
2012-11-7 00:50 上传
You can simplify it further by combining the two loops into one:
- void BCDDataConversion(uint32 Bign)
- {
- unsigned char i = 8; //只填充前7个字符位置,因为第8个位置上至少要显示一个“0”,不需要进行预填充
- do
- {
- i = i-1;
- if(Bign) ledcache[i] = leddatacl[Bign%10];
- else ledcache[i] = leddatacl[_BLANK_]; //这里表示“空白”字符,即消隐控制码
- Bign /= 10;
- }while(i); //防止传入的参数超过99,999,999而造成内存访问越界
- }
复制代码一周热门 更多>