- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 !!
- Labels:
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content