32#include <util/delay.h>
40#define BME280_ADDRESS_W 0xEC
41#define BME280_ADDRESS_R 0xED
44#define CONFIG_REG 0xF5
45#define CTRL_MEAS_REG 0xF4
46#define STATUS_REG 0xF3
47#define CTRL_HUM_REG 0xF2
49#define SOFT_RESET_REG 0xE0
50#define CHIP_ID_REG 0xD0
54#define OVERSAMPLE_x1 1
55#define OVERSAMPLE_x2 2
56#define OVERSAMPLE_x4 3
57#define OVERSAMPLE_x8 4
58#define OVERSAMPLE_x16 5
64#define BUS_SPEED_100KHZ 100000U
68 .p_system_tag =
"BME280",
69 .file_log_level = DEBUG,
73static uint8_t BME280_read_buffer[33];
76static uint16_t dig_T1 = 0;
77static int16_t dig_T2 = 0;
78static int16_t dig_T3 = 0;
81static uint16_t dig_P1 = 0;
82static int16_t dig_P2 = 0;
83static int16_t dig_P3 = 0;
84static int16_t dig_P4 = 0;
85static int16_t dig_P5 = 0;
86static int16_t dig_P6 = 0;
87static int16_t dig_P7 = 0;
88static int16_t dig_P8 = 0;
89static int16_t dig_P9 = 0;
92static uint8_t dig_H1 = 0;
93static int16_t dig_H2 = 0;
94static uint8_t dig_H3 = 1;
95static int16_t dig_H4 = 0;
96static int16_t dig_H5 = 0;
97static int16_t dig_H6 = 0;
104void bme280_write_word(uint8_t reg1, uint8_t reg2, uint8_t msb, uint8_t lsb);
106uint8_t bme280_read_byte(uint8_t reg);
108uint32_t bme280_get_raw_temp_val(
void);
109uint32_t bme280_get_raw_hum_val(
void);
110void bme280_burst_read(uint8_t start_addr,
111 uint8_t start_position,
131 uint8_t
id = bme280_read_byte(CHIP_ID_REG);
134 log_message(&bme280_log,
136 "In init_bme280(), Chip ID check passed.");
140 log_message(&bme280_log,
142 "In init_bme280(), Chip ID check failed.");
167 uint32_t raw_temp_adc_val;
171 raw_temp_adc_val = bme280_get_raw_temp_val();
186 uint16_t raw_hum_adc_val;
190 raw_hum_adc_val = bme280_get_raw_hum_val();
193 retval = (retval >> 10);
210 uint16_t raw_hum_adc_val;
214 raw_hum_adc_val = bme280_get_raw_hum_val();
217 retval = (retval >> 10);
254uint8_t bme280_read_byte(uint8_t reg)
269void bme280_burst_read(uint8_t start_address,
270 uint8_t start_position,
280 for (i = 0; i < (length - 1); ++i)
344 dig_H1 = bme280_read_byte(0xA1);
346 dig_H3 = bme280_read_byte(0xE3);
348 h_msb = bme280_read_byte(0xE4);
349 h_lsb = bme280_read_byte(0xE5);
352 dig_H4 |= (h_msb << 4);
354 h_msb = bme280_read_byte(0xE6);
355 h_lsb = bme280_read_byte(0xE5);
357 dig_H5 = (h_lsb >> 4);
358 dig_H5 |= (h_msb << 4);
360 dig_H6 = bme280_read_byte(0xE7);
364uint32_t bme280_get_raw_temp_val(
void)
381 retval = temp_msb << 12;
382 retval |= temp_lsb << 4;
383 retval |= temp_xlsb >> 4;
388uint32_t bme280_get_raw_hum_val(
void)
403 retval = hum_msb << 8;
422 int32_t var1, var2, T;
423 var1 = (((adc_T >> 3) - ((int32_t)dig_T1 << 1)) * ((int32_t)dig_T2)) >> 11;
424 var2 = (((((adc_T >> 4) - ((int32_t)dig_T1)) * ((adc_T >> 4)
425 - ((int32_t)dig_T1))) >> 12) * ((int32_t)dig_T3)) >> 14;
426 t_fine = var1 + var2;
427 T = (t_fine * 5 + 128) >> 8;
440 var1 = (t_fine >> 1) - 64000;
441 var2 = (((var1 >> 2) * (var1 >> 2)) >> 11 ) * ((int32_t)dig_P6);
442 var2 = var2 + ((var1 * ((int32_t)dig_P5)) << 1);
443 var2 = (var2 >> 2) + (((int32_t)dig_P4) << 16);
444 var1 = ((((int32_t)dig_P3 * (((var1 >> 2) * (var1 >> 2)) >> 13 )) >> 3)
445 + (((((int32_t)dig_P2) * var1) >> 1 ))) >> 18;
446 var1 = ((((32768 + var1)) * ((int32_t)dig_P1)) >> 15);
453 p = ((((1048576) - adc_P) - (var2 >> 12))) * 3125;
457 p = (p << 1) / (var1);
465 var1 = ((dig_P9) * ((((p>>3) * (p >> 3)) >> 13))) >> 12;
466 var2 = (((p >> 2)) * (dig_P8)) >> 13;
467 p = (p + ((var1 + var2 + dig_P7) >> 4));
481 v_x1_u32r = (t_fine - (76800));
483 v_x1_u32r = (((((adc_H << 14) - (((int32_t)dig_H4) << 20)
484 - (((int32_t)dig_H5) * v_x1_u32r)) + (16384)) >> 15) * (((((((v_x1_u32r
485 * ((int32_t)dig_H6)) >> 10) * (((v_x1_u32r * ((int32_t)dig_H3)) >> 11)
486 + (32768))) >> 10) + (2097152)) * ((int32_t)dig_H2) + 8192) >> 14));
488 v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7)
489 * ((int32_t)dig_H1)) >> 4));
490 v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
491 v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
492 return (uint32_t)(v_x1_u32r>>12);
void init_i2c(uint32_t bus_speed)
Sets pullups and initializes i2c clock to desired bus speed.
uint8_t i2c_read_no_ack(void)
Read in from slave, sending NOACK when done (no TWEA).
uint8_t i2c_read_ack(void)
Read in from slave, sending ACK when done (sets TWEA).
void i2c_stop(void)
Sends a stop condition (sets TWSTO).
void i2c_send(uint8_t data)
Loads data, sends it out, waiting for completion.
void i2c_start(void)
Sends a start condition (sets TWSTA).
uint16_t bme280_get_humidity(void)
Fetches the latest humidity data from the sensor, compensates and formats the data as RH * 100.
void bme280_read_comp_data(void)
Reads in all compensation data from the device and stores it in a buffer.
int32_t bme280_compensate_temp(int32_t adc_T)
Returns temperature in DegC, resolution is 0.01 DegC.
void init_bme280(void)
Initialise the device ready for measurements to be taken.
int16_t bme280_get_temperature(void)
Fetches the latest temperature data from the sensor, compensates and formats the data in degrees Celc...
int32_t bme280_compensate_pressure(int32_t adc_P)
Returns pressure in Pa as unsigned 32 bit integer.
uint16_t bme280_get_pressure(void)
Fetches the latest pressure data from the sensor, compensates and formats the data as Pa * 100.
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...
uint16_t bme280_read_word(uint8_t start_address)
Reads a 16bit unsigned integer from a specified register.
void bme280_write_byte(uint8_t reg, uint8_t byte)
Writes a single byte to a specified reguster.
I2C communications driver for AVR MCU's.
Driver file providing logging functionality over USART, to print debug messages and values to a temin...
Config object, to be instantiated in each file the log system is to be used, then pass it's address i...