p2p-pingpong-clickbutton
Dependencies: PingPong-P2P-ClickButtonToWork-SX1272 Lora_SX1272_Coragem
bme280.txt@5:ef21a65a3450, 2019-12-04 (annotated)
- Committer:
- marcoantonioara
- Date:
- Wed Dec 04 11:50:35 2019 +0000
- Revision:
- 5:ef21a65a3450
Test Coragem using Lora
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcoantonioara | 5:ef21a65a3450 | 1 | |
marcoantonioara | 5:ef21a65a3450 | 2 | I2C sensor(p13, p15); |
marcoantonioara | 5:ef21a65a3450 | 3 | const int BME280_ADDR = (0x77 << 1); |
marcoantonioara | 5:ef21a65a3450 | 4 | |
marcoantonioara | 5:ef21a65a3450 | 5 | /////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 6 | // Globals variables |
marcoantonioara | 5:ef21a65a3450 | 7 | /////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 8 | |
marcoantonioara | 5:ef21a65a3450 | 9 | char cmd_bme[18]; |
marcoantonioara | 5:ef21a65a3450 | 10 | uint32_t hum_raw; |
marcoantonioara | 5:ef21a65a3450 | 11 | float humf; |
marcoantonioara | 5:ef21a65a3450 | 12 | uint32_t temp_raw; |
marcoantonioara | 5:ef21a65a3450 | 13 | float tempf; |
marcoantonioara | 5:ef21a65a3450 | 14 | uint32_t press_raw; |
marcoantonioara | 5:ef21a65a3450 | 15 | float pressf; |
marcoantonioara | 5:ef21a65a3450 | 16 | //char cmd[4]; |
marcoantonioara | 5:ef21a65a3450 | 17 | uint16_t dig_T1,dig_P1,dig_H1, dig_H3; |
marcoantonioara | 5:ef21a65a3450 | 18 | int16_t dig_T2, dig_T3,dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9,dig_H2, dig_H4, dig_H5, dig_H6; |
marcoantonioara | 5:ef21a65a3450 | 19 | int32_t t_fine; |
marcoantonioara | 5:ef21a65a3450 | 20 | |
marcoantonioara | 5:ef21a65a3450 | 21 | |
marcoantonioara | 5:ef21a65a3450 | 22 | void bme_init() { //bme initialization |
marcoantonioara | 5:ef21a65a3450 | 23 | |
marcoantonioara | 5:ef21a65a3450 | 24 | sensor.frequency(1000000); |
marcoantonioara | 5:ef21a65a3450 | 25 | |
marcoantonioara | 5:ef21a65a3450 | 26 | cmd_bme[0] = 0xf2; // ctrl_hum |
marcoantonioara | 5:ef21a65a3450 | 27 | cmd_bme[1] = 0x01; |
marcoantonioara | 5:ef21a65a3450 | 28 | sensor.write(BME280_ADDR, cmd_bme, 2); |
marcoantonioara | 5:ef21a65a3450 | 29 | |
marcoantonioara | 5:ef21a65a3450 | 30 | cmd_bme[0] = 0xf4; // ctrl_meas |
marcoantonioara | 5:ef21a65a3450 | 31 | cmd_bme[1] = 0x27; |
marcoantonioara | 5:ef21a65a3450 | 32 | sensor.write(BME280_ADDR, cmd_bme, 2); |
marcoantonioara | 5:ef21a65a3450 | 33 | |
marcoantonioara | 5:ef21a65a3450 | 34 | cmd_bme[0] = 0xf5; // config |
marcoantonioara | 5:ef21a65a3450 | 35 | cmd_bme[1] = 0xa0; |
marcoantonioara | 5:ef21a65a3450 | 36 | sensor.write(BME280_ADDR, cmd_bme, 2); |
marcoantonioara | 5:ef21a65a3450 | 37 | |
marcoantonioara | 5:ef21a65a3450 | 38 | cmd_bme[0] = 0x88; // read dig_T regs |
marcoantonioara | 5:ef21a65a3450 | 39 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 40 | sensor.read(BME280_ADDR, cmd_bme, 6); |
marcoantonioara | 5:ef21a65a3450 | 41 | |
marcoantonioara | 5:ef21a65a3450 | 42 | dig_T1 = (cmd_bme[1] << 8) | cmd_bme[0]; |
marcoantonioara | 5:ef21a65a3450 | 43 | dig_T2 = (cmd_bme[3] << 8) | cmd_bme[2]; |
marcoantonioara | 5:ef21a65a3450 | 44 | dig_T3 = (cmd_bme[5] << 8) | cmd_bme[4]; |
marcoantonioara | 5:ef21a65a3450 | 45 | |
marcoantonioara | 5:ef21a65a3450 | 46 | // DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3); |
marcoantonioara | 5:ef21a65a3450 | 47 | |
marcoantonioara | 5:ef21a65a3450 | 48 | cmd_bme[0] = 0x8E; // read dig_P regs |
marcoantonioara | 5:ef21a65a3450 | 49 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 50 | sensor.read(BME280_ADDR, cmd_bme, 18); |
marcoantonioara | 5:ef21a65a3450 | 51 | |
marcoantonioara | 5:ef21a65a3450 | 52 | dig_P1 = (cmd_bme[ 1] << 8) | cmd_bme[ 0]; |
marcoantonioara | 5:ef21a65a3450 | 53 | dig_P2 = (cmd_bme[ 3] << 8) | cmd_bme[ 2]; |
marcoantonioara | 5:ef21a65a3450 | 54 | dig_P3 = (cmd_bme[ 5] << 8) | cmd_bme[ 4]; |
marcoantonioara | 5:ef21a65a3450 | 55 | dig_P4 = (cmd_bme[ 7] << 8) | cmd_bme[ 6]; |
marcoantonioara | 5:ef21a65a3450 | 56 | dig_P5 = (cmd_bme[ 9] << 8) | cmd_bme[ 8]; |
marcoantonioara | 5:ef21a65a3450 | 57 | dig_P6 = (cmd_bme[11] << 8) | cmd_bme[10]; |
marcoantonioara | 5:ef21a65a3450 | 58 | dig_P7 = (cmd_bme[13] << 8) | cmd_bme[12]; |
marcoantonioara | 5:ef21a65a3450 | 59 | dig_P8 = (cmd_bme[15] << 8) | cmd_bme[14]; |
marcoantonioara | 5:ef21a65a3450 | 60 | dig_P9 = (cmd_bme[17] << 8) | cmd_bme[16]; |
marcoantonioara | 5:ef21a65a3450 | 61 | |
marcoantonioara | 5:ef21a65a3450 | 62 | // 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); |
marcoantonioara | 5:ef21a65a3450 | 63 | |
marcoantonioara | 5:ef21a65a3450 | 64 | cmd_bme[0] = 0xA1; // read dig_H regs |
marcoantonioara | 5:ef21a65a3450 | 65 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 66 | sensor.read(BME280_ADDR, cmd_bme, 9); |
marcoantonioara | 5:ef21a65a3450 | 67 | |
marcoantonioara | 5:ef21a65a3450 | 68 | dig_H1 = cmd_bme[0]; |
marcoantonioara | 5:ef21a65a3450 | 69 | dig_H2 = (cmd_bme[2] << 8) | cmd_bme[1]; |
marcoantonioara | 5:ef21a65a3450 | 70 | dig_H3 = cmd_bme[3]; |
marcoantonioara | 5:ef21a65a3450 | 71 | dig_H4 = (cmd_bme[4] << 4) | (cmd_bme[5] & 0x0f); |
marcoantonioara | 5:ef21a65a3450 | 72 | dig_H5 = (cmd_bme[7] << 4) | ((cmd_bme[6]>>4) & 0x0f); |
marcoantonioara | 5:ef21a65a3450 | 73 | dig_H6 = cmd_bme[8]; |
marcoantonioara | 5:ef21a65a3450 | 74 | |
marcoantonioara | 5:ef21a65a3450 | 75 | // 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); |
marcoantonioara | 5:ef21a65a3450 | 76 | } |
marcoantonioara | 5:ef21a65a3450 | 77 | |
marcoantonioara | 5:ef21a65a3450 | 78 | float getTemperature(){ |
marcoantonioara | 5:ef21a65a3450 | 79 | |
marcoantonioara | 5:ef21a65a3450 | 80 | |
marcoantonioara | 5:ef21a65a3450 | 81 | cmd_bme[0] = 0xfa; // temp_msb |
marcoantonioara | 5:ef21a65a3450 | 82 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 83 | sensor.read(BME280_ADDR, &cmd_bme[1], 1); |
marcoantonioara | 5:ef21a65a3450 | 84 | |
marcoantonioara | 5:ef21a65a3450 | 85 | cmd_bme[0] = 0xfb; // temp_lsb |
marcoantonioara | 5:ef21a65a3450 | 86 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 87 | sensor.read(BME280_ADDR, &cmd_bme[2], 1); |
marcoantonioara | 5:ef21a65a3450 | 88 | |
marcoantonioara | 5:ef21a65a3450 | 89 | cmd_bme[0] = 0xfc; // temp_xlsb |
marcoantonioara | 5:ef21a65a3450 | 90 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 91 | sensor.read(BME280_ADDR, &cmd_bme[3], 1); |
marcoantonioara | 5:ef21a65a3450 | 92 | |
marcoantonioara | 5:ef21a65a3450 | 93 | temp_raw = (cmd_bme[1] << 12) | (cmd_bme[2] << 4) | (cmd_bme[3] >> 4); |
marcoantonioara | 5:ef21a65a3450 | 94 | |
marcoantonioara | 5:ef21a65a3450 | 95 | int32_t temp; |
marcoantonioara | 5:ef21a65a3450 | 96 | |
marcoantonioara | 5:ef21a65a3450 | 97 | temp = |
marcoantonioara | 5:ef21a65a3450 | 98 | (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) + |
marcoantonioara | 5:ef21a65a3450 | 99 | ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14); |
marcoantonioara | 5:ef21a65a3450 | 100 | |
marcoantonioara | 5:ef21a65a3450 | 101 | t_fine = temp; |
marcoantonioara | 5:ef21a65a3450 | 102 | temp = (temp * 5 + 128) >> 8; |
marcoantonioara | 5:ef21a65a3450 | 103 | tempf = (float)temp; |
marcoantonioara | 5:ef21a65a3450 | 104 | |
marcoantonioara | 5:ef21a65a3450 | 105 | return (tempf/100.0f); |
marcoantonioara | 5:ef21a65a3450 | 106 | } |
marcoantonioara | 5:ef21a65a3450 | 107 | |
marcoantonioara | 5:ef21a65a3450 | 108 | float getPressure(){ |
marcoantonioara | 5:ef21a65a3450 | 109 | |
marcoantonioara | 5:ef21a65a3450 | 110 | cmd_bme[0] = 0xf7; // press_msb |
marcoantonioara | 5:ef21a65a3450 | 111 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 112 | sensor.read(BME280_ADDR, &cmd_bme[1], 1); |
marcoantonioara | 5:ef21a65a3450 | 113 | |
marcoantonioara | 5:ef21a65a3450 | 114 | cmd_bme[0] = 0xf8; // press_lsb |
marcoantonioara | 5:ef21a65a3450 | 115 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 116 | sensor.read(BME280_ADDR, &cmd_bme[2], 1); |
marcoantonioara | 5:ef21a65a3450 | 117 | |
marcoantonioara | 5:ef21a65a3450 | 118 | cmd_bme[0] = 0xf9; // press_xlsb |
marcoantonioara | 5:ef21a65a3450 | 119 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 120 | sensor.read(BME280_ADDR, &cmd_bme[3], 1); |
marcoantonioara | 5:ef21a65a3450 | 121 | |
marcoantonioara | 5:ef21a65a3450 | 122 | press_raw = (cmd_bme[1] << 12) | (cmd_bme[2] << 4) | (cmd_bme[3] >> 4); |
marcoantonioara | 5:ef21a65a3450 | 123 | |
marcoantonioara | 5:ef21a65a3450 | 124 | int32_t var1, var2; |
marcoantonioara | 5:ef21a65a3450 | 125 | uint32_t press; |
marcoantonioara | 5:ef21a65a3450 | 126 | |
marcoantonioara | 5:ef21a65a3450 | 127 | var1 = (t_fine >> 1) - 64000; |
marcoantonioara | 5:ef21a65a3450 | 128 | var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6; |
marcoantonioara | 5:ef21a65a3450 | 129 | var2 = var2 + ((var1 * dig_P5) << 1); |
marcoantonioara | 5:ef21a65a3450 | 130 | var2 = (var2 >> 2) + (dig_P4 << 16); |
marcoantonioara | 5:ef21a65a3450 | 131 | var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18; |
marcoantonioara | 5:ef21a65a3450 | 132 | var1 = ((32768 + var1) * dig_P1) >> 15; |
marcoantonioara | 5:ef21a65a3450 | 133 | if (var1 == 0) { |
marcoantonioara | 5:ef21a65a3450 | 134 | return 0; |
marcoantonioara | 5:ef21a65a3450 | 135 | } |
marcoantonioara | 5:ef21a65a3450 | 136 | press = (((1048576 - press_raw) - (var2 >> 12))) * 3125; |
marcoantonioara | 5:ef21a65a3450 | 137 | if(press < 0x80000000) { |
marcoantonioara | 5:ef21a65a3450 | 138 | press = (press << 1) / var1; |
marcoantonioara | 5:ef21a65a3450 | 139 | } |
marcoantonioara | 5:ef21a65a3450 | 140 | else { |
marcoantonioara | 5:ef21a65a3450 | 141 | press = (press / var1) * 2; |
marcoantonioara | 5:ef21a65a3450 | 142 | } |
marcoantonioara | 5:ef21a65a3450 | 143 | var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12; |
marcoantonioara | 5:ef21a65a3450 | 144 | var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13; |
marcoantonioara | 5:ef21a65a3450 | 145 | press = (press + ((var1 + var2 + dig_P7) >> 4)); |
marcoantonioara | 5:ef21a65a3450 | 146 | |
marcoantonioara | 5:ef21a65a3450 | 147 | pressf = (float)press; |
marcoantonioara | 5:ef21a65a3450 | 148 | return (pressf/100.0f); |
marcoantonioara | 5:ef21a65a3450 | 149 | } |
marcoantonioara | 5:ef21a65a3450 | 150 | |
marcoantonioara | 5:ef21a65a3450 | 151 | float getHumidity(){ |
marcoantonioara | 5:ef21a65a3450 | 152 | |
marcoantonioara | 5:ef21a65a3450 | 153 | |
marcoantonioara | 5:ef21a65a3450 | 154 | cmd_bme[0] = 0xfd; // hum_msb |
marcoantonioara | 5:ef21a65a3450 | 155 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 156 | sensor.read(BME280_ADDR, &cmd_bme[1], 1); |
marcoantonioara | 5:ef21a65a3450 | 157 | |
marcoantonioara | 5:ef21a65a3450 | 158 | cmd_bme[0] = 0xfd; // hum_lsb |
marcoantonioara | 5:ef21a65a3450 | 159 | sensor.write(BME280_ADDR, cmd_bme, 1); |
marcoantonioara | 5:ef21a65a3450 | 160 | sensor.read(BME280_ADDR, &cmd_bme[2], 1); |
marcoantonioara | 5:ef21a65a3450 | 161 | |
marcoantonioara | 5:ef21a65a3450 | 162 | hum_raw = (cmd_bme[1] << 8) | cmd_bme[2]; |
marcoantonioara | 5:ef21a65a3450 | 163 | |
marcoantonioara | 5:ef21a65a3450 | 164 | int32_t v_x1; |
marcoantonioara | 5:ef21a65a3450 | 165 | |
marcoantonioara | 5:ef21a65a3450 | 166 | v_x1 = t_fine - 76800; |
marcoantonioara | 5:ef21a65a3450 | 167 | v_x1 = (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) + |
marcoantonioara | 5:ef21a65a3450 | 168 | ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) * |
marcoantonioara | 5:ef21a65a3450 | 169 | (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) * |
marcoantonioara | 5:ef21a65a3450 | 170 | (int32_t)dig_H2 + 8192) >> 14)); |
marcoantonioara | 5:ef21a65a3450 | 171 | v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4)); |
marcoantonioara | 5:ef21a65a3450 | 172 | v_x1 = (v_x1 < 0 ? 0 : v_x1); |
marcoantonioara | 5:ef21a65a3450 | 173 | v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1); |
marcoantonioara | 5:ef21a65a3450 | 174 | |
marcoantonioara | 5:ef21a65a3450 | 175 | humf = (float)(v_x1 >> 12); |
marcoantonioara | 5:ef21a65a3450 | 176 | |
marcoantonioara | 5:ef21a65a3450 | 177 | return (humf/1024.0f); |
marcoantonioara | 5:ef21a65a3450 | 178 | } |