Skip to content

Commit 7ef516c

Browse files
committed
Adding filter and standby control.
1 parent 7875e5c commit 7ef516c

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

src/SparkFunBME280.cpp

+43-10
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ BME280::BME280( void )
4646

4747
//Select CS pin for SPI. Does nothing for I2C
4848
settings.chipSelectPin = 10;
49-
50-
settings.runMode = 0;
5149
}
5250

5351

@@ -128,13 +126,11 @@ uint8_t BME280::begin()
128126
calibration.dig_H5 = ((int16_t)((readRegister(BME280_DIG_H5_MSB_REG) << 4) + ((readRegister(BME280_DIG_H4_LSB_REG) >> 4) & 0x0F)));
129127
calibration.dig_H6 = ((int8_t)readRegister(BME280_DIG_H6_REG));
130128

131-
settings.runMode = 3; // 3, Normal mode
132-
settings.tStandby = 0; // 0, 0.5ms
133-
settings.filter = 0; // 0, filter off
134-
135-
setPressureOverSample(1); //Default
136-
setHumidityOverSample(1); //Default
137-
setTempOverSample(1); //Default
129+
setStandbyTime(0); //Default to 0.5ms
130+
setFilter(0); //Default filter off
131+
setPressureOverSample(1); //Default of 1x oversample
132+
setHumidityOverSample(1); //Default of 1x oversample
133+
setTempOverSample(1); //Default of 1x oversample
138134

139135
setMode(MODE_NORMAL); //Go!
140136

@@ -155,7 +151,7 @@ bool BME280::beginI2C(TwoWire &wirePort)
155151
}
156152

157153
//Set the mode bits in the ctrl_meas register
158-
//Mode 00 = Sleep
154+
// Mode 00 = Sleep
159155
// 01 and 10 = Forced
160156
// 11 = Normal mode
161157
void BME280::setMode(uint8_t mode)
@@ -168,6 +164,43 @@ void BME280::setMode(uint8_t mode)
168164
writeRegister(BME280_CTRL_MEAS_REG, controlData);
169165
}
170166

167+
//Set the standby bits in the config register
168+
//tStandby can be:
169+
// 0, 0.5ms
170+
// 1, 62.5ms
171+
// 2, 125ms
172+
// 3, 250ms
173+
// 4, 500ms
174+
// 5, 1000ms
175+
// 6, 10ms
176+
// 7, 20ms
177+
void BME280::setStandbyTime(uint8_t timeSetting)
178+
{
179+
if(timeSetting > 0b111) timeSetting = 0; //Error check. Default to 0.5ms
180+
181+
uint8_t controlData = readRegister(BME280_CONFIG_REG);
182+
controlData &= ~( (1<<7) | (1<<6) | (1<<5) ); //Clear the 7/6/5 bits
183+
controlData |= (timeSetting << 5); //Align with bits 7/6/5
184+
writeRegister(BME280_CONFIG_REG, controlData);
185+
}
186+
187+
//Set the filter bits in the config register
188+
//filter can be:
189+
// 0, filter off
190+
// 1, 2
191+
// 2, 4
192+
// 3, 8
193+
// 5+, 16
194+
void BME280::setFilter(uint8_t filterSetting)
195+
{
196+
if(filterSetting > 0b111) filterSetting = 0; //Error check. Default to filter off
197+
198+
uint8_t controlData = readRegister(BME280_CONFIG_REG);
199+
controlData &= ~( (1<<4) | (1<<3) | (1<<2) ); //Clear the 4/3/2 bits
200+
controlData |= (filterSetting << 2); //Align with bits 4/3/2
201+
writeRegister(BME280_CONFIG_REG, controlData);
202+
}
203+
171204
//Gets the current mode bits in the ctrl_meas register
172205
//Mode 00 = Sleep
173206
// 01 and 10 = Forced

src/SparkFunBME280.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ struct SensorSettings
111111
uint8_t commInterface;
112112
uint8_t I2CAddress;
113113
uint8_t chipSelectPin;
114-
115-
uint8_t runMode;
116-
uint8_t tStandby;
117-
uint8_t filter;
118114
};
119115

120116
//Used to hold the calibration constants. These are used
@@ -171,6 +167,8 @@ class BME280
171167
void setTempOverSample(uint8_t overSampleAmount); //Set the temperature sample mode
172168
void setPressureOverSample(uint8_t overSampleAmount); //Set the pressure sample mode
173169
void setHumidityOverSample(uint8_t overSampleAmount); //Set the humidity sample mode
170+
void setStandbyTime(uint8_t timeSetting); //Set the standby time between measurements
171+
void setFilter(uint8_t filterSetting); //Set the filter
174172

175173
void setI2CAddress(uint8_t i2caddress); //Set the address the library should use to communicate. Use if address jumper is closed (0x76).
176174

0 commit comments

Comments
 (0)