Page 1 of 1

Computer Maths & Programming Question

Posted: Thu Oct 28, 2010 12:05 am
by retepsnikrep
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


Code: Select all

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 (-)
   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 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

Re: Computer Maths & Programming Question

Posted: Thu Oct 28, 2010 2:38 pm
by JonSpence
Woops Posted half way through.

Anyway, here we go, reverse of your code.
<pre>
Amps = Amps * 512
Amps DIV32 2500
if AmpSign == 45 then '-
Amps = 0 - Amps
else
'I don't know Pic, do nothing
endif
Amps = Amps + 2048
Byte2 = Amps & &7f 'split of HEXBYTE2
Amps = Amps >> 7 'Div by 128
Byte1 = Amps & 0x1f '
</pre>

Re: Computer Maths & Programming Question

Posted: Thu Oct 28, 2010 2:51 pm
by JonSpence
Well I don't know what happened there. Most of my text was lost and some of the characters came out wrong.

I question the first line of your code, I think that it's wrong. If your ADC has more than 8 bits it usually fills 8 bits rather than ignoring one of the bits. Also the zero point is either at one end of the ADC range, in the middle of the ADC range or at some point that does not relate to word size. You seem to have put the zero point at the mid range of the variable that you are using, which seems odd to me.

Anyway my code reversed your maths then used bitwise operators and shifts to split off the psudo-ADC values assuming that your first line was correct.

Byte2 = Amps & $7f 'split of HEXBYTE2
Amps = Amps >> 7 'Div by 128
Byte1 = Amps & $1f '

The funny character is the ampersand. ie bitwise and (mask off some bits)

Re: Computer Maths & Programming Question

Posted: Thu Oct 28, 2010 3:08 pm
by retepsnikrep
Jon

Thanks for that reply. I'll study it.

The incomming data is not directly from an ADC but is from a battery management module and the output is in this weird propietary format. The first line is correct as far as we know and gives the correct current values.