PDA

View Full Version : Problem with Div32



lerameur
- 22nd April 2008, 01:06
HI,

I have a problem using div32, I think it might be related to memory issue because the first time I use Div32, the division works, but the second time, the result is 65535, humm I think I have seen this number before :)
here is the code, thanks in advance

if counter > 126 then
dummy=(counter *10000)
test11= div32 506

dummy =0

dummy=(126 * 10000)
test22=div32 506 'this is always 65535....

test3=test11-test22

gauss= test3 / 25
gauss1= test3 // 25

lcdout $FE,1, "1:",dec test11," 2:", dec test22," 3:", dec test3
lcdout $FE,$C0, "C:", dec counter, "G:", dec dummy

Darrel Taylor
- 22nd April 2008, 02:05
The second time, you are multiplying two constants.
Which is actually multiplied when the program gets compiled. It's called "Constant Folding".

Make at least one of the values a WORD variable, and it should work better.
<br>

lerameur
- 22nd April 2008, 02:26
hi,

all my variables are word variables: here is the full program, except for the overhead:

'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////

oldcounter var word
counter var word
gauss var word
gauss1 var word
dummy var word
test11 var word
test22 var word
test3 var word
difference var word

counter = 0
oldcounter =0
gauss1 =0

'//////////////////////////
'// Program starts here //
'//////////////////////////

loop:

ADCON0.2 = 1 'Start A/D Conversion
oldcounter = counter

ADCIN 0, counter 'Read channel PORTA.0
pause 30

'Reducing the varying output (average)
counter = (counter + oldcounter) / 2

'************** Condition 1 ***********************
if counter == 126 then
lcdout $FE,1, " Neutral ",dec counter
lcdout $FE,$C0, "Gauss: ", dec gauss1
endif

'************** Condition 2 ***********************
if counter > 126 then 'GOOD CALCULATION
dummy=(counter *10000)
test11= div32 506

dummy =0

dummy=(126 * 10000)
test22=div32 506

test3=test11-test22

gauss= test3 / 25
gauss1= test3 // 25

lcdout $FE,1, "1:",dec test11," 2:", dec test22," 3:", dec test3
lcdout $FE,$C0, "C:", dec counter, "G:", dec dummy

'lcdout $FE,1, " Negative S: ",dec counter
'lcdout $FE,$C0, "Gauss: -", dec gauss,".", dec gauss1
endif

Darrel Taylor
- 22nd April 2008, 02:35
I've highlighted the offending line in your last post.

In order for DIV32 to work. Certain PBP system variables must be preloaded with the result of a WORD multiplication.

If the multiplication happens at "Compile-Time", those variables don't have the correct value.

When the WORD multiplication happens at "Run-Time", PBP's system variables still have the 32-bit result when it gets to the DIV32.
<br>

lerameur
- 22nd April 2008, 02:45
Thats the line I though was problematic.
This is why I added
dummy =0
I even tried dummy = 0000000000000000
to remove any values in dummy, but it is not enough.
I dont quite get your solution.

k

lerameur
- 22nd April 2008, 02:54
a ok,
I added
constant var word
constant = 126

thanks