BME680 is an integrated environmental sensor developed specifically for mobile applications and wearables where size and low power consumption are key requirements.
Dependents: Example_DS3231_test
Diff: BME680.cpp
- Revision:
- 1:85088a918342
- Parent:
- 0:c70b7ececf93
--- a/BME680.cpp Fri Jul 22 17:38:11 2016 +0000 +++ b/BME680.cpp Wed Aug 03 15:07:42 2016 +0000 @@ -4,35 +4,139 @@ //const double BME680::const_array1[16] = {1,1,1,1,1,0.99,1,0.992,1,1,0.998,0.995,1,0.99,1,1}; //const double BME680::const_array2[16] = {8000000,4000000,2000000,1000000,499500.4995,248262.1648,125000,63004.03226,31281.28128,15625,7812.5,3906.25,1953.125,976.5625,488.28125,244.140625}; - const uint64_t BME680::lookup_k1_range[16] = { - 2147483647UL, 2147483647UL, 2147483647UL, 2147483647UL, 2147483647UL, - 2126008810UL, 2147483647UL, 2130303777UL, 2147483647UL, 2147483647UL, - 2143188679UL, 2136746228UL, 2147483647UL, 2126008810UL, 2147483647UL, - 2147483647UL - }; + 2147483647UL, 2147483647UL, 2147483647UL, 2147483647UL, 2147483647UL, + 2126008810UL, 2147483647UL, 2130303777UL, 2147483647UL, 2147483647UL, + 2143188679UL, 2136746228UL, 2147483647UL, 2126008810UL, 2147483647UL, + 2147483647UL +}; const uint64_t BME680::lookup_k2_range[16] = { - 4096000000UL, 2048000000UL, 1024000000UL, 512000000UL, - 255744255UL, 127110228UL, 64000000UL, 32258064UL, 16016016UL, - 8000000UL, 4000000UL, 2000000UL, 1000000UL, 500000UL, 250000UL, - 125000UL - }; + 4096000000UL, 2048000000UL, 1024000000UL, 512000000UL, + 255744255UL, 127110228UL, 64000000UL, 32258064UL, 16016016UL, + 8000000UL, 4000000UL, 2000000UL, 1000000UL, 500000UL, 250000UL, + 125000UL +}; const double BME680::_lookup_k1_range[BME680_GAS_RANGE_RL_LENGTH] = { 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -0.8, 0.0, 0.0, -0.2, -0.5, 0.0, -1.0, 0.0, 0.0 }; + const double BME680::_lookup_k2_range[BME680_GAS_RANGE_RL_LENGTH] = { 0.0, 0.0, 0.0, 0.0, 0.1, 0.7, 0.0, -0.8, -0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; +void BME680::setParallelMode() +{ + //Select oversampling for T, P, H + setOversamplingTemperature(2); + setOversamplingPressure(5); + setOversamplingHumidity(1); + + //Select IIR filter for pressure & temperature + setIIRfilterCoefficient(0); + + //Enable gas coversion + runGasConversion(); + + //Select heater set-points to be used + setHeaterProfile(1); + + //Set wait time between TPHG submeasurements + //Set gas_wait_shared<7:0> (time base unit is 0.477ms) + setGasWaitShared(32, 1); + + //Define heater-on times + //Convert durations to register codes + //Set gas_wait_x<7:0> (time base unit is ms) + setGasWaitTime(0,25,4); + +#ifdef FIXED_POINT_COMPENSATION + setTargetHeaterResistance(0, convertTemperatureResistanceInt(350,30)); +#else + setTargetHeaterResistance(0, convertTemperatureResistanceDouble(350,30)); +#endif + + setMode(2); +} + +void BME680::setForcedMode() +{ + //Select oversampling for T, P, H + setOversamplingTemperature(2); + setOversamplingPressure(5); + setOversamplingHumidity(1); + + //Select IIR filter for pressure & temperature + setIIRfilterCoefficient(0); + + //Enable gas coversion + runGasConversion(); + + //Select heater set-points to be used + setHeaterProfile(1); + + //Define heater-on times + //Convert durations to register codes + //Set gas_wait_x<7:0> (time base unit is ms) + setGasWaitTime(0,25,4); + +#ifdef FIXED_POINT_COMPENSATION + setTargetHeaterResistance(0, convertTemperatureResistanceInt(350,30)); +#else + setTargetHeaterResistance(0, convertTemperatureResistanceDouble(350,30)); +#endif + + //Set mode to sequential mode + //Set mode<1:0> to 0b01 + setMode(1); +} + +void BME680::setSequentialMode() +{ + //Select stand-by time between measurements + setWakePeriod(1); + + //Select oversampling for T, P, H + setOversamplingTemperature(2); + setOversamplingPressure(5); + setOversamplingHumidity(1); + + //Select IIR filter for pressure & temperature + setIIRfilterCoefficient(0); + + //Enable gas coversion + runGasConversion(); + + //Select heater set-points to be used + setHeaterProfile(1); + + //Define heater-on times + //Convert durations to register codes + //Set gas_wait_x<7:0> (time base unit is ms) + setGasWaitTime(0,25,4); + + //Set heater temperatures + //Convert temperature to register code + //Set res_heat_x<7:0> + +#ifdef FIXED_POINT_COMPENSATION + setTargetHeaterResistance(0, convertTemperatureResistanceInt(350,30)); +#else + setTargetHeaterResistance(0, convertTemperatureResistanceDouble(350,30)); +#endif + + //Set mode to sequential mode + //Set mode<1:0> to 0b11 + setMode(3); +} #ifdef FIXED_POINT_COMPENSATION int32_t BME680::getCompensatedTemperature(int field) { - uint32_t v_uncomp_temperature_u32 = getTemp1Data(field); + uint32_t v_uncomp_temperature_u32 = getUncompensatedTemp1Data(field); int32_t var1 = ((int32_t)v_uncomp_temperature_u32 >> 3) - ((int32_t)(par_T1 << 1)); int32_t var2 = (var1 * (int32_t) par_T2) >> 11; @@ -49,7 +153,7 @@ int32_t BME680::getCompensateHumidity(int field) { - uint32_t v_uncomp_humidity_u32 = getHumidityData(field); + uint32_t v_uncomp_humidity_u32 = getUncompensatedHumidityData(field); int32_t temp_scaled = (t_fine * 5 + 128) >> 8; int32_t var1 = (int32_t)v_uncomp_humidity_u32 - @@ -90,7 +194,7 @@ int32_t BME680::getCompensatePressure(int field) { - uint32_t v_uncomp_pressure_u32 = getPressureData(field); + uint32_t v_uncomp_pressure_u32 = getUncompensatedPressureData(field); int32_t var1 = (((int32_t)t_fine) >> 1) - 64000; int32_t var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * (int32_t)par_P6) >> 2; @@ -156,7 +260,7 @@ int32_t BME680::getCalculateGasInt(int field) { uint8_t gas_range_u8 = getGasResistanceRange(field); - uint16_t gas_adc_u16 = getGasResistanceData(field); + uint16_t gas_adc_u16 = getUncompensatedGasResistanceData(field); int64_t var1 = (int64_t)((1340 + (5 * (int64_t)range_switching_error)) * ((int64_t)lookup_k1_range[gas_range_u8])) >> 16; @@ -383,25 +487,25 @@ return true; } -uint32_t BME680::getPressureData(int field) +uint32_t BME680::getUncompensatedPressureData(int field) { readRegister(0x1F + field * 0x11, 3); return (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); } -uint32_t BME680::getTemp1Data(int field) +uint32_t BME680::getUncompensatedTemp1Data(int field) { readRegister(0x22 + field * 0x11, 3); return (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); } -uint32_t BME680::getHumidityData(int field) +uint32_t BME680::getUncompensatedHumidityData(int field) { readRegister(0x25 + field * 0x11, 2); return (data[0] << 8) | data[1]; } -uint16_t BME680::getGasResistanceData(int field) +uint16_t BME680::getUncompensatedGasResistanceData(int field) { readRegister(0x2A + field * 0x11, 2); return (data[0] << 2) | (data[1] >> 6);