Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct implementation of forced measurements #91

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions examples/BME280_Modes/BME280_Modes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ BME280I2C::Settings settings(
BME280::Mode_Forced,
BME280::StandbyTime_1000ms,
BME280::Filter_Off,
BME280::SpiEnable_False,
BME280I2C::I2CAddr_0x76 // I2C address. I2C specific.
);

Expand Down Expand Up @@ -111,7 +110,7 @@ void setup()
void loop()
{
printBME280Data(&Serial);
delay(500);
delay(1000);
}

//////////////////////////////////////////////////////////////////
Expand All @@ -120,13 +119,19 @@ void printBME280Data
Stream* client
)
{
float temp(NAN), hum(NAN), pres(NAN);
// Force measurement and wait till it is completed (see Data sheet)
bme.force();
while(bme.busy()) delay(1);

// Read last measurement
float temp(NAN), hum(NAN), pres(NAN);

BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);

bme.read(pres, temp, hum, tempUnit, presUnit);

// Print result
client->print("Temp: ");
client->print(temp);
client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
Expand All @@ -136,6 +141,4 @@ void printBME280Data
client->print("\t\tPressure: ");
client->print(pres);
client->println("Pa");

delay(1000);
}
7 changes: 4 additions & 3 deletions examples/BME_280_BRZO_I2C_Test/BME_280_BRZO_I2C_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void setup()
void loop()
{
printBME280Data(&Serial);
delay(500);
delay(1000);
}

//////////////////////////////////////////////////////////////////
Expand All @@ -81,6 +81,9 @@ void printBME280Data
Stream* client
)
{
bme.force();
while(bme.busy()) delay(1);

float temp(NAN), hum(NAN), pres(NAN);

BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
Expand All @@ -97,6 +100,4 @@ void printBME280Data
client->print("\t\tPressure: ");
client->print(pres);
client->println("Pa");

delay(1000);
}
9 changes: 5 additions & 4 deletions examples/BME_280_I2C_Test/BME_280_I2C_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void setup()
void loop()
{
printBME280Data(&Serial);
delay(500);
delay(1000);
}

//////////////////////////////////////////////////////////////////
Expand All @@ -69,7 +69,10 @@ void printBME280Data
Stream* client
)
{
float temp(NAN), hum(NAN), pres(NAN);
bme.force();
delay(10);

float temp(NAN), hum(NAN), pres(NAN);

BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
Expand All @@ -85,6 +88,4 @@ void printBME280Data
client->print("\t\tPressure: ");
client->print(pres);
client->println("Pa");

delay(1000);
}
5 changes: 3 additions & 2 deletions examples/BME_280_Spi_Sw_Test/BME_280_Spi_Sw_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ void printBME280Data
Stream* client
)
{
bme.force();
while(bme.busy()) delay(1);

float temp(NAN), hum(NAN), pres(NAN);

BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
Expand All @@ -89,6 +92,4 @@ void printBME280Data
client->print("\t\tPressure: ");
client->print(pres);
client->println("Pa");

delay(1000);
}
7 changes: 4 additions & 3 deletions examples/BME_280_Spi_Test/BME_280_Spi_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void setup()
void loop()
{
printBME280Data(&Serial);
delay(500);
delay(1000);
}

//////////////////////////////////////////////////////////////////
Expand All @@ -74,6 +74,9 @@ void printBME280Data
Stream* client
)
{
bme.force();
while(bme.busy()) delay(1);

float temp(NAN), hum(NAN), pres(NAN);

BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
Expand All @@ -90,6 +93,4 @@ void printBME280Data
client->print("\t\tPressure: ");
client->print(pres);
client->println("Pa");

delay(1000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ BME280I2C::Settings settings(
BME280::Mode_Forced,
BME280::StandbyTime_1000ms,
BME280::Filter_16,
BME280::SpiEnable_False,
BME280I2C::I2CAddr_0x76
);

Expand Down Expand Up @@ -81,7 +80,7 @@ void setup()
void loop()
{
printBME280Data(&Serial);
delay(500);
delay(1000);
}

//////////////////////////////////////////////////////////////////
Expand All @@ -90,6 +89,9 @@ void printBME280Data
Stream* client
)
{
bme.force();
while(bme.busy()) delay(1);

float temp(NAN), hum(NAN), pres(NAN);

BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
Expand Down Expand Up @@ -140,6 +142,4 @@ void printBME280Data

client->print("\t\tAbsolute Humidity: ");
client->println(absHum);

delay(1000);
}
102 changes: 80 additions & 22 deletions src/BME280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ bool BME280::InitializeFilter()
read(dummy, dummy, dummy);

m_settings.filter = filter;

return true;
}


Expand Down Expand Up @@ -112,13 +114,13 @@ bool BME280::ReadChipID()
/****************************************************************/
bool BME280::WriteSettings()
{
uint8_t ctrlHum, ctrlMeas, config;
CalculateRegisters();

CalculateRegisters(ctrlHum, ctrlMeas, config);
WriteRegister(CTRL_HUM_ADDR, mCtrlHum);
WriteRegister(CTRL_MEAS_ADDR, mCtrlMeas);
WriteRegister(CONFIG_ADDR, mConfig);

WriteRegister(CTRL_HUM_ADDR, ctrlHum);
WriteRegister(CTRL_MEAS_ADDR, ctrlMeas);
WriteRegister(CONFIG_ADDR, config);
return true;
}


Expand Down Expand Up @@ -152,19 +154,14 @@ bool BME280::begin
}

/****************************************************************/
void BME280::CalculateRegisters
(
uint8_t& ctrlHum,
uint8_t& ctrlMeas,
uint8_t& config
)
void BME280::CalculateRegisters()
{
// ctrl_hum register. (ctrl_hum[2:0] = Humidity oversampling rate.)
ctrlHum = (uint8_t)m_settings.humOSR;
mCtrlHum = (uint8_t)m_settings.humOSR;
// ctrl_meas register. (ctrl_meas[7:5] = temperature oversampling rate, ctrl_meas[4:2] = pressure oversampling rate, ctrl_meas[1:0] = mode.)
ctrlMeas = ((uint8_t)m_settings.tempOSR << 5) | ((uint8_t)m_settings.presOSR << 2) | (uint8_t)m_settings.mode;
mCtrlMeas = ((uint8_t)m_settings.tempOSR << 5) | ((uint8_t)m_settings.presOSR << 2) | (uint8_t)m_settings.mode;
// config register. (config[7:5] = standby time, config[4:2] = filter, ctrl_meas[0] = spi enable.)
config = ((uint8_t)m_settings.standbyTime << 5) | ((uint8_t)m_settings.filter << 2) | (uint8_t)m_settings.spiEnable;
mConfig = ((uint8_t)m_settings.standbyTime << 5) | ((uint8_t)m_settings.filter << 2) | (uint8_t)m_settings.spiEnable;
}


Expand Down Expand Up @@ -204,6 +201,14 @@ bool BME280::ReadTrim()
}


/****************************************************************/
bool BME280::ForceMeasurement()
{
// For forced mode we need to write the mode to BME280 register before reading
return (m_settings.mode == Mode_Forced) && WriteRegister(CTRL_MEAS_ADDR, mCtrlMeas);
}


/****************************************************************/
bool BME280::ReadData
(
Expand All @@ -213,11 +218,6 @@ bool BME280::ReadData
bool success;
uint8_t buffer[SENSOR_DATA_LENGTH];

// For forced mode we need to write the mode to BME280 register before reading
if (m_settings.mode == Mode_Forced)
{
WriteSettings();
}

// Registers are in order. So we can start at the pressure register and read 8 bytes.
success = ReadRegister(PRESS_ADDR, buffer, SENSOR_DATA_LENGTH);
Expand All @@ -241,6 +241,35 @@ bool BME280::ReadData
}


/****************************************************************/
bool BME280::ReadStatus
(
bool& measuring,
bool& im_update
)
{
uint8_t status = 0;
bool success = ReadRegister(STATUS_ADDR, &status, 1);

if(success) {
measuring = status & (1 << 3);
im_update = status & 1;
}

return success;
}


/****************************************************************/
bool BME280::ReadCtrlMeas
(
uint8_t& data
)
{
return ReadRegister(CTRL_MEAS_ADDR, &data, 1);
}


/****************************************************************/
float BME280::CalculateTemperature
(
Expand Down Expand Up @@ -397,6 +426,37 @@ float BME280::hum()
}


/****************************************************************/
bool BME280::force()
{
return ForceMeasurement();
}


/****************************************************************/
bool BME280::busy()
{
bool measuring, dummy;

if(!ReadStatus(measuring, dummy)) { return false; }

return measuring;
}


/****************************************************************/
BME280::Mode BME280::mode()
{
uint8_t ctrlMeas;

if(!ReadCtrlMeas(ctrlMeas)) {
return static_cast<Mode>(0);
}

return static_cast<Mode>(ctrlMeas & 3);
}


/****************************************************************/
void BME280::read
(
Expand All @@ -423,9 +483,7 @@ void BME280::read


/****************************************************************/
BME280::ChipModel BME280::chipModel
(
)
BME280::ChipModel BME280::chipModel()
{
return m_chip_model;
}
Loading