Math problem


Closed Thread
Results 1 to 6 of 6

Thread: Math problem

  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default Math problem

    I need to understand this code and convert it to PBP code. I *think* it's for another microcontroller.


    v[0]=0.1*(((d[0]&#0111)-(d[0]&#1000))*256+d[1])

    I don't understand what the & and # signs are really doing. I also don't understand why they have 7 in the left and 8 in the right. Why not just just use 1? I'm sure there's a reason for it but I don't understand why.

  2. #2
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default Re: Math problem

    Just a guess that the & is a Logical AND and that the # is used as an indicator for binary so the the variable d[0] is being masked to get bits 0-2 in the first case and bit three in the second case. eg (d[0] AND 7) -(d[0] AND 8)

    Of course I could be way off base LOL
    Keith

    www.diyha.co.uk
    www.kat5.tv

  3. #3
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Math problem

    Quote Originally Posted by keithdoxey View Post
    Just a guess that the & is a Logical AND and that the # is used as an indicator for binary so the the variable d[0] is being masked to get bits 0-2 in the first case and bit three in the second case. eg (d[0] AND 7) -(d[0] AND 8)

    Of course I could be way off base LOL
    Using the binary numbers as a mask is a good guess. I'm not a math expert. When the term logical is used, what exactly does it mean?

  4. #4
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default Re: Math problem

    It means the mask is applied to the variable being masked and bacause it is an AND, only the bits that are a 1 in both number appear in the result eg assume the number 26 for d[0]

    00011010 = 26 = d[o]
    00000111 = 7 = 1st mask

    00000010 = 2 = 1st result

    00011010 = 26 = d[o]
    00001000 = 8 = 2nd mask

    00001000 = 2 = 2nd result

    Hope that makes sense
    Keith

    www.diyha.co.uk
    www.kat5.tv

  5. #5
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Math problem

    I see why they use that formula. What's an easy way (fast/less code) to convert this number into usable information.

    The first byte will go from 0 to 7 but also from 15 to 8. When it is 0-7, the number is negative....0, -1, -2, -3..... When it is 15 to 8 (stepping down), the number is positive. In this case 15=1, 14=2, 13=3.....

    I'm still not sure I understand the original equation correctly but when I was driving I got something like this:

    D0=15 and D1=243. I know this is charging current and D0=1, which is a rollover from D1. In other words, once D1 increases from 255 to 256, D1 goes to 0 and D0 increases by 1. In this case, it went from 15 to 0. More current stepped down from 15 to 14. How would I apply this to that formula:

    v[0]=0.1*(((d[0]&#0111)-(d[0]&#1000))*256+d[1])

    v[0]=.1*(((7)-(8))*256+243

    v[0]=.1*(-1)*256+243

    v[0]=-.1*256+243

    v[0]= -25.6 + 243

    v[0] = 217.4

    This number doesn't seem correct from a functional perspective but also when plugging in 14 in D0, I would expect the number to increase, not decrease. Is my math above correct?

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: Math problem

    Hi Christopher

    Your equation simply does this calculation

    v[0] = 0.1 * number

    where
    Number is the value in d[0], d[1] MSB, LSB
    Number ranges from -7 to +7 where bit 3 (weight 8) is the sign bit.

    How does it work? Let's break it up

    v[0]=0.1*(((d[0]&#0111)-(d[0]&#1000))*256+d[1])

    Keep aside the 0.1 for the moment

    Starting with ( ( (d[0] & #111) ....
    you keep only bits 0,1,2 of the MSB (max value of #111 is 7)
    ......-(d[0]&#1000))
    subtract 0 for d[0].3 = 0 or 8 if d[0].3 is 1
    d[0].3 is 1 when the number is negative.

    ( ( (value)-(weight of sign bit) ) * 256 ) ...

    Now, multiply this number by 256 or in binary 100hex. Simply means the author wants to move the MSB number by 8 bits left.
    Equivalent code would be "MSB number left shifted by 8 bits"

    (...... + d[1] )
    Now, add in the LSB of the number to get the whole 12bit (4 from msb, 8 from lsb) value

    Now you have the entire number with its sign taken care of in some temporary variable generated by the compiler.

    v[0] = 0.1 * temporary number

    This is the part PBP will not be able to handle for you since it does not accept fractions.

    What you can do to overcome this is

    v[0] = temp number / 10 which is the same as 0.1 * temp number


    Cheers
    Jerson

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