euler2dot7 / android_things_bmp180

Android Things Bosh BMP85/BMP180 Driver Example Java
8 stars 1 forks source link

MSB LSB XLSV are not meant to be signed #1

Open IjonTichyRaumpilot opened 6 years ago

IjonTichyRaumpilot commented 6 years ago

As I see, you are reading a byte value for MSB, LSB, XLSB to compose the pressure. In Java they are signed. The values are not meant to be signed, so it helps to change the following lines to get it working properly change line 171-173 int msb = mDevice.readRegByte(BMP180_PRESSURE_DATA); int lsb = mDevice.readRegByte(BMP180_PRESSURE_DATA + 1); int xlsb = mDevice.readRegByte(BMP180_PRESSURE_DATA + 2);

to

int msb = ( mDevice.readRegByte(BMP180_PRESSURE_DATA) & 0xff); int lsb = ( mDevice.readRegByte(BMP180_PRESSURE_DATA + 1) & 0xff); int xlsb = ( mDevice.readRegByte(BMP180_PRESSURE_DATA + 2) & 0xff);

Similar to the calculation on B4 and B7. As in Java int has 4 bytes that should be big enough to store the unsigned long values in C as in the datasheet (page 15)

line 229-230 long B4 = (AC4 (X3 + 32768)) >> 15; long B7 = (UP - B3) (50000 >> mode);

to int B4 = (((AC4 (X3 + 32768)) >> 15)& 0xffffffff); int B7 = ((UP - B3) (50000 >> mode)& 0xffffffff);

See https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-12.pdf https://stackoverflow.com/questions/4266756/can-we-make-unsigned-byte-in-java

Similar problems may occur with AC4, AC5, AC6 as they are unsigned short and read as byte (signed in JAVA)

Chris1234567899 commented 6 years ago

@IjonTichyRaumpilot is right I think. After implementing his suggestions I get fairly correct values. Before that is was way off.