Retrieving 32bit Multiply Result


Closed Thread
Results 1 to 40 of 43

Hybrid View

  1. #1
    kasapo's Avatar
    kasapo Guest


    Did you find this post helpful? Yes | No

    Question Math problem

    Hi darel,

    i have a motor control application. in my application pic 16f877 is reading 2 position and then take the ratio of these values and multiply with the constant velocity. for example
    x=5 y=10 then Vy = Vc (constan velocity) , Vx = Vc*x/y
    so two motor begin and stop at the same time. i wrote some code by using Div32 but sometimes i cannot get the values thet it must be.

    // for Trial i give posy greater than posx
    POSX VAR WORD
    POSX1 VAR POSX.BYTE1
    POSX0 VAR POSX.BYTE0

    POSY VAR WORD
    POSY1 VAR POSY.BYTE1
    POSY0 VAR POSY.BYTE0

    V VAR WORD
    V0 VAR V.BYTE0
    V1 VAR V.BYTE1

    HV VAR WORD
    HV0 VAR HV.BYTE0
    HV1 VAR HV.BYTE1

    A VAR WORD
    A0 VAR A.BYTE0
    A1 VAR A.BYTE1


    C VAR WORD
    C1 VAR C.BYTE1
    C0 VAR C.BYTE0

    DUMMY VAR WORD


    Asm
    PutMulResult macro Const32
    MOVE?CB Low Const32, R2
    MOVE?CB Low (Const32 >> 8), R2 + 1
    MOVE?CB Low (Const32 >> 16), R0
    MOVE?CB Low (Const32 >> 24), R0 + 1
    endm
    EndAsm

    GoSub USBREAD
    POSX1=HB
    POSX0=LB
    GoSub USBREAD
    POSY1=HB
    POSY0=LB

    IF POSY > 6554 Then
    DUMMY = POSY * 10 // i scale posy by 10
    C= Div32 POSX
    @ PutMulResult 2236920 // i also enter here the scaled c constant velocity by 10
    V= Div32 C
    HV = 34 / C
    Else
    DUMMY = POSY * 10
    C = DUMMY / POSX
    @ PutMulResult 2236920
    V= Div32 C // when this is greter than 16 bits it returns ffff
    HV = 34 / C
    EndIF

    how can i get the V when it is greater than 16 bit ?
    thanks

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


    Did you find this post helpful? Yes | No

    Default

    There's no way to do that with DIV32. The Quotient must be less than 65535.

    You would need a TRUE 32 bit divide routine.

    Or you could use the Floating Point Math routines from melabs.
    http://www.melabs.com/resources/fp.htm

    Darrel

  3. #3
    kasapo's Avatar
    kasapo Guest


    Did you find this post helpful? Yes | No

    Question

    hi darrel ,
    do you have some example about the usage of floating point numbers.

    after i do my multiplication and division how can i use aint variable?
    i have to send the aint to my motor controller byte by byte.

    thanks

  4. #4
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Question Any way to use a variable for DIV32 divisor?

    Hi Darrel,

    I like your ideas about getting at DIV32 system variables, but I have a slightly different situation. How do I use a variable for the divisor that wont violate the 15 bit rule?

    I tried the following example, but it don't work (I get: Error Bad expression):
    Code:
    divisor var word
    dummy var word
    
    dummy = DIV32 divisor
    I really need the ability to alter the divisor via program control, and at times it will need to be bigger then an 8-Bit value. Any ideas?

    Thanks,

    Edit: Opps! my mistake. It does work properly with a word variable as the divisor, just so long as you stay with the 15 bit rule (32767). This is good news.
    Last edited by mytekcontrols; - 18th June 2005 at 01:24.

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


    Did you find this post helpful? Yes | No

    Default

    hehe,

    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default Adding 2 Dwords

    I just saw this link. it is of great interest to me right now...

    I have this question... how do you add two of these Dwords?

    how do i know if the add of two words overflows?
    i.e. $1FFFE + $2E

    my guess is to add the lower words $FFFE+$2E. But how do i know if there is an overflow? so that i can add one to the higher WORD? even worse..

    i.e. $EFFF8 + $6FFE4

    My guess again: add two lower words... if over flow add two higher words +1, else add two higher words....

    I think i can manage myself once i am sure abour the overflow

    Thanks,

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


    Did you find this post helpful? Yes | No

    Default

    For words and bytes, you can detect an overflow by testing if the result is less than what you added.

    Due to trucation of a 16-bit word ...
    $FF00 + $0180 = $0080

    So the result will always be less than either of the values added if an overflow occurs.

    I think i can manage myself once i am sure abour the overflow
    I'm sure you could. But that's just not my way ...

    Code:
    ;Initialize your hardware first
    
    A         VAR WORD[2]
    B         VAR WORD[2]
    Result    VAR WORD[2]
    OVRFLOW32 VAR BIT
    
    ASM
    ; ===== Load 32bit Constant into DWORD =============================
    MOVE?CD macro C32in, Dout                      ; Max= 4,294,967,296
        MOVE?CB   low C32in, Dout
        MOVE?CB   low (C32in >> 8), Dout + 1
        MOVE?CB   low (C32in >> 16), Dout + 2
        MOVE?CB   low (C32in >> 24), Dout + 3
      endm
    ENDASM
    
    @  MOVE?CD  0xEFFF8, _A  ; Load 983,032 into A
    @  MOVE?CD  0x6FFE4, _B  ; Load 458,724 into B
    
    GOSUB Add32
    
    LCDOUT  $FE,1,  "A=   ",HEX4 A[1],":",HEX4 A[0]
    LCDOUT  $FE,$C0,"B=   ",HEX4 B[1],":",HEX4 B[0]
    LCDOUT  $FE,$90,"Res= ",HEX4 Result[1],":",HEX4 Result[0]
    LCDOUT  $FE,$D0,"OVRFLOW32 = ", BIN1 OVRFLOW32
    stop
    
    ; ===== Add 2 32bit variables ======================================
    Add32:
        OVRFLOW32 = 0
        Result[0] = A[0] + B[0]
        IF Result[0] < B[0] then Result[1] = 1
        Result[1] = Result[1] + A[1]
        IF Result[1] < A[1] then OVRFLOW32 = 1
        Result[1] = Result[1] + B[1]
        IF Result[1] < B[1] then OVRFLOW32 = 1
    RETURN
    Which displays ...
    Code:
    A=   000E:FFF8
    B=   0006:FFE4
    Res= 0015:FFDC
    OVRFLOW32 = 0
    HTH,
    DT

Similar Threads

  1. Strugling without floating point
    By pjsmith in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 27th March 2011, 07:29
  2. Math help please!!!
    By jbirnsch in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 10th August 2007, 15:45
  3. 32-bit Variables and DIV32, Hourmeter 99999.9
    By Darrel Taylor in forum Code Examples
    Replies: 9
    Last Post: - 23rd November 2006, 08:23
  4. PBP 16-bit ADC result math
    By sonic in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2005, 15:21
  5. Retrieving Div32 Remainder
    By Darrel Taylor in forum Code Examples
    Replies: 4
    Last Post: - 20th August 2003, 04:53

Members who have read this thread : 0

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