Fail to print floating/double number using sprintf

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 Hi all,

   

I got a long codes which used to calculate some statistical parameters using PSoC MCU. To reserve precision, I tried to print the result using XBee through an UART interface. However, the PSoC doesn't print any floating or double character to the XBee. 

   

Since it is quite a long code, I will only extract the part which might cause the problem.

   

 ================================================================================

   

#include <device.h>

   

#include <string.h>

   

#include <stdio.h>

   

#include <stdlib.h>

   

#include <math.h>

   

//..............Many other codes.............

   

char DataBuffer[50] = {'\0'};

   

int16 array_ADC2[8192]; //Channel for ADC

   
    //..............Many other codes including ADC.............   
   

    //Mean

   

    for(i=0; i<8192; i++)

   

        mean += ((double)array_ADC2)/8192;

   

    sprintf(DataBuffer, "Mean: %f\r\n",mean);

   

    UART_PutString(DataBuffer);

   

 ================================================================================

   

However, one the UART interface, it onlyshows "Mean: " without printing out the double-typed variable. I tried to print it with int and it actually works so perhaps there is some configuration/coding that I missed out?

   

Thank you very much.

0 Likes
6 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Not knowing what version of Creator you are using the nano library had

   

to be enabled for psoc -

   

 

   

    

   

          

   

http://www.cypress.com/?id=4&rID=87354 newlib-nano     -u_printf_float     command line linker

   

 

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 Hi Dana,

   

Thanks for the reply. Currently I am using PSoC Creator  3.0 SP2 (3.0.0.3140).

   

*******Update********

   

Thanks for the article! Now it works like a charm! 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Glad I could help.

   

 

   

Regards, Dana.

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 Ha, the old printf problem again.

   

I think a little button on the IDE tu "Use Nano" or Not would save a lot of time.

   

By the way, is double the preferred variable to use instead of float? Saw a post somewhere suggesting double should be preferred over float type?

0 Likes
Anonymous
Not applicable

Hi,

   

I am using PSOC 4.0, trying to print values out of the code below

   

double x, y, result;

   

char  dataPrint[50];

   

  x = -10.0;

   

  y = 10.0;

   

  result = atan2 (y,x) * 180 / M_PI;

   

sprintf(dataPrint, "result %lf \r\n", result) ;
UART_DEB_UartPutChar(dataPrint);

   

I am not able to print the float/double values. I have followed instructions on the link mentioned above http://www.cypress.com/?id=4&rID=87354 and also increased the heap size. please let me know if I am missing on anything.

   

Thank you,

   

Likhitha

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Can you please post your complete project or a shortened version that shows the error so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes