burst read temp & pressure

This commit is contained in:
ladyada 2016-01-21 20:04:35 -05:00
parent 30003e7b98
commit b6369f7a6b
2 changed files with 48 additions and 13 deletions

View File

@ -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 @brief Reads the factory-set coefficients
@ -230,9 +274,7 @@ float Adafruit_BME280::readTemperature(void)
{ {
int32_t var1, var2; int32_t var1, var2;
int32_t adc_T = read16(BME280_REGISTER_TEMPDATA); int32_t adc_T = read24(BME280_REGISTER_TEMPDATA);
adc_T <<= 8;
adc_T |= read8(BME280_REGISTER_TEMPDATA+2);
adc_T >>= 4; adc_T >>= 4;
var1 = ((((adc_T>>3) - ((int32_t)_bme280_calib.dig_T1 <<1))) * 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 readTemperature(); // must be done first to get t_fine
int32_t adc_P = read16(BME280_REGISTER_PRESSUREDATA); int32_t adc_P = read24(BME280_REGISTER_PRESSUREDATA);
adc_P <<= 8;
adc_P |= read8(BME280_REGISTER_PRESSUREDATA+2);
adc_P >>= 4; adc_P >>= 4;
var1 = ((int64_t)t_fine) - 128000; var1 = ((int64_t)t_fine) - 128000;

View File

@ -24,13 +24,7 @@
#endif #endif
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <Wire.h>
#ifdef __AVR_ATtiny85__
#include "TinyWireM.h"
#define Wire TinyWireM
#else
#include <Wire.h>
#endif
/*========================================================================= /*=========================================================================
I2C ADDRESS/BITS I2C ADDRESS/BITS
@ -150,6 +144,7 @@ class Adafruit_BME280
void write8(byte reg, byte value); void write8(byte reg, byte value);
uint8_t read8(byte reg); uint8_t read8(byte reg);
uint16_t read16(byte reg); uint16_t read16(byte reg);
uint32_t read24(byte reg);
int16_t readS16(byte reg); int16_t readS16(byte reg);
uint16_t read16_LE(byte reg); // little endian uint16_t read16_LE(byte reg); // little endian
int16_t readS16_LE(byte reg); // little endian int16_t readS16_LE(byte reg); // little endian