AVRly - AVR Development Resources
#include <stdint.h>

Go to the source code of this file.

Functions

void init_bme280 (void)
 Initialise the device ready for measurements to be taken. More...
 
int16_t bme280_get_temperature (void)
 Fetches the latest temperature data from the sensor, compensates and formats the data in degrees Celcius * 100. More...
 
uint16_t bme280_get_humidity (void)
 Fetches the latest humidity data from the sensor, compensates and formats the data as RH * 100. More...
 
uint16_t bme280_get_pressure (void)
 Fetches the latest pressure data from the sensor, compensates and formats the data as Pa * 100. More...
 

Detailed Description

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author
Jason Duffy
Date
22nd June 2022
See also

Definition in file bme280.h.

Function Documentation

◆ init_bme280()

void init_bme280 ( void  )

Initialise the device ready for measurements to be taken.

Definition at line 125 of file bme280.c.

126{
127 // Setup I2C hardware fpr use.
128 init_i2c(BUS_SPEED_100KHZ);
129
130 // Perform ID check.
131 uint8_t id = bme280_read_byte(CHIP_ID_REG);
132 if (id == 0x60)
133 {
134 log_message(&bme280_log,
135 DEBUG,
136 "In init_bme280(), Chip ID check passed.");
137 }
138 else
139 {
140 log_message(&bme280_log,
141 ERROR,
142 "In init_bme280(), Chip ID check failed.");
143 }
144
145 // Read compensation data from sensor and save it.
147
148 //
149 bme280_write_byte(CTRL_HUM_REG, OVERSAMPLE_x1);
150
151 //
152 bme280_write_byte(CONFIG_REG, 0b10100000);
153
154 // oversample x1 temp, pressure off, forced mode
155 bme280_write_byte(CTRL_MEAS_REG, 0b00100001);
156}
void init_i2c(uint32_t bus_speed)
Sets pullups and initializes i2c clock to desired bus speed.
Definition: atmega_i2c.c:49
void bme280_read_comp_data(void)
Reads in all compensation data from the device and stores it in a buffer.
Definition: bme280.c:322
void bme280_write_byte(uint8_t reg, uint8_t byte)
Writes a single byte to a specified reguster.
Definition: bme280.c:244

◆ bme280_get_temperature()

int16_t bme280_get_temperature ( void  )

Fetches the latest temperature data from the sensor, compensates and formats the data in degrees Celcius * 100.

Returns
eg. A return value of 5123 = 51.23˚C (Degrees Celcius).

Definition at line 164 of file bme280.c.

165{
166 int32_t retval;
167 uint32_t raw_temp_adc_val;
168
169 // oversample x1 temp, pressure off, forced mode.
170 bme280_write_byte(CTRL_MEAS_REG, 0b00100001);
171 raw_temp_adc_val = bme280_get_raw_temp_val();
172 retval = bme280_compensate_temp(raw_temp_adc_val);
173
174 return retval;
175}
int32_t bme280_compensate_temp(int32_t adc_T)
Returns temperature in DegC, resolution is 0.01 DegC.
Definition: bme280.c:420

◆ bme280_get_humidity()

uint16_t bme280_get_humidity ( void  )

Fetches the latest humidity data from the sensor, compensates and formats the data as RH * 100.

Returns
eg. A return value of 2852 = 28.50 RH (Relative Humidity).

Definition at line 207 of file bme280.c.

208{
209 uint32_t retval;
210 uint16_t raw_hum_adc_val;
211
212 // oversample x1 temp, pressure off, forced mode
213 bme280_write_byte(CTRL_MEAS_REG, 0b00100001);
214 raw_hum_adc_val = bme280_get_raw_hum_val();
215 retval = bme280_compensate_humidity(raw_hum_adc_val);
216 retval *= 100;
217 retval = (retval >> 10);
218
219 return retval;
220}
uint32_t bme280_compensate_humidity(int32_t adc_H)
Returns humidity in RH as unsigned 32 bit integer in Q22.10 format (22 integer and 10 fractional bits...
Definition: bme280.c:477

◆ bme280_get_pressure()

uint16_t bme280_get_pressure ( void  )

Fetches the latest pressure data from the sensor, compensates and formats the data as Pa * 100.

Returns
eg. A return value of 96386 = 963.86 Pa (Pascals).

Definition at line 228 of file bme280.c.

229{
230 // Some pressure-getting code goes here.
231 return 0;
232}