東北大学学友会準加盟団体 From The Earth の高高度ロケットFTE-06(通称:海豚)にて使用したソフトウェアです.ご自由にお使いください.このプログラムによって生じた損害について当団体は一切責任を負いません.また,各モジュールのライブラリは当団体が作成したものではないので再配布は禁止します.

Dependencies:   mbed FATFileSystem

Fork of FTE-06 by Tetsushi Amano

Committer:
mizuki_akaike
Date:
Mon Aug 14 04:56:52 2017 +0000
Revision:
23:9be034083282
Child:
44:4e10bf6f9213
?????????????;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mizuki_akaike 23:9be034083282 1 /**
mizuki_akaike 23:9be034083282 2 * BME280 Combined humidity and pressure sensor library
mizuki_akaike 23:9be034083282 3 *
mizuki_akaike 23:9be034083282 4 * @author Toyomasa Watarai
mizuki_akaike 23:9be034083282 5 * @version 1.0
mizuki_akaike 23:9be034083282 6 * @date 06-April-2015
mizuki_akaike 23:9be034083282 7 *
mizuki_akaike 23:9be034083282 8 * Library for "BME280 temperature, humidity and pressure sensor module" from Switch Science
mizuki_akaike 23:9be034083282 9 * https://www.switch-science.com/catalog/2236/
mizuki_akaike 23:9be034083282 10 *
mizuki_akaike 23:9be034083282 11 * For more information about the BME280:
mizuki_akaike 23:9be034083282 12 * http://ae-bst.resource.bosch.com/media/products/dokumente/bme280/BST-BME280_DS001-10.pdf
mizuki_akaike 23:9be034083282 13 */
mizuki_akaike 23:9be034083282 14
mizuki_akaike 23:9be034083282 15 #include "mbed.h"
mizuki_akaike 23:9be034083282 16 #include "BME280.h"
mizuki_akaike 23:9be034083282 17
mizuki_akaike 23:9be034083282 18 BME280::BME280(PinName sda, PinName scl, char slave_adr)
mizuki_akaike 23:9be034083282 19 :
mizuki_akaike 23:9be034083282 20 i2c_p(new I2C(sda, scl)),
mizuki_akaike 23:9be034083282 21 i2c(*i2c_p),
mizuki_akaike 23:9be034083282 22 address(slave_adr),
mizuki_akaike 23:9be034083282 23 t_fine(0)
mizuki_akaike 23:9be034083282 24 {
mizuki_akaike 23:9be034083282 25 initialize();
mizuki_akaike 23:9be034083282 26 }
mizuki_akaike 23:9be034083282 27
mizuki_akaike 23:9be034083282 28 BME280::BME280(I2C &i2c_obj, char slave_adr)
mizuki_akaike 23:9be034083282 29 :
mizuki_akaike 23:9be034083282 30 i2c_p(NULL),
mizuki_akaike 23:9be034083282 31 i2c(i2c_obj),
mizuki_akaike 23:9be034083282 32 address(slave_adr),
mizuki_akaike 23:9be034083282 33 t_fine(0)
mizuki_akaike 23:9be034083282 34 {
mizuki_akaike 23:9be034083282 35 initialize();
mizuki_akaike 23:9be034083282 36 }
mizuki_akaike 23:9be034083282 37
mizuki_akaike 23:9be034083282 38 BME280::~BME280()
mizuki_akaike 23:9be034083282 39 {
mizuki_akaike 23:9be034083282 40 if (NULL != i2c_p)
mizuki_akaike 23:9be034083282 41 delete i2c_p;
mizuki_akaike 23:9be034083282 42 }
mizuki_akaike 23:9be034083282 43
mizuki_akaike 23:9be034083282 44 void BME280::initialize()
mizuki_akaike 23:9be034083282 45 {
mizuki_akaike 23:9be034083282 46 char cmd[18];
mizuki_akaike 23:9be034083282 47
mizuki_akaike 23:9be034083282 48 cmd[0] = 0xf2; // ctrl_hum
mizuki_akaike 23:9be034083282 49 cmd[1] = 0x01; // Humidity oversampling x1
mizuki_akaike 23:9be034083282 50 i2c.write(address, cmd, 2);
mizuki_akaike 23:9be034083282 51
mizuki_akaike 23:9be034083282 52 cmd[0] = 0xf4; // ctrl_meas
mizuki_akaike 23:9be034083282 53 cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
mizuki_akaike 23:9be034083282 54 i2c.write(address, cmd, 2);
mizuki_akaike 23:9be034083282 55
mizuki_akaike 23:9be034083282 56 cmd[0] = 0xf5; // config
mizuki_akaike 23:9be034083282 57 cmd[1] = 0xa0; // Standby 1000ms, Filter off
mizuki_akaike 23:9be034083282 58 i2c.write(address, cmd, 2);
mizuki_akaike 23:9be034083282 59
mizuki_akaike 23:9be034083282 60 cmd[0] = 0x88; // read dig_T regs
mizuki_akaike 23:9be034083282 61 i2c.write(address, cmd, 1);
mizuki_akaike 23:9be034083282 62 i2c.read(address, cmd, 6);
mizuki_akaike 23:9be034083282 63
mizuki_akaike 23:9be034083282 64 dig_T1 = (cmd[1] << 8) | cmd[0];
mizuki_akaike 23:9be034083282 65 dig_T2 = (cmd[3] << 8) | cmd[2];
mizuki_akaike 23:9be034083282 66 dig_T3 = (cmd[5] << 8) | cmd[4];
mizuki_akaike 23:9be034083282 67
mizuki_akaike 23:9be034083282 68 DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
mizuki_akaike 23:9be034083282 69
mizuki_akaike 23:9be034083282 70 cmd[0] = 0x8E; // read dig_P regs
mizuki_akaike 23:9be034083282 71 i2c.write(address, cmd, 1);
mizuki_akaike 23:9be034083282 72 i2c.read(address, cmd, 18);
mizuki_akaike 23:9be034083282 73
mizuki_akaike 23:9be034083282 74 dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
mizuki_akaike 23:9be034083282 75 dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
mizuki_akaike 23:9be034083282 76 dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
mizuki_akaike 23:9be034083282 77 dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
mizuki_akaike 23:9be034083282 78 dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
mizuki_akaike 23:9be034083282 79 dig_P6 = (cmd[11] << 8) | cmd[10];
mizuki_akaike 23:9be034083282 80 dig_P7 = (cmd[13] << 8) | cmd[12];
mizuki_akaike 23:9be034083282 81 dig_P8 = (cmd[15] << 8) | cmd[14];
mizuki_akaike 23:9be034083282 82 dig_P9 = (cmd[17] << 8) | cmd[16];
mizuki_akaike 23:9be034083282 83
mizuki_akaike 23:9be034083282 84 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);
mizuki_akaike 23:9be034083282 85
mizuki_akaike 23:9be034083282 86 cmd[0] = 0xA1; // read dig_H regs
mizuki_akaike 23:9be034083282 87 i2c.write(address, cmd, 1);
mizuki_akaike 23:9be034083282 88 i2c.read(address, cmd, 1);
mizuki_akaike 23:9be034083282 89 cmd[1] = 0xE1; // read dig_H regs
mizuki_akaike 23:9be034083282 90 i2c.write(address, &cmd[1], 1);
mizuki_akaike 23:9be034083282 91 i2c.read(address, &cmd[1], 7);
mizuki_akaike 23:9be034083282 92
mizuki_akaike 23:9be034083282 93 dig_H1 = cmd[0];
mizuki_akaike 23:9be034083282 94 dig_H2 = (cmd[2] << 8) | cmd[1];
mizuki_akaike 23:9be034083282 95 dig_H3 = cmd[3];
mizuki_akaike 23:9be034083282 96 dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
mizuki_akaike 23:9be034083282 97 dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
mizuki_akaike 23:9be034083282 98 dig_H6 = cmd[7];
mizuki_akaike 23:9be034083282 99
mizuki_akaike 23:9be034083282 100 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);
mizuki_akaike 23:9be034083282 101 }
mizuki_akaike 23:9be034083282 102
mizuki_akaike 23:9be034083282 103 float BME280::getTemperature()
mizuki_akaike 23:9be034083282 104 {
mizuki_akaike 23:9be034083282 105 uint32_t temp_raw;
mizuki_akaike 23:9be034083282 106 float tempf;
mizuki_akaike 23:9be034083282 107 char cmd[4];
mizuki_akaike 23:9be034083282 108
mizuki_akaike 23:9be034083282 109 cmd[0] = 0xfa; // temp_msb
mizuki_akaike 23:9be034083282 110 i2c.write(address, cmd, 1);
mizuki_akaike 23:9be034083282 111 i2c.read(address, &cmd[1], 3);
mizuki_akaike 23:9be034083282 112
mizuki_akaike 23:9be034083282 113 temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
mizuki_akaike 23:9be034083282 114
mizuki_akaike 23:9be034083282 115 int32_t temp;
mizuki_akaike 23:9be034083282 116
mizuki_akaike 23:9be034083282 117 temp =
mizuki_akaike 23:9be034083282 118 (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
mizuki_akaike 23:9be034083282 119 ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
mizuki_akaike 23:9be034083282 120
mizuki_akaike 23:9be034083282 121 t_fine = temp;
mizuki_akaike 23:9be034083282 122 temp = (temp * 5 + 128) >> 8;
mizuki_akaike 23:9be034083282 123 tempf = (float)temp;
mizuki_akaike 23:9be034083282 124
mizuki_akaike 23:9be034083282 125 return (tempf/100.0f);
mizuki_akaike 23:9be034083282 126 }
mizuki_akaike 23:9be034083282 127
mizuki_akaike 23:9be034083282 128 float BME280::getPressure()
mizuki_akaike 23:9be034083282 129 {
mizuki_akaike 23:9be034083282 130 uint32_t press_raw;
mizuki_akaike 23:9be034083282 131 float pressf;
mizuki_akaike 23:9be034083282 132 char cmd[4];
mizuki_akaike 23:9be034083282 133
mizuki_akaike 23:9be034083282 134 cmd[0] = 0xf7; // press_msb
mizuki_akaike 23:9be034083282 135 i2c.write(address, cmd, 1);
mizuki_akaike 23:9be034083282 136 i2c.read(address, &cmd[1], 3);
mizuki_akaike 23:9be034083282 137
mizuki_akaike 23:9be034083282 138 press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
mizuki_akaike 23:9be034083282 139
mizuki_akaike 23:9be034083282 140 int32_t var1, var2;
mizuki_akaike 23:9be034083282 141 uint32_t press;
mizuki_akaike 23:9be034083282 142
mizuki_akaike 23:9be034083282 143 var1 = (t_fine >> 1) - 64000;
mizuki_akaike 23:9be034083282 144 var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
mizuki_akaike 23:9be034083282 145 var2 = var2 + ((var1 * dig_P5) << 1);
mizuki_akaike 23:9be034083282 146 var2 = (var2 >> 2) + (dig_P4 << 16);
mizuki_akaike 23:9be034083282 147 var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
mizuki_akaike 23:9be034083282 148 var1 = ((32768 + var1) * dig_P1) >> 15;
mizuki_akaike 23:9be034083282 149 if (var1 == 0) {
mizuki_akaike 23:9be034083282 150 return 0;
mizuki_akaike 23:9be034083282 151 }
mizuki_akaike 23:9be034083282 152 press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
mizuki_akaike 23:9be034083282 153 if(press < 0x80000000) {
mizuki_akaike 23:9be034083282 154 press = (press << 1) / var1;
mizuki_akaike 23:9be034083282 155 } else {
mizuki_akaike 23:9be034083282 156 press = (press / var1) * 2;
mizuki_akaike 23:9be034083282 157 }
mizuki_akaike 23:9be034083282 158 var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
mizuki_akaike 23:9be034083282 159 var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
mizuki_akaike 23:9be034083282 160 press = (press + ((var1 + var2 + dig_P7) >> 4));
mizuki_akaike 23:9be034083282 161
mizuki_akaike 23:9be034083282 162 pressf = (float)press;
mizuki_akaike 23:9be034083282 163 return (pressf/100.0f);
mizuki_akaike 23:9be034083282 164 }
mizuki_akaike 23:9be034083282 165
mizuki_akaike 23:9be034083282 166 float BME280::getHumidity()
mizuki_akaike 23:9be034083282 167 {
mizuki_akaike 23:9be034083282 168 uint32_t hum_raw;
mizuki_akaike 23:9be034083282 169 float humf;
mizuki_akaike 23:9be034083282 170 char cmd[4];
mizuki_akaike 23:9be034083282 171
mizuki_akaike 23:9be034083282 172 cmd[0] = 0xfd; // hum_msb
mizuki_akaike 23:9be034083282 173 i2c.write(address, cmd, 1);
mizuki_akaike 23:9be034083282 174 i2c.read(address, &cmd[1], 2);
mizuki_akaike 23:9be034083282 175
mizuki_akaike 23:9be034083282 176 hum_raw = (cmd[1] << 8) | cmd[2];
mizuki_akaike 23:9be034083282 177
mizuki_akaike 23:9be034083282 178 int32_t v_x1;
mizuki_akaike 23:9be034083282 179
mizuki_akaike 23:9be034083282 180 v_x1 = t_fine - 76800;
mizuki_akaike 23:9be034083282 181 v_x1 = (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
mizuki_akaike 23:9be034083282 182 ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
mizuki_akaike 23:9be034083282 183 (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
mizuki_akaike 23:9be034083282 184 (int32_t)dig_H2 + 8192) >> 14));
mizuki_akaike 23:9be034083282 185 v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
mizuki_akaike 23:9be034083282 186 v_x1 = (v_x1 < 0 ? 0 : v_x1);
mizuki_akaike 23:9be034083282 187 v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
mizuki_akaike 23:9be034083282 188
mizuki_akaike 23:9be034083282 189 humf = (float)(v_x1 >> 12);
mizuki_akaike 23:9be034083282 190
mizuki_akaike 23:9be034083282 191 return (humf/1024.0f);
mizuki_akaike 23:9be034083282 192 }