OK thanks to all i believe I have the answers now.
For the last issue masking the four bits works.
BD[2] = bd[2] AND $0F
OK thanks to all i believe I have the answers now.
For the last issue masking the four bits works.
BD[2] = bd[2] AND $0F
Forum members kindly helped me with this code a while back.
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.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
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
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
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?
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.
This gives current to two decimal places when divided and a polarity sign to indicate current flowCode: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
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
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
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 '
Bookmarks