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.

Usage of sprintf()



Hi, I'm try to implement a temperature logger by using LM4F232H5QD.

I followed the tutorial and sample code, I can successfully read the values from the external temperature sensor.

But when I want to convert the data type from "unsigned long" to "char" by using sprintf(), the operation will just stop.

May I ask whats wrong with my code? 

5327.hello.c

  •  

    Yun Kan Tsui said:
    convert the data type from "unsigned long" to "char" by using sprintf(),

    sprintf() doesn't actually convert data types.  sprintf() builds a string according to the format & parameters supplied.

    Yun Kan Tsui said:
    the operation will just stop

    No, it won't "just stop". You need to use the debugger to see what is actually happening!

    Is this the code in question:

    Yun Kan Tsui said:

    volatile unsigned long ulTempValueC;

    :
    :

    sprintf(temp_message, "Temperature = %u", ulTempValueC );

    I think you need to check the meaning of the %u format specifier...

  • Thanks Andy, I changed my code to "sprintf(temp_message, "%lu", ulTempValueC );"

    but the broad still stuck when this code was executed, when I comment this line of code out, the broad operate normally..

  • A common problem when using functions from the printf/sprintf/fprintf family is the stack size - these functions take a lot of stack.  Do you have enough allocated for your application?  For instance, temp_message is taking up 120 bytes of your stack space.

    I also note that temp_message is not only a local variable, but only has scope within the if statement.  This will cause the variable to be initialized again every time the if statement is true.  There's nothing particularly wrong with that if it's what you intended.

  • slandrum said:
    A common problem when using functions from the printf/sprintf/fprintf family is the stack size - these functions take a lot of stack.  

    Indeed - see: http://e2e.ti.com/support/microcontrollers/stellaris_arm_cortex-m3_microcontroller/f/474/t/227176.aspx

     

  • Thanks for your reply Slandrum. I solved this problem by a very weird method. I copied my code to one of the example folder "\boards\ek-lm4f232\sd_card", and the problem is gone. I have no idea why it works.. 

  • probably because your project settings were, somehow, bad - then, by moving, you "inherited" good project settings from the example.

    Stack size could be one such setting...