sprintf

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

sprintf

4,724 Views
Klayton
Contributor III

I have a quick (and probably dumb) question on sprintf for the kinetis MCU.  I am trying to use sprintf on a floating point number and have had trouble (yes I realize sprintf is huge when used on floats).  It works fine with integers but not floats.  What is the trick to get it to work? Here is an example of how I am formatting the string:

 

#include <stdlib.h>

#include <stdio.h>

 

float test = 10.123;

sprintf(tempString, "%2.1f", test);

 

When I have used sprintf with other MCU's (microchip) this would spit out "10.1" to my display. This prints out "%2.1f" when I use it with CodeWarrior / kinetis.  What am I missing?  I am using CodeWarrior 10.1 with all the latest updates/patches.  

 

Thanks!

0 Kudos
8 Replies

1,386 Views
SDame
Contributor I

Has this situation improved yet for CW10.2 and processor expert?  This seems like a capability that should be there for such a powerful processor as the Kinetis, or at least an option to include a special library.

 

I really need to be able to printf and sprintf float quantities without having to totally rewrite code that was probably written 30 years ago.

0 Kudos

1,386 Views
MarkP_
Contributor V

Hi,

There is an definition:

/*

** When MQX_INCLUDE_FLOATING_POINT_IO is defined as 1,

** _io_printf and _io_scanf will include floating point I/O code.

*/

#ifndef

MQX_INCLUDE_FLOATING_POINT_IO

#define

MQX_INCLUDE_FLOATING_POINT_IO 0

#endif

 

Set this to one in user_config.h.

 

But this eats lot of stack, in io_dopr.c:_io_doprint():

#if

MQX_INCLUDE_FLOATING_POINT_IO

charprec_set;

doublefnumber;

char fstring[324]; /* Floating point output string */

#endif

 

~Mark

 

0 Kudos

1,386 Views
alfredogualdron
Contributor III

I got the same problem, but I'm not using MQX, I'm using Processor Expert...
Someone could figure out how to use sprintf with float in processor expert??

0 Kudos

1,386 Views
alfredogualdron
Contributor III

I solved the issue and I posted Here

sprintf problem in MCF51QE128 and Codewarrior.


hope it helps

0 Kudos

1,386 Views
ConstYu
NXP Employee
NXP Employee

Here I have a modified printf.c code, it was modified from KL25' demo code, and can printf floating point number in 6 bit precision, also you can change it by adjusting the parameter-precision=n. !

Wish it can help you.

0 Kudos

1,386 Views
admin
Specialist II

Are you using the code that comes with the Kinetis example projects?  These contain custom printf and sprintf implementations in printf.c that does not include floating-point print support.  To print floating points you will need to use a c standard library that has been compiled with floating point enabled.

0 Kudos

1,386 Views
Klayton
Contributor III

I am not using any code from an example project.  The stdio.h header file i am including is in the ARM_EABI_Support\ewl\EWL_C\include directory.  I have no idea how to enable float for this header.  Thanks!

0 Kudos

1,386 Views
admin
Specialist II

Unfortunately, you can't enable floating point support with a few #defines in a header file.  Most printf implementations for embedded processors don't include floating point support in the c library to keep the size of the library down.  Printf is usually found in libc.a (the standard c library) for a c compiler, and you're going to have to recompile that library to enable floating point support.  The other option is to provide your own custom printf routine that implements floating point.  Check out newlib for an example, but I'll warn you ahead of time that you'll also need a working implementation of malloc and free if you want to use that one.

0 Kudos