BME 280 library files

Committer:
Swabey89
Date:
Sun Nov 04 23:12:22 2018 +0000
Revision:
0:34bca8bf2146
BME280 library files added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Swabey89 0:34bca8bf2146 1 /**
Swabey89 0:34bca8bf2146 2 ******************************************************************************
Swabey89 0:34bca8bf2146 3 * @file BME280.cpp
Swabey89 0:34bca8bf2146 4 * @author Toyomasa Watarai
Swabey89 0:34bca8bf2146 5 * @version V1.0.0
Swabey89 0:34bca8bf2146 6 * @date 11 March 2017
Swabey89 0:34bca8bf2146 7 * @brief BME280 class implementation
Swabey89 0:34bca8bf2146 8 ******************************************************************************
Swabey89 0:34bca8bf2146 9 * @attention
Swabey89 0:34bca8bf2146 10 *
Swabey89 0:34bca8bf2146 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
Swabey89 0:34bca8bf2146 12 * of this software and associated documentation files (the "Software"), to deal
Swabey89 0:34bca8bf2146 13 * in the Software without restriction, including without limitation the rights
Swabey89 0:34bca8bf2146 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Swabey89 0:34bca8bf2146 15 * copies of the Software, and to permit persons to whom the Software is
Swabey89 0:34bca8bf2146 16 * furnished to do so, subject to the following conditions:
Swabey89 0:34bca8bf2146 17 *
Swabey89 0:34bca8bf2146 18 * The above copyright notice and this permission notice shall be included in
Swabey89 0:34bca8bf2146 19 * all copies or substantial portions of the Software.
Swabey89 0:34bca8bf2146 20 *
Swabey89 0:34bca8bf2146 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Swabey89 0:34bca8bf2146 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Swabey89 0:34bca8bf2146 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Swabey89 0:34bca8bf2146 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Swabey89 0:34bca8bf2146 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Swabey89 0:34bca8bf2146 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Swabey89 0:34bca8bf2146 27 * THE SOFTWARE.
Swabey89 0:34bca8bf2146 28 */
Swabey89 0:34bca8bf2146 29
Swabey89 0:34bca8bf2146 30 #include "mbed.h"
Swabey89 0:34bca8bf2146 31 #include "BME280.h"
Swabey89 0:34bca8bf2146 32
Swabey89 0:34bca8bf2146 33 BME280::BME280(PinName sda, PinName scl, char slave_adr)
Swabey89 0:34bca8bf2146 34 :
Swabey89 0:34bca8bf2146 35 i2c_p(new I2C(sda, scl)),
Swabey89 0:34bca8bf2146 36 i2c(*i2c_p),
Swabey89 0:34bca8bf2146 37 address(slave_adr),
Swabey89 0:34bca8bf2146 38 t_fine(0)
Swabey89 0:34bca8bf2146 39 {
Swabey89 0:34bca8bf2146 40 initialize();
Swabey89 0:34bca8bf2146 41 }
Swabey89 0:34bca8bf2146 42
Swabey89 0:34bca8bf2146 43 BME280::BME280(I2C &i2c_obj, char slave_adr)
Swabey89 0:34bca8bf2146 44 :
Swabey89 0:34bca8bf2146 45 i2c_p(NULL),
Swabey89 0:34bca8bf2146 46 i2c(i2c_obj),
Swabey89 0:34bca8bf2146 47 address(slave_adr),
Swabey89 0:34bca8bf2146 48 t_fine(0)
Swabey89 0:34bca8bf2146 49 {
Swabey89 0:34bca8bf2146 50 initialize();
Swabey89 0:34bca8bf2146 51 }
Swabey89 0:34bca8bf2146 52
Swabey89 0:34bca8bf2146 53 BME280::~BME280()
Swabey89 0:34bca8bf2146 54 {
Swabey89 0:34bca8bf2146 55 if (NULL != i2c_p)
Swabey89 0:34bca8bf2146 56 delete i2c_p;
Swabey89 0:34bca8bf2146 57 }
Swabey89 0:34bca8bf2146 58
Swabey89 0:34bca8bf2146 59 void BME280::initialize()
Swabey89 0:34bca8bf2146 60 {
Swabey89 0:34bca8bf2146 61 char cmd[18];
Swabey89 0:34bca8bf2146 62
Swabey89 0:34bca8bf2146 63 cmd[0] = 0xf2; // ctrl_hum
Swabey89 0:34bca8bf2146 64 cmd[1] = 0x01; // Humidity oversampling x1
Swabey89 0:34bca8bf2146 65 i2c.write(address, cmd, 2);
Swabey89 0:34bca8bf2146 66
Swabey89 0:34bca8bf2146 67 cmd[0] = 0xf4; // ctrl_meas
Swabey89 0:34bca8bf2146 68 cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
Swabey89 0:34bca8bf2146 69 i2c.write(address, cmd, 2);
Swabey89 0:34bca8bf2146 70
Swabey89 0:34bca8bf2146 71 cmd[0] = 0xf5; // config
Swabey89 0:34bca8bf2146 72 cmd[1] = 0xa0; // Standby 1000ms, Filter off
Swabey89 0:34bca8bf2146 73 i2c.write(address, cmd, 2);
Swabey89 0:34bca8bf2146 74
Swabey89 0:34bca8bf2146 75 cmd[0] = 0x88; // read dig_T regs
Swabey89 0:34bca8bf2146 76 i2c.write(address, cmd, 1);
Swabey89 0:34bca8bf2146 77 i2c.read(address, cmd, 6);
Swabey89 0:34bca8bf2146 78
Swabey89 0:34bca8bf2146 79 dig_T1 = (cmd[1] << 8) | cmd[0];
Swabey89 0:34bca8bf2146 80 dig_T2 = (cmd[3] << 8) | cmd[2];
Swabey89 0:34bca8bf2146 81 dig_T3 = (cmd[5] << 8) | cmd[4];
Swabey89 0:34bca8bf2146 82
Swabey89 0:34bca8bf2146 83 DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
Swabey89 0:34bca8bf2146 84
Swabey89 0:34bca8bf2146 85 cmd[0] = 0x8E; // read dig_P regs
Swabey89 0:34bca8bf2146 86 i2c.write(address, cmd, 1);
Swabey89 0:34bca8bf2146 87 i2c.read(address, cmd, 18);
Swabey89 0:34bca8bf2146 88
Swabey89 0:34bca8bf2146 89 dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
Swabey89 0:34bca8bf2146 90 dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
Swabey89 0:34bca8bf2146 91 dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
Swabey89 0:34bca8bf2146 92 dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
Swabey89 0:34bca8bf2146 93 dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
Swabey89 0:34bca8bf2146 94 dig_P6 = (cmd[11] << 8) | cmd[10];
Swabey89 0:34bca8bf2146 95 dig_P7 = (cmd[13] << 8) | cmd[12];
Swabey89 0:34bca8bf2146 96 dig_P8 = (cmd[15] << 8) | cmd[14];
Swabey89 0:34bca8bf2146 97 dig_P9 = (cmd[17] << 8) | cmd[16];
Swabey89 0:34bca8bf2146 98
Swabey89 0:34bca8bf2146 99 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);
Swabey89 0:34bca8bf2146 100
Swabey89 0:34bca8bf2146 101 cmd[0] = 0xA1; // read dig_H regs
Swabey89 0:34bca8bf2146 102 i2c.write(address, cmd, 1);
Swabey89 0:34bca8bf2146 103 i2c.read(address, cmd, 1);
Swabey89 0:34bca8bf2146 104 cmd[1] = 0xE1; // read dig_H regs
Swabey89 0:34bca8bf2146 105 i2c.write(address, &cmd[1], 1);
Swabey89 0:34bca8bf2146 106 i2c.read(address, &cmd[1], 7);
Swabey89 0:34bca8bf2146 107
Swabey89 0:34bca8bf2146 108 dig_H1 = cmd[0];
Swabey89 0:34bca8bf2146 109 dig_H2 = (cmd[2] << 8) | cmd[1];
Swabey89 0:34bca8bf2146 110 dig_H3 = cmd[3];
Swabey89 0:34bca8bf2146 111 dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
Swabey89 0:34bca8bf2146 112 dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
Swabey89 0:34bca8bf2146 113 dig_H6 = cmd[7];
Swabey89 0:34bca8bf2146 114
Swabey89 0:34bca8bf2146 115 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);
Swabey89 0:34bca8bf2146 116 }
Swabey89 0:34bca8bf2146 117
Swabey89 0:34bca8bf2146 118 float BME280::getTemperature()
Swabey89 0:34bca8bf2146 119 {
Swabey89 0:34bca8bf2146 120 uint32_t temp_raw;
Swabey89 0:34bca8bf2146 121 float tempf;
Swabey89 0:34bca8bf2146 122 char cmd[4];
Swabey89 0:34bca8bf2146 123
Swabey89 0:34bca8bf2146 124 cmd[0] = 0xfa; // temp_msb
Swabey89 0:34bca8bf2146 125 i2c.write(address, cmd, 1);
Swabey89 0:34bca8bf2146 126 i2c.read(address, &cmd[1], 3);
Swabey89 0:34bca8bf2146 127
Swabey89 0:34bca8bf2146 128 temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
Swabey89 0:34bca8bf2146 129
Swabey89 0:34bca8bf2146 130 int32_t temp;
Swabey89 0:34bca8bf2146 131
Swabey89 0:34bca8bf2146 132 temp =
Swabey89 0:34bca8bf2146 133 (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
Swabey89 0:34bca8bf2146 134 ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
Swabey89 0:34bca8bf2146 135
Swabey89 0:34bca8bf2146 136 t_fine = temp;
Swabey89 0:34bca8bf2146 137 temp = (temp * 5 + 128) >> 8;
Swabey89 0:34bca8bf2146 138 tempf = (float)temp;
Swabey89 0:34bca8bf2146 139
Swabey89 0:34bca8bf2146 140 return (tempf/100.0f);
Swabey89 0:34bca8bf2146 141 }
Swabey89 0:34bca8bf2146 142
Swabey89 0:34bca8bf2146 143 float BME280::getPressure()
Swabey89 0:34bca8bf2146 144 {
Swabey89 0:34bca8bf2146 145 uint32_t press_raw;
Swabey89 0:34bca8bf2146 146 float pressf;
Swabey89 0:34bca8bf2146 147 char cmd[4];
Swabey89 0:34bca8bf2146 148
Swabey89 0:34bca8bf2146 149 cmd[0] = 0xf7; // press_msb
Swabey89 0:34bca8bf2146 150 i2c.write(address, cmd, 1);
Swabey89 0:34bca8bf2146 151 i2c.read(address, &cmd[1], 3);
Swabey89 0:34bca8bf2146 152
Swabey89 0:34bca8bf2146 153 press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
Swabey89 0:34bca8bf2146 154
Swabey89 0:34bca8bf2146 155 int32_t var1, var2;
Swabey89 0:34bca8bf2146 156 uint32_t press;
Swabey89 0:34bca8bf2146 157
Swabey89 0:34bca8bf2146 158 var1 = (t_fine >> 1) - 64000;
Swabey89 0:34bca8bf2146 159 var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
Swabey89 0:34bca8bf2146 160 var2 = var2 + ((var1 * dig_P5) << 1);
Swabey89 0:34bca8bf2146 161 var2 = (var2 >> 2) + (dig_P4 << 16);
Swabey89 0:34bca8bf2146 162 var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
Swabey89 0:34bca8bf2146 163 var1 = ((32768 + var1) * dig_P1) >> 15;
Swabey89 0:34bca8bf2146 164 if (var1 == 0) {
Swabey89 0:34bca8bf2146 165 return 0;
Swabey89 0:34bca8bf2146 166 }
Swabey89 0:34bca8bf2146 167 press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
Swabey89 0:34bca8bf2146 168 if(press < 0x80000000) {
Swabey89 0:34bca8bf2146 169 press = (press << 1) / var1;
Swabey89 0:34bca8bf2146 170 } else {
Swabey89 0:34bca8bf2146 171 press = (press / var1) * 2;
Swabey89 0:34bca8bf2146 172 }
Swabey89 0:34bca8bf2146 173 var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
Swabey89 0:34bca8bf2146 174 var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
Swabey89 0:34bca8bf2146 175 press = (press + ((var1 + var2 + dig_P7) >> 4));
Swabey89 0:34bca8bf2146 176
Swabey89 0:34bca8bf2146 177 pressf = (float)press;
Swabey89 0:34bca8bf2146 178 return (pressf/100.0f);
Swabey89 0:34bca8bf2146 179 }
Swabey89 0:34bca8bf2146 180
Swabey89 0:34bca8bf2146 181 float BME280::getHumidity()
Swabey89 0:34bca8bf2146 182 {
Swabey89 0:34bca8bf2146 183 uint32_t hum_raw;
Swabey89 0:34bca8bf2146 184 float humf;
Swabey89 0:34bca8bf2146 185 char cmd[4];
Swabey89 0:34bca8bf2146 186
Swabey89 0:34bca8bf2146 187 cmd[0] = 0xfd; // hum_msb
Swabey89 0:34bca8bf2146 188 i2c.write(address, cmd, 1);
Swabey89 0:34bca8bf2146 189 i2c.read(address, &cmd[1], 2);
Swabey89 0:34bca8bf2146 190
Swabey89 0:34bca8bf2146 191 hum_raw = (cmd[1] << 8) | cmd[2];
Swabey89 0:34bca8bf2146 192
Swabey89 0:34bca8bf2146 193 int32_t v_x1;
Swabey89 0:34bca8bf2146 194
Swabey89 0:34bca8bf2146 195 v_x1 = t_fine - 76800;
Swabey89 0:34bca8bf2146 196 v_x1 = (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
Swabey89 0:34bca8bf2146 197 ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
Swabey89 0:34bca8bf2146 198 (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
Swabey89 0:34bca8bf2146 199 (int32_t)dig_H2 + 8192) >> 14));
Swabey89 0:34bca8bf2146 200 v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
Swabey89 0:34bca8bf2146 201 v_x1 = (v_x1 < 0 ? 0 : v_x1);
Swabey89 0:34bca8bf2146 202 v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
Swabey89 0:34bca8bf2146 203
Swabey89 0:34bca8bf2146 204 humf = (float)(v_x1 >> 12);
Swabey89 0:34bca8bf2146 205
Swabey89 0:34bca8bf2146 206 return (humf/1024.0f);
Swabey89 0:34bca8bf2146 207 }