PbPro Maths Help


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1

    Default PbPro Maths Help

    I'm a real PbPro Maths dunce and am asking for members advice on converting the following maths
    routines into code.

    Baiscally i have a byte array of hex data BD[11] recieved via a serial interface. I'm happy with this and the data recd.

    Now taking bytes from array BD [4] and BD [5] as an example i need to perform the following calcu
    lation on the bytes.

    (Multiply 1st number by 128, add result to 2nd number, take off 2048, divide the result by 20.48)
    I appreciate the decimal point will cause an issue. Any examples of code that would perform this.

    I came up with

    Code:
    Result = (((BD[4] * 128) + BD[5]) - 2048) / 20
    But this losses the accuracy of the end part of the formula?

    Also for a second formula involving BD [2] & BD [3] I need to

    (Multiply the right hand digit of 1st hex number by 128, add result to 2nd number)
    I'm stuck on extracting a value for the right hand digit of the hex number?

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    I'm a real PbPro Maths dunce ...

    Baiscally i have a byte array of hex data BD[11] recieved via a serial interface. I'm happy with this and the data recd.

    Now taking bytes from array BD [4] and BD [5] as an example i need to perform the following calcu
    lation on the bytes.



    I appreciate the decimal point will cause an issue. Any examples of code that would perform this.

    I came up with

    Code:
    Result = (((BD[4] * 128) + BD[5]) - 2048) / 20.48
    But this losses the accuracy of the end part of the formula?

    ...

    Also for a second formula involving BD [2] & BD [3] I need to

    I'm stuck on extracting a value for the right hand digit of the hex number?

    Hi,

    1) Result = (((BD[4] * 128) + BD[5]) - 2048) / 20.48

    lets write it ...

    20.48 = 512 /25 ... ; 2048 = 100 * 20.48 ...

    result = ((((BD[4] << 7) + BD[5] ) * 25) >> 9) - 100

    not so good as a calculation ... don't you have a better formula ???
    ( BTW... I'm curious to see your project ...)

    BD[5] obviously is a word ( or smaller !!! ) ... so, at least, its 4 lower digits are lost in the calculation ...


    2) It's a typical "masking" job ... ( I suppose it's a HEX digit, you look for ? ) ...

    so, you could use ...

    Ldigit = Res & $000F( for a Word )

    Ldigit = Res & $0000000F ( for a LONG )


    Alain
    Last edited by Acetronics2; - 6th July 2010 at 11:17.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3


    Did you find this post helpful? Yes | No

    Default

    BD is a byte array. All the values are hex bytes. Thanks for the ideas so far.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    (Multiply the right hand digit of 1st hex number by 128, add result to 2nd number)
    In the second example if we assume That the first hex byte number is 1E how do i extract the value for the E to use in the above? Sorry if i'm being thick.

  5. #5


    Did you find this post helpful? Yes | No

    Default

    In the first example it may help if i explain the range of values my forumla will encounter, for BD[4] it is $00-$18 and for BD[5] it is $00-$7F. The application is a current sensor data stream which measures from -100 to +50A

    This i think will allow me multiply the result by 10 and still fit in a WORD before performing the divide by 20.48 increasing integer accuracy as I can divide by 205

    A problem however is that the result of a multiplication can be negative

    If the result of a multiplication could possibly be negative, it should be
    stored to a long-sized variable type to preserve the sign. If a negative
    result is placed in a variable type other than long, subsequent
    calculations using this value will interpret it as a positive number.
    I don't know how to manage that PBL?

    For the second example the range of values for BD[2] is $21 -$34 and for BD[3] it's again $00-$7F This isnt a current sensor but relates to a SOC reading probably varying from 0-100%

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I don't get it.... $18 and $7F is 24 and 127 respectively. Putting these in the formula should result in "real life values" ranging from -100 to 50 representing the current in amperes, correct?

    ((0*128)+0-2048) / 20.48 = -100 OK
    ((24*128)+127-2048) / 20.48 = 56 ??

    Next question is what you're going to do with the values? If you're displaying them to a human then it makes sense to scale them properly but of you're continuing to do massage the numbers it doesn't matter what "units" they are in.

    Anyway, how about:
    Code:
    Result var WORD
    Sign VAR BIT
     
    Result = (BD[4] * 128) + BD[5] - 2048
    Sign = Result.15
    Result = ABS Result
    Result = Result * 2500
    Result = DIV32 512
    If Sign = 1 Then
    LCDOUT $FE, 1, "-", #RESULT/100, ".", DEC2 RESULT//100
    ELSE
    LCDOUT $FE, 1, #RESULT/100, ".", DEC2 RESULT//100 
    ENDIF
    In the above, if BD[4]=3 and BD[5]=90 the correct result is -76.855 and the code displays -76.85. If BD[4]=19 and BD[5]=89 the correct result is 23.096 and the code displays 23.09. Is that close enough?

    /Henrik.

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