| Author |
Message |
|
|
Posted: Apr 17, 2012 - 08:47 PM |
|

Joined: Mar 22, 2012
Posts: 15
|
|
I am trying to send a float out of my Atmel's UART, meaning I need to convert the float to ASCII. I am aware of only one "standard" way to do this: sprintf or snprintf. However, I can't seem to get anything useful out of that function. All I'm getting is one character: ?. It really is an ASCII ?, as I've checked the hex value.
Code:
char i = 0;
double temp = 123.456;
snprintf(buffer, 10, "#f", temp);
while(buffer[i] != 0)
{
// Wait for transmit buffer to be ready
while(((UCSR1A & (1 << UDRE)) == 0);
// Transmit character
UDR1 = buffer[i];
i++;
}
buffer is defined as char buffer[10] in the header file, if that's relevant.
When I check the return value of the above snprintf, I get 1. I've looked at countless forums, man pages, etc., but to no avail.
(The #f, instead of percent-f, is due to the "Bad Request" issue with posting new threads) |
|
|
| |
|
|
|
|
|
Posted: Apr 17, 2012 - 09:17 PM |
|


Joined: Feb 19, 2001
Posts: 25923
Location: Wisconsin USA
|
|
Most AVR C toolchains will have a variety of "levels" of xxxprintf(), each with more capabilities--and larger code size. Find the selector for your toolchain.
Quote:
I am aware of only one "standard" way to do this: sprintf or snprintf.
Your toolchain might provide ftoa() or dtoa() or dtostr() or ... |
|
|
| |
|
|
|
|
|
Posted: Apr 17, 2012 - 09:40 PM |
|


Joined: Mar 27, 2002
Posts: 18589
Location: Lund, Sweden
|
|
If this is re the avr-gcc tool chain (as supplied in the WinAVR package and recent versions of AVR/Atmel Studio) then there is documentation on how to get floating point support working for printf and friends here.
And if so, then hopefully a MODERATOR will move this to the avr-gcc forum, since this is specific for that tool chain. (Not that we lack previous answers to this question there.. ) |
|
|
| |
|
|
|
|
|
Posted: Apr 17, 2012 - 09:57 PM |
|

Joined: Mar 22, 2012
Posts: 15
|
|
| I apologize for the redundancy and misplacement of this post, but thank you for your helpful answers anyway. |
|
|
| |
|
|
|
|
|
Posted: Apr 17, 2012 - 10:08 PM |
|


Joined: Feb 09, 2012
Posts: 223
|
|
I also get '?', since floating point is not supported for the sprintf function by the avr-gcc tool chain.
Use the dtostrf function. |
|
|
| |
|
|
|
|
|
Posted: Apr 17, 2012 - 10:15 PM |
|

Joined: Mar 22, 2012
Posts: 15
|
|
| Yep, that works, and is apparently a little faster than sprintf. Many thanks. |
|
|
| |
|
|
|
|
|
Posted: Apr 17, 2012 - 10:53 PM |
|


Joined: Jul 18, 2005
Posts: 62359
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Kun.io wrote:
I also get '?', since floating point is not supported for the sprintf function by the avr-gcc tool chain.
Use the dtostrf function.
Don't spread misinformation just because you didn't read the user manual.
Of course sprintf in avr-gcc supports %f if you override vprintf and link against libprintf_flt.a (just as the user manual tells you). If you are using float then also link against libm.a too. |
_________________
|
| |
|
|
|
|
|
Posted: Apr 17, 2012 - 11:56 PM |
|


Joined: Feb 09, 2012
Posts: 223
|
|
|
Quote:
if you override vprintf and link against libprintf_flt.a (just as the user manual tells you). If you are using float then also link against libm.a too
I tried that, but could not do it. Too complicated |
|
|
| |
|
|
|
|
|
Posted: Apr 18, 2012 - 12:33 AM |
|


Joined: Feb 19, 2001
Posts: 25923
Location: Wisconsin USA
|
|
|
Quote:
I tried that, but could not do it. Too complicated
I tried to milk a cow following instructions and demonstrations. I found it too complicated therefore it cannot be done. |
|
|
| |
|
|
|
|
|
Posted: Apr 18, 2012 - 08:34 AM |
|


Joined: Jul 18, 2005
Posts: 62359
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Kun.io wrote:
Quote:
if you override vprintf and link against libprintf_flt.a (just as the user manual tells you). If you are using float then also link against libm.a too
I tried that, but could not do it. Too complicated
Yeah it's real scary stuff - you have to edit a whole two lines - sheesh! |
_________________
|
| |
|
|
|
|
|
Posted: Apr 18, 2012 - 08:54 AM |
|


Joined: Feb 09, 2012
Posts: 223
|
|
| I have now this:
Code:
avr-gcc -Wa,-ahl=outfile.s -g -Os -fwhole-program -fpack-struct -fno-inline-small-functions -mmcu=atmega8 -combine -c main.c -c delay.c -c eemem.c
avr-gcc -g -mmcu=atmega8 -Wl,--gc-sections -Wl,--relax -o main.elf main.o
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
So, how do I milk this cow to get floating point in sprintf ? |
|
|
| |
|
|
|
|
|
Posted: Apr 18, 2012 - 09:02 AM |
|


Joined: Jul 18, 2005
Posts: 62359
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
I'm typing this on an Android phone so don't have easy access right now but on your second line there, the one with avr-gcc but no -c you need to add towards the start of the line -Wl, -u, vprintf and towards the end -lm -lprintf_flt
If it's not vprintf try vfprintf |
_________________
|
| |
|
|
|
|
|
Posted: Apr 18, 2012 - 09:07 AM |
|


Joined: Mar 27, 2002
Posts: 18589
Location: Lund, Sweden
|
|
Kun.io! Have you read the documentation that I linked to?
It is a matter of throwing out the default vfprintf, linking in the library libprinf_flt.a that has the float version of vfprintf, and you also need the math library (libm.a). So the documentation explicitly tells you to add these linker switches:
Quote:
-Wl,-u,vfprintf -lprintf_flt -lm
|
|
|
| |
|
|
|
|
|
Posted: Apr 18, 2012 - 09:49 AM |
|


Joined: Feb 09, 2012
Posts: 223
|
|
JohanEkdahl, yes I read your link to the header file, but couldn't see the forest for the trees.
It's working! Thank you all!
This is my linker line:
Code:
OPTIMIZE_OPTIONS="-Wl,--gc-sections -Wl,--relax"
avr-gcc -Wl,-u,vfprintf -g -mmcu=atmega8 $OPTIMIZE_OPTIONS -lprintf_flt -lm -o main.elf main.o
The code size increased by 1510 bytes.
With the default libaries, the dtostrf increases the code by 828 bytes.
The people at Arduino have also problems with this question mark for sprintf. http://arduino.cc/forum/index.php/topic,100374.0.html
Why do they have these troubles, if it can be fixed. |
|
|
| |
|
|
|
|
|
Posted: Apr 18, 2012 - 11:36 AM |
|


Joined: Mar 27, 2002
Posts: 18589
Location: Lund, Sweden
|
|
|
Quote:
The people at Arduino have also problems with this question mark for sprintf. http://arduino.cc/forum/index.php/topic,100374.0.html
Why do they have these troubles, if it can be fixed.
You'll have to ask them. I speculate that it is not more than a matter of changing something very trivial in the Arduino software framework and perhaps rebuild it. Also, the last post in the thread you link to makes it clear that it works for streams, so they are perhaps going with that instead.
I'm sure it is as possible with Arduino as it is with "plain avr-gcc". The fact that some Arduino users have problems with floats and printf's is not an indicator that it is not possible. Don't equal "somewhat complicated" with "not possible". |
|
|
| |
|
|
|
|
|
Posted: Apr 18, 2012 - 04:28 PM |
|


Joined: Sep 06, 2007
Posts: 129
|
|
Misinformation heaped upon misinformation. Does no one ever read a user manual any more? Arduino uses avr-gcc and using exactly the same technique you just found to work successfully it will be possible to make floats print as other than '?' there too. The only complexity in Arduino is how to modify LDFLAGS. But the people behind Arduino are very clever and I doubt they have actually precluded the ability to do this. In fact, since nothing else in the Arduino code cares about efficiency I'm very surprised they didn't just make libprintf_flt.a the default anyway.
When I get back to my computer I'll see what you have to change in Arduino to make this happen.
Cliff (aka clawson above) |
|
|
| |
|
|
|
|
|
Posted: Apr 19, 2012 - 03:58 AM |
|

Joined: Dec 18, 2001
Posts: 4717
|
|
Love it! He couldn't do it so it is impossible.
Never mind that zillions have. |
|
|
| |
|
|
|
|
|