diff options
author | Joey Castillo <jose.castillo@gmail.com> | 2021-08-05 12:29:09 -0400 |
---|---|---|
committer | Joey Castillo <jose.castillo@gmail.com> | 2021-08-05 12:29:09 -0400 |
commit | fb249f9b487ae2d6b14ae9ce875ef4054d05c6f2 (patch) | |
tree | 673b9c187a2f43ff97dc09bede2fbf6034af0454 | |
parent | 67e9d173ca0b504305a3fd3d8d1ad7b64e2d23dd (diff) | |
download | Sensor-Watch-fb249f9b487ae2d6b14ae9ce875ef4054d05c6f2.tar.gz Sensor-Watch-fb249f9b487ae2d6b14ae9ce875ef4054d05c6f2.tar.bz2 Sensor-Watch-fb249f9b487ae2d6b14ae9ce875ef4054d05c6f2.zip |
bme280 app: use forced mode, only measure when needed
-rw-r--r-- | Sensor Watch BME280 Project/app.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/Sensor Watch BME280 Project/app.c b/Sensor Watch BME280 Project/app.c index f2605070..bcbd3080 100644 --- a/Sensor Watch BME280 Project/app.c +++ b/Sensor Watch BME280 Project/app.c @@ -77,7 +77,7 @@ void app_setup() { watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL_HUMID, BME280_CONTROL_HUMID_SAMPLING_X16); watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL, BME280_CONTROL_TEMPERATURE_SAMPLING_X16 | BME280_CONTROL_PRESSURE_SAMPLING_NONE | - BME280_CONTROL_MODE_NORMAL); + BME280_CONTROL_MODE_FORCED); watch_enable_display(); @@ -107,12 +107,22 @@ bool app_loop() { switch (application_state.mode) { case MODE_TEMPERATURE: + // take one reading + watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL, BME280_CONTROL_TEMPERATURE_SAMPLING_X16 | + BME280_CONTROL_MODE_FORCED); + // wait for reading to finish + while(watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_STATUS) & BME280_STATUS_UPDATING_MASK); temperature = read_temperature(NULL); sprintf(buf, "TE %4.1f#C", temperature); watch_display_string(buf, 0); watch_clear_pixel(1, 16); break; case MODE_HUMIDITY: + // take one reading + watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL, BME280_CONTROL_TEMPERATURE_SAMPLING_X16 | + BME280_CONTROL_MODE_FORCED); + // wait for reading to finish + while(watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_STATUS) & BME280_STATUS_UPDATING_MASK); temperature = read_temperature(&t_fine); humidity = read_humidity(t_fine); sprintf(buf, "HU rH %3d", (int)humidity); @@ -155,7 +165,8 @@ float read_temperature(int32_t *p_t_fine) { /** * Reads the humidity from the BME280 * @param t_fine - the t_fine measurement from a call to read_temperature - * @return a float indicating the temperature in degrees celsius. + * @return a float indicating the relative humidity as a percentage from 0-100. + * @todo the returned value is glitchy, need to fix. */ float read_humidity(int32_t t_fine) { int32_t adc_value = watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_HUMID_DATA); |