Titech Cansat / Mbed 2 deprecated pressuesensor

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bme280.cpp Source File

bme280.cpp

00001 #include "mbed.h"
00002 #include "bme280.h"
00003 
00004 BME280::BME280()
00005 {
00006     t_fine=0;
00007     char cmd[18];
00008     DigitalOut cs(p8);
00009     SPI spi(p5,p6,p7);
00010     cs=1;
00011     spi.format(8, 0); // 8-bit, mode=0
00012     spi.frequency(1000000); // 1MHZ    
00013     
00014     //ctrl_hum
00015     cs=0;
00016     spi.write(0xF2 & BME280_MASK);
00017     spi.write(0x03);//Humidity oversampling ×4
00018     cs=1;
00019     
00020     //ctrl_meas
00021     cs=0;
00022     spi.write(0xF4 & BME280_MASK);
00023     spi.write((3<<5|3<<2)|3);//Temparature oversampling x4, Pressure oversampling x4, Normal mode
00024     cs=1;
00025     
00026     //config
00027     cs=0;
00028     spi.write(0xF5 & BME280_MASK);
00029     spi.write(0b10100000);
00030     cs=1;
00031     
00032     //設定おわり
00033     wait(1);
00034     
00035     //校正用データの処理
00036     //温度の校正値取得
00037     cs=0;
00038     spi.write(0x88);
00039     for(char i = 0;i<6;i++)
00040     {
00041         cmd[i]=spi.write(0);
00042     }
00043     cs=1;
00044     dig_T1=(cmd[1]<<8)|cmd[0];
00045     dig_T2=(cmd[3]<<8)|cmd[2];
00046     dig_T3=(cmd[5]<<8)|cmd[4];
00047     
00048     //圧力の校正値取得
00049     cs=0;
00050     spi.write(0x8E);
00051     for(char i = 0;i<18;i++)
00052     {
00053         cmd[i]=spi.write(0);   
00054     }
00055     cs=1;
00056     dig_P1=(cmd[1]<<8)|cmd[0];
00057     dig_P2=(cmd[3]<<8)|cmd[2];
00058     dig_P3=(cmd[5]<<8)|cmd[4];
00059     dig_P4=(cmd[7]<<8)|cmd[6];
00060     dig_P5=(cmd[9]<<8)|cmd[8];
00061     dig_P6=(cmd[11]<<8)|cmd[10];
00062     dig_P7=(cmd[13]<<8)|cmd[12];
00063     dig_P8=(cmd[15]<<8)|cmd[14];
00064     dig_P9=(cmd[17]<<8)|cmd[16];
00065     
00066     //湿度の校正値取得
00067     cs=0;
00068     spi.write(0xA1);
00069     cmd[0]=spi.write(0);
00070     cs=1;
00071     
00072     cs=0;
00073     spi.write(0xE1);
00074     for(char i = 0;i<7;i++)
00075     {
00076         cmd[1+i]=spi.write(0);
00077     }
00078     cs=1;
00079     dig_H1=cmd[0];
00080     dig_H2=(cmd[2]<<8)|cmd[1];
00081     dig_H3=cmd[3];
00082     dig_H4=(cmd[4]<<4)|(cmd[5]&0x0F);
00083     dig_H5=(cmd[6]<<4)|((cmd[5]>>4)&0x0F);
00084     dig_H6=cmd[7];
00085     
00086     //校正データ終わり
00087 }    
00088 
00089     float BME280::getTemperature()
00090     {
00091         char cmd[18];
00092         DigitalOut cs(p8);
00093         SPI spi(p5,p6,p7);
00094         cs=1;
00095         spi.format(8, 0); // 8-bit, mode=0
00096         spi.frequency(1000000); // 1MHZ    
00097     
00098         cs=0;
00099         spi.write(0xFA);
00100         for(char i =0;i<3;i++)
00101         {
00102             cmd[i]=spi.write(0);
00103         }
00104         cs=1;
00105         uint32_t temp_raw;
00106         float tempf;
00107         temp_raw = (cmd[0] << 12) | (cmd[1] << 4) | (cmd[2] >> 4);
00108 
00109         int32_t temp;
00110 
00111     temp =
00112         (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
00113         ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
00114 
00115     t_fine = temp;
00116     temp = (temp * 5 + 128) >> 8;
00117     tempf = (float)temp;
00118 
00119     return (tempf/100.0f);
00120     }
00121     
00122     float BME280::getPressure()
00123     {
00124         int32_t cmd[18];
00125         DigitalOut cs(p8);
00126         SPI spi(p5,p6,p7);
00127         cs=1;
00128         spi.format(8, 0); // 8-bit, mode=0
00129         spi.frequency(1000000); // 1MHZ   
00130         cs=0;
00131         spi.write(0xF7);
00132         for(char i =0;i<3;i++)
00133         {
00134             cmd[i]=spi.write(0);
00135         }
00136         cs=1;
00137         
00138         //ここから
00139     uint32_t press_raw;
00140     float pressf;
00141     press_raw = (cmd[0] << 12) | (cmd[1] << 4) | (cmd[2] >> 4);
00142 
00143     int32_t var1, var2;
00144     uint32_t press;
00145 
00146     var1 = (t_fine >> 1) - 64000;
00147     var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
00148     var2 = var2 + ((var1 * dig_P5) << 1);
00149     var2 = (var2 >> 2) + (dig_P4 << 16);
00150     var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
00151     var1 = ((32768 + var1) * dig_P1) >> 15;
00152     if (var1 == 0) {
00153         return 0;
00154     }
00155     press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
00156     if(press < 0x80000000) {
00157         press = (press << 1) / var1;
00158     } else {
00159         press = (press / var1) * 2;
00160     }
00161     var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
00162     var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
00163     press = (press + ((var1 + var2 + dig_P7) >> 4));
00164 
00165     pressf = (float)press;
00166     return (pressf/100.0f);
00167     }
00168     
00169     float BME280::getHumidity()
00170     {
00171         char cmd[18];
00172         DigitalOut cs(p8);
00173         SPI spi(p5,p6,p7);
00174         cs=1;
00175         spi.format(8, 0); // 8-bit, mode=0
00176         spi.frequency(1000000); // 1MHZ   
00177         cs=0;
00178         spi.write(0xFD);
00179         for(char i = 0;i<2;i++)
00180         {
00181             cmd[i]=spi.write(0);   
00182         }
00183         cs=1;
00184     uint32_t hum_raw;
00185     float humf;
00186     hum_raw = (cmd[0] << 8) | cmd[1];
00187 
00188     int32_t v_x1;
00189 
00190     v_x1 = t_fine - 76800;
00191     v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
00192                ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
00193                                             (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
00194                                             (int32_t)dig_H2 + 8192) >> 14));
00195     v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
00196     v_x1 = (v_x1 < 0 ? 0 : v_x1);
00197     v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
00198 
00199     humf = (float)(v_x1 >> 12);
00200 
00201     return (humf/1024.0f);
00202 
00203     }