To complement solution how to printf float number in MCUXpresso IDE

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

To complement solution how to printf float number in MCUXpresso IDE

5,503 Views
daweiyou
NXP Employee
NXP Employee

Hi:

 

In previous topics there are some discussions how to print float number in MCUXpresso IDE and SDK.

To try them and summarize the final solution:

1.PRINTF the float number to UART console

set below macro in project configuration->C/C++ build ->setting

PRINTF_FLOAT_ENABLE=1

such code will work.

float test1 = 0.15;

PRINTF("%f\r\n",test1);

2.Transform float number to string by sprintf() function.

there is one error in SDK user manual, " Ensure Redlib: Use floating point version of printf is selected
" during project creation does not work.

The default C library Redlib doesn't support floating, so it couldn't work with redlib.

daweiyou_0-1647929545419.png

 

The correct solution are:

(1)Change link library to NewLib, it's full C library and support float  printf.

But notice need include <stdio.h> in related c file, or else sprintf(float) doesn't work as expected.

daweiyou_1-1647929662327.png

(2)Change link library to NewLib Nano, it's compact C library , and need click "enable print float" to enable float function, which actually add " -u _printf_float" link symbol.

But notice need include <stdio.h> in related c file, or else sprintf(float) doesn't work as expected.

daweiyou_2-1647929807476.png

 

So the solution surely add flash & RAM consumption in project, but for i.MXRT series it's not problem.

Attach is one example for RT1020 EVK.

 

 

 

 

 

2 Replies

112 Views
Nikolai_Baschinski
Contributor I

Thank you! That solved my problem. 

0 Kudos
Reply

5,471 Views
PabloAvalos
NXP TechSupport
NXP TechSupport

Hi @daweiyou 

 

Thank you so much for your contribution. The information was pretty helpful and it might be helpful for many people.

 

Thank you again.

Best Regards.
Pablo Avalos.

0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-1431699%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ETo%20complement%20solution%20how%20to%20printf%20float%20number%20in%20MCUXpresso%20IDE%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1431699%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%3A%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3EIn%20previous%20topics%20there%20are%20some%20discussions%20how%20to%20print%20float%20number%20in%20MCUXpresso%20IDE%20and%20SDK.%3C%2FP%3E%0A%3CP%3ETo%20try%20them%20and%20summarize%20the%20final%20solution%3A%3C%2FP%3E%0A%3CP%3E1.PRINTF%20the%20float%20number%20to%20UART%20console%3C%2FP%3E%0A%3CP%3Eset%20below%20macro%20in%20project%20configuration-%26gt%3BC%2FC%2B%2B%20build%20-%26gt%3Bsetting%3C%2FP%3E%0A%3CP%3EPRINTF_FLOAT_ENABLE%3D1%3C%2FP%3E%0A%3CP%3Esuch%20code%20will%20work.%3C%2FP%3E%0A%3CP%3Efloat%20test1%20%3D%200.15%3B%3C%2FP%3E%0A%3CP%3EPRINTF(%22%25f%5Cr%5Cn%22%2Ctest1)%3B%3C%2FP%3E%0A%3CP%3E2.Transform%20float%20number%20to%20string%20by%20sprintf()%20function.%3C%2FP%3E%0A%3CP%3Ethere%20is%20one%20error%20in%20SDK%20user%20manual%2C%20%22%20%3CSPAN%20class%3D%22fontstyle0%22%3EEnsure%20Redlib%3A%20Use%20floating%20point%20version%20of%20printf%20is%20selected%3C%2FSPAN%3E%20%3CBR%20%2F%3E%22%20during%20project%20creation%20does%20not%20work.%3C%2FP%3E%0A%3CP%3EThe%20default%20C%20library%20Redlib%20doesn't%20support%20floating%2C%20so%20it%20couldn't%20work%20with%20redlib.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22daweiyou_0-1647929545419.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22daweiyou_0-1647929545419.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F174349i8D8DD62A0480DE0C%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22daweiyou_0-1647929545419.png%22%20alt%3D%22daweiyou_0-1647929545419.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3EThe%20correct%20solution%20are%3A%3C%2FP%3E%0A%3CP%3E(1)Change%20link%20library%20to%20NewLib%2C%20it's%20full%20C%20library%20and%20support%20float%26nbsp%3B%20printf.%3C%2FP%3E%0A%3CP%3EBut%20notice%20need%20include%20%3CSTDIO.H%3E%20in%20related%20c%20file%2C%20or%20else%20sprintf(float)%20doesn't%20work%20as%20expected.%3C%2FSTDIO.H%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22daweiyou_1-1647929662327.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22daweiyou_1-1647929662327.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F174350i78FA8A38E9F3B7D1%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22daweiyou_1-1647929662327.png%22%20alt%3D%22daweiyou_1-1647929662327.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E(2)Change%20link%20library%20to%20NewLib%20Nano%2C%20it's%20compact%20C%20library%20%2C%20and%20need%20click%20%22enable%20print%20float%22%20to%20enable%20float%20function%2C%20which%20actually%20add%20%22%20-u%20_printf_float%22%20link%20symbol.%3C%2FP%3E%0A%3CP%3EBut%20notice%20need%20include%20%3CSTDIO.H%3E%20in%20related%20c%20file%2C%20or%20else%20sprintf(float)%20doesn't%20work%20as%20expected.%3C%2FSTDIO.H%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22daweiyou_2-1647929807476.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22daweiyou_2-1647929807476.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F174352iB3C9D221400C3091%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22daweiyou_2-1647929807476.png%22%20alt%3D%22daweiyou_2-1647929807476.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3ESo%20the%20solution%20surely%20add%20flash%20%26amp%3B%20RAM%20consumption%20in%20project%2C%20but%20for%20i.MXRT%20series%20it's%20not%20problem.%3C%2FP%3E%0A%3CP%3EAttach%20is%20one%20example%20for%20RT1020%20EVK.%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2387948%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20To%20complement%20solution%20how%20to%20printf%20float%20number%20in%20MCUXpresso%20IDE%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2387948%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EThank%20you!%20That%20solved%20my%20problem.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1435737%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20To%20complement%20solution%20how%20to%20printf%20float%20number%20in%20MCUXpresso%20IDE%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1435737%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F54205%22%20target%3D%22_blank%22%3E%40daweiyou%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3EThank%20you%20so%20much%20for%20your%20contribution.%20The%20information%20was%20pretty%20helpful%20and%20it%20might%20be%20helpful%20for%20many%20people.%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3EThank%20you%20again.%3C%2FP%3E%0A%3CP%3EBest%20Regards.%3CBR%20%2F%3EPablo%20Avalos.%3C%2FP%3E%3C%2FLINGO-BODY%3E