Implemented sensor modes and settings among other improvements

This commit is contained in:
mozzbozz 2016-08-18 23:24:14 +02:00
parent c96d20544a
commit 9e676f1d64
3 changed files with 621 additions and 291 deletions

View File

@ -19,12 +19,9 @@
#include <SPI.h>
#include "Adafruit_BME280.h"
/***************************************************************************
PRIVATE FUNCTIONS
***************************************************************************/
Adafruit_BME280::Adafruit_BME280()
: _cs(-1), _mosi(-1), _miso(-1), _sck(-1)
{ }
@ -38,16 +35,29 @@ Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, i
{ }
bool Adafruit_BME280::begin(uint8_t a) {
_i2caddr = a;
/**************************************************************************/
/*!
@brief Initialise sensor with given parameters / settings
*/
/**************************************************************************/
bool Adafruit_BME280::begin(uint8_t addr,
sensor_mode mode,
sensor_sampling tempSampling,
sensor_sampling pressSampling,
sensor_sampling humSampling,
sensor_filter filter,
standby_duration duration
)
{
_i2caddr = addr;
// init I2C or SPI sensor interface
if (_cs == -1) {
// i2c
// I2C
Wire.begin();
} else {
digitalWrite(_cs, HIGH);
pinMode(_cs, OUTPUT);
if (_sck == -1) {
// hardware SPI
SPI.begin();
@ -59,24 +69,63 @@ bool Adafruit_BME280::begin(uint8_t a) {
}
}
// check if sensor, i.e. the chip ID is correct
if (read8(BME280_REGISTER_CHIPID) != 0x60)
return false;
readCoefficients();
readCoefficients(); // read trimming parameters, see DS 4.2.2
//Set before CONTROL_meas (DS 5.4.3)
write8(BME280_REGISTER_CONTROLHUMID, 0x05); //16x oversampling
_measReg.mode = mode;
_measReg.osrs_t = tempSampling;
_measReg.osrs_p = pressSampling;
_humReg.osrs_h = humSampling;
_configReg.filter = filter;
_configReg.t_sb = duration;
// 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)
write8(BME280_REGISTER_CONTROLHUMID, _humReg.get());
write8(BME280_REGISTER_CONFIG, _configReg.get());
write8(BME280_REGISTER_CONTROL, _measReg.get());
write8(BME280_REGISTER_CONTROL, 0xB7); // 16x ovesampling, normal mode
return true;
}
/**************************************************************************/
/*!
@brief Initialise sensor with given parameters / settings
This is simply a overload to the normal begin()-function, so SPI users
don't get confused about the library requiring an address.
*/
/**************************************************************************/
bool Adafruit_BME280::begin(sensor_mode mode,
sensor_sampling tempSampling,
sensor_sampling pressSampling,
sensor_sampling humSampling,
sensor_filter filter,
standby_duration duration
)
{
return begin(BME280_ADDRESS, mode, tempSampling, pressSampling, humSampling, filter, duration);
}
/**************************************************************************/
/*!
@brief Encapsulate hardware and software SPI transfer into one function
*/
/**************************************************************************/
uint8_t Adafruit_BME280::spixfer(uint8_t x) {
// hardware SPI
if (_sck == -1)
return SPI.transfer(x);
// software spi
//Serial.println("Software SPI");
// software SPI
uint8_t reply = 0;
for (int i=7; i>=0; i--) {
reply <<= 1;
@ -89,13 +138,13 @@ uint8_t Adafruit_BME280::spixfer(uint8_t x) {
return reply;
}
/**************************************************************************/
/*!
@brief Writes an 8 bit value over I2C/SPI
@brief Writes an 8 bit value over I2C or SPI
*/
/**************************************************************************/
void Adafruit_BME280::write8(byte reg, byte value)
{
void Adafruit_BME280::write8(byte reg, byte value) {
if (_cs == -1) {
Wire.beginTransmission((uint8_t)_i2caddr);
Wire.write((uint8_t)reg);
@ -113,13 +162,13 @@ void Adafruit_BME280::write8(byte reg, byte value)
}
}
/**************************************************************************/
/*!
@brief Reads an 8 bit value over I2C
@brief Reads an 8 bit value over I2C or SPI
*/
/**************************************************************************/
uint8_t Adafruit_BME280::read8(byte reg)
{
uint8_t Adafruit_BME280::read8(byte reg) {
uint8_t value;
if (_cs == -1) {
@ -128,7 +177,6 @@ uint8_t Adafruit_BME280::read8(byte reg)
Wire.endTransmission();
Wire.requestFrom((uint8_t)_i2caddr, (byte)1);
value = Wire.read();
} else {
if (_sck == -1)
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
@ -142,9 +190,10 @@ uint8_t Adafruit_BME280::read8(byte reg)
return value;
}
/**************************************************************************/
/*!
@brief Reads a 16 bit value over I2C
@brief Reads a 16 bit value over I2C or SPI
*/
/**************************************************************************/
uint16_t Adafruit_BME280::read16(byte reg)
@ -157,7 +206,6 @@ uint16_t Adafruit_BME280::read16(byte reg)
Wire.endTransmission();
Wire.requestFrom((uint8_t)_i2caddr, (byte)2);
value = (Wire.read() << 8) | Wire.read();
} else {
if (_sck == -1)
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
@ -172,27 +220,37 @@ uint16_t Adafruit_BME280::read16(byte reg)
return value;
}
uint16_t Adafruit_BME280::read16_LE(byte reg) {
uint16_t temp = read16(reg);
return (temp >> 8) | (temp << 8);
}
/**************************************************************************/
/*!
@brief Reads a signed 16 bit value over I2C
*/
/**************************************************************************/
uint16_t Adafruit_BME280::read16_LE(byte reg) {
uint16_t temp = read16(reg);
return (temp >> 8) | (temp << 8);
}
/**************************************************************************/
/*!
@brief Reads a signed 16 bit value over I2C or SPI
*/
/**************************************************************************/
int16_t Adafruit_BME280::readS16(byte reg)
{
return (int16_t)read16(reg);
}
/**************************************************************************/
/*!
*/
/**************************************************************************/
int16_t Adafruit_BME280::readS16_LE(byte reg)
{
return (int16_t)read16_LE(reg);
}
@ -201,7 +259,6 @@ 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;
@ -217,7 +274,6 @@ uint32_t Adafruit_BME280::read24(byte reg)
value |= Wire.read();
value <<= 8;
value |= Wire.read();
} else {
if (_sck == -1)
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
@ -239,6 +295,27 @@ uint32_t Adafruit_BME280::read24(byte reg)
}
/**************************************************************************/
/*!
@brief Take a new measurement (only possible in forced mode)
*/
/**************************************************************************/
void Adafruit_BME280::takeForcedMeasurement()
{
// 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
// it will take the next measurement and then return to sleep again.
// In normal mode simply does new measurements periodically.
if (_measReg.mode == MODE_FORCED) {
// set to forced mode, i.e. "take next measurement"
write8(BME280_REGISTER_CONTROL, _measReg.get());
// wait until measurement has been completed, otherwise we would read
// the values from the last measurement
while (read8(BME280_REGISTER_STATUS) & 0x08);
}
}
/**************************************************************************/
/*!
@brief Reads the factory-set coefficients
@ -268,9 +345,10 @@ void Adafruit_BME280::readCoefficients(void)
_bme280_calib.dig_H6 = (int8_t)read8(BME280_REGISTER_DIG_H6);
}
/**************************************************************************/
/*!
@brief Returns the temperature from the sensor
*/
/**************************************************************************/
float Adafruit_BME280::readTemperature(void)
@ -278,6 +356,8 @@ float Adafruit_BME280::readTemperature(void)
int32_t var1, var2;
int32_t adc_T = read24(BME280_REGISTER_TEMPDATA);
if (adc_T == 0x800000) // value in case temp measurement was disabled
return NAN;
adc_T >>= 4;
var1 = ((((adc_T>>3) - ((int32_t)_bme280_calib.dig_T1 <<1))) *
@ -293,9 +373,10 @@ float Adafruit_BME280::readTemperature(void)
return T/100;
}
/**************************************************************************/
/*!
@brief Returns the temperature from the sensor
*/
/**************************************************************************/
float Adafruit_BME280::readPressure(void) {
@ -304,6 +385,8 @@ float Adafruit_BME280::readPressure(void) {
readTemperature(); // must be done first to get t_fine
int32_t adc_P = read24(BME280_REGISTER_PRESSUREDATA);
if (adc_P == 0x800000) // value in case pressure measurement was disabled
return NAN;
adc_P >>= 4;
var1 = ((int64_t)t_fine) - 128000;
@ -329,14 +412,15 @@ float Adafruit_BME280::readPressure(void) {
/**************************************************************************/
/*!
@brief Returns the humidity from the sensor
*/
/**************************************************************************/
float Adafruit_BME280::readHumidity(void) {
readTemperature(); // must be done first to get t_fine
int32_t adc_H = read16(BME280_REGISTER_HUMIDDATA);
if (adc_H == 0x8000) // value in case humidity measurement was disabled
return NAN;
int32_t v_x1_u32r;
@ -357,6 +441,7 @@ float Adafruit_BME280::readHumidity(void) {
return h / 1024.0;
}
/**************************************************************************/
/*!
Calculates the altitude (in meters) from the specified atmospheric
@ -379,6 +464,7 @@ float Adafruit_BME280::readAltitude(float seaLevel)
return 44330.0 * (1.0 - pow(atmospheric / seaLevel, 0.1903));
}
/**************************************************************************/
/*!
Calculates the pressure at sea level (in hPa) from the specified altitude

View File

@ -65,11 +65,12 @@
BME280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0
BME280_REGISTER_CONTROLHUMID = 0xF2,
BME280_REGISTER_STATUS = 0XF3,
BME280_REGISTER_CONTROL = 0xF4,
BME280_REGISTER_CONFIG = 0xF5,
BME280_REGISTER_PRESSUREDATA = 0xF7,
BME280_REGISTER_TEMPDATA = 0xFA,
BME280_REGISTER_HUMIDDATA = 0xFD,
BME280_REGISTER_HUMIDDATA = 0xFD
};
/*=========================================================================*/
@ -123,22 +124,76 @@ class Adafruit_BME280_Unified : public Adafruit_Sensor
*/
class Adafruit_BME280
{
class Adafruit_BME280 {
public:
enum sensor_sampling {
SAMPLING_NONE = 0b000,
SAMPLING_X1 = 0b001,
SAMPLING_X2 = 0b010,
SAMPLING_X4 = 0b011,
SAMPLING_X8 = 0b100,
SAMPLING_X16 = 0b101
};
enum sensor_mode {
MODE_SLEEP = 0b00,
MODE_FORCED = 0b01,
MODE_NORMAL = 0b11
};
enum sensor_filter {
FILTER_OFF = 0b000,
FILTER_X2 = 0b001,
FILTER_X4 = 0b010,
FILTER_X8 = 0b011,
FILTER_X16 = 0b100
};
// standby durations in ms
enum standby_duration {
STANDBY_MS_0_5 = 0b000,
STANDBY_MS_10 = 0b110,
STANDBY_MS_20 = 0b111,
STANDBY_MS_62_5 = 0b001,
STANDBY_MS_125 = 0b010,
STANDBY_MS_250 = 0b011,
STANDBY_MS_500 = 0b100,
STANDBY_MS_1000 = 0b101
};
// constructors
Adafruit_BME280(void);
Adafruit_BME280(int8_t cspin);
Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin);
bool begin(uint8_t addr = BME280_ADDRESS);
bool begin(uint8_t addr = BME280_ADDRESS,
sensor_mode mode = MODE_NORMAL,
sensor_sampling tempSampling = SAMPLING_X16,
sensor_sampling pressSampling = SAMPLING_X16,
sensor_sampling humSampling = SAMPLING_X16,
sensor_filter filter = FILTER_OFF,
standby_duration duration = STANDBY_MS_0_5
);
// overload function without address (-> for SPI users)
bool begin(sensor_mode mode = MODE_NORMAL,
sensor_sampling tempSampling = SAMPLING_X16,
sensor_sampling pressSampling = SAMPLING_X16,
sensor_sampling humSampling = SAMPLING_X16,
sensor_filter filter = FILTER_OFF,
standby_duration duration = STANDBY_MS_0_5
);
void takeForcedMeasurement();
float readTemperature(void);
float readPressure(void);
float readHumidity(void);
float readAltitude(float seaLevel);
float seaLevelForAltitude(float altitude, float atmospheric);
float seaLevelForAltitude(float altitude, float pressure);
private:
void readCoefficients(void);
uint8_t spixfer(uint8_t x);
@ -158,6 +213,90 @@ class Adafruit_BME280
bme280_calib_data _bme280_calib;
// The config register
struct config {
// inactive duration (standby time) in normal mode
// 000 = 0.5 ms
// 001 = 62.5 ms
// 010 = 125 ms
// 011 = 250 ms
// 100 = 500 ms
// 101 = 1000 ms
// 110 = 10 ms
// 111 = 20 ms
unsigned int t_sb : 3;
// filter settings
// 000 = filter off
// 001 = 2x filter
// 010 = 4x filter
// 011 = 8x filter
// 100 and above = 16x filter
unsigned int filter : 3;
// unused - don't set
unsigned int none : 1;
unsigned int spi3w_en : 1;
unsigned int get() {
return (t_sb << 5) | (filter << 3) | spi3w_en;
}
};
config _configReg;
// The ctrl_meas register
struct ctrl_meas {
// temperature oversampling
// 000 = skipped
// 001 = x1
// 010 = x2
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_t : 3;
// pressure oversampling
// 000 = skipped
// 001 = x1
// 010 = x2
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_p : 3;
// device mode
// 00 = sleep
// 01 or 10 = forced
// 11 = normal
unsigned int mode : 2;
unsigned int get() {
return (osrs_t << 5) | (osrs_p << 3) | mode;
}
};
ctrl_meas _measReg;
// The ctrl_hum register
struct ctrl_hum {
// unused - don't set
unsigned int none : 5;
// pressure oversampling
// 000 = skipped
// 001 = x1
// 010 = x2
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_h : 3;
unsigned int get() {
return (osrs_h);
}
};
ctrl_hum _humReg;
};
#endif

View File

@ -5,7 +5,7 @@
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface.
to interface. The device's I2C address is either 0x76 or 0x77.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
@ -29,19 +29,125 @@
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
unsigned long delayTime;
void setup() {
Serial.begin(9600);
Serial.println(F("BME280 test"));
if (!bme.begin()) {
bool status;
// default settings
status = bme.begin();
Serial.println("-- Default Test --");
Serial.println("normal mode, 16x oversampling for all, filter off,");
Serial.println("0.5ms standby period");
delayTime = 5000;
// For more details on the following scenarious, see chapter
// 3.5 "Recommended modes of operation" in the datasheet
/*
// weather monitoring
Serial.println("-- Weather Station Scenario --");
Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
Serial.println("filter off");
status = bme.begin(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF
);
// suggested rate is 1/60Hz (1m)
delayTime = 60e3;
*/
/*
// humidity sensing
Serial.println("-- Humidity Sensing Scenario --");
Serial.println("forced mode, 1x temperature / 1x humidity / 0x pressure oversampling");
Serial.println("= pressure off, filter off");
status = bme.begin(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_NONE, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF
);
// suggested rate is 1Hz (1s)
delayTime = 1e3;
*/
/*
// indoor navigation
Serial.println("-- Indoor Navigation Scenario --");
Serial.println("normal mode, 16x pressure / 2x temperature / 1x humidity oversampling,");
Serial.println("0.5ms standby period, filter 16x");
status = bme.begin(Adafruit_BME280::MODE_NORMAL,
Adafruit_BME280::SAMPLING_X2, // temperature
Adafruit_BME280::SAMPLING_X16, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_X16,
Adafruit_BME280::STANDBY_MS_0_5
);
// suggested rate is 25Hz
// 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
// T_ovs = 2
// P_ovs = 16
// H_ovs = 1
// = 40ms (25Hz)
// with standby time that should really be 24.16913... Hz
delayTime = 41;
*/
/*
// gaming
Serial.println("-- Gaming Scenario --");
Serial.println("normal mode, 4x pressure / 1x temperature / 0x humidity oversampling,");
Serial.println("= humidity off, 0.5ms standby period, filter 16x");
status = bme.begin(Adafruit_BME280::MODE_NORMAL,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X4, // pressure
Adafruit_BME280::SAMPLING_NONE, // humidity
Adafruit_BME280::FILTER_X16,
Adafruit_BME280::STANDBY_MS_0_5
);
// Suggested rate is 83Hz
// 1 + (2 * T_ovs) + (2 * P_ovs + 0.5)
// T_ovs = 1
// P_ovs = 4
// = 11.5ms + 0.5ms standby
delayTime = 12;
*/
Serial.println();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
void loop() {
// Only needed in forced mode! In normal mode, you can remove the next line.
bme.takeForcedMeasurement(); // has no effect in normal mode
printValues();
delay(delayTime);
}
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
@ -60,5 +166,4 @@ void loop() {
Serial.println(" %");
Serial.println();
delay(2000);
}