PDA

View Full Version : Calculation error within a for next loop



lerameur
- 2nd March 2011, 01:23
Hi,

I have some weird going on. When the voltage_check value come to 220 the output shows 1% instead of the calculated 67%
I have both voltage_check , voltage_percent as var word
anybody knows why ? running on a 18F4550


for i=215 to 230
voltage_check =i
voltage_percent = (((voltage_check -179) * 1623 ) / 10 )

lcdout $FE,1, "Output"," ", dec (voltage_percent / 100) ,".", dec2 (voltage_percent // 100 )
lcdout $FE,$C0, "Ana4: ", dec voltage_check
pause 500

next i

Jerson
- 2nd March 2011, 01:36
Typical problem of word size and technique.

Take i = 220
Now, plug into the formula
220-179 = 41
41 * 1623 = 66543
which ends up in the variable as 66543-65535(0FFFFH) = 1008

Now, you do the rest of the math and end up with the 1%

You need to re-factor the way you calculate the problem.

lerameur
- 2nd March 2011, 01:46
aaarg yep correct thank you

K