PbPro Maths Help


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1


    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

  2. #2
    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

  3. #3


    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