i have two variables float number (may be 1.28 and 15.9, note the position of decimal point), can anyone tell me how to display to lcd using keil c ???
the only one way is to convert above two float number to array of ascii and then output to lcd, more trouble is how to determine the position of decimal point, the exact method i don't know !!!! mcu i am using is 89s52.
Can you:
1. type cast the real number to an integer
2. take the difference between the integer and the float
3. multiply the difference by some value
4. then type cast that value to an integer?
You would then have two integer values you could convert to ascii. You would then display a '.' between the two integers.
float realValue;
int intValue = (int)realValue;
float diffValue = realValue - (float)intValue;
int anotherIntValue = (int)(diffValue * 1000.0)
1. for integral part
equate temp1(int ) to 23.57;
temp1 will be 23 only as it is of int type so 23.57 will be typecasted to 23 .you have integral value.
2. for float part
multiply the number by 100 store it in int variable(0 to 65535) say temp;
now temp = 2357
get the lower two digits as 5 and 7 (i hope you can do it).just split 2357 into 4 separate values by taking remainder (after dividing by 10).
once you have 4 separate values of 2 , 3 ,5,7 dislplay it anywhere.
float xdata a, b;
char xdata str[20];
a = 1.28;
b = 15.9;
sprintf(str,"A: %4.2f B: %4.1",a,b); // Adjust the formatting to your liking.
gputs(str); // Or whatever you use to write strings to the display.
Hi,
I use keil compiler for 8051 mcu, i use sprintf to display float number to lcd pls refer to following example. Note: sprintf take a lot of memory space, but easier to use. All you need to do is to declare one array.
Code:
void main() {
char buf[8]; // declare array
delay_ms(100); //settle time for lcd
lcd_init(); // initialization of lcd
lcd_clear(); //clear lcd display
sprintf (buf, "float=%f", 1.156); // here is sprintf prototype function usage
// 1.156 is just example you can change it
lcd_puts(buf); // display float number from array to lcd
}
i guys i refered mazdi book of embedded system .. i need the asm coding for the floating operation...........
in c it working fine . but problem is it occupies more memory size while using sprintf fun......
sprintf() or ftoa() function is nice but it take lots of bytes in code so when we do not have enough memory to use it we have to use alternative method:
- float tmp; tmp=12.3456
- get integer part :int a,b; a = tmp; (so a=12)
- get float part: tmp= tmp-a; (so tmp=0.3456)
- b = tmp * 1000; (so b=345) - if 3 digit is enough
- convert a to "12" by using a/10 and a%10 method;
- convert b to "345" ; LCDstring "12"; LCDstring "." ; LCDstring "345";