View previous topic :: View next topic |
Author |
Message |
boily
Joined: 27 Jan 2009 Posts: 19
|
SprintF question ... |
Posted: Tue Feb 03, 2009 1:45 pm |
|
|
I'm trying to do a sprintf and I'm only getting constent 0.00000 reading from my analog imput....
What am I doing wrong ??
Is there another way to print decimals on my LCD ?
Thanks
Code: |
int8 x;
float g;
char msg [10];
char espace [21];
x = read_adc();
g = (x-110)*(1/36);
sprintf(msg, "%f", g);
strcpy(espace, " ");
strcat(msg, espace);
delay_ms(500);
ecrire_chaine(msg);
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 03, 2009 2:22 pm |
|
|
Make and post a full test program (meaning, with #include, #fuses,
etc). You can test this problem with two lines of code in main().
Get rid of all the escrite stuff and run the test program in MPLAB simulator
and display the output with printf in the Output window. You can test the
whole thing. You can experiment with it. You can try different width and
precision values for the "%f". You can play around with it until you make
it work.
Sample of short test program with link to instructions on using UART1
in MPLAB with the MPSIM simulator to test a program's output:
http://www.ccsinfo.com/forum/viewtopic.php?t=36961&start=1
How to display floats on an LCD:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1 |
|
![](templates/subSilver/images/spacer.gif) |
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Tue Feb 03, 2009 3:22 pm |
|
|
Remember, when doing arithematic in C that you must think like the compiler/processor. Look at the line where you assign a calculated value to g. First, x is an int8 so it can only have values from 0 to 255. What happens if you subtract 110 from a value that is less than 110? Also since you are doing integer math, what is 1/36? The answer to the second question should answer your question.
To achieve the results that you are hoping for, you will need to learn about "casting". Do a search on this. |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Feb 04, 2009 3:15 am |
|
|
ptr=strcat (s1, s2)
Concatenate s2 onto s1
Code: |
int8 x;
float g;
char msg [10];
char espace [21];
x = read_adc();
g = (x-110)*(1/36);
sprintf(msg, "%f", g);
strcpy(espace, " ");
strcat(msg, espace);
delay_ms(500);
ecrire_chaine(msg);
|
s1 (msg) is 10 chars that is 9 chars + the null termination char '0'
s2 espace is 21 chars, 19 space + null = 20 (OK)
s1 + s2 = 29 chars. buffer overrun. Your msg array is not big enough!
Also, are you sure sprintf(msg, "%f", g); will produce a string of less than 10 chars ? you may be better fixing the size:-
sprintf(msg, "%6.2f", g); // "nnnnnn.nn\0" 10 chars |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Wed Feb 04, 2009 7:55 am |
|
|
thanks guys
![](http://i43.tinypic.com/212s2z4.jpg) |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Wed Feb 04, 2009 9:59 am |
|
|
how do I print only 3 decimals ??
I did the %3.2F thing but its not working ... |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Feb 04, 2009 10:56 am |
|
|
You'd want something like %6.3f The last digit is the 'number of decimals', but the first is the 'total field width'. Your number can have a digit in front of the decimal, and at times a - sign, and there is the decimal itself. Hence the total field needs to be 3 more than the number of decimals.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Wed Feb 04, 2009 5:14 pm |
|
|
thanks, its working fine now ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|