Martin Slade / GroupUproject

Fork of 352 by Elec351 MMB

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BMP280.cpp Source File

BMP280.cpp

00001 /**
00002  *  BMP280 Combined humidity and pressure sensor library
00003  *
00004  *  @author  Toyomasa Watarai
00005  *  @version 1.0
00006  *  @date    06-April-2015
00007  *
00008  * bugfixing by charly
00009  *
00010  *  Library for "BMP280 temperature, humidity and pressure sensor module" from Switch Science
00011  *    https://www.switch-science.com/catalog/2236/
00012  *
00013  *  For more information about the BMP280:
00014  *    http://ae-bst.resource.bosch.com/media/products/dokumente/BMP280/BST-BMP280_DS001-10.pdf
00015  */
00016 
00017 #include "mbed.h"
00018 #include "BMP280.h"
00019 
00020 BMP280::BMP280(PinName sda, PinName scl, char slave_adr)
00021     :
00022     i2c_p(new I2C(sda, scl)), 
00023     i2c(*i2c_p),
00024     address(slave_adr<<1),
00025     t_fine(0)
00026 {
00027     initialize();
00028 }
00029 
00030 BMP280::BMP280(I2C &i2c_obj, char slave_adr)
00031     :
00032     i2c_p(NULL), 
00033     i2c(i2c_obj),
00034     address(slave_adr<<1),
00035     t_fine(0)
00036 {
00037     initialize();
00038 }
00039 
00040 BMP280::~BMP280()
00041 {
00042     if (NULL != i2c_p)
00043         delete  i2c_p;
00044 }
00045     
00046 void BMP280::initialize()
00047 {
00048     char cmd[18];
00049  
00050     //cmd[0] = 0xf2; // ctrl_hum
00051     //cmd[1] = 0x01; // Humidity oversampling x1
00052     //i2c.write(address, cmd, 2);
00053  
00054     cmd[0] = 0xf4; // ctrl_meas
00055     //cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
00056     cmd[1] = 0b01010111; // Temparature oversampling x2 010, Pressure oversampling x16 101, Normal mode 11
00057     i2c.write(address, cmd, 2);
00058  
00059     cmd[0] = 0xf5; // config
00060     cmd[1] = 0b10111100; // Standby 1000ms, Filter x16
00061     i2c.write(address, cmd, 2);
00062  
00063     cmd[0] = 0x88; // read dig_T regs
00064     i2c.write(address, cmd, 1);
00065     i2c.read(address, cmd, 6);
00066  
00067     dig_T1 = (cmd[1] << 8) | cmd[0];
00068     dig_T2 = (cmd[3] << 8) | cmd[2];
00069     dig_T3 = (cmd[5] << 8) | cmd[4];
00070  
00071     DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n\r", dig_T1, dig_T2, dig_T3);
00072     DEBUG_PRINT("dig_T = %d, %d, %d\n\r", dig_T1, dig_T2, dig_T3);
00073  
00074     cmd[0] = 0x8E; // read dig_P regs
00075     i2c.write(address, cmd, 1);
00076     i2c.read(address, cmd, 18);
00077  
00078     dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
00079     dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
00080     dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
00081     dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
00082     dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
00083     dig_P6 = (cmd[11] << 8) | cmd[10];
00084     dig_P7 = (cmd[13] << 8) | cmd[12];
00085     dig_P8 = (cmd[15] << 8) | cmd[14];
00086     dig_P9 = (cmd[17] << 8) | cmd[16];
00087  
00088     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);
00089  
00090   /*  cmd[0] = 0xA1; // read dig_H regs
00091     i2c.write(address, cmd, 1);
00092     i2c.read(address, cmd, 1);
00093      cmd[1] = 0xE1; // read dig_H regs
00094     i2c.write(address, &cmd[1], 1);
00095     i2c.read(address, &cmd[1], 7);
00096 
00097     dig_H1 = cmd[0];
00098     dig_H2 = (cmd[2] << 8) | cmd[1];
00099     dig_H3 = cmd[3];
00100     dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
00101     dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
00102     dig_H6 = cmd[7];
00103  
00104     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);
00105 */
00106 }
00107  
00108 float BMP280::getTemperature()
00109 {
00110     int32_t temp_raw;
00111     float tempf;
00112     char cmd[4];
00113  
00114     cmd[0] = 0xfa; // temp_msb
00115     i2c.write(address, cmd, 1);
00116     i2c.read(address, &cmd[1], 3);
00117  
00118     temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00119     DEBUG_PRINT("\r\ntemp_raw:%d",temp_raw);
00120  
00121     int32_t temp1, temp2,temp;
00122  
00123     temp1 =((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11;
00124     temp2 =(((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14;
00125     DEBUG_PRINT("   temp1:%d   temp2:%d",temp1, temp2);
00126     t_fine = temp1+temp2;
00127     DEBUG_PRINT("   t_fine:%d",t_fine);
00128     temp = (t_fine * 5 + 128) >> 8;
00129     tempf = (float)temp;
00130     DEBUG_PRINT("   tempf:%f",tempf);
00131   
00132     return (tempf/100.0f);
00133 }
00134  
00135 float BMP280::getPressure()
00136 {
00137     uint32_t press_raw;
00138     float pressf;
00139     char cmd[4];
00140  
00141     cmd[0] = 0xf7; // press_msb
00142     i2c.write(address, cmd, 1);
00143     i2c.read(address, &cmd[1], 3);
00144  
00145     press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00146  
00147     int32_t var1, var2;
00148     uint32_t press;
00149  
00150     var1 = (t_fine >> 1) - 64000;
00151     var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
00152     var2 = var2 + ((var1 * dig_P5) << 1);
00153     var2 = (var2 >> 2) + (dig_P4 << 16);
00154     var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
00155     var1 = ((32768 + var1) * dig_P1) >> 15;
00156     if (var1 == 0) {
00157         return 0;
00158     }
00159     press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
00160     if(press < 0x80000000) {
00161         press = (press << 1) / var1;
00162     } else {
00163         press = (press / var1) * 2;
00164     }
00165     var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
00166     var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
00167     press = (press + ((var1 + var2 + dig_P7) >> 4));
00168  
00169     pressf = (float)press;
00170     return (pressf/100.0f);
00171 }