Page 1 of 1

Floating-point

Posted: 01 Oct 2010 16:18
by marco
Hello again my friends!

I´m having some difficulties to solve this problemes.
i´m reading from ADC 10bits , and i want to do this simple operation !

Ex: ADC value = 700;

700/120=5,83333333

I want to read 5,83333333 but instead i only read 5 !

Or:
51*3.14=160,14 but instead i only read 160

I want to read and write Floating-point numbers.

Can anyone healp me?

Thanks

Re: Floating-point

Posted: 01 Oct 2010 18:31
by Sparky1039
Have you created a variable that accepts floating point values... i.e "float myVar" ?

Might be simpler to use interger math.

Code: Select all

unsigned long tempVal = 0;
unsigned char integer = 0;
unsigned int decimal = 0;

tempVal = (adcIn * 1000) / 120; // (700 * 1000) / 120 = 5833

integer = tempVal / 1000; // = 5
decimal = tempVal % 1000; // = 833
then just print the results like
LCD print integer
LCD print "."
LCD print decimal

... to give you 5.833

Re: Floating-point

Posted: 05 Oct 2010 13:37
by marco
As always a simple and perfect answer !!

Thank you again for the great help my friends.