Convert float number to ASCII to transfer data via UART to other microcontroller

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

cross mob
Anonymous
Not applicable

 Hi there,

   

I would like to convert float number into ASCII or character or string in oder to send over UART. 

   

I tried sprintf and all other function available, but it doesn't work, when I check in teraterm or realterm software. 

   

So, I am looking for actual method that can convert float to ascii same as itoa. I got itoa method as below, which works fine

   

char* itoa(int val, int base){

   

 

   

static char buf[32] = {0};

   

 

   

int i = 30;

   

 

   

for(; val && i ; --i, val /= base)

   

 

   

buf = "0123456789abcdef"[val % base];

   

 

   

return &buf[i+1];

   

 

   

   

 

   

but, would like to make ftoa. In above method modulo of floating point doesn't work. So looking for some other method.

   

Any help would be great !!

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

You may use ftoa macro as the first approach.

   

Quite more sophisticated is to use sprintf() and use formatted conversion. You have to #include stdio.h and an explanation of the formatting you'll find here.

   

Check whether you are using latest Creator 3.2 version. In build settings use newlib_nano and allow for float formatting.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

 Thanks Bob. I am have included stdio.h, but sprintf isn't working, I have got Psoc creator 3.1. Is that reason for sprintf not working ?

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

My suggestions were for Creator 3.2

   

Creator 3.1 has quite different settings to have sprintf running. I would suggest you to update to 3.2, there have been corrections for some components, error corrections and new components.

   

 

   

Bob

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

In 3.1 to get it to support sprintf and floats -

   

 

   

    

   

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

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 I have updated PSoc creator to 3.2 and changed linker build settings as you have suggested. 

   

 I did add these librarires

   

#include <stdio.h>

   

#include <stdlib.h>

   

#include <string.h>

   

But, sprintf function isn't working.My code is as follow:

   

char string[50];

   

float number = 1.23;

   

sprintf(string, "%f", number); 

   

UART_PutString(string);

   

UART_PutString("\n");

   

I have attached screenshot of realterm.

   

I did try to use ftoa function, but it showing following error :

   

implicit declaration of function 'ftoa' is invalid in c99

   

 

   

Any help would be great 

   

Thanks

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

Take a look at this, especially heap size as well entry -

   

 

   

www.cypress.com/

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 I found this function to convert float into ascii and it works well.

   

char *ftoa(float f)

   

{

   

    static char buf[17];

   

    char *cp = buf;

   

    unsigned long l, rem;

   

 

   

    if(f < 0) {

   

        *cp++ = '-';

   

        f = -f;

   

    }

   

    l = (unsigned long)f;

   

    f -= (float)l;

   

    rem = (unsigned long)(f * 1e6);

   

    sprintf(cp, "%lu.%3.3lu", l, rem);

   

    return buf;

   

}

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

AK B,

   

this function is based on same 'sprintf()', which you tried to get rid of. Isn't it kind of pointless?

   

By replacing sprintf() with concatenated itoa(int) + '.' + itoa(frac),  and removing reference to sprintf() you will speed up conversion ~20 times, and free ~20% of flash. 

   

odissey1

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

Attached project works on a CY8CKIT-059 with PuTTY.

   

Changes made to default settings:

   
        
  • Heap increased to 0x200
  •     
  • Use newlib-nano float formatting
  •    
   

Happy coding

   

Bob

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

Yes, and the project...

   

 

   

Bob

0 Likes