wakaranai

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