read raw bme280 values
This commit is contained in:
parent
f7da5a3712
commit
6f8d2b8814
3 changed files with 43 additions and 3 deletions
29
bme280.c
29
bme280.c
|
|
@ -41,7 +41,7 @@
|
|||
|
||||
// #define REG_PRESSURE_XLSB _u(0xF9)
|
||||
// #define REG_PRESSURE_LSB _u(0xF8)
|
||||
// #define REG_PRESSURE_MSB _u(0xF7)
|
||||
#define REG_PRESSURE_MSB _u(0xF7)
|
||||
|
||||
// calibration registers
|
||||
#define REG_DIG_T1_LSB _u(0x88)
|
||||
|
|
@ -143,3 +143,30 @@ void bme280_init(bme280_config *config, i2c_inst_t *i2c, uint8_t sda_pin,
|
|||
config->i2c = i2c;
|
||||
return;
|
||||
}
|
||||
|
||||
static void bmp280_read_raw(bme280_config *config, int32_t* temp, int32_t* pressure, int32_t* humidity) {
|
||||
// BMP280 data registers are auto-incrementing and we have 3 temperature and
|
||||
// pressure registers each, so we start at 0xF7 and read 6 bytes to 0xFC
|
||||
// note: normal mode does not require further ctrl_meas and config register writes
|
||||
|
||||
uint8_t buf[6];
|
||||
uint8_t reg = REG_PRESSURE_MSB;
|
||||
i2c_write_blocking(config->i2c, ADDR, ®, 1, true); // true to keep master control of bus
|
||||
i2c_read_blocking(config->i2c, ADDR, buf, 6, false); // false - finished with bus
|
||||
|
||||
// store the 20 bit read in a 32 bit signed integer for conversion
|
||||
*pressure = (buf[0] << 12) | (buf[1] << 4) | (buf[2] >> 4);
|
||||
*temp = (buf[3] << 12) | (buf[4] << 4) | (buf[5] >> 4);
|
||||
}
|
||||
|
||||
bme280_reading bme280_read(bme280_config *config) {
|
||||
int32_t raw_temperature = 0;
|
||||
int32_t raw_pressure = 0;
|
||||
int32_t raw_humidity = 0;
|
||||
bmp280_read_raw(config, &raw_temperature, &raw_pressure, &raw_humidity);
|
||||
bme280_reading reading;
|
||||
reading.temperature = raw_temperature;
|
||||
reading.pressure = raw_pressure;
|
||||
reading.humidity = raw_humidity;
|
||||
return reading;
|
||||
}
|
||||
|
|
|
|||
8
bme280.h
8
bme280.h
|
|
@ -31,6 +31,14 @@ typedef struct {
|
|||
bme280_compensation_params params;
|
||||
} bme280_config;
|
||||
|
||||
typedef struct {
|
||||
int32_t temperature;
|
||||
int32_t pressure;
|
||||
int32_t humidity;
|
||||
} bme280_reading;
|
||||
|
||||
void bme280_init(bme280_config *config, i2c_inst_t *i2c,
|
||||
uint8_t sda_pin, uint8_t scl_pin);
|
||||
|
||||
bme280_reading bme280_read(bme280_config *config);
|
||||
#endif /* BME280_H */
|
||||
|
|
|
|||
9
node1.c
9
node1.c
|
|
@ -9,8 +9,13 @@ int main() {
|
|||
stdio_init_all();
|
||||
bme280_config config;
|
||||
bme280_init(&config, i2c1, 14, 15);
|
||||
bme280_reading current_reading;
|
||||
while (true) {
|
||||
sleep_ms(1000);
|
||||
printf("One Second\n");
|
||||
sleep_ms(2500);
|
||||
printf("Making reading\n");
|
||||
current_reading = bme280_read(&config);
|
||||
printf("Tempature: %d\n", current_reading.temperature);
|
||||
printf("Pressure: %d\n", current_reading.pressure);
|
||||
printf("Humidity: %d\n", current_reading.humidity);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue