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 CCS Technical Support

problem with displaying float number on LCD. ASAP!!!

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



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

problem with displaying float number on LCD. ASAP!!!
PostPosted: Tue Jan 29, 2013 6:05 am     Reply with quote

I have to display .0001 in LCD. But when i print a float number, it is printed as 0.0001. For my case, whole number not to be display on LCD. I'm using 8x1 LCD display. Is there any possible solution to print as .0001 on LCD??

Thanks in advance Smile
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jan 29, 2013 6:09 am     Reply with quote

show your code.
odds on that you are not FORMATTING correctly.
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Tue Jan 29, 2013 6:14 am     Reply with quote

It displays 0.0001 in LCD. What i have to change?

Code:
float i = .0001;

void main()
{
lcd_init();
while(1)
{
printf(lcd_data,"%.4f",i);
delay_ms(1000);
}
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Tue Jan 29, 2013 8:25 am     Reply with quote

Question.
Is your number always going to be less than 1?.
A leading zero, is standard in C. Many more complex C's support printf functions to suppress this, but CCS doesn't.
You can do something like:
Code:

float i = .0001;

void main(void) {
   char buff_string[10];
   lcd_init();
   while(TRUE) {
      if (i<1.0) {
         sprintf(buff_string,"%.4f",i);
         printf(lcd_data,"%s",buff_string+1);
      }
      else
         printf(cd_data,"%.3f",i);
      delay_ms(1000);
   }
}

This will display your required 'no leading digit', if below one, and switch to 3 decimals above this.
However to handle things properly, you also need to deal with the possibility of negative numbers, and even larger ones....

Best Wishes
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed Jan 30, 2013 11:21 am     Reply with quote

First i have to appreciate you Ttelmah. Thank you.
I have implemented the below code in my program. It worked perfectly. But can you explain me how it works? what's that buff_string+1??? Whats happening in that?

Code:
sprintf(buff_string,"%.4f",i);
         printf(lcd_data,"%s",buff_string+1);
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Wed Jan 30, 2013 1:17 pm     Reply with quote

In C, a 'string' is an array of characters, with a terminating 'null' (zero) character.
In C, the 'name' of an array, is a shortcut to it's address.
sprintf, puts the characters into the character array, so (with your 0.0001 example), the array will contain:
0x30
0x2E
0x30
0x30
0x30
0x31
0

So if we print from the second character in the array, we will throw away the leading zero character, and get .0001

Given the name of the array is it's address. Name+1 is the location of the second character. printf(lcd_data,"%s",buff_string+1); prints the array starting from the second character (+1).

Now, this will go wrong if the number is greater or equal to one, since it would then throw away the first digit. Hence the test....

Best Wishes
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed Jan 30, 2013 9:23 pm     Reply with quote

Nice explanation. It's clear now. Thank you once again Smile
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