Binary maths, shifting etc etc is not my strong point so perhaps someone can give me a few pointers here for some program code.
In the below simplified sample code two hex bytes of incomming battery current data are
evaluated to give a display of current. The below code to interpret and evaluate the data works very well. The range of values the below formula encounters is HEXBYTE1 is $00-$17 and HEXBYTE2 it is $00-$7F. The application is a current sensor data stream which measures from -100 to +50A
Amps = ((HEXBYTE1 * 128) + HEXBYTE2) - 2048 'Calculate Battery Current
if Amps.15 = 1 then 'If Bit15 of Amps (WORD) = 1 then
AmpSign = 45 'Set AmpSign to 45 (-)
AmpSign = 43 'Set AmpSign to 43 (+)
Amps = ABS Amps
Amps = Amps * 2500
Amps = DIV32 512
This gives current to two decimal places when AMPS is divided by 100 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. I have asked on the pbpro forum but it's more of a question about how to tackle it from the maths POV, not the actual mechanics of the code. Thanks