目的是用在单片机数码管显示,高位清零,本来的代码能用,但是一个数如果是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 上传
楼上几位写得都比较好呀.感觉单片机中,LZ这种写法特费空间呀
一周热门 更多>