View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
problem with displaying float number on LCD. ASAP!!! |
Posted: Tue Jan 29, 2013 6:05 am |
|
|
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 |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Jan 29, 2013 6:09 am |
|
|
show your code.
odds on that you are not FORMATTING correctly. |
|
|
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
|
Posted: Tue Jan 29, 2013 6:14 am |
|
|
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: 19520
|
|
Posted: Tue Jan 29, 2013 8:25 am |
|
|
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
|
|
Posted: Wed Jan 30, 2013 11:21 am |
|
|
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: 19520
|
|
Posted: Wed Jan 30, 2013 1:17 pm |
|
|
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
|
|
Posted: Wed Jan 30, 2013 9:23 pm |
|
|
Nice explanation. It's clear now. Thank you once again |
|
|
|