convert bme280 pressure number
This commit is contained in:
parent
659e98dbc6
commit
be5b5ca2d5
3 changed files with 47 additions and 13 deletions
56
bme280.c
56
bme280.c
|
|
@ -115,9 +115,9 @@ static void bmp280_get_compensation_params(i2c_inst_t *i2c,
|
||||||
params->dig_h5 = (int16_t)(buf[33] << 8) | buf[32];
|
params->dig_h5 = (int16_t)(buf[33] << 8) | buf[32];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main Config for the sensor. Defined here so it can be use to inital configure the sensor and to proc further reads
|
// Main Config for the sensor. Defined here so it can be use to inital configure
|
||||||
// 001 sets osrs_t temp over sampling to 1, 001 sets osrs_p pressure
|
// the sensor and to proc further reads 001 sets osrs_t temp over sampling to 1,
|
||||||
// oversampling to 1, 01 sets sensor mode to forced
|
// 001 sets osrs_p pressure oversampling to 1, 01 sets sensor mode to forced
|
||||||
static const uint8_t main_config = 0b00100101;
|
static const uint8_t main_config = 0b00100101;
|
||||||
void bme280_init(bme280_config *config, i2c_inst_t *i2c, uint8_t sda_pin,
|
void bme280_init(bme280_config *config, i2c_inst_t *i2c, uint8_t sda_pin,
|
||||||
uint8_t scl_pin) {
|
uint8_t scl_pin) {
|
||||||
|
|
@ -179,7 +179,7 @@ static void bme280_read_raw(bme280_config *config, int32_t *temp,
|
||||||
*temp = (buf[3] << 12) | (buf[4] << 4) | (buf[5] >> 4);
|
*temp = (buf[3] << 12) | (buf[4] << 4) | (buf[5] >> 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
float bmp280_convert_temp(bme280_config *config, int32_t temp) {
|
static int32_t bme280_caculate_t_fine(bme280_config *config, int32_t temp) {
|
||||||
bme280_compensation_params params = config->params;
|
bme280_compensation_params params = config->params;
|
||||||
int32_t var1, var2;
|
int32_t var1, var2;
|
||||||
var1 = ((((temp >> 3) - ((int32_t)params.dig_t1 << 1))) *
|
var1 = ((((temp >> 3) - ((int32_t)params.dig_t1 << 1))) *
|
||||||
|
|
@ -190,11 +190,44 @@ float bmp280_convert_temp(bme280_config *config, int32_t temp) {
|
||||||
12) *
|
12) *
|
||||||
((int32_t)params.dig_t3)) >>
|
((int32_t)params.dig_t3)) >>
|
||||||
14;
|
14;
|
||||||
int32_t var3 = var1 + var2;
|
return var1 + var2;
|
||||||
// uses the BMP280 calibration parameters to compensate the temperature value
|
}
|
||||||
// read from its registers
|
|
||||||
int32_t var4 = (var3 * 5 + 128) >> 8;
|
float bme280_convert_temp(int32_t t_fine) {
|
||||||
return (float)var4 / 100;
|
int32_t temp = (t_fine * 5 + 128) >> 8;
|
||||||
|
return (float)temp / 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
float bmp280_convert_pressure(bme280_config *config, int32_t t_fine,
|
||||||
|
int32_t pressure) {
|
||||||
|
bme280_compensation_params params = config->params;
|
||||||
|
int32_t var1, var2;
|
||||||
|
uint32_t converted = 0.0;
|
||||||
|
var1 = (((int32_t)t_fine) >> 1) - (int32_t)64000;
|
||||||
|
var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * ((int32_t)params.dig_p6);
|
||||||
|
var2 += ((var1 * ((int32_t)params.dig_p5)) << 1);
|
||||||
|
var2 = (var2 >> 2) + (((int32_t)params.dig_p4) << 16);
|
||||||
|
var1 = (((params.dig_p3 * (((var1 >> 2) * (var1 >> 2)) >> 13)) >> 3) +
|
||||||
|
((((int32_t)params.dig_p2) * var1) >> 1)) >>
|
||||||
|
18;
|
||||||
|
var1 = ((((32768 + var1)) * ((int32_t)params.dig_p1)) >> 15);
|
||||||
|
if (var1 == 0) {
|
||||||
|
return 0; // avoid exception caused by division by zero
|
||||||
|
}
|
||||||
|
converted =
|
||||||
|
(((uint32_t)(((int32_t)1048576) - pressure) - (var2 >> 12))) * 3125;
|
||||||
|
if (converted < 0x80000000) {
|
||||||
|
converted = (converted << 1) / ((uint32_t)var1);
|
||||||
|
} else {
|
||||||
|
converted = (converted / (uint32_t)var1) * 2;
|
||||||
|
}
|
||||||
|
var1 = (((int32_t)params.dig_p9) *
|
||||||
|
((int32_t)(((converted >> 3) * (converted >> 3)) >> 13))) >>
|
||||||
|
12;
|
||||||
|
var2 = (((int32_t)(converted >> 2)) * ((int32_t)params.dig_p8)) >> 13;
|
||||||
|
converted =
|
||||||
|
(uint32_t)((int32_t)converted + ((var1 + var2 + params.dig_p7) >> 4));
|
||||||
|
return (float)converted / 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
bme280_reading bme280_read(bme280_config *config) {
|
bme280_reading bme280_read(bme280_config *config) {
|
||||||
|
|
@ -206,8 +239,9 @@ bme280_reading bme280_read(bme280_config *config) {
|
||||||
int32_t raw_humidity = 0;
|
int32_t raw_humidity = 0;
|
||||||
bme280_read_raw(config, &raw_temperature, &raw_pressure, &raw_humidity);
|
bme280_read_raw(config, &raw_temperature, &raw_pressure, &raw_humidity);
|
||||||
bme280_reading reading;
|
bme280_reading reading;
|
||||||
reading.temperature = bmp280_convert_temp(config, raw_temperature);
|
int32_t t_fine = bme280_caculate_t_fine(config, raw_temperature);
|
||||||
reading.pressure = raw_pressure;
|
reading.temperature = bme280_convert_temp(t_fine);
|
||||||
|
reading.pressure = bmp280_convert_pressure(config, t_fine, raw_pressure);
|
||||||
reading.humidity = raw_humidity;
|
reading.humidity = raw_humidity;
|
||||||
return reading;
|
return reading;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
bme280.h
2
bme280.h
|
|
@ -33,7 +33,7 @@ typedef struct {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float temperature;
|
float temperature;
|
||||||
int32_t pressure;
|
float pressure;
|
||||||
int32_t humidity;
|
int32_t humidity;
|
||||||
} bme280_reading;
|
} bme280_reading;
|
||||||
|
|
||||||
|
|
|
||||||
2
node1.c
2
node1.c
|
|
@ -15,7 +15,7 @@ int main() {
|
||||||
printf("Making reading\n");
|
printf("Making reading\n");
|
||||||
current_reading = bme280_read(&config);
|
current_reading = bme280_read(&config);
|
||||||
printf("Tempature: %.2f\n", current_reading.temperature);
|
printf("Tempature: %.2f\n", current_reading.temperature);
|
||||||
printf("Pressure: %d\n", current_reading.pressure);
|
printf("Pressure: %.2f\n", current_reading.pressure);
|
||||||
printf("Humidity: %d\n", current_reading.humidity);
|
printf("Humidity: %d\n", current_reading.humidity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue