Replaced original getTemperature Routine as it was returned strange values
Dependents: LPC1768-GPS-FUSION-17102018-1
Fork of BME280 by
Diff: BME280.cpp
- Revision:
- 5:691e06db489f
- Parent:
- 4:ddcaa259e65b
--- a/BME280.cpp Tue May 12 13:32:09 2015 +0000 +++ b/BME280.cpp Tue Nov 29 18:54:29 2016 +0000 @@ -5,6 +5,10 @@ * @version 1.0 * @date 06-April-2015 * + * 29 November 2016 + * Toyomasa Watarai code Modified by jon trinder jont@ninelocks.com + * readtemperature routine replaced + * * Library for "BME280 temperature, humidity and pressure sensor module" from Switch Science * https://www.switch-science.com/catalog/2236/ * @@ -100,6 +104,13 @@ DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6); } + + /* +As Thomas routine was giving me weird values I rewrote it to look mostly like the adafruit +arduino code, which in turn is a lot like the bosch datasheet +jon Trinder +*/ + /* float BME280::getTemperature() { uint32_t temp_raw; @@ -124,6 +135,45 @@ return (tempf/100.0f); } +*/ + +// new version of getTemperature jont@ninelocks.com November 2016 +float BME280::getTemperature() +{ + int32_t var1, var2, T; + int32_t adc_T; + float temp_as_float; + char cmd[4]; + cmd[0] = 0xfa; // temp_msb + i2c.write(address, cmd, 1); + i2c.read(address, &cmd[1], 3); + + //jons slow painful shifting of bits to make it readable + adc_T = cmd[1]; + adc_T <<= 8; + adc_T |= cmd[2]; + adc_T <<= 8; + adc_T |= cmd[3]; + + //from here more or less same as ada and bosch + + adc_T >>= 4; + + var1 = ((((adc_T>>3) - ((int32_t)dig_T1 <<1))) * + ((int32_t)dig_T2)) >> 11; + + var2 = (((((adc_T>>4) - ((int32_t)dig_T1)) * + ((adc_T>>4) - ((int32_t)dig_T1))) >> 12) * + ((int32_t)dig_T3)) >> 14; + + t_fine = var1 + var2; + + T = (t_fine * 5 + 128) >> 8; + temp_as_float = T/100.0; + return temp_as_float; +} + + float BME280::getPressure() {