Join our technical discussions about Freescale Microcontrollers: M68HC12. (Freescale Semiconductor is a Subsidiary of Motorola).
how to print floats to serial port? - me...@madteckhead.dreamhosters.com - Apr 12 4:49:56 2008
Ok, this is probably something I will look back on and think "what the hell was I
thinking, that was so obvious".
But at the moment I can't figure out how to print a float to a serial port. This is for a
university course. We have been given a function Putchar(), which will print out a given
character. So in order to print a float (in decimal) I have to get each 10's place
individually and call Putchar() with the resulting value plus the ascii offset. Only
problem is that you can't mod or bitshift a float to determine the 10's info.
So what is an easy way to print a float to a serial port as characters? I have to right
my own functions, but would appreciate knowing about how to use libraries to achieve
this.
Many thanks,
Nathan
------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )
Re: how to print floats to serial port? - Petrescu - Apr 14 0:48:08 2008
Use sprintf() to a character buffer then use putchar() or printf() directly,
depends on your development tools.
Ioan
m...@madteckhead.dreamhosters.com wrote:
>
> Ok, this is probably something I will look back on and think "what the
> hell was I thinking, that was so obvious".
>
> But at the moment I can't figure out how to print a float to a serial
> port. This is for a university course. We have been given a function
> Putchar(), which will print out a given character. So in order to
> print a float (in decimal) I have to get each 10's place individually
> and call Putchar() with the resulting value plus the ascii offset.
> Only problem is that you can't mod or bitshift a float to determine
> the 10's info.
>
> So what is an easy way to print a float to a serial port as
> characters? I have to right my own functions, but would appreciate
> knowing about how to use libraries to achieve this.
>
> Many thanks,
>
> Nathan
>
>
------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )
Re: how to print floats to serial port? - "No.Spam" - Apr 16 2:41:21 2008
Ok. Thanks very much for your replys. I was trying not to use sprintf.
But out of curiosity how do I include the sprintf library? (stdio.h?) Is
there a special set of libraries for the embedded devices? Or is it just
a standard gnu library? (A pointer to a how to would be appreciated!)
I was actually trying to write an algorithm that converted a float to
chars. Perhaps its just easier to include the sprintf function. Is there
much overhead in the sprintf function?
Cheers,
Nathan
Benito Aquino wrote:
> What about
>
> void PrintFloat (float fpNumber)
> { int i;
> char LocBuffAscii[25];
>
> char LocBuffAScii[0]= 0x00; // Buffer Initialitation
>
> sprintf (LocBuffAscii,"%+10.2f", fpNumber); // Generates one
> zero ended string about the value fpNumber.. ej 123.45
>
> while (LocBuffASci[i])
> { putchar (LocBuffAscii[i];
> }
>
> }
>
>
------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )