This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[SOLVED] sprintf to format float

hi,

I have tried to convert a float to string using sprintf, but it hangs my application.

I read so many post about this issue, but I haven't understand at all, so I ask again:

does sprintf works with floating point numbers?

if not how con I convert from float to string?

best regards

  • mastupristi said:
    I read so many post about this issue

    So you should have gathered some basic issues:

    • floating-point is often best avoided on embedded microcontrollers - so, by default, it is likely that floating-point support is not included.
    • sprintf (and printf) can be very large functions, so some toolchains give you configuration options to limit them - are you sure that you have the correct option(s) selected?
    • sprintf (and printf) can use a significant amount of stack - your toolchain's default may not be sufficient.

    mastupristi said:
    does sprintf works with floating point numbers?

    Yes, but you must pay attention to the implementation details - these will be in the manuals for your toolchain.

    You omitted to say what toolchain you're using.

    mastupristi said:
    if not how con I convert from float to string?

    If you're using floating-point, then code-size can't be an issue for you - so sprintf will most likely be the best way for you to go.

     

  • Andy Neil said:
    floating-point is often best avoided on embedded microcontrollers - so, by default, it is likely that floating-point support is not included.

    that's true, but LM4F has Floating point support in hardware so i thought I could use them.


    Andy Neil said:
    You omitted to say what toolchain you're using.

    ops... I use CCS5


    best regards

  • mastupristi said:
    LM4F has Floating point support in hardware so i thought I could use them

    Of course you can - but that's not the point.

    My point was that FP support may well not be enabled by default; in which case,  you will need to explicitly enable it before you can use it - have you checked that?

  • Andy Neil said:
    My point was that FP support may well not be enabled by default; in which case,  you will need to explicitly enable it before you can use it - have you checked that?

    I'm not absolutely shure:

    the compile commend line is:


    "C:/ti/ccsv5/tools/compiler/tms470/bin/cl470" -mv7M4 --code_state=16 --float_support=FPv4SPD16 --abi=eabi -me -O0 -g --include_path="C:/ti/ccsv5/tools/compiler/tms470/include" --include_path="C:/StellarisWare/boards/ek-lm4f232/timers/ccs/../../../../boards/ek-lm4f232" --include_path="C:/StellarisWare/boards/ek-lm4f232/timers/ccs/../../../.." --gcc --define=ccs --define=PART_LM4F232H5QD --define=TARGET_IS_BLIZZARD_RA1 --diag_warning=225 --gen_func_subsections=on --optimizer_interlist --printf_support=full --ual --preproc_with_compile --preproc_dependency="timers.pp"  "../timers.c"
    'Finished building: ../timers.c'


    what I believe to be the right options are highlithed in red.

    Is that right? Are some other options missing?

    Maybe the issue is about stack size, how can I increase it?


    best regards

  • Solved.

    I found the stack setting and increase it from 1024 to 1524, and now works

    thank you

    best regards

  • #include <msp430.h>

    #include <stdio.h> //////////////!!!!!!!!!!!!!!!

    char value[20];

    unsigned char x;

    sprintf(value,"%d ", x);
    glcd_text57(10, 30, value ,1,1);

    enjoy....

  • 1980best said:

    #include <msp430.h>

    #include <stdio.h> //////////////!!!!!!!!!!!!!!!

    char value[20];

    unsigned char x;

    sprintf(value,"%d ", x);
    glcd_text57(10, 30, value ,1,1);

    enjoy....

    I'm not sure what question this is trying to either ask or answer, especially since it is being added to an answered and basically stale thread.  In addition, the code fragment suggests it's perhaps intended for one of the MSP430 forums.