Jon Trinder / BMP280

Fork of BMP280 by Edwin Cho

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  *  29 November 2016 
00009  *  Toyomasa Watarai code Modified  by jon trinder jont@ninelocks.com 
00010  *  readtemperature routine replaced
00011  *
00012  *
00013  *  Library for "BMP280 temperature, humidity and pressure sensor module" from Switch Science
00014  *    https://www.switch-science.com/catalog/2236/
00015  *
00016  *  For more information about the BMP280:
00017  *    http://ae-bst.resource.bosch.com/media/products/dokumente/BMP280/BST-BMP280_DS001-10.pdf
00018  */
00019 
00020 #include "mbed.h"
00021 #include "BMP280.h"
00022 
00023 BMP280::BMP280(PinName sda, PinName scl, char slave_adr)
00024     :
00025     i2c_p(new I2C(sda, scl)), 
00026     i2c(*i2c_p),
00027     address(slave_adr),
00028     t_fine(0)
00029 {
00030     initialize();
00031 }
00032 
00033 BMP280::BMP280(I2C &i2c_obj, char slave_adr)
00034     :
00035     i2c_p(NULL), 
00036     i2c(i2c_obj),
00037     address(slave_adr),
00038     t_fine(0)
00039 {
00040     initialize();
00041 }
00042 
00043 BMP280::~BMP280()
00044 {
00045     if (NULL != i2c_p)
00046         delete  i2c_p;
00047 }
00048     
00049 void BMP280::initialize()
00050 {
00051     char cmd[18];
00052  
00053     cmd[0] = 0xf2; // ctrl_hum
00054     cmd[1] = 0x01; // Humidity oversampling x1
00055     i2c.write(address, cmd, 2);
00056  
00057     cmd[0] = 0xf4; // ctrl_meas
00058     cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
00059     i2c.write(address, cmd, 2);
00060  
00061     cmd[0] = 0xf5; // config
00062     cmd[1] = 0xa0; // Standby 1000ms, Filter off
00063     i2c.write(address, cmd, 2);
00064  
00065     cmd[0] = 0x88; // read dig_T regs
00066     i2c.write(address, cmd, 1);
00067     i2c.read(address, cmd, 6);
00068  
00069     dig_T1 = (cmd[1] << 8) | cmd[0];
00070     dig_T2 = (cmd[3] << 8) | cmd[2];
00071     dig_T3 = (cmd[5] << 8) | cmd[4];
00072  
00073     DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
00074  
00075     cmd[0] = 0x8E; // read dig_P regs
00076     i2c.write(address, cmd, 1);
00077     i2c.read(address, cmd, 18);
00078  
00079     dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
00080     dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
00081     dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
00082     dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
00083     dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
00084     dig_P6 = (cmd[11] << 8) | cmd[10];
00085     dig_P7 = (cmd[13] << 8) | cmd[12];
00086     dig_P8 = (cmd[15] << 8) | cmd[14];
00087     dig_P9 = (cmd[17] << 8) | cmd[16];
00088  
00089     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);
00090  
00091   /*  cmd[0] = 0xA1; // read dig_H regs
00092     i2c.write(address, cmd, 1);
00093     i2c.read(address, cmd, 1);
00094      cmd[1] = 0xE1; // read dig_H regs
00095     i2c.write(address, &cmd[1], 1);
00096     i2c.read(address, &cmd[1], 7);
00097 
00098     dig_H1 = cmd[0];
00099     dig_H2 = (cmd[2] << 8) | cmd[1];
00100     dig_H3 = cmd[3];
00101     dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
00102     dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
00103     dig_H6 = cmd[7];
00104  
00105     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);
00106 */
00107 }
00108  /*
00109  
00110  this gives a weird value, one day maybe I will debug it jt
00111  float BMP280::getTemperature()
00112 {
00113     uint32_t temp_raw;
00114     float tempf;
00115     char cmd[4];
00116  
00117     cmd[0] = 0xfa; // temp_msb
00118     i2c.write(address, cmd, 1);
00119     i2c.read(address, &cmd[1], 3);
00120  
00121     temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00122  
00123     int32_t temp;
00124  
00125     temp =
00126         (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
00127         ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
00128  
00129     t_fine = temp;
00130     temp = (temp * 5 + 128) >> 8;
00131     tempf = (float)temp;
00132  
00133     return (tempf/100.0f);
00134 }
00135  */
00136  
00137 /* 
00138 As Thomas routine was giving me weird values I rewrote it to look mostly like the adafruit 
00139 arduino code, which in turn is a lot like the bosch datasheet 
00140 jon Trinder
00141 */
00142 
00143 
00144 float BMP280::getTemperature()
00145 {
00146     int32_t var1, var2, T;
00147     int32_t adc_T;
00148     float temp_as_float;
00149     char cmd[4];
00150     cmd[0] = 0xfa; // temp_msb
00151     i2c.write(address, cmd, 1);
00152     i2c.read(address, &cmd[1], 3);
00153 
00154     //jons slow painful shifting of bits to make it readable
00155     adc_T = cmd[1];
00156     adc_T <<= 8;
00157     adc_T |= cmd[2];
00158     adc_T <<= 8;
00159     adc_T |= cmd[3];
00160      
00161     //from here more or less same as ada and bosch
00162  
00163     adc_T >>= 4;
00164     
00165     var1  = ((((adc_T>>3) - ((int32_t)dig_T1 <<1))) *
00166        ((int32_t)dig_T2)) >> 11;
00167     
00168     var2  = (((((adc_T>>4) - ((int32_t)dig_T1)) *
00169          ((adc_T>>4) - ((int32_t)dig_T1))) >> 12) *
00170        ((int32_t)dig_T3)) >> 14;
00171     
00172     t_fine = var1 + var2;
00173     
00174     T  = (t_fine * 5 + 128) >> 8;
00175     temp_as_float = T/100.0;
00176     return temp_as_float;   
00177 }
00178 
00179 
00180 
00181  
00182 float BMP280::getPressure()
00183 {
00184     uint32_t press_raw;
00185     float pressf;
00186     char cmd[4];
00187  
00188     cmd[0] = 0xf7; // press_msb
00189     i2c.write(address, cmd, 1);
00190     i2c.read(address, &cmd[1], 3);
00191  
00192     press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
00193  
00194     int32_t var1, var2;
00195     uint32_t press;
00196  
00197     var1 = (t_fine >> 1) - 64000;
00198     var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
00199     var2 = var2 + ((var1 * dig_P5) << 1);
00200     var2 = (var2 >> 2) + (dig_P4 << 16);
00201     var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
00202     var1 = ((32768 + var1) * dig_P1) >> 15;
00203     if (var1 == 0) {
00204         return 0;
00205     }
00206     press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
00207     if(press < 0x80000000) {
00208         press = (press << 1) / var1;
00209     } else {
00210         press = (press / var1) * 2;
00211     }
00212     var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
00213     var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
00214     press = (press + ((var1 + var2 + dig_P7) >> 4));
00215  
00216     pressf = (float)press;
00217     return (pressf/100.0f);
00218 }