Data Sheet
Bosch Sensortec | BME280 Data sheet
50 | 55
Modifications reserved | Data subject to change without notice
Document number: BST-BME280-DS002-15
Revision_1.6_092018
8.2 Pressure compensation in 32 bit fixed point
Please note that it is strongly advised to use the API available from Bosch Sensortec to perform
readout and compensation. If this is not wanted, the code below can be applied at the user’s risk. Both
pressure and temperature values are expected to be received in 20 bit format, positive, stored in a 32
bit signed integer.
The variable t_fine (signed 32 bit) carries a fine resolution temperature value over to the pressure
compensation formula and could be implemented as a global variable.
The data type “BME280_S32_t” should define a 32 bit signed integer variable type and can usually be
defined as “long signed int”.
The data type “BME280_U32_t” should define a 32 bit unsigned integer variable type and can usually
be defined as “long unsigned int”.
Compensating the pressure value with 32 bit integer has an accuracy of typically 1 Pa (1-sigma). At
high filter levels this adds a significant amount of noise to the output values and reduces their
resolution.
// Returns temperature in DegC, resolution is 0.01 DegC. Output value of “5123” equals 51.23
DegC.
// t_fine carries fine temperature as global value
BME280_S32_t t_fine;
BME280_S32_t BME280_compensate_T_int32(BME280_S32_t adc_T)
{
BME280_S32_t var1, var2, T;
var1 = ((((adc_T>>3) – ((BME280_S32_t)dig_T1<<1))) * ((BME280_S32_t)dig_T2)) >> 11;
var2 = (((((adc_T>>4) – ((BME280_S32_t)dig_T1)) * ((adc_T>>4) – ((BME280_S32_t)dig_T1)))
>> 12) *
((BME280_S32_t)dig_T3)) >> 14;
t_fine = var1 + var2;
T = (t_fine * 5 + 128) >> 8;
return T;
}
// Returns pressure in Pa as unsigned 32 bit integer. Output value of “96386” equals 96386 Pa
= 963.86 hPa
BME280_U32_t BME280_compensate_P_int32(BME280_S32_t adc_P)
{
BME280_S32_t var1, var2;
BME280_U32_t p;
var1 = (((BME280_S32_t)t_fine)>>1) – (BME280_S32_t)64000;
var2 = (((var1>>2) * (var1>>2)) >> 11 ) * ((BME280_S32_t)dig_P6);
var2 = var2 + ((var1*((BME280_S32_t)dig_P5))<<1);
var2 = (var2>>2)+(((BME280_S32_t)dig_P4)<<16);
var1 = (((dig_P3 * (((var1>>2) * (var1>>2)) >> 13 )) >> 3) + ((((BME280_S32_t)dig_P2) *
var1)>>1))>>18;
var1 =((((32768+var1))*((BME280_S32_t)dig_P1))>>15);
if (var1 == 0)
{
return 0; // avoid exception caused by division by zero
}
p = (((BME280_U32_t)(((BME280_S32_t)1048576)-adc_P)-(var2>>12)))*3125;
if (p < 0x80000000)
{
p = (p << 1) / ((BME280_U32_t)var1);
}
else
{
p = (p / (BME280_U32_t)var1) * 2;
}
var1 = (((BME280_S32_t)dig_P9) * ((BME280_S32_t)(((p>>3) * (p>>3))>>13)))>>12;
var2 = (((BME280_S32_t)(p>>2)) * ((BME280_S32_t)dig_P8))>>13;
p = (BME280_U32_t)((BME280_S32_t)p + ((var1 + var2 + dig_P7) >> 4));
return p;
}