A modified library for BME280 sensor.

Dependents:   Auto_pilot_prototype_3_2

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     //I2C initialization
00062     i2c.frequency(400000); //400 kHz
00063     
00064     char cmd[18];
00065  
00066     cmd[0] = 0xf2; // ctrl_hum
00067     cmd[1] = 0x01; // Humidity oversampling x1
00068     i2c.write(address, cmd, 2);
00069  
00070     cmd[0] = 0xf4; // ctrl_meas
00071     cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
00072     i2c.write(address, cmd, 2);
00073  
00074     cmd[0] = 0xf5; // config
00075     cmd[1] = 0xa0; // Standby 1000ms, Filter off
00076     i2c.write(address, cmd, 2);
00077  
00078     cmd[0] = 0x88; // read dig_T regs
00079     i2c.write(address, cmd, 1);
00080     i2c.read(address, cmd, 6);
00081  
00082     dig_T1 = (cmd[1] << 8) | cmd[0];
00083     dig_T2 = (cmd[3] << 8) | cmd[2];
00084     dig_T3 = (cmd[5] << 8) | cmd[4];
00085  
00086     DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
00087  
00088     cmd[0] = 0x8E; // read dig_P regs
00089     i2c.write(address, cmd, 1);
00090     i2c.read(address, cmd, 18);
00091  
00092     dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
00093     dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
00094     dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
00095     dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
00096     dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
00097     dig_P6 = (cmd[11] << 8) | cmd[10];
00098     dig_P7 = (cmd[13] << 8) | cmd[12];
00099     dig_P8 = (cmd[15] << 8) | cmd[14];
00100     dig_P9 = (cmd[17] << 8) | cmd[16];
00101  
00102     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);
00103  
00104     cmd[0] = 0xA1; // read dig_H regs
00105     i2c.write(address, cmd, 1);
00106     i2c.read(address, cmd, 1);
00107      cmd[1] = 0xE1; // read dig_H regs
00108     i2c.write(address, &cmd[1], 1);
00109     i2c.read(address, &cmd[1], 7);
00110 
00111     dig_H1 = cmd[0];
00112     dig_H2 = (cmd[2] << 8) | cmd[1];
00113     dig_H3 = cmd[3];
00114     dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
00115     dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
00116     dig_H6 = cmd[7];
00117  
00118     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);
00119 }
00120 
00121 float BME280::getTemperature()
00122 {
00123     uint32_t temp_raw;
00124     float tempf;
00125     char cmd[4];
00126  
00127     cmd[0] = 0xfa; // temp_msb
00128     i2c.write(address, cmd, 1);
00129     i2c.read(address, &cmd[1], 3);
00130  
00131     temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00132  
00133     int32_t temp;
00134  
00135     temp =
00136         (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
00137         ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
00138  
00139     t_fine = temp;
00140     temp = (temp * 5 + 128) >> 8;
00141     tempf = (float)temp;
00142  
00143     return (tempf/100.0f);
00144 }
00145  
00146 float BME280::getPressure()
00147 {
00148     uint32_t press_raw;
00149     float pressf;
00150     char cmd[4];
00151  
00152     cmd[0] = 0xf7; // press_msb
00153     i2c.write(address, cmd, 1);
00154     i2c.read(address, &cmd[1], 3);
00155  
00156     press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00157  
00158     int32_t var1, var2;
00159     uint32_t press;
00160  
00161     var1 = (t_fine >> 1) - 64000;
00162     var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
00163     var2 = var2 + ((var1 * dig_P5) << 1);
00164     var2 = (var2 >> 2) + (dig_P4 << 16);
00165     var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
00166     var1 = ((32768 + var1) * dig_P1) >> 15;
00167     if (var1 == 0) {
00168         return 0;
00169     }
00170     press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
00171     if(press < 0x80000000) {
00172         press = (press << 1) / var1;
00173     } else {
00174         press = (press / var1) * 2;
00175     }
00176     var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
00177     var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
00178     press = (press + ((var1 + var2 + dig_P7) >> 4));
00179  
00180     pressf = (float)press;
00181     return (pressf/100.0f);
00182 }
00183  
00184 float BME280::getHumidity()
00185 {
00186     uint32_t hum_raw;
00187     float humf;
00188     char cmd[4];
00189  
00190     cmd[0] = 0xfd; // hum_msb
00191     i2c.write(address, cmd, 1);
00192     i2c.read(address, &cmd[1], 2);
00193  
00194     hum_raw = (cmd[1] << 8) | cmd[2];
00195  
00196     int32_t v_x1;
00197  
00198     v_x1 = t_fine - 76800;
00199     v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
00200                ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
00201                                             (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
00202                                             (int32_t)dig_H2 + 8192) >> 14));
00203     v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
00204     v_x1 = (v_x1 < 0 ? 0 : v_x1);
00205     v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
00206  
00207     humf = (float)(v_x1 >> 12);
00208  
00209     return (humf/1024.0f);
00210 }