阅读数: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;
}
上一篇:STC12C5A60S2单片机的内置EEPROM的使用
下一篇:单片机实现PT2262解码示例代码
推荐阅读最新更新时间:2024-03-16 16:06