单片机中的浮点数转换成串口可打印格式
2018-06-20 来源:eefocus
阅读数:363
uint8 len = 0; #define uint8 unsigned char
len=Float2Char(arp_count_xishu,b);
USART0_send_n(b,len);
#define uint32 unsigned int
/************************************************************************/
/* 函数名:Float2Char */
/* 功能:执行Float转换成char */
/*参数:value: Float值 array:char数组 */
/*返回值:数组长度 */
/************************************************************************/
unsigned char Float2Char(float value, unsigned char *array){
uint8 IntegerPart;
float DecimalPart;
uint8 i = 0;
uint8 j = 0;
char temp;
//分离整数和小数
if(value >= 1) {
IntegerPart = (uint8)value;
DecimalPart = value - IntegerPart;
}
else {
IntegerPart = 0;
DecimalPart = value - IntegerPart;
}
if(IntegerPart==0){
array[0]='0';
array[1]='.';
i=1;
}else{
while(IntegerPart>0){
array[i]=IntegerPart%10+'0';
IntegerPart/=10;
i++;
}
i--;
//fix the result
for(j=0;j temp=array[j];
array[j]=array[i-j];
array[i-j]=temp;
}
i++;
array[i] = '.';
}
//convert the Decimalpart
i++;
array[i++] = (uint32)(DecimalPart * 10)%10 + '0';
array[i++] = (uint32)(DecimalPart * 100)%10 + '0';
array[i++] = (uint32)(DecimalPart * 1000)%10 + '0';
array[i++] = (uint32)(DecimalPart * 10000)%10 + '0';
array[i] = '\0';
return i;
}