Thing Innovations / BME280

Dependents:   mDot_TTN_OTAA_Node LoraGPSLogger mDot_TTN_OTAA_Node_send_data_as_string

Fork of BME280 by Toyomasa Watarai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BME280.cpp Source File

BME280.cpp

00001 /**
00002  *  BME280 Combined humidity and pressure sensor library
00003  *
00004  *  @author  Toyomasa Watarai
00005  *  @version 1.0
00006  *  @date    06-April-2015
00007  *
00008  *  Library for "BME280 temperature, humidity and pressure sensor module" from Switch Science
00009  *    https://www.switch-science.com/catalog/2236/
00010  *
00011  *  For more information about the BME280:
00012  *    http://ae-bst.resource.bosch.com/media/products/dokumente/bme280/BST-BME280_DS001-10.pdf
00013  */
00014 
00015 #include "mbed.h"
00016 #include "BME280.h"
00017 
00018 
00019 BME280::BME280(PinName sda, PinName scl, char slave_adr) :
00020     i2c_p(new I2C(sda, scl)), 
00021     i2c(*i2c_p),
00022     address(slave_adr),
00023     t_fine(0)
00024 {
00025     initialize();
00026 }
00027 
00028 BME280::BME280(I2C &i2c_obj, char slave_adr) :
00029     i2c_p(NULL), 
00030     i2c(i2c_obj),
00031     address(slave_adr),
00032     t_fine(0)
00033 {
00034     initialize();
00035 }
00036 
00037 BME280::~BME280()
00038 {
00039     if (NULL != i2c_p)
00040         delete  i2c_p;
00041 }
00042     
00043 void BME280::initialize()
00044 {
00045     char cmd[18];
00046  
00047     cmd[0] = 0xf2; // ctrl_hum
00048     cmd[1] = 0x05; // Humidity oversampling x16
00049     i2c.write(address, cmd, 2);
00050  
00051     cmd[0] = 0xf4; // ctrl_meas
00052     cmd[1] = 0xB7; // Temparature oversampling x16, Pressure oversampling x16, Normal mode
00053     i2c.write(address, cmd, 2);
00054  
00055     cmd[0] = 0xf5; // config
00056     cmd[1] = 0xa0; // Standby 1000ms, Filter off
00057     i2c.write(address, cmd, 2);
00058  
00059     cmd[0] = 0x88; // read dig_T regs
00060     i2c.write(address, cmd, 1);
00061     i2c.read(address, cmd, 6);
00062  
00063     dig_T1 = (cmd[1] << 8) | cmd[0];
00064     dig_T2 = (cmd[3] << 8) | cmd[2];
00065     dig_T3 = (cmd[5] << 8) | cmd[4];
00066  
00067     DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
00068  
00069     cmd[0] = 0x8E; // read dig_P regs
00070     i2c.write(address, cmd, 1);
00071     i2c.read(address, cmd, 18);
00072  
00073     dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
00074     dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
00075     dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
00076     dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
00077     dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
00078     dig_P6 = (cmd[11] << 8) | cmd[10];
00079     dig_P7 = (cmd[13] << 8) | cmd[12];
00080     dig_P8 = (cmd[15] << 8) | cmd[14];
00081     dig_P9 = (cmd[17] << 8) | cmd[16];
00082  
00083     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);
00084  
00085     cmd[0] = 0xA1; // read dig_H regs
00086     i2c.write(address, cmd, 1);
00087     i2c.read(address, cmd, 1);
00088      cmd[1] = 0xE1; // read dig_H regs
00089     i2c.write(address, &cmd[1], 1);
00090     i2c.read(address, &cmd[1], 7);
00091 
00092     dig_H1 = cmd[0];
00093     dig_H2 = (cmd[2] << 8) | cmd[1];
00094     dig_H3 = cmd[3];
00095     dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
00096     dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
00097     dig_H6 = cmd[7];
00098  
00099     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);
00100 }
00101 
00102 bool BME280::deviceExists() {
00103     char cmd[4];
00104  
00105     cmd[0] = 0xd0; // device ID
00106     i2c.write(address, cmd, 1);
00107     i2c.read(address, &cmd[1], 1);
00108 
00109     return ( cmd[1] == 0x60 );
00110 }
00111  
00112 float BME280::getTemperature()
00113 {
00114     uint32_t temp_raw;
00115     float tempf;
00116     char cmd[4];
00117  
00118     cmd[0] = 0xfa; // temp_msb
00119     i2c.write(address, cmd, 1);
00120     i2c.read(address, &cmd[1], 3);
00121  
00122     temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00123  
00124     int32_t temp;
00125  
00126     temp =
00127         (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
00128         ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
00129  
00130     t_fine = temp;
00131     temp = (temp * 5 + 128) >> 8;
00132     tempf = (float)temp;
00133  
00134     return (tempf/100.0f);
00135 }
00136  
00137 float BME280::getPressure()
00138 {
00139     uint32_t press_raw;
00140     float pressf;
00141     char cmd[4];
00142  
00143     cmd[0] = 0xf7; // press_msb
00144     i2c.write(address, cmd, 1);
00145     i2c.read(address, &cmd[1], 3);
00146  
00147     press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00148  
00149     int32_t var1, var2;
00150     uint32_t press;
00151  
00152     var1 = (t_fine >> 1) - 64000;
00153     var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
00154     var2 = var2 + ((var1 * dig_P5) << 1);
00155     var2 = (var2 >> 2) + (dig_P4 << 16);
00156     var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
00157     var1 = ((32768 + var1) * dig_P1) >> 15;
00158     if (var1 == 0) {
00159         return 0;
00160     }
00161     press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
00162     if(press < 0x80000000) {
00163         press = (press << 1) / var1;
00164     } else {
00165         press = (press / var1) * 2;
00166     }
00167     var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
00168     var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
00169     press = (press + ((var1 + var2 + dig_P7) >> 4));
00170  
00171     pressf = (float)press;
00172     return (pressf/100.0f);
00173 }
00174  
00175 float BME280::getHumidity()
00176 {
00177     uint32_t hum_raw;
00178     float humf;
00179     char cmd[4];
00180  
00181     cmd[0] = 0xfd; // hum_msb
00182     i2c.write(address, cmd, 1);
00183     i2c.read(address, &cmd[1], 2);
00184  
00185     hum_raw = (cmd[1] << 8) | cmd[2];
00186  
00187     int32_t v_x1;
00188  
00189     v_x1 = t_fine - 76800;
00190     v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
00191                ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
00192                                             (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
00193                                             (int32_t)dig_H2 + 8192) >> 14));
00194     v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
00195     v_x1 = (v_x1 < 0 ? 0 : v_x1);
00196     v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
00197  
00198     humf = (float)(v_x1 >> 12);
00199  
00200     return (humf/1024.0f);
00201 }