BME280 Combined humidity and pressure sensor library with I2C interface

Dependents:   BME280_LCD IFTTT_BME280_demo TweetTest NetworkThermometer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BME280.cpp Source File

BME280.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    BME280.cpp
00004  * @author  Toyomasa Watarai
00005  * @version V1.0.0
00006  * @date    11 March 2017
00007  * @brief   BME280 class implementation
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * Permission is hereby granted, free of charge, to any person obtaining a copy
00012  * of this software and associated documentation files (the "Software"), to deal
00013  * in the Software without restriction, including without limitation the rights
00014  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015  * copies of the Software, and to permit persons to whom the Software is
00016  * furnished to do so, subject to the following conditions:
00017  *
00018  * The above copyright notice and this permission notice shall be included in
00019  * all copies or substantial portions of the Software.
00020  *
00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027  * THE SOFTWARE.
00028  */
00029 
00030 #include "mbed.h"
00031 #include "BME280.h"
00032 
00033 BME280::BME280(PinName sda, PinName scl, char slave_adr)
00034     :
00035     i2c_p(new I2C(sda, scl)), 
00036     i2c(*i2c_p),
00037     address(slave_adr),
00038     t_fine(0)
00039 {
00040     initialize();
00041 }
00042 
00043 BME280::BME280(I2C &i2c_obj, char slave_adr)
00044     :
00045     i2c_p(NULL), 
00046     i2c(i2c_obj),
00047     address(slave_adr),
00048     t_fine(0)
00049 {
00050     initialize();
00051 }
00052 
00053 BME280::~BME280()
00054 {
00055     if (NULL != i2c_p)
00056         delete  i2c_p;
00057 }
00058     
00059 void BME280::initialize()
00060 {
00061     char cmd[18];
00062  
00063     cmd[0] = 0xf2; // ctrl_hum
00064     cmd[1] = 0x01; // Humidity oversampling x1
00065     i2c.write(address, cmd, 2);
00066  
00067     cmd[0] = 0xf4; // ctrl_meas
00068     cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
00069     i2c.write(address, cmd, 2);
00070  
00071     cmd[0] = 0xf5; // config
00072     cmd[1] = 0xa0; // Standby 1000ms, Filter off
00073     i2c.write(address, cmd, 2);
00074  
00075     cmd[0] = 0x88; // read dig_T regs
00076     i2c.write(address, cmd, 1);
00077     i2c.read(address, cmd, 6);
00078  
00079     dig_T1 = (cmd[1] << 8) | cmd[0];
00080     dig_T2 = (cmd[3] << 8) | cmd[2];
00081     dig_T3 = (cmd[5] << 8) | cmd[4];
00082  
00083     DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
00084  
00085     cmd[0] = 0x8E; // read dig_P regs
00086     i2c.write(address, cmd, 1);
00087     i2c.read(address, cmd, 18);
00088  
00089     dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
00090     dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
00091     dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
00092     dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
00093     dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
00094     dig_P6 = (cmd[11] << 8) | cmd[10];
00095     dig_P7 = (cmd[13] << 8) | cmd[12];
00096     dig_P8 = (cmd[15] << 8) | cmd[14];
00097     dig_P9 = (cmd[17] << 8) | cmd[16];
00098  
00099     DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
00100  
00101     cmd[0] = 0xA1; // read dig_H regs
00102     i2c.write(address, cmd, 1);
00103     i2c.read(address, cmd, 1);
00104      cmd[1] = 0xE1; // read dig_H regs
00105     i2c.write(address, &cmd[1], 1);
00106     i2c.read(address, &cmd[1], 7);
00107 
00108     dig_H1 = cmd[0];
00109     dig_H2 = (cmd[2] << 8) | cmd[1];
00110     dig_H3 = cmd[3];
00111     dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
00112     dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
00113     dig_H6 = cmd[7];
00114  
00115     DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
00116 }
00117  
00118 float BME280::getTemperature()
00119 {
00120     int32_t temp_raw;
00121     float tempf;
00122     char cmd[4];
00123  
00124     cmd[0] = 0xfa; // temp_msb
00125     i2c.write(address, cmd, 1);
00126     i2c.read(address, &cmd[1], 3);
00127  
00128     temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00129  
00130     int32_t temp;
00131  
00132     temp =
00133         (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
00134         ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
00135  
00136     t_fine = temp;
00137     temp = (temp * 5 + 128) >> 8;
00138     tempf = (float)temp;
00139  
00140     return (tempf/100.0f);
00141 }
00142  
00143 float BME280::getPressure()
00144 {
00145     uint32_t press_raw;
00146     float pressf;
00147     char cmd[4];
00148  
00149     cmd[0] = 0xf7; // press_msb
00150     i2c.write(address, cmd, 1);
00151     i2c.read(address, &cmd[1], 3);
00152  
00153     press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00154  
00155     int32_t var1, var2;
00156     uint32_t press;
00157  
00158     var1 = (t_fine >> 1) - 64000;
00159     var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
00160     var2 = var2 + ((var1 * dig_P5) << 1);
00161     var2 = (var2 >> 2) + (dig_P4 << 16);
00162     var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
00163     var1 = ((32768 + var1) * dig_P1) >> 15;
00164     if (var1 == 0) {
00165         return 0;
00166     }
00167     press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
00168     if(press < 0x80000000) {
00169         press = (press << 1) / var1;
00170     } else {
00171         press = (press / var1) * 2;
00172     }
00173     var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
00174     var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
00175     press = (press + ((var1 + var2 + dig_P7) >> 4));
00176  
00177     pressf = (float)press;
00178     return (pressf/100.0f);
00179 }
00180  
00181 float BME280::getHumidity()
00182 {
00183     uint32_t hum_raw;
00184     float humf;
00185     char cmd[4];
00186  
00187     cmd[0] = 0xfd; // hum_msb
00188     i2c.write(address, cmd, 1);
00189     i2c.read(address, &cmd[1], 2);
00190  
00191     hum_raw = (cmd[1] << 8) | cmd[2];
00192  
00193     int32_t v_x1;
00194  
00195     v_x1 = t_fine - 76800;
00196     v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
00197                ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
00198                                             (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
00199                                             (int32_t)dig_H2 + 8192) >> 14));
00200     v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
00201     v_x1 = (v_x1 < 0 ? 0 : v_x1);
00202     v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
00203  
00204     humf = (float)(v_x1 >> 12);
00205  
00206     return (humf/1024.0f);
00207 }