How to fix printf(%f) issue in CW 10.6 with MCU MKE06Z ?

cancel
Showing results for 
Search instead for 
Did you mean: 

How to fix printf(%f) issue in CW 10.6 with MCU MKE06Z ?

1,450 Views
eyeow84
Contributor III

Hello,

 

I am kinda beginner for CW 10.6 and C programming. My target mcu is MKE06Z128VLK4 on FRDM-KE06Z. I found there is a problem when i want to use the printf(%f) to show the temperature reading in 2 decimal points. Here is the scenario: 

 

crop from my codes:

printf("Temperature sensor conversion value:%f\n",ADC_PollRead(ADC,ADC_CHANNEL_AD12));

printf("Temperature sensor conversion value:%f\n",12.18);

printf("Temperature sensor conversion value:%.2f\n",c);

printf("Temperature sensor conversion value:%.2f\n",result); 

 

crop output from the hyperterminal:

Temperature sensor conversion value:f
Temperature sensor conversion value:f
Temperature sensor conversion value:f
Temperature sensor conversion value:f

 

I had read through some posts from the community and other tutorials. Is it due to some common library like stdio.h or float.h doesn't get installed/populated in CW? Thus printf(%f) is not recognized correctly in this environment?

 

How to fix this issue? Thank you in advance!

Labels (1)
6 Replies

788 Views
michael_galda
NXP Employee
NXP Employee

Hi,

I am reading this again.

Now, it is a bit clearer for me, that you want to use "printf" function for writing to the console / terminal.

As you are normally using the same for the PC programs.

But I think, that the printf implemented / used in CW10.6 doesn't convert float numbers to characters.

My recommended workaround is:

1. Use "sprintf" function at first to format the strings at first. This should work in all cases

2. Then use "printf" for printing this created string.

Thanks,

Michael

788 Views
eyeow84
Contributor III

Hi Michael,

Thanks for your suggestion to use sprintf. I think it should be working fine for my case, that i need to put in extra lines of code to output floating point at hyperterminal.

crop from my codes:

char str[100];
float cal = 12.345678;
int d1 = cal; // Get the integer part
float f2 = cal - d1; // Get fractional part
int d2 = trunc(f2*10000); // Turn into integer
float f3 = f2*10000 - d2; // Get next fractional part
int d3 = trunc(f3*10000); // Turn into integer
printf("d1:%d\n",d1);
printf("d2:%d\n",d2);
printf("d3:%d\n",d3);
sprintf(str, "calA = %d.%04d%04d\n",d1,d2,d3);
puts(str);
printf("cal float:%f\n",cal);

 

crop output from the hyperterminal:

d1:12
d2:3456
d3:7832
calA = 12.34567832
cal float:f

I can conclude that actually float is still recognized, just that printf(%f) is not recognized and always show value f as output. Of course welcome if any of you manage to advice the most appropriate method to enable printf(%f) function in CW 10.6 with MCU MKE06Z it would be great.

Thanks...

0 Kudos

788 Views
michael_galda
NXP Employee
NXP Employee

Hi Perhaps the issue is caused by the library settings in the project properties.

Go to:

Prj. Properties

C/C++ build -> Settings

Librarian

Tool Settings tab

See the library configuration listboxes in the window

Select the printf / scanf formats

By default it is "int"

Try to Change to FP or LL

Rebuild all

/Michael

0 Kudos

788 Views
eyeow84
Contributor III

Hi Michael,

Appreciate your advice. Unfortunately it doesn't work for my case. I have tried all the possible options under Print formats and Scan formats and the problem still persists...screenshot.jpg

0 Kudos

788 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

I just made a quick test from my side with CW10.7 and k60dn512 board, floating number can be printed well to terminal.

see my attached video and demo code.


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

788 Views
eyeow84
Contributor III

Hi Zhang,

Thanks for your reply and appreciate your shared example with demo clip. I think the error message attached should b expected as i don't have k60dn512 with me right now, right?error screen shot.jpg

I am not sure what is missing right now? Or is it due to CW 10.6 configuration setting? Changing the library settings in the project properties seems like doesn't help for my case. 

The sample code is modified from FRDM-KEXX Driver Library Package. So far I had tried on three of those demo codes below like ADC_poll_demo, ADC_int_demo and ADC_FIFO_demo, the same problem persists when I am trying to use printf(%f) function at console.

Appreciate if there is any other advice...

0 Kudos