CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SprintF question ...

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

SprintF question ...
PostPosted: Tue Feb 03, 2009 1:45 pm     Reply with quote

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 Smile

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);

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 2:22 pm     Reply with quote

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
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 3:22 pm     Reply with quote

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.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 3:15 am     Reply with quote

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
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 7:55 am     Reply with quote

thanks guys Smile

boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 9:59 am     Reply with quote

how do I print only 3 decimals ??
I did the %3.2F thing but its not working ...
Ttelmah
Guest







PostPosted: Wed Feb 04, 2009 10:56 am     Reply with quote

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
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 5:14 pm     Reply with quote

thanks, its working fine now Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group