Teste de BME280 para Coragem

Dependencies:   BME280

Committer:
brunnobbco
Date:
Thu Jul 11 18:03:58 2019 +0000
Revision:
4:f772c1ae7c97
Parent:
0:2c9585cecfde
Coragem BME280 Teste

Who changed what in which revision?

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