Problem with Div32


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default Problem with Div32

    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

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

  3. #3
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by Darrel Taylor; - 22nd April 2008 at 02:31. Reason: Highlight

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    a ok,
    I added
    constant var word
    constant = 126

    thanks

Similar Threads

  1. RPM - DIV32 Problem
    By davewanna in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 11th April 2008, 04:33
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. 32-bit Variables and DIV32, Hourmeter 99999.9
    By Darrel Taylor in forum Code Examples
    Replies: 9
    Last Post: - 23rd November 2006, 07:23
  4. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59
  5. Retrieving Div32 Remainder
    By Darrel Taylor in forum Code Examples
    Replies: 4
    Last Post: - 20th August 2003, 03:53

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts