Improved pressure computation function by reading the pressure and temperature registers in one shot
This commit is contained in:
parent
2098121ae0
commit
e47498b8bd
@ -117,6 +117,28 @@ static bool _BMP280_read_calibration_data(void)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static float _BMP280_compute_temperature(uint8_t *data)
|
||||||
|
{
|
||||||
|
int32_t var1, var2;
|
||||||
|
int32_t adc_T = data[0] << 16 | data[1] << 8 | data[2];
|
||||||
|
adc_T >>= 4;
|
||||||
|
|
||||||
|
var1 = ((((adc_T >> 3) - ((int32_t)_calibration_data.dig_T1 << 1))) *
|
||||||
|
((int32_t)_calibration_data.dig_T2)) >>
|
||||||
|
11;
|
||||||
|
|
||||||
|
var2 = (((((adc_T >> 4) - ((int32_t)_calibration_data.dig_T1)) *
|
||||||
|
((adc_T >> 4) - ((int32_t)_calibration_data.dig_T1))) >>
|
||||||
|
12) *
|
||||||
|
((int32_t)_calibration_data.dig_T3)) >>
|
||||||
|
14;
|
||||||
|
|
||||||
|
t_fine = var1 + var2;
|
||||||
|
|
||||||
|
float T = (t_fine * 5 + 128) >> 8;
|
||||||
|
return T / 100.0;
|
||||||
|
}
|
||||||
|
|
||||||
bool BMP280_init(void)
|
bool BMP280_init(void)
|
||||||
{
|
{
|
||||||
uint8_t data;
|
uint8_t data;
|
||||||
@ -162,40 +184,28 @@ bool BMP280_is_measuring(void)
|
|||||||
|
|
||||||
float BMP280_get_temperature(void)
|
float BMP280_get_temperature(void)
|
||||||
{
|
{
|
||||||
int32_t var1, var2;
|
|
||||||
uint8_t data[3];
|
uint8_t data[3];
|
||||||
|
|
||||||
if(!i2c_read(BMP280_I2C_ADDR, BMP280_TEMP_MSB, data, sizeof data)) return 0.0f;
|
if(!i2c_read(BMP280_I2C_ADDR, BMP280_TEMP_MSB, data, sizeof data)) return 0.0f;
|
||||||
|
|
||||||
int32_t adc_T = data[0] << 16 | data[1] << 8 | data[2];
|
return _BMP280_compute_temperature(data);
|
||||||
adc_T >>= 4;
|
|
||||||
|
|
||||||
var1 = ((((adc_T >> 3) - ((int32_t)_calibration_data.dig_T1 << 1))) *
|
|
||||||
((int32_t)_calibration_data.dig_T2)) >>
|
|
||||||
11;
|
|
||||||
|
|
||||||
var2 = (((((adc_T >> 4) - ((int32_t)_calibration_data.dig_T1)) *
|
|
||||||
((adc_T >> 4) - ((int32_t)_calibration_data.dig_T1))) >>
|
|
||||||
12) *
|
|
||||||
((int32_t)_calibration_data.dig_T3)) >>
|
|
||||||
14;
|
|
||||||
|
|
||||||
t_fine = var1 + var2;
|
|
||||||
|
|
||||||
float T = (t_fine * 5 + 128) >> 8;
|
|
||||||
return T / 100;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float BMP280_get_pressure(float * const temperature)
|
float BMP280_get_pressure(float * const temperature)
|
||||||
{
|
{
|
||||||
int64_t var1, var2, p;
|
int64_t var1, var2, p;
|
||||||
uint8_t data[3];
|
uint8_t data[6];
|
||||||
|
|
||||||
float temp = BMP280_get_temperature();
|
/* When reading the pressure, we also need to get the associated temperature, so let's read everything at once */
|
||||||
|
if(!i2c_read(BMP280_I2C_ADDR, BMP280_PRESS_MSB, data, sizeof data))
|
||||||
|
{
|
||||||
|
if(temperature) *temperature = 0.0f;
|
||||||
|
return 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
float temp = _BMP280_compute_temperature(&data[3]);
|
||||||
if(temperature) *temperature = temp;
|
if(temperature) *temperature = temp;
|
||||||
|
|
||||||
if(!i2c_read(BMP280_I2C_ADDR, BMP280_PRESS_MSB, data, sizeof data)) return 0.0f;
|
|
||||||
|
|
||||||
int32_t adc_P = data[0] << 16 | data[1] << 8 | data[2];
|
int32_t adc_P = data[0] << 16 | data[1] << 8 | data[2];
|
||||||
adc_P >>= 4;
|
adc_P >>= 4;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user