View previous topic :: View next topic |
Author |
Message |
betomax
Joined: 20 Aug 2009 Posts: 4
|
print using %f |
Posted: Thu Aug 20, 2009 12:11 pm |
|
|
Hi
I have to send a float number [voltage_var variable contents] over rs232 using the next print sentence:
Code: |
float voltage_var;
.
printf("Current voltage =%2.2f\r\n",voltage_var);
.
|
It works but I need my output string always same length, I mean if I have
1 in voltage var then I need to send "01.XX" NOT "1.XX" as actually it does !
How can I achieve this ?
Thanks by any help ! |
|
|
betomax
Joined: 20 Aug 2009 Posts: 4
|
|
Posted: Thu Aug 20, 2009 12:31 pm |
|
|
I tried also:
Code: | printf("Current voltage =%02.2f\r\n",voltage_var); |
but still sending "Current voltage = 1.XX" |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 20, 2009 12:41 pm |
|
|
Make a test program and experiment with it. The first number in "%2.2"
is the minimum width of the printf output. The 2nd number is the number
of digits to the right of the decimal point. You can also put a "0" in front
of the first digit to zero-fill it on the left side, where applicable.
Take the code below, and change the width specifier (the first number)
until you get what you want. Use the "UART1" feature of the MPLAB
simulator to easily do these experiments without having to program
a hardware board each time.
Code: |
#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main()
{
float voltage_var = 1.23;
printf("Current voltage =%2.2f\r\n",voltage_var);
while(1);
} |
See this post for instructions on how to use the "UART1" feature of the
MPLAB simulator to display serial output in the MPLAB Output Window:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&start=1 |
|
|
betomax
Joined: 20 Aug 2009 Posts: 4
|
|
Posted: Thu Aug 20, 2009 1:11 pm |
|
|
PCM programmer wrote: | You can also put a "0" in front
of the first digit to zero-fill it on the left side, where applicable.
|
Thanks by reply, and yes actually I'm trying under mplab sim, but I don't understand what's wrong !
with
Code: | printf("Current voltage =%05.2f\r\n",voltage_var); |
I got finally
Code: | Current voltage = 01.23 |
BUT it doesn't work when the voltage var is negative, I set voltage variable to -1.23 and then mplab outputs is:
Code: | Current voltage = -1.23 |
I can deal with this comparing before printing, but it is a good idea ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 20, 2009 1:19 pm |
|
|
It works if you change the field width to "6". Then you get -01.23
but if you want a constant field width, you will have to check if the
value is negative before calling the appropriate printf statement. |
|
|
betomax
Joined: 20 Aug 2009 Posts: 4
|
|
Posted: Thu Aug 20, 2009 1:53 pm |
|
|
Quote: | you will have to check if the
value is negative before calling the appropriate printf statement. |
Is exactly what I did, Thanks it works perfect !!! |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 20, 2009 3:07 pm |
|
|
Also, a search would have found 'why' the original format didn't work.
In C (not just CCS), the number in front of the decimal point, is the _total field width_. Then the number after the decimal, is the digits to be placed after the decimal, _in this field_. The decimal, is included in the field width calculations.
Hence the need for the 05, to handle 2 digits in front of the decimal, and two after.
Best Wishes |
|
|
minoan_war Guest
|
|
Posted: Wed Sep 09, 2009 9:31 am |
|
|
good point Ttelmah |
|
|
|