added spi fixes

This commit is contained in:
Jan Hoffmann 2019-06-20 18:26:48 +02:00
parent 4ea4fa8ae7
commit 7e39c445f0
2 changed files with 687 additions and 778 deletions

View File

@ -28,95 +28,78 @@
* *
*/ */
#include "Arduino.h"
#include <Wire.h>
#include <SPI.h>
#include "Adafruit_BME280.h" #include "Adafruit_BME280.h"
#include "Arduino.h"
#include <SPI.h>
#include <Wire.h>
/**************************************************************************/
/*! /*!
@brief class constructor * @brief class constructor
*/ */
/**************************************************************************/ Adafruit_BME280::Adafruit_BME280() : _cs(-1), _mosi(-1), _miso(-1), _sck(-1) {}
Adafruit_BME280::Adafruit_BME280()
: _cs(-1), _mosi(-1), _miso(-1), _sck(-1)
{ }
/**************************************************************************/
/*! /*!
@brief class constructor if using hardware SPI * @brief class constructor if using hardware SPI
@param cspin the chip select pin to use * @param cspin the chip select pin to use
* @param *theSPI
* optional SPI object
*/ */
/**************************************************************************/ Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) {
Adafruit_BME280::Adafruit_BME280(int8_t cspin) _cs = cspin;
: _cs(cspin), _mosi(-1), _miso(-1), _sck(-1) _mosi = _miso = _sck = -1;
{ } _spi = theSPI;
}
/**************************************************************************/
/*! /*!
@brief class constructor if using software SPI * @brief class constructor if using software SPI
@param cspin the chip select pin to use * @param cspin the chip select pin to use
@param mosipin the MOSI pin to use * @param mosipin the MOSI pin to use
@param misopin the MISO pin to use * @param misopin the MISO pin to use
@param sckpin the SCK pin to use * @param sckpin the SCK pin to use
*/ */
/**************************************************************************/ Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin,
Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin) int8_t sckpin)
: _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin) : _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin) {}
{ }
/**************************************************************************/
/*! /*!
@brief Initialise sensor with given parameters / settings * @brief Initialise sensor with given parameters / settings
@param theWire the I2C object to use * @param theWire the I2C object to use
@returns true on success, false otherwise * @returns true on success, false otherwise
*/ */
/**************************************************************************/ bool Adafruit_BME280::begin(TwoWire *theWire) {
bool Adafruit_BME280::begin(TwoWire *theWire)
{
_wire = theWire; _wire = theWire;
_i2caddr = BME280_ADDRESS; _i2caddr = BME280_ADDRESS;
return init(); return init();
} }
/**************************************************************************/
/*! /*!
@brief Initialise sensor with given parameters / settings * @brief Initialise sensor with given parameters / settings
@param addr the I2C address the device can be found on * @param addr the I2C address the device can be found on
@returns true on success, false otherwise * @returns true on success, false otherwise
*/ */
/**************************************************************************/ bool Adafruit_BME280::begin(uint8_t addr) {
bool Adafruit_BME280::begin(uint8_t addr)
{
_i2caddr = addr; _i2caddr = addr;
_wire = &Wire; _wire = &Wire;
return init(); return init();
} }
/**************************************************************************/
/*! /*!
@brief Initialise sensor with given parameters / settings * @brief Initialise sensor with given parameters / settings
@param addr the I2C address the device can be found on * @param addr the I2C address the device can be found on
@param theWire the I2C object to use * @param theWire the I2C object to use
@returns true on success, false otherwise * @returns true on success, false otherwise
*/ */
/**************************************************************************/ bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) {
bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire)
{
_i2caddr = addr; _i2caddr = addr;
_wire = theWire; _wire = theWire;
return init(); return init();
} }
/**************************************************************************/
/*! /*!
@brief Initialise sensor with given parameters / settings * @brief Initialise sensor with given parameters / settings
@returns true on success, false otherwise * @returns true on success, false otherwise
*/ */
/**************************************************************************/ bool Adafruit_BME280::begin(void) {
bool Adafruit_BME280::begin(void)
{
bool status = false; bool status = false;
_i2caddr = BME280_ADDRESS; _i2caddr = BME280_ADDRESS;
_wire = &Wire; _wire = &Wire;
@ -128,14 +111,11 @@ bool Adafruit_BME280::begin(void)
return status; return status;
} }
/**************************************************************************/
/*! /*!
@brief Initialise sensor with given parameters / settings * @brief Initialise sensor with given parameters / settings
@returns true on success, false otherwise * @returns true on success, false otherwise
*/ */
/**************************************************************************/ bool Adafruit_BME280::init() {
bool Adafruit_BME280::init()
{
// init I2C or SPI sensor interface // init I2C or SPI sensor interface
if (_cs == -1) { if (_cs == -1) {
// I2C // I2C
@ -145,7 +125,7 @@ bool Adafruit_BME280::init()
pinMode(_cs, OUTPUT); pinMode(_cs, OUTPUT);
if (_sck == -1) { if (_sck == -1) {
// hardware SPI // hardware SPI
SPI.begin(); _spi->begin();
} else { } else {
// software SPI // software SPI
pinMode(_sck, OUTPUT); pinMode(_sck, OUTPUT);
@ -179,20 +159,18 @@ bool Adafruit_BME280::init()
return true; return true;
} }
/**************************************************************************/
/*! /*!
@brief setup sensor with given parameters / settings * @brief setup sensor with given parameters / settings
*
This is simply a overload to the normal begin()-function, so SPI users * This is simply a overload to the normal begin()-function, so SPI users
don't get confused about the library requiring an address. * don't get confused about the library requiring an address.
@param mode the power mode to use for the sensor * @param mode the power mode to use for the sensor
@param tempSampling the temp samping rate to use * @param tempSampling the temp samping rate to use
@param pressSampling the pressure sampling rate to use * @param pressSampling the pressure sampling rate to use
@param humSampling the humidity sampling rate to use * @param humSampling the humidity sampling rate to use
@param filter the filter mode to use * @param filter the filter mode to use
@param duration the standby duration to use * @param duration the standby duration to use
*/ */
/**************************************************************************/
void Adafruit_BME280::setSampling(sensor_mode mode, void Adafruit_BME280::setSampling(sensor_mode mode,
sensor_sampling tempSampling, sensor_sampling tempSampling,
sensor_sampling pressSampling, sensor_sampling pressSampling,
@ -203,31 +181,28 @@ void Adafruit_BME280::setSampling(sensor_mode mode,
_measReg.osrs_t = tempSampling; _measReg.osrs_t = tempSampling;
_measReg.osrs_p = pressSampling; _measReg.osrs_p = pressSampling;
_humReg.osrs_h = humSampling; _humReg.osrs_h = humSampling;
_configReg.filter = filter; _configReg.filter = filter;
_configReg.t_sb = duration; _configReg.t_sb = duration;
// you must make sure to also set REGISTER_CONTROL after setting the // you must make sure to also set REGISTER_CONTROL after setting the
// CONTROLHUMID register, otherwise the values won't be applied (see DS 5.4.3) // CONTROLHUMID register, otherwise the values won't be applied (see
// DS 5.4.3)
write8(BME280_REGISTER_CONTROLHUMID, _humReg.get()); write8(BME280_REGISTER_CONTROLHUMID, _humReg.get());
write8(BME280_REGISTER_CONFIG, _configReg.get()); write8(BME280_REGISTER_CONFIG, _configReg.get());
write8(BME280_REGISTER_CONTROL, _measReg.get()); write8(BME280_REGISTER_CONTROL, _measReg.get());
} }
/**************************************************************************/
/*! /*!
@brief Encapsulate hardware and software SPI transfer into one function * @brief Encapsulate hardware and software SPI transfer into one
@param x the data byte to transfer * function
@returns the data byte read from the device * @param x the data byte to transfer
* @returns the data byte read from the device
*/ */
/**************************************************************************/
uint8_t Adafruit_BME280::spixfer(uint8_t x) { uint8_t Adafruit_BME280::spixfer(uint8_t x) {
// hardware SPI // hardware SPI
if (_sck == -1) if (_sck == -1)
return SPI.transfer(x); return _spi->transfer(x);
// software SPI // software SPI
uint8_t reply = 0; uint8_t reply = 0;
@ -242,14 +217,11 @@ uint8_t Adafruit_BME280::spixfer(uint8_t x) {
return reply; return reply;
} }
/**************************************************************************/
/*! /*!
@brief Writes an 8 bit value over I2C or SPI * @brief Writes an 8 bit value over I2C or SPI
@param reg the register address to write to * @param reg the register address to write to
@param value the value to write to the register * @param value the value to write to the register
*/ */
/**************************************************************************/
void Adafruit_BME280::write8(byte reg, byte value) { void Adafruit_BME280::write8(byte reg, byte value) {
if (_cs == -1) { if (_cs == -1) {
_wire->beginTransmission((uint8_t)_i2caddr); _wire->beginTransmission((uint8_t)_i2caddr);
@ -258,24 +230,21 @@ void Adafruit_BME280::write8(byte reg, byte value) {
_wire->endTransmission(); _wire->endTransmission();
} else { } else {
if (_sck == -1) if (_sck == -1)
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); _spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW); digitalWrite(_cs, LOW);
spixfer(reg & ~0x80); // write, bit 7 low spixfer(reg & ~0x80); // write, bit 7 low
spixfer(value); spixfer(value);
digitalWrite(_cs, HIGH); digitalWrite(_cs, HIGH);
if (_sck == -1) if (_sck == -1)
SPI.endTransaction(); // release the SPI bus _spi->endTransaction(); // release the SPI bus
} }
} }
/**************************************************************************/
/*! /*!
@brief Reads an 8 bit value over I2C or SPI * @brief Reads an 8 bit value over I2C or SPI
@param reg the register address to read from * @param reg the register address to read from
@returns the data byte read from the device * @returns the data byte read from the device
*/ */
/**************************************************************************/
uint8_t Adafruit_BME280::read8(byte reg) { uint8_t Adafruit_BME280::read8(byte reg) {
uint8_t value; uint8_t value;
@ -287,27 +256,23 @@ uint8_t Adafruit_BME280::read8(byte reg) {
value = _wire->read(); value = _wire->read();
} else { } else {
if (_sck == -1) if (_sck == -1)
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); _spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW); digitalWrite(_cs, LOW);
spixfer(reg | 0x80); // read, bit 7 high spixfer(reg | 0x80); // read, bit 7 high
value = spixfer(0); value = spixfer(0);
digitalWrite(_cs, HIGH); digitalWrite(_cs, HIGH);
if (_sck == -1) if (_sck == -1)
SPI.endTransaction(); // release the SPI bus _spi->endTransaction(); // release the SPI bus
} }
return value; return value;
} }
/**************************************************************************/
/*! /*!
@brief Reads a 16 bit value over I2C or SPI * @brief Reads a 16 bit value over I2C or SPI
@param reg the register address to read from * @param reg the register address to read from
@returns the 16 bit data value read from the device * @returns the 16 bit data value read from the device
*/ */
/**************************************************************************/ uint16_t Adafruit_BME280::read16(byte reg) {
uint16_t Adafruit_BME280::read16(byte reg)
{
uint16_t value; uint16_t value;
if (_cs == -1) { if (_cs == -1) {
@ -318,67 +283,50 @@ uint16_t Adafruit_BME280::read16(byte reg)
value = (_wire->read() << 8) | _wire->read(); value = (_wire->read() << 8) | _wire->read();
} else { } else {
if (_sck == -1) if (_sck == -1)
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); _spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW); digitalWrite(_cs, LOW);
spixfer(reg | 0x80); // read, bit 7 high spixfer(reg | 0x80); // read, bit 7 high
value = (spixfer(0) << 8) | spixfer(0); value = (spixfer(0) << 8) | spixfer(0);
digitalWrite(_cs, HIGH); digitalWrite(_cs, HIGH);
if (_sck == -1) if (_sck == -1)
SPI.endTransaction(); // release the SPI bus _spi->endTransaction(); // release the SPI bus
} }
return value; return value;
} }
/**************************************************************************/
/*! /*!
@brief Reads a signed 16 bit little endian value over I2C or SPI * @brief Reads a signed 16 bit little endian value over I2C or SPI
@param reg the register address to read from * @param reg the register address to read from
@returns the 16 bit data value read from the device * @returns the 16 bit data value read from the device
*/ */
/**************************************************************************/
uint16_t Adafruit_BME280::read16_LE(byte reg) { uint16_t Adafruit_BME280::read16_LE(byte reg) {
uint16_t temp = read16(reg); uint16_t temp = read16(reg);
return (temp >> 8) | (temp << 8); return (temp >> 8) | (temp << 8);
} }
/**************************************************************************/
/*! /*!
@brief Reads a signed 16 bit value over I2C or SPI * @brief Reads a signed 16 bit value over I2C or SPI
@param reg the register address to read from * @param reg the register address to read from
@returns the 16 bit data value read from the device * @returns the 16 bit data value read from the device
*/ */
/**************************************************************************/ int16_t Adafruit_BME280::readS16(byte reg) { return (int16_t)read16(reg); }
int16_t Adafruit_BME280::readS16(byte reg)
{
return (int16_t)read16(reg);
}
/**************************************************************************/
/*! /*!
@brief Reads a signed little endian 16 bit value over I2C or SPI * @brief Reads a signed little endian 16 bit value over I2C or SPI
@param reg the register address to read from * @param reg the register address to read from
@returns the 16 bit data value read from the device * @returns the 16 bit data value read from the device
*/ */
/**************************************************************************/ int16_t Adafruit_BME280::readS16_LE(byte reg) {
int16_t Adafruit_BME280::readS16_LE(byte reg)
{
return (int16_t)read16_LE(reg); return (int16_t)read16_LE(reg);
} }
/**************************************************************************/
/*! /*!
@brief Reads a 24 bit value over I2C * @brief Reads a 24 bit value over I2C
@param reg the register address to read from * @param reg the register address to read from
@returns the 24 bit data value read from the device * @returns the 24 bit data value read from the device
*/ */
/**************************************************************************/ uint32_t Adafruit_BME280::read24(byte reg) {
uint32_t Adafruit_BME280::read24(byte reg)
{
uint32_t value; uint32_t value;
if (_cs == -1) { if (_cs == -1) {
@ -394,7 +342,7 @@ uint32_t Adafruit_BME280::read24(byte reg)
value |= _wire->read(); value |= _wire->read();
} else { } else {
if (_sck == -1) if (_sck == -1)
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); _spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW); digitalWrite(_cs, LOW);
spixfer(reg | 0x80); // read, bit 7 high spixfer(reg | 0x80); // read, bit 7 high
@ -406,20 +354,16 @@ uint32_t Adafruit_BME280::read24(byte reg)
digitalWrite(_cs, HIGH); digitalWrite(_cs, HIGH);
if (_sck == -1) if (_sck == -1)
SPI.endTransaction(); // release the SPI bus _spi->endTransaction(); // release the SPI bus
} }
return value; return value;
} }
/**************************************************************************/
/*! /*!
@brief Take a new measurement (only possible in forced mode) * @brief Take a new measurement (only possible in forced mode)
*/ */
/**************************************************************************/ void Adafruit_BME280::takeForcedMeasurement() {
void Adafruit_BME280::takeForcedMeasurement()
{
// If we are in forced mode, the BME sensor goes back to sleep after each // If we are in forced mode, the BME sensor goes back to sleep after each
// measurement and we need to set it to forced mode once at this point, so // measurement and we need to set it to forced mode once at this point, so
// it will take the next measurement and then return to sleep again. // it will take the next measurement and then return to sleep again.
@ -434,14 +378,10 @@ void Adafruit_BME280::takeForcedMeasurement()
} }
} }
/**************************************************************************/
/*! /*!
@brief Reads the factory-set coefficients * @brief Reads the factory-set coefficients
*/ */
/**************************************************************************/ void Adafruit_BME280::readCoefficients(void) {
void Adafruit_BME280::readCoefficients(void)
{
_bme280_calib.dig_T1 = read16_LE(BME280_REGISTER_DIG_T1); _bme280_calib.dig_T1 = read16_LE(BME280_REGISTER_DIG_T1);
_bme280_calib.dig_T2 = readS16_LE(BME280_REGISTER_DIG_T2); _bme280_calib.dig_T2 = readS16_LE(BME280_REGISTER_DIG_T2);
_bme280_calib.dig_T3 = readS16_LE(BME280_REGISTER_DIG_T3); _bme280_calib.dig_T3 = readS16_LE(BME280_REGISTER_DIG_T3);
@ -459,33 +399,28 @@ void Adafruit_BME280::readCoefficients(void)
_bme280_calib.dig_H1 = read8(BME280_REGISTER_DIG_H1); _bme280_calib.dig_H1 = read8(BME280_REGISTER_DIG_H1);
_bme280_calib.dig_H2 = readS16_LE(BME280_REGISTER_DIG_H2); _bme280_calib.dig_H2 = readS16_LE(BME280_REGISTER_DIG_H2);
_bme280_calib.dig_H3 = read8(BME280_REGISTER_DIG_H3); _bme280_calib.dig_H3 = read8(BME280_REGISTER_DIG_H3);
_bme280_calib.dig_H4 = (read8(BME280_REGISTER_DIG_H4) << 4) | (read8(BME280_REGISTER_DIG_H4+1) & 0xF); _bme280_calib.dig_H4 = (read8(BME280_REGISTER_DIG_H4) << 4) |
_bme280_calib.dig_H5 = (read8(BME280_REGISTER_DIG_H5+1) << 4) | (read8(BME280_REGISTER_DIG_H5) >> 4); (read8(BME280_REGISTER_DIG_H4 + 1) & 0xF);
_bme280_calib.dig_H5 = (read8(BME280_REGISTER_DIG_H5 + 1) << 4) |
(read8(BME280_REGISTER_DIG_H5) >> 4);
_bme280_calib.dig_H6 = (int8_t)read8(BME280_REGISTER_DIG_H6); _bme280_calib.dig_H6 = (int8_t)read8(BME280_REGISTER_DIG_H6);
} }
/**************************************************************************/
/*! /*!
@brief return true if chip is busy reading cal data * @brief return true if chip is busy reading cal data
@returns true if reading calibration, false otherwise * @returns true if reading calibration, false otherwise
*/ */
/**************************************************************************/ bool Adafruit_BME280::isReadingCalibration(void) {
bool Adafruit_BME280::isReadingCalibration(void)
{
uint8_t const rStatus = read8(BME280_REGISTER_STATUS); uint8_t const rStatus = read8(BME280_REGISTER_STATUS);
return (rStatus & (1 << 0)) != 0; return (rStatus & (1 << 0)) != 0;
} }
/**************************************************************************/
/*! /*!
@brief Returns the temperature from the sensor * @brief Returns the temperature from the sensor
@returns the temperature read from the device * @returns the temperature read from the device
*/ */
/**************************************************************************/ float Adafruit_BME280::readTemperature(void) {
float Adafruit_BME280::readTemperature(void)
{
int32_t var1, var2; int32_t var1, var2;
int32_t adc_T = read24(BME280_REGISTER_TEMPDATA); int32_t adc_T = read24(BME280_REGISTER_TEMPDATA);
@ -494,11 +429,14 @@ float Adafruit_BME280::readTemperature(void)
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))) *
((int32_t)_bme280_calib.dig_T2)) >> 11; ((int32_t)_bme280_calib.dig_T2)) >>
11;
var2 = (((((adc_T >> 4) - ((int32_t)_bme280_calib.dig_T1)) * var2 = (((((adc_T >> 4) - ((int32_t)_bme280_calib.dig_T1)) *
((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1))) >> 12) * ((adc_T >> 4) - ((int32_t)_bme280_calib.dig_T1))) >>
((int32_t)_bme280_calib.dig_T3)) >> 14; 12) *
((int32_t)_bme280_calib.dig_T3)) >>
14;
t_fine = var1 + var2; t_fine = var1 + var2;
@ -506,13 +444,10 @@ float Adafruit_BME280::readTemperature(void)
return T / 100; return T / 100;
} }
/**************************************************************************/
/*! /*!
@brief Returns the pressure from the sensor * @brief Returns the pressure from the sensor
@returns the pressure value (in Pascal) read from the device * @returns the pressure value (in Pascal) read from the device
*/ */
/**************************************************************************/
float Adafruit_BME280::readPressure(void) { float Adafruit_BME280::readPressure(void) {
int64_t var1, var2, p; int64_t var1, var2, p;
@ -529,7 +464,8 @@ float Adafruit_BME280::readPressure(void) {
var2 = var2 + (((int64_t)_bme280_calib.dig_P4) << 35); var2 = var2 + (((int64_t)_bme280_calib.dig_P4) << 35);
var1 = ((var1 * var1 * (int64_t)_bme280_calib.dig_P3) >> 8) + var1 = ((var1 * var1 * (int64_t)_bme280_calib.dig_P3) >> 8) +
((var1 * (int64_t)_bme280_calib.dig_P2) << 12); ((var1 * (int64_t)_bme280_calib.dig_P2) << 12);
var1 = (((((int64_t)1)<<47)+var1))*((int64_t)_bme280_calib.dig_P1)>>33; var1 =
(((((int64_t)1) << 47) + var1)) * ((int64_t)_bme280_calib.dig_P1) >> 33;
if (var1 == 0) { if (var1 == 0) {
return 0; // avoid exception caused by division by zero return 0; // avoid exception caused by division by zero
@ -543,13 +479,10 @@ float Adafruit_BME280::readPressure(void) {
return (float)p / 256; return (float)p / 256;
} }
/**************************************************************************/
/*! /*!
@brief Returns the humidity from the sensor * @brief Returns the humidity from the sensor
@returns the humidity value read from the device * @returns the humidity value read from the device
*/ */
/**************************************************************************/
float Adafruit_BME280::readHumidity(void) { float Adafruit_BME280::readHumidity(void) {
readTemperature(); // must be done first to get t_fine readTemperature(); // must be done first to get t_fine
@ -562,13 +495,21 @@ float Adafruit_BME280::readHumidity(void) {
v_x1_u32r = (t_fine - ((int32_t)76800)); v_x1_u32r = (t_fine - ((int32_t)76800));
v_x1_u32r = (((((adc_H << 14) - (((int32_t)_bme280_calib.dig_H4) << 20) - v_x1_u32r = (((((adc_H << 14) - (((int32_t)_bme280_calib.dig_H4) << 20) -
(((int32_t)_bme280_calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) * (((int32_t)_bme280_calib.dig_H5) * v_x1_u32r)) +
((int32_t)16384)) >>
15) *
(((((((v_x1_u32r * ((int32_t)_bme280_calib.dig_H6)) >> 10) * (((((((v_x1_u32r * ((int32_t)_bme280_calib.dig_H6)) >> 10) *
(((v_x1_u32r * ((int32_t)_bme280_calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) + (((v_x1_u32r * ((int32_t)_bme280_calib.dig_H3)) >> 11) +
((int32_t)2097152)) * ((int32_t)_bme280_calib.dig_H2) + 8192) >> 14)); ((int32_t)32768))) >>
10) +
((int32_t)2097152)) *
((int32_t)_bme280_calib.dig_H2) +
8192) >>
14));
v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
((int32_t)_bme280_calib.dig_H1)) >> 4)); ((int32_t)_bme280_calib.dig_H1)) >>
4));
v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r; v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r; v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
@ -576,18 +517,13 @@ float Adafruit_BME280::readHumidity(void) {
return h / 1024.0; return h / 1024.0;
} }
/**************************************************************************/
/*! /*!
Calculates the altitude (in meters) from the specified atmospheric * Calculates the altitude (in meters) from the specified atmospheric
pressure (in hPa), and sea-level pressure (in hPa). * pressure (in hPa), and sea-level pressure (in hPa).
* @param seaLevel Sea-level pressure in hPa
@param seaLevel Sea-level pressure in hPa * @returns the altitude value read from the device
@returns the altitude value read from the device
*/ */
/**************************************************************************/ float Adafruit_BME280::readAltitude(float seaLevel) {
float Adafruit_BME280::readAltitude(float seaLevel)
{
// Equation taken from BMP180 datasheet (page 16): // Equation taken from BMP180 datasheet (page 16):
// http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
@ -599,18 +535,14 @@ float Adafruit_BME280::readAltitude(float seaLevel)
return 44330.0 * (1.0 - pow(atmospheric / seaLevel, 0.1903)); return 44330.0 * (1.0 - pow(atmospheric / seaLevel, 0.1903));
} }
/**************************************************************************/
/*! /*!
Calculates the pressure at sea level (in hPa) from the specified altitude * Calculates the pressure at sea level (in hPa) from the specified
(in meters), and atmospheric pressure (in hPa). * altitude (in meters), and atmospheric pressure (in hPa).
@param altitude Altitude in meters * @param altitude Altitude in meters
@param atmospheric Atmospheric pressure in hPa * @param atmospheric Atmospheric pressure in hPa
@returns the pressure at sea level (in hPa) from the specified altitude * @returns the pressure at sea level (in hPa) from the specified altitude
*/ */
/**************************************************************************/ float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric) {
float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric)
{
// Equation taken from BMP180 datasheet (page 17): // Equation taken from BMP180 datasheet (page 17):
// http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
@ -621,13 +553,8 @@ float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric)
return atmospheric / pow(1.0 - (altitude / 44330.0), 5.255); return atmospheric / pow(1.0 - (altitude / 44330.0), 5.255);
} }
/**************************************************************************/
/*! /*!
Returns Sensor ID found by init() for diagnostics * Returns Sensor ID found by init() for diagnostics
@returns Sensor ID 0x60 for BME280, 0x56, 0x57, 0x58 BMP280 * @returns Sensor ID 0x60 for BME280, 0x56, 0x57, 0x58 BMP280
*/ */
/**************************************************************************/ uint32_t Adafruit_BME280::sensorID(void) { return _sensorID; }
uint32_t Adafruit_BME280::sensorID(void)
{
return _sensorID;
}

View File

@ -21,36 +21,25 @@
#ifndef __BME280_H__ #ifndef __BME280_H__
#define __BME280_H__ #define __BME280_H__
#if (ARDUINO >= 100)
#include "Arduino.h" #include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <SPI.h>
#include <Wire.h> #include <Wire.h>
/**************************************************************************/
/*! /*!
@brief default I2C address * @brief default I2C address
*/ */
/**************************************************************************/
#define BME280_ADDRESS (0x77) // Primary I2C Address #define BME280_ADDRESS (0x77) // Primary I2C Address
/**************************************************************************/
/*! /*!
@brief alternate I2C address * @brief alternate I2C address
*/ */
/**************************************************************************/
#define BME280_ADDRESS_ALTERNATE (0x76) // Alternate Address #define BME280_ADDRESS_ALTERNATE (0x76) // Alternate Address
/*=========================================================================*/
/**************************************************************************/
/*! /*!
@brief Register addresses * @brief Register addresses
*/ */
/**************************************************************************/ enum {
enum
{
BME280_REGISTER_DIG_T1 = 0x88, BME280_REGISTER_DIG_T1 = 0x88,
BME280_REGISTER_DIG_T2 = 0x8A, BME280_REGISTER_DIG_T2 = 0x8A,
BME280_REGISTER_DIG_T3 = 0x8C, BME280_REGISTER_DIG_T3 = 0x8C,
@ -92,8 +81,7 @@
@brief calibration data @brief calibration data
*/ */
/**************************************************************************/ /**************************************************************************/
typedef struct typedef struct {
{
uint16_t dig_T1; ///< temperature compensation value uint16_t dig_T1; ///< temperature compensation value
int16_t dig_T2; ///< temperature compensation value int16_t dig_T2; ///< temperature compensation value
int16_t dig_T3; ///< temperature compensation value int16_t dig_T3; ///< temperature compensation value
@ -200,11 +188,12 @@ class Adafruit_BME280 {
}; };
// constructors // constructors
Adafruit_BME280(void); Adafruit_BME280();
Adafruit_BME280(int8_t cspin); Adafruit_BME280(int8_t cspin, SPIClass *theSPI = &SPI)
Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin); Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin,
int8_t sckpin);
bool begin(void); bool begin();
bool begin(TwoWire *theWire); bool begin(TwoWire *theWire);
bool begin(uint8_t addr); bool begin(uint8_t addr);
bool begin(uint8_t addr, TwoWire *theWire); bool begin(uint8_t addr, TwoWire *theWire);
@ -215,8 +204,7 @@ class Adafruit_BME280 {
sensor_sampling pressSampling = SAMPLING_X16, sensor_sampling pressSampling = SAMPLING_X16,
sensor_sampling humSampling = SAMPLING_X16, sensor_sampling humSampling = SAMPLING_X16,
sensor_filter filter = FILTER_OFF, sensor_filter filter = FILTER_OFF,
standby_duration duration = STANDBY_MS_0_5 standby_duration duration = STANDBY_MS_0_5);
);
void takeForcedMeasurement(); void takeForcedMeasurement();
float readTemperature(void); float readTemperature(void);
@ -229,6 +217,7 @@ class Adafruit_BME280 {
protected: protected:
TwoWire *_wire; //!< pointer to a TwoWire object TwoWire *_wire; //!< pointer to a TwoWire object
SPIClass *_spi; //!< pointer to SPI object
void readCoefficients(void); void readCoefficients(void);
bool isReadingCalibration(void); bool isReadingCalibration(void);
uint8_t spixfer(uint8_t x); uint8_t spixfer(uint8_t x);
@ -243,7 +232,9 @@ class Adafruit_BME280 {
uint8_t _i2caddr; //!< I2C addr for the TwoWire interface uint8_t _i2caddr; //!< I2C addr for the TwoWire interface
int32_t _sensorID; //!< ID of the BME Sensor int32_t _sensorID; //!< ID of the BME Sensor
int32_t t_fine; //!< temperature with high resolution, stored as an attribute as this is used for temperature compensation reading humidity and pressure int32_t t_fine; //!< temperature with high resolution, stored as an attribute
//!< as this is used for temperature compensation reading
//!< humidity and pressure
int8_t _cs; //!< for the SPI interface int8_t _cs; //!< for the SPI interface
int8_t _mosi; //!< for the SPI interface int8_t _mosi; //!< for the SPI interface
@ -252,7 +243,6 @@ class Adafruit_BME280 {
bme280_calib_data _bme280_calib; //!< here calibration data is stored bme280_calib_data _bme280_calib; //!< here calibration data is stored
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief config register @brief config register
@ -283,13 +273,10 @@ class Adafruit_BME280 {
unsigned int spi3w_en : 1; ///< unused - don't set unsigned int spi3w_en : 1; ///< unused - don't set
/// @return combined config register /// @return combined config register
unsigned int get() { unsigned int get() { return (t_sb << 5) | (filter << 2) | spi3w_en; }
return (t_sb << 5) | (filter << 2) | spi3w_en;
}
}; };
config _configReg; //!< config register object config _configReg; //!< config register object
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief ctrl_meas register @brief ctrl_meas register
@ -321,13 +308,10 @@ class Adafruit_BME280 {
unsigned int mode : 2; ///< device mode unsigned int mode : 2; ///< device mode
/// @return combined ctrl register /// @return combined ctrl register
unsigned int get() { unsigned int get() { return (osrs_t << 5) | (osrs_p << 2) | mode; }
return (osrs_t << 5) | (osrs_p << 2) | mode;
}
}; };
ctrl_meas _measReg; //!< measurement register object ctrl_meas _measReg; //!< measurement register object
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief ctrl_hum register @brief ctrl_hum register
@ -347,9 +331,7 @@ class Adafruit_BME280 {
unsigned int osrs_h : 3; ///< pressure oversampling unsigned int osrs_h : 3; ///< pressure oversampling
/// @return combined ctrl hum register /// @return combined ctrl hum register
unsigned int get() { unsigned int get() { return (osrs_h); }
return (osrs_h);
}
}; };
ctrl_hum _humReg; //!< hum register object ctrl_hum _humReg; //!< hum register object
}; };