finitespace / BME280

Provides an Arduino library for reading and interpreting Bosch BME280 data over I2C, SPI or Sw SPI.
GNU General Public License v3.0
212 stars 105 forks source link

Parsing H4/H5 humidity calibration data #102

Closed AmedeeBulle closed 3 years ago

AmedeeBulle commented 5 years ago

According to the datasheet, dig_H4 and dig_H5 are signed integers on 12 bits

The following code handles these as pure 16bits signed, which will never be negative...

https://github.com/finitespace/BME280/blob/7a211f03aa3ac5567b14e2cbc12ac21e185b6e0b/src/BME280.cpp#L277-L278

The datasheet is ambiguous as it refers to these 12 bits values as signed short, but the BoschSensortec reference code definitely handles the values as 12 bits signed integer -- the (int8_t)cast on the msb together with a multiplication instead of a shift ensure the sign is propagated properly to the int16_t

Bolukan commented 5 years ago

formula: x = (x >> 11) == 0 ? x : -1 ^ 0xFFF | x;

PS: Checked all my BME280's (4), but all calibrated with small positive dig_H4 and dig_H5.

finitespace commented 5 years ago

This code was taken directly from the Bosch example code.

Have you seen adverse affects due to it?

AmedeeBulle commented 5 years ago

I am not sure which Bosch example code you are referring to, but in all the Bosch code I have seen, dig_H4 and dig_H5 are taken as signed integers on 12 bits.

On the sensors I have, dig_H4 and dig_H5 are positive so it doesn't make a difference, but it would if they were negative.

Bolukan commented 5 years ago

Don't they cast it to signed? dig_H4_msb = (int16_t)(int8_t)reg_data[3] * 16;

AmedeeBulle commented 5 years ago

Yes they do, and this is what I am proposing in #103