Discussion in "8051 Discussion Forum" started by    aimseeker    Feb 8, 2011.
Tue Feb 08 2011, 12:48 am
#1
Hey everyone...
I have got two questions..
1.While programming 8051 in C ... Is there a more effective(in saving memory) way to deal with decimal points .... Though we all know that we can just create a variable "float" or "double" and do "multiplications" and "division" on them easily but doing that wont be an effective way to program 8051 using C language....

2. I used a sprintf function in a program to convert a "floating" value to string and display it on LCD, but an engineer at my workplace told me not to do that instead i should write my own "effective" function to display a "floating" value on Lcd.. I would like to know what other options do i have??

Thanks in advance...
Tue Feb 08 2011, 11:27 pm
#2
waiting for others... I also want to know tha t
Wed Feb 09 2011, 12:02 am
#3
there are some ways to doing this
u can see these attachments
ieee math file is for ieee standard math calculations

Attachment

Attachment

 romel_emperado like this.
Tags float to asciiftoafloat lcddecimal number lcdfloating point lcd display
Wed Feb 09 2011, 12:31 am
#4

1.While programming 8051 in C ... Is there a more effective(in saving memory) way to deal with decimal points .... Though we all know that we can just create a variable "float" or "double" and do "multiplications" and "division" on them easily but doing that wont be an effective way to program 8051 using C language....


Effectiveness does depends on the way of writing code. especially incase of floating points. Usually the companies who make C compilers does take care of the efficiency of the program in all respect. But its not bad to try it on your own. You can see examples in majoka's post. you can also find some math libraries in our download section.

2. I used a sprintf function in a program to convert a "floating" value to string and display it on LCD, but an engineer at my workplace told me not to do that instead i should write my own "effective" function to display a "floating" value on Lcd.. I would like to know what other options do i have??


Its usually not recommended to use %f in sprintf. but %d is ok to use. wont effect much. so if you want a floating point to be displayed, consider it as a decimal before displaying it.

Lets take an example to explain:
you have a nnumber 20.34 (or any number) and you want this number to be displayed with 2 decimal precision. so... what we do is reserve 2 decimal points
how? like this:
float f = 20.34;
int i; //this will store our number in integer format
i = f * 100; //multiply by 100 so we reserve 2 decimal places in a float
//i iwll now have 2043 in it :)


now come to the display part, as i said using %d will not harm at all
so we use the same sprintf method.
int count; //this will store how many bytes written to buffer
char buff[6]; //this will be used to display

count = sprintf(buff, "%d", i); //i is number from example

//i am assuming disp_str is function to display string
disp_str(buff, 2); //display 2 bytes from buffer on display
//we know we have 2 decimal places so next on lcd has to be a point
disp_str(".", 1); //display a point
//now comes the decimal point
disp_str(buff+2, 2); //we incremented buff by 2 coz we already display the integer part

so you see we did not use any %f switch which makes this floating free ascii conversion though it seems a bit lenthy but if you put this in a function, its much smaller than any converter function


[ Edited Wed Feb 09 2011, 12:32 am ]
 romel_emperadomajoka like this.
Tags float to asciiftoafloat lcddecimal number lcdfloating point lcd display
Wed Feb 09 2011, 07:57 am
#5
thanks for that explanation ajay although it's looks lengthy but a good basic example for beginners like me..

let me copy your explaination to my learning thread...

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am