Skip to content

Commit 45f27fe

Browse files
committed
Fixed rounding bug from issue 8 on breakout board.
See issue sparkfun/Qwiic_BME280_CCS811_Combo#8
1 parent f2419e5 commit 45f27fe

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/SparkFunCCS811.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -465,21 +465,32 @@ CCS811Core::status CCS811::setEnvironmentalData( float relativeHumidity, float t
465465
byte envData[4];
466466

467467
//Split value into 7-bit integer and 9-bit fractional
468-
envData[0] = ((rH % 1000) / 100) > 7 ? (rH / 1000 + 1) << 1 : (rH / 1000) << 1;
468+
469+
//Incorrect way from datasheet.
470+
//envData[0] = ((rH % 1000) / 100) > 7 ? (rH / 1000 + 1) << 1 : (rH / 1000) << 1;
471+
//envData[1] = 0; //CCS811 only supports increments of 0.5 so bits 7-0 will always be zero
472+
//if (((rH % 1000) / 100) > 2 && (((rH % 1000) / 100) < 8))
473+
//{
474+
// envData[0] |= 1; //Set 9th bit of fractional to indicate 0.5%
475+
//}
476+
477+
//Correct rounding. See issue 8: https://github.com/sparkfun/Qwiic_BME280_CCS811_Combo/issues/8
478+
envData[0] = (rH + 250) / 500;
469479
envData[1] = 0; //CCS811 only supports increments of 0.5 so bits 7-0 will always be zero
470-
if (((rH % 1000) / 100) > 2 && (((rH % 1000) / 100) < 8))
471-
{
472-
envData[0] |= 1; //Set 9th bit of fractional to indicate 0.5%
473-
}
474480

475481
temp += 25000; //Add the 25C offset
476482
//Split value into 7-bit integer and 9-bit fractional
477-
envData[2] = ((temp % 1000) / 100) > 7 ? (temp / 1000 + 1) << 1 : (temp / 1000) << 1;
483+
//envData[2] = ((temp % 1000) / 100) > 7 ? (temp / 1000 + 1) << 1 : (temp / 1000) << 1;
484+
//envData[3] = 0;
485+
//if (((temp % 1000) / 100) > 2 && (((temp % 1000) / 100) < 8))
486+
//{
487+
// envData[2] |= 1; //Set 9th bit of fractional to indicate 0.5C
488+
//}
489+
490+
//Correct rounding
491+
envData[2] = (temp + 250) / 500;
478492
envData[3] = 0;
479-
if (((temp % 1000) / 100) > 2 && (((temp % 1000) / 100) < 8))
480-
{
481-
envData[2] |= 1; //Set 9th bit of fractional to indicate 0.5C
482-
}
493+
483494
CCS811Core::status returnError = multiWriteRegister(CSS811_ENV_DATA, envData, 4);
484495
return returnError;
485496
}

0 commit comments

Comments
 (0)