Bosch BMP085 Barometer FP Pres. Cal.


Author Message
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 07:44pm 12 Mar 2012      

after I gave you info on twos complement recently I was looking at the method and thought it can be done easier. Your code, much as I suggested,
BMP085_AC1 = (BMP085_buff(0)*2^8) + BMP085_buff(1)
If BMP085_AC1 >= &H8000 Then
BMP085_AC1 = BMP085_AC1 Xor &HFFFF
BMP085_AC1 = BMP085_AC1 + &h1
BMP085_AC1 = BMP085_AC1 * (-1)
EndIf

may be simplified as the following:
BMP085_AC1 = (BMP085_buff(0)*2^8) + BMP085_buff(1)
If BMP085_AC1 >= &H8000 Then
BMP085_AC1 = BMP085_AC1 - 65536
EndIf

This includes the correct sign. The general formula is (number - 2^n) where n is the number of bits in "number". So for 16 bit negative TC numbers subtract 2^16 which is &H10000 or 65536.