Titech Cansat / Mbed 2 deprecated myBME280

Dependencies:   mbed

Committer:
takepiyo
Date:
Tue Jul 16 12:19:17 2019 +0000
Revision:
0:f2d2dc1e207d
love

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takepiyo 0:f2d2dc1e207d 1 #include "mbed.h"
takepiyo 0:f2d2dc1e207d 2 #include "bme280.h"
takepiyo 0:f2d2dc1e207d 3
takepiyo 0:f2d2dc1e207d 4 BME280::BME280()
takepiyo 0:f2d2dc1e207d 5 {
takepiyo 0:f2d2dc1e207d 6 t_fine=0;
takepiyo 0:f2d2dc1e207d 7 char cmd[18];
takepiyo 0:f2d2dc1e207d 8 DigitalOut cs(p8);
takepiyo 0:f2d2dc1e207d 9 SPI spi(p5,p6,p7);
takepiyo 0:f2d2dc1e207d 10 cs=1;
takepiyo 0:f2d2dc1e207d 11 spi.format(8, 0); // 8-bit, mode=0
takepiyo 0:f2d2dc1e207d 12 spi.frequency(1000000); // 1MHZ
takepiyo 0:f2d2dc1e207d 13
takepiyo 0:f2d2dc1e207d 14 //ctrl_hum
takepiyo 0:f2d2dc1e207d 15 cs=0;
takepiyo 0:f2d2dc1e207d 16 spi.write(0xF2 & BME280_MASK);
takepiyo 0:f2d2dc1e207d 17 spi.write(0x03);//Humidity oversampling ×4
takepiyo 0:f2d2dc1e207d 18 cs=1;
takepiyo 0:f2d2dc1e207d 19
takepiyo 0:f2d2dc1e207d 20 //ctrl_meas
takepiyo 0:f2d2dc1e207d 21 cs=0;
takepiyo 0:f2d2dc1e207d 22 spi.write(0xF4 & BME280_MASK);
takepiyo 0:f2d2dc1e207d 23 spi.write((3<<5|3<<2)|3);//Temparature oversampling x4, Pressure oversampling x4, Normal mode
takepiyo 0:f2d2dc1e207d 24 cs=1;
takepiyo 0:f2d2dc1e207d 25
takepiyo 0:f2d2dc1e207d 26 //config
takepiyo 0:f2d2dc1e207d 27 cs=0;
takepiyo 0:f2d2dc1e207d 28 spi.write(0xF5 & BME280_MASK);
takepiyo 0:f2d2dc1e207d 29 spi.write(0b10100000);
takepiyo 0:f2d2dc1e207d 30 cs=1;
takepiyo 0:f2d2dc1e207d 31
takepiyo 0:f2d2dc1e207d 32 //設定おわり
takepiyo 0:f2d2dc1e207d 33 wait(1);
takepiyo 0:f2d2dc1e207d 34
takepiyo 0:f2d2dc1e207d 35 //校正用データの処理
takepiyo 0:f2d2dc1e207d 36 //温度の校正値取得
takepiyo 0:f2d2dc1e207d 37 cs=0;
takepiyo 0:f2d2dc1e207d 38 spi.write(0x88);
takepiyo 0:f2d2dc1e207d 39 for(char i = 0;i<6;i++)
takepiyo 0:f2d2dc1e207d 40 {
takepiyo 0:f2d2dc1e207d 41 cmd[i]=spi.write(0);
takepiyo 0:f2d2dc1e207d 42 }
takepiyo 0:f2d2dc1e207d 43 cs=1;
takepiyo 0:f2d2dc1e207d 44 dig_T1=(cmd[1]<<8)|cmd[0];
takepiyo 0:f2d2dc1e207d 45 dig_T2=(cmd[3]<<8)|cmd[2];
takepiyo 0:f2d2dc1e207d 46 dig_T3=(cmd[5]<<8)|cmd[4];
takepiyo 0:f2d2dc1e207d 47
takepiyo 0:f2d2dc1e207d 48 //圧力の校正値取得
takepiyo 0:f2d2dc1e207d 49 cs=0;
takepiyo 0:f2d2dc1e207d 50 spi.write(0x8E);
takepiyo 0:f2d2dc1e207d 51 for(char i = 0;i<18;i++)
takepiyo 0:f2d2dc1e207d 52 {
takepiyo 0:f2d2dc1e207d 53 cmd[i]=spi.write(0);
takepiyo 0:f2d2dc1e207d 54 }
takepiyo 0:f2d2dc1e207d 55 cs=1;
takepiyo 0:f2d2dc1e207d 56 dig_P1=(cmd[1]<<8)|cmd[0];
takepiyo 0:f2d2dc1e207d 57 dig_P2=(cmd[3]<<8)|cmd[2];
takepiyo 0:f2d2dc1e207d 58 dig_P3=(cmd[5]<<8)|cmd[4];
takepiyo 0:f2d2dc1e207d 59 dig_P4=(cmd[7]<<8)|cmd[6];
takepiyo 0:f2d2dc1e207d 60 dig_P5=(cmd[9]<<8)|cmd[8];
takepiyo 0:f2d2dc1e207d 61 dig_P6=(cmd[11]<<8)|cmd[10];
takepiyo 0:f2d2dc1e207d 62 dig_P7=(cmd[13]<<8)|cmd[12];
takepiyo 0:f2d2dc1e207d 63 dig_P8=(cmd[15]<<8)|cmd[14];
takepiyo 0:f2d2dc1e207d 64 dig_P9=(cmd[17]<<8)|cmd[16];
takepiyo 0:f2d2dc1e207d 65
takepiyo 0:f2d2dc1e207d 66 //湿度の校正値取得
takepiyo 0:f2d2dc1e207d 67 cs=0;
takepiyo 0:f2d2dc1e207d 68 spi.write(0xA1);
takepiyo 0:f2d2dc1e207d 69 cmd[0]=spi.write(0);
takepiyo 0:f2d2dc1e207d 70 cs=1;
takepiyo 0:f2d2dc1e207d 71
takepiyo 0:f2d2dc1e207d 72 cs=0;
takepiyo 0:f2d2dc1e207d 73 spi.write(0xE1);
takepiyo 0:f2d2dc1e207d 74 for(char i = 0;i<7;i++)
takepiyo 0:f2d2dc1e207d 75 {
takepiyo 0:f2d2dc1e207d 76 cmd[1+i]=spi.write(0);
takepiyo 0:f2d2dc1e207d 77 }
takepiyo 0:f2d2dc1e207d 78 cs=1;
takepiyo 0:f2d2dc1e207d 79 dig_H1=cmd[0];
takepiyo 0:f2d2dc1e207d 80 dig_H2=(cmd[2]<<8)|cmd[1];
takepiyo 0:f2d2dc1e207d 81 dig_H3=cmd[3];
takepiyo 0:f2d2dc1e207d 82 dig_H4=(cmd[4]<<4)|(cmd[5]&0x0F);
takepiyo 0:f2d2dc1e207d 83 dig_H5=(cmd[6]<<4)|((cmd[5]>>4)&0x0F);
takepiyo 0:f2d2dc1e207d 84 dig_H6=cmd[7];
takepiyo 0:f2d2dc1e207d 85
takepiyo 0:f2d2dc1e207d 86 //校正データ終わり
takepiyo 0:f2d2dc1e207d 87 }
takepiyo 0:f2d2dc1e207d 88
takepiyo 0:f2d2dc1e207d 89 float BME280::getTemperature()
takepiyo 0:f2d2dc1e207d 90 {
takepiyo 0:f2d2dc1e207d 91 char cmd[18];
takepiyo 0:f2d2dc1e207d 92 DigitalOut cs(p8);
takepiyo 0:f2d2dc1e207d 93 SPI spi(p5,p6,p7);
takepiyo 0:f2d2dc1e207d 94 cs=1;
takepiyo 0:f2d2dc1e207d 95 spi.format(8, 0); // 8-bit, mode=0
takepiyo 0:f2d2dc1e207d 96 spi.frequency(1000000); // 1MHZ
takepiyo 0:f2d2dc1e207d 97
takepiyo 0:f2d2dc1e207d 98 cs=0;
takepiyo 0:f2d2dc1e207d 99 spi.write(0xFA);
takepiyo 0:f2d2dc1e207d 100 for(char i =0;i<3;i++)
takepiyo 0:f2d2dc1e207d 101 {
takepiyo 0:f2d2dc1e207d 102 cmd[i]=spi.write(0);
takepiyo 0:f2d2dc1e207d 103 }
takepiyo 0:f2d2dc1e207d 104 cs=1;
takepiyo 0:f2d2dc1e207d 105 uint32_t temp_raw;
takepiyo 0:f2d2dc1e207d 106 float tempf;
takepiyo 0:f2d2dc1e207d 107 temp_raw = (cmd[0] << 12) | (cmd[1] << 4) | (cmd[2] >> 4);
takepiyo 0:f2d2dc1e207d 108
takepiyo 0:f2d2dc1e207d 109 int32_t temp;
takepiyo 0:f2d2dc1e207d 110
takepiyo 0:f2d2dc1e207d 111 temp =
takepiyo 0:f2d2dc1e207d 112 (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
takepiyo 0:f2d2dc1e207d 113 ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
takepiyo 0:f2d2dc1e207d 114
takepiyo 0:f2d2dc1e207d 115 t_fine = temp;
takepiyo 0:f2d2dc1e207d 116 temp = (temp * 5 + 128) >> 8;
takepiyo 0:f2d2dc1e207d 117 tempf = (float)temp;
takepiyo 0:f2d2dc1e207d 118
takepiyo 0:f2d2dc1e207d 119 return (tempf/100.0f);
takepiyo 0:f2d2dc1e207d 120 }
takepiyo 0:f2d2dc1e207d 121
takepiyo 0:f2d2dc1e207d 122 float BME280::getPressure()
takepiyo 0:f2d2dc1e207d 123 {
takepiyo 0:f2d2dc1e207d 124 int32_t cmd[18];
takepiyo 0:f2d2dc1e207d 125 DigitalOut cs(p8);
takepiyo 0:f2d2dc1e207d 126 SPI spi(p5,p6,p7);
takepiyo 0:f2d2dc1e207d 127 cs=1;
takepiyo 0:f2d2dc1e207d 128 spi.format(8, 0); // 8-bit, mode=0
takepiyo 0:f2d2dc1e207d 129 spi.frequency(1000000); // 1MHZ
takepiyo 0:f2d2dc1e207d 130 cs=0;
takepiyo 0:f2d2dc1e207d 131 spi.write(0xF7);
takepiyo 0:f2d2dc1e207d 132 for(char i =0;i<3;i++)
takepiyo 0:f2d2dc1e207d 133 {
takepiyo 0:f2d2dc1e207d 134 cmd[i]=spi.write(0);
takepiyo 0:f2d2dc1e207d 135 }
takepiyo 0:f2d2dc1e207d 136 cs=1;
takepiyo 0:f2d2dc1e207d 137
takepiyo 0:f2d2dc1e207d 138 //ここから
takepiyo 0:f2d2dc1e207d 139 uint32_t press_raw;
takepiyo 0:f2d2dc1e207d 140 float pressf;
takepiyo 0:f2d2dc1e207d 141 press_raw = (cmd[0] << 12) | (cmd[1] << 4) | (cmd[2] >> 4);
takepiyo 0:f2d2dc1e207d 142
takepiyo 0:f2d2dc1e207d 143 int32_t var1, var2;
takepiyo 0:f2d2dc1e207d 144 uint32_t press;
takepiyo 0:f2d2dc1e207d 145
takepiyo 0:f2d2dc1e207d 146 var1 = (t_fine >> 1) - 64000;
takepiyo 0:f2d2dc1e207d 147 var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
takepiyo 0:f2d2dc1e207d 148 var2 = var2 + ((var1 * dig_P5) << 1);
takepiyo 0:f2d2dc1e207d 149 var2 = (var2 >> 2) + (dig_P4 << 16);
takepiyo 0:f2d2dc1e207d 150 var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
takepiyo 0:f2d2dc1e207d 151 var1 = ((32768 + var1) * dig_P1) >> 15;
takepiyo 0:f2d2dc1e207d 152 if (var1 == 0) {
takepiyo 0:f2d2dc1e207d 153 return 0;
takepiyo 0:f2d2dc1e207d 154 }
takepiyo 0:f2d2dc1e207d 155 press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
takepiyo 0:f2d2dc1e207d 156 if(press < 0x80000000) {
takepiyo 0:f2d2dc1e207d 157 press = (press << 1) / var1;
takepiyo 0:f2d2dc1e207d 158 } else {
takepiyo 0:f2d2dc1e207d 159 press = (press / var1) * 2;
takepiyo 0:f2d2dc1e207d 160 }
takepiyo 0:f2d2dc1e207d 161 var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
takepiyo 0:f2d2dc1e207d 162 var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
takepiyo 0:f2d2dc1e207d 163 press = (press + ((var1 + var2 + dig_P7) >> 4));
takepiyo 0:f2d2dc1e207d 164
takepiyo 0:f2d2dc1e207d 165 pressf = (float)press;
takepiyo 0:f2d2dc1e207d 166 return (pressf/100.0f);
takepiyo 0:f2d2dc1e207d 167 }
takepiyo 0:f2d2dc1e207d 168
takepiyo 0:f2d2dc1e207d 169 float BME280::getHumidity()
takepiyo 0:f2d2dc1e207d 170 {
takepiyo 0:f2d2dc1e207d 171 char cmd[18];
takepiyo 0:f2d2dc1e207d 172 DigitalOut cs(p8);
takepiyo 0:f2d2dc1e207d 173 SPI spi(p5,p6,p7);
takepiyo 0:f2d2dc1e207d 174 cs=1;
takepiyo 0:f2d2dc1e207d 175 spi.format(8, 0); // 8-bit, mode=0
takepiyo 0:f2d2dc1e207d 176 spi.frequency(1000000); // 1MHZ
takepiyo 0:f2d2dc1e207d 177 cs=0;
takepiyo 0:f2d2dc1e207d 178 spi.write(0xFD);
takepiyo 0:f2d2dc1e207d 179 for(char i = 0;i<2;i++)
takepiyo 0:f2d2dc1e207d 180 {
takepiyo 0:f2d2dc1e207d 181 cmd[i]=spi.write(0);
takepiyo 0:f2d2dc1e207d 182 }
takepiyo 0:f2d2dc1e207d 183 cs=1;
takepiyo 0:f2d2dc1e207d 184 uint32_t hum_raw;
takepiyo 0:f2d2dc1e207d 185 float humf;
takepiyo 0:f2d2dc1e207d 186 hum_raw = (cmd[0] << 8) | cmd[1];
takepiyo 0:f2d2dc1e207d 187
takepiyo 0:f2d2dc1e207d 188 int32_t v_x1;
takepiyo 0:f2d2dc1e207d 189
takepiyo 0:f2d2dc1e207d 190 v_x1 = t_fine - 76800;
takepiyo 0:f2d2dc1e207d 191 v_x1 = (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
takepiyo 0:f2d2dc1e207d 192 ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
takepiyo 0:f2d2dc1e207d 193 (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
takepiyo 0:f2d2dc1e207d 194 (int32_t)dig_H2 + 8192) >> 14));
takepiyo 0:f2d2dc1e207d 195 v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
takepiyo 0:f2d2dc1e207d 196 v_x1 = (v_x1 < 0 ? 0 : v_x1);
takepiyo 0:f2d2dc1e207d 197 v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
takepiyo 0:f2d2dc1e207d 198
takepiyo 0:f2d2dc1e207d 199 humf = (float)(v_x1 >> 12);
takepiyo 0:f2d2dc1e207d 200
takepiyo 0:f2d2dc1e207d 201 return (humf/1024.0f);
takepiyo 0:f2d2dc1e207d 202
takepiyo 0:f2d2dc1e207d 203 }