PbPro Maths Help


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default More help

    Forum members kindly helped me with this code a while back.

    Code:
    Amps = ((BCM87[4] * 128) + BCM87[5]) - 2048			'Calculate Battery Current
    	if Amps.15 = 1 then						'Calculate AmpSign
    	AmpSign = 45							'Set AmpSign to 45 (-)
    	else
    	AmpSign = 43							'Set AmpSign to 43 (+)
    	endif
    	Amps = ABS Amps
    	Amps = Amps * 2500
    	Amps = DIV32 512
    It evaluates a 16 bit hex number from an array and produces a current reading to two decimal places with a + or - sign to denote charging or discharging. It work fine.

    Now I need to manipulate the current, so say the current is +10.00A i need to be able to reduce it by say 8 or 16% and then generate two new hex bytes in the correct format to overwrite those it came from.

    Imagine the device sits passing through the traffic current data. it now needs to get the data evaluate, modify it by X% and squirt it out the other side.

    I would be grateful for ideas as maths is not my strong point. Thanks Peter

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    The following code should work.

    Cheers

    Al.

    Code:
    Percent  var byte
    R_Amp var Word
    I_Amp var Byte
    D_Amp var Byte
    
    main:
    Percent = 8 ' set variable with your percent
    gosub Calc
    LDCOUT I_Amp, ".", D_Amp ' display values
    goto main
    
    Calc:
    R_Amp = (100 - Percent) * Amps ' here Amps is your variable with the primitive value
    I_Amp = R_Amp/100 ' integerpart
    D_Amp = R_Amp//100 ' decimal part
    Return
    All progress began with an idea

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Thanks i think that helps with the percentage, but it's reconstructing the two hex bytes with the new data to forward on which is really tricky? If you get me?

  4. #4


    Did you find this post helpful? Yes | No

    Default Perhaps i need to be a bit clearer.

    In the below simplified sample code two hex bytes of incomming battery current data are
    evaluated to give a display of current. The code works very well.

    Code:
    Amps =  ((HEXBYTE1 * 128) + HEXBYTE2) - 2048	'Calculate Battery Current
    	if Amps.15 = 1 then						'Calculate AmpSign
    	AmpSign = 45							'Set AmpSign to 45 (-)
    	else
    	AmpSign = 43							'Set AmpSign to 43 (+)
    	endif
    	Amps = ABS Amps
    	Amps = Amps * 2500
    	Amps = DIV32 512
    This gives current to two decimal places when divided and a polarity sign to indicate current flow
    direction. All that is fine.

    Now I need to reduce the Amps by say 8-16% (That's fine) and then recreate the HEXBYTES by
    some reverse calculation or application of the above code. So they contain the new value and when evaluated by the above routine would give the new current and sign etc. Does that make sense? I hope somone can give me
    me some ideas. Thanks

  5. #5
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    retepsnikrep , Why not just use the "*/" command? The variable "Amps" is a word so just multiply it by the fraction of 84 to 92 %. The "*/" command will give you the middle 16 bits of the 32 bit result. Then just put the result back into bytes.... Thats what I would do.....

    Dave Purola,
    N8NTA

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Thanks for that.

    How about this?

    Anyway, a reverse of my code? Does it make sense.

    Code:
    Amps = Amps * 512
    Amps DIV32 2500
    
    if AmpSign == 45 then '-
    Amps = 0 - Amps
    else
    ? not sure here
    endif
    
    Amps = Amps + 2048
    Byte2 = Amps & &7f 'split of HEXBYTE2
    Amps = Amps >> 7 'Div by 128
    Byte1 = Amps & 0x1f '

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