Skip to content

Commit

Permalink
Fixed a bug in integer compensation equations
Browse files Browse the repository at this point in the history
  • Loading branch information
kegov committed Jan 22, 2020
1 parent 29e873a commit a0acb75
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions bme280.c
Original file line number Diff line number Diff line change
Expand Up @@ -1273,13 +1273,13 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
uint32_t pressure_max = 110000;

var1 = (((int32_t)calib_data->t_fine) / 2) - (int32_t)64000;
var2 = (((var1 / 4) * (var1 / 4)) / 2048) * ((int32_t)calib_data->dig_P6);
var2 = var2 + ((var1 * ((int32_t)calib_data->dig_P5)) * 2);
var2 = (var2 / 4) + (((int32_t)calib_data->dig_P4) * 65536);
var3 = (calib_data->dig_P3 * (((var1 / 4) * (var1 / 4)) / 8192)) / 8;
var4 = (((int32_t)calib_data->dig_P2) * var1) / 2;
var2 = (((var1 / 4) * (var1 / 4)) / 2048) * ((int32_t)calib_data->dig_p6);
var2 = var2 + ((var1 * ((int32_t)calib_data->dig_p5)) * 2);
var2 = (var2 / 4) + (((int32_t)calib_data->dig_p4) * 65536);
var3 = (calib_data->dig_p3 * (((var1 / 4) * (var1 / 4)) / 8192)) / 8;
var4 = (((int32_t)calib_data->dig_p2) * var1) / 2;
var1 = (var3 + var4) / 262144;
var1 = (((32768 + var1)) * ((int32_t)calib_data->dig_P1)) / 32768;
var1 = (((32768 + var1)) * ((int32_t)calib_data->dig_p1)) / 32768;

/* avoid exception caused by division by zero */
if (var1)
Expand All @@ -1294,9 +1294,9 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
{
pressure = (pressure / (uint32_t)var1) * 2;
}
var1 = (((int32_t)calib_data->dig_P9) * ((int32_t)(((pressure / 8) * (pressure / 8)) / 8192))) / 4096;
var2 = (((int32_t)(pressure / 4)) * ((int32_t)calib_data->dig_P8)) / 8192;
pressure = (uint32_t)((int32_t)pressure + ((var1 + var2 + calib_data->dig_P7) / 16));
var1 = (((int32_t)calib_data->dig_p9) * ((int32_t)(((pressure / 8) * (pressure / 8)) / 8192))) / 4096;
var2 = (((int32_t)(pressure / 4)) * ((int32_t)calib_data->dig_p8)) / 8192;
pressure = (uint32_t)((int32_t)pressure + ((var1 + var2 + calib_data->dig_p7) / 16));
if (pressure < pressure_min)
{
pressure = pressure_min;
Expand Down

0 comments on commit a0acb75

Please sign in to comment.