Own Register Settings

Fork of BMP280 by Edwin Cho

Committer:
MACRUM
Date:
Mon Apr 06 09:13:00 2015 +0000
Revision:
0:ade9be832910
Child:
1:763a4018aaec
Initial release

Who changed what in which revision?

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