How STM32 prints floating point numbers via printf

First, the problem

Using IAR to develop STM32, it was found that floating point could not be printed out through printf and redirected to the serial port. code show as below:

How STM32 prints floating point numbers via printf

The output is as follows:

How STM32 prints floating point numbers via printf

It can be seen that some floating point numbers cannot be displayed properly.

Second, the solution

This is because IAR's default choice of printf library does not support floating-point output. Can be modified in the setting options. As follows: The default is to use small and change it to auto.

How STM32 prints floating point numbers via printf Third, note

Without modifying the settings, try the following two codes to solve. One is to convert a floating-point number to a string output by sprintf, and the other is to decompose the integer and fractional parts and output them separately. The first method is also not feasible, only decomposition can.

code show as below:

/*

* cyang 2018/2/27

* mcu printf float value

*/

#include "stdio.h"

Void printf_float(float a)

{

Char tmp[8]={0};

Int i;

Sprintf(tmp, "%f", a);

For(i=0; i“8; i++)

Printf("%c", tmp[i]);

Printf("");

}

Void PrintFloat(float value)

{

Int tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;

Tmp = (int)value;

Tmp1=(int)((value-tmp)*10)%10;

Tmp2=(int)((value-tmp)*100)%10;

Tmp3=(int)((value-tmp)*1000)%10;

Tmp4=(int)((value-tmp)*10000)%10;

Tmp5=(int)((value-tmp)*100000)%10;

Tmp6=(int)((value-tmp)*1000000)%10;

Printf("f-value=%d.%d%d%d%d%d%d",tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6);

}

Int main(int argc, char const *argv[])

{

/* code */

Float a = 2.354954;

Printf("a = %f", a);

Printf_float(a);

PrintFloat(a);

Return 0;

}

Voltage Transformer

Our company`s current transformers have high precision,wide range,small volume and good linearity that can be used to the field of electronic watt-hour meter, electric energy metering, electronic detection.

Performance

1.Power frequency insulation strength:The insulation between the primary winding and the secondary winding and the ground part of the CT can bear 4kV power frequency voltage for 1minute

2.Interturn insulation strength:The secondary open circuit, the primary winding through the rated current 1min, no inter-turn damage in the transformer

3.The deviation is better than the industry standards and national standards

Voltage Transforvmer,Durable Voltage Transformer,New Voltage Transformer,Good Voltage Transformer,High-class Voltage Transformer

Anyang Kayo Amorphous Technology Co.,Ltd. , https://www.kayoamotech.com

Posted on