adafruit / Adafruit_CircuitPython_BME680

CircuitPython driver for BME680
MIT License
57 stars 41 forks source link

Wrong conversion result for register 0x04 #31

Closed Kampi closed 4 years ago

Kampi commented 4 years ago

I checked out the Adafruit Circuit Python Module for the BME680 sensor from https://pypi.org/project/adafruit-circuitpython-bme680/#files to compare the results with my C driver for the BME680. I add some code to the Adafruit library to print the calibration value for the range switching error in the Adafruit library and I got the result 15.0

self._sw_err = (self._read_byte(0x04) & 0xF0) / 16
print(self._sw_err)

When I read the whole register at address 0x04, I got the result 243 which is ‭1111 0011‬. Bosch writes the following in the device datasheet (Page 22):

  1. Read range switching error from register address 0x04<7:4>(signed 4 bit)

So I take the upper nibble of the byte 243, which is 1111, and handle this as a signed 4-bit value which should be -1 and not 15.

evaherrada commented 4 years ago

Are you sure the value you're getting (15) isn't meant to be the gas range? Also, why do you think that it should be -1?

Kampi commented 4 years ago

Are you sure the value you're getting (15) isn't meant to be the gas range? Also, why do you think that it should be -1?

Because it is a signed value and signed values use the highest bit for the sign.

evaherrada commented 4 years ago

Oh yeah, you're totally right.

siddacious commented 4 years ago

@Kampi _read_byte returns the raw untyped, unsigned byte from the register so you will need to either use unpack to make it signed or do the conversion from two's complement to a signed type yourself