From b6369f7a6be8c84e3c85831541ed9ae8c9c7e3a2 Mon Sep 17 00:00:00 2001 From: ladyada Date: Thu, 21 Jan 2016 20:04:35 -0500 Subject: [PATCH] burst read temp & pressure --- Adafruit_BME280.cpp | 52 +++++++++++++++++++++++++++++++++++++++------ Adafruit_BME280.h | 9 ++------ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index 38f8698..f662916 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -192,6 +192,50 @@ int16_t Adafruit_BME280::readS16_LE(byte reg) } + +/**************************************************************************/ +/*! + @brief Reads a 24 bit value over I2C +*/ +/**************************************************************************/ + +uint32_t Adafruit_BME280::read24(byte reg) +{ + uint32_t value; + + if (_cs == -1) { + Wire.beginTransmission((uint8_t)_i2caddr); + Wire.write((uint8_t)reg); + Wire.endTransmission(); + Wire.requestFrom((uint8_t)_i2caddr, (byte)3); + + value = Wire.read(); + value <<= 8; + value |= Wire.read(); + value <<= 8; + value |= Wire.read(); + + } else { + if (_sck == -1) + SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); + digitalWrite(_cs, LOW); + spixfer(reg | 0x80); // read, bit 7 high + + value = spixfer(0); + value <<= 8; + value |= spixfer(0); + value <<= 8; + value |= spixfer(0); + + digitalWrite(_cs, HIGH); + if (_sck == -1) + SPI.endTransaction(); // release the SPI bus + } + + return value; +} + + /**************************************************************************/ /*! @brief Reads the factory-set coefficients @@ -230,9 +274,7 @@ float Adafruit_BME280::readTemperature(void) { int32_t var1, var2; - int32_t adc_T = read16(BME280_REGISTER_TEMPDATA); - adc_T <<= 8; - adc_T |= read8(BME280_REGISTER_TEMPDATA+2); + int32_t adc_T = read24(BME280_REGISTER_TEMPDATA); adc_T >>= 4; var1 = ((((adc_T>>3) - ((int32_t)_bme280_calib.dig_T1 <<1))) * @@ -258,9 +300,7 @@ float Adafruit_BME280::readPressure(void) { readTemperature(); // must be done first to get t_fine - int32_t adc_P = read16(BME280_REGISTER_PRESSUREDATA); - adc_P <<= 8; - adc_P |= read8(BME280_REGISTER_PRESSUREDATA+2); + int32_t adc_P = read24(BME280_REGISTER_PRESSUREDATA); adc_P >>= 4; var1 = ((int64_t)t_fine) - 128000; diff --git a/Adafruit_BME280.h b/Adafruit_BME280.h index af2661e..e480cb9 100644 --- a/Adafruit_BME280.h +++ b/Adafruit_BME280.h @@ -24,13 +24,7 @@ #endif #include - -#ifdef __AVR_ATtiny85__ - #include "TinyWireM.h" - #define Wire TinyWireM -#else - #include -#endif +#include /*========================================================================= I2C ADDRESS/BITS @@ -150,6 +144,7 @@ class Adafruit_BME280 void write8(byte reg, byte value); uint8_t read8(byte reg); uint16_t read16(byte reg); + uint32_t read24(byte reg); int16_t readS16(byte reg); uint16_t read16_LE(byte reg); // little endian int16_t readS16_LE(byte reg); // little endian