Multi environmental sensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bme280.h Source File

bme280.h

00001 #ifndef BME280_H
00002 #define BME280_H
00003 
00004 #include "mbed.h"
00005 
00006 #if(defined(TARGET_KL25Z) || defined(TARGET_K64F))
00007 
00008 //    #define BME280i2cLOWLEVEL   1           //if the use of low-level I2C routines is needed
00009 //    #warning "BME280 using low level I2C routines"
00010     
00011 #endif
00012 
00013 
00014 #define SEA_PRESS               1013.25             //default sea level pressure level in mb
00015 //#define KNOWNALT                327.0               //default known altitude in feet
00016 #define METERS_FEET             3.2808399
00017 #define FEET_METERS             0.3048006
00018 #define MB_INHG_DOUBLE          0.02952998751
00019 #define INHG_MB_DOUBLE          33.8638815767
00020 #define DEGC_DEGF_FLOAT         9.0 / 5.0 + 32.0
00021 #define DEGC_DEGF_INT           9 / 5 + 32
00022 
00023 #define BME280_WADDR                        0xEC  //i2c address write mode (CSBpin = 0);
00024 #define BME280_RADDR                        0xED  //i2c address read mode
00025 
00026 #define BME280_CHIP_ID_REG                  0xD0  //Chip ID Register 
00027 #define BME280_CAL_DATA_START_1             0x88  //Calibration data parameters
00028 #define BME280_CAL_DATA_START_2             0xE1  //More calibration data parameters
00029 #define BME280_RST_REG                      0xE0  //Softreset Register 
00030 #define BME280_STAT_REG                     0xF3  //Status Register 
00031 #define BME280_CTRL_MEAS_REG                0xF4  //Ctrl Measure Register 
00032 #define BME280_CTRL_HUMIDITY_REG            0xF2  //Ctrl Humidity Register
00033 #define BME280_CONFIG_REG                   0xF5  //Configuration Register 
00034 #define BME280_PRESSURE_MSB_REG             0xF7  //Pressure MSB Register 
00035 #define BME280_PRESSURE_LSB_REG             0xF8  //Pressure LSB Register 
00036 #define BME280_PRESSURE_XLSB_REG            0xF9  //Pressure XLSB Register 
00037 #define BME280_TEMPERATURE_MSB_REG          0xFA  //Temperature MSB Reg 
00038 #define BME280_TEMPERATURE_LSB_REG          0xFB  //Temperature LSB Reg 
00039 #define BME280_TEMPERATURE_XLSB_REG         0xFC  //Temperature XLSB Reg 
00040 #define BME280_HUMIDITY_MSB_REG             0xFD  //Humidity MSB Reg 
00041 #define BME280_HUMIDITY_LSB_REG             0xFE  //Humidity LSB Reg 
00042 
00043 // Status register bits
00044 #define BME280_STATUS_BUSY                  0x08
00045 #define BME280_STATUS_UNKNOWN               0x04
00046 #define BME280_STATUS_IMUPDATE              0x01
00047 
00048 #define BME280_RESET_VALUE                  0xB6
00049 
00050 // Config register equates
00051 #define BME280_OVERSAMPLE_SKIP              0
00052 #define BME280_OVERSAMPLE_x1                1
00053 #define BME280_OVERSAMPLE_x2                2
00054 #define BME280_OVERSAMPLE_x4                3
00055 #define BME280_OVERSAMPLE_x8                4
00056 #define BME280_OVERSAMPLE_x16               5
00057 
00058 #define BME280_TEMP_OVERSAMPLE              BME280_OVERSAMPLE_x1 //was x2 
00059 #define BME280_PRESS_OVERSAMPLE             BME280_OVERSAMPLE_x1 //was x4
00060 #define BME280_HUM_OVERSAMPLE               BME280_OVERSAMPLE_x1 //was x2
00061 
00062 #define BME280_MODE_SLEEP                   0
00063 #define BME280_MODE_FORCED                  1
00064 #define BME280_MODE_NORMAL                  3
00065 
00066 #define BME280_STANDBY_0m5                  0
00067 #define BME280_STANDBY_62m5                 1
00068 #define BME280_STANDBY_125m                 2
00069 #define BME280_STANDBY_250m                 3
00070 #define BME280_STANDBY_500m                 4
00071 #define BME280_STANDBY_1000m                5
00072 #define BME280_STANDBY_10m                  6
00073 #define BME280_STANDBY_20m                  7
00074 
00075 #define BME280_FILTER_OFF                   0
00076 #define BME280_FILTER_2                     1
00077 #define BME280_FILTER_4                     2
00078 #define BME280_FILTER_8                     3
00079 #define BME280_FILTER_16                    4
00080 
00081 //osrs_t(7..5), osrs_p(4..2), mode(1..0)
00082 #define BME280_CTRL_MEAS_REG_DATA           ((BME280_TEMP_OVERSAMPLE << 5) | (BME280_PRESS_OVERSAMPLE << 2) | BME280_MODE_NORMAL)
00083 //0(7..3), osrs_h(2..0)
00084 #define BME280_CTRL_HUMIDITY_REG_DATA       ((BME280_HUM_OVERSAMPLE))
00085 //sb(7..5), filter(4..2), 0(1), spl3w_en(0)
00086 #define BME280_CONFIG_REG_DATA              ((BME280_STANDBY_1000m << 5) | (BME280_FILTER_OFF << 2)) //was FILTER_4
00087 
00088 //Until it is understood why data cannot be raed out of BME280 right after BUSY goes false, the 
00089 //defines below introduce a variable delay in main(), based on sb bits used.
00090 // example: wait_ms(BME280_MAIN_WAIT);  must be put between pth.getBmeRawData(bmed_struct); accesses 
00091 #if   ((BME280_CONFIG_REG_DATA >> 5) == BME280_STANDBY_0m5)
00092 #define BME280_MAIN_WAIT                           100
00093 #elif ((BME280_CONFIG_REG_DATA >> 5) == BME280_STANDBY_62m5)
00094 #define BME280_MAIN_WAIT                           60
00095 #elif ((BME280_CONFIG_REG_DATA >> 5) == BME280_STANDBY_125m)
00096 #define BME280_MAIN_WAIT                           120
00097 #elif ((BME280_CONFIG_REG_DATA >> 5) == BME280_STANDBY_250m)
00098 #define BME280_MAIN_WAIT                           240
00099 #elif ((BME280_CONFIG_REG_DATA >> 5) == BME280_STANDBY_500m)
00100 #define BME280_MAIN_WAIT                           100
00101 #elif ((BME280_CONFIG_REG_DATA >> 5) == BME280_STANDBY_1000m)
00102 #define BME280_MAIN_WAIT                           500
00103 #elif ((BME280_CONFIG_REG_DATA >> 5) == BME280_STANDBY_10m)
00104 #define BME280_MAIN_WAIT                           50
00105 #elif ((BME280_CONFIG_REG_DATA >> 5) == BME280_STANDBY_20m)
00106 #define BME280_MAIN_WAIT                           59
00107 #endif
00108 
00109 
00110     /** 
00111      * Create bme280 controller class
00112      *
00113      * @param bme280 class
00114      *
00115      */
00116 class bme280 {
00117 
00118 public:
00119 
00120     /** 
00121      * Public data structure for CME280 correction values.
00122      * 
00123     **/
00124     typedef struct {
00125         uint16_t dig_T1 ;    /*!< calibration parameter -> Temp T1 */  
00126         int16_t  dig_T2 ;    /*!< calibration parameter -> Temp T2 */  
00127         int16_t  dig_T3 ;    /*!< calibration parameter -> Temp T3 */
00128           
00129         uint16_t dig_P1 ;    /*!< calibration parameter -> Press P1 */  
00130         int16_t  dig_P2 ;    /*!< calibration parameter -> Press P2 */  
00131         int16_t  dig_P3 ;    /*!< calibration parameter -> Press P3 */  
00132         int16_t  dig_P4 ;    /*!< calibration parameter -> Press P4 */  
00133         int16_t  dig_P5 ;    /*!< calibration parameter -> Press P5 */  
00134         int16_t  dig_P6 ;    /*!< calibration parameter -> Press P6 */  
00135         int16_t  dig_P7 ;    /*!< calibration parameter -> Press P7 */  
00136         int16_t  dig_P8 ;    /*!< calibration parameter -> Press P8 */  
00137         int16_t  dig_P9 ;    /*!< calibration parameter -> Press P9 */  
00138     
00139         uint8_t  dig_H1 ;    /*!< calibration parameter -> Hum H1 */  
00140         int16_t  dig_H2 ;    /*!< calibration parameter -> Hum H2 */  
00141         uint8_t  dig_H3 ;    /*!< calibration parameter -> Hum H3 */  
00142         int16_t  dig_H4 ;    /*!< calibration parameter -> Hum H4 */  
00143         int16_t  dig_H5 ;    /*!< calibration parameter -> Hum H5 */  
00144         int8_t   dig_H6 ;    /*!< calibration parameter -> Hum H6 */  
00145     
00146         uint32_t t_fine ;    /*!< scratch T_FINE */
00147          
00148         uint8_t  ctrl_hum_reg ;  /*!< config -> hum */
00149         uint8_t  ctrl_meas_reg ; /*!< config -> temp press mode */
00150         uint8_t  config_reg ;    /*!< config -> delay filter spi3w */ 
00151     } bme_cal;
00152          
00153     /** 
00154      * Public data structure to obtain CME280 data.
00155      * 
00156     **/
00157     typedef struct {
00158         int raw_temp ;       /*!< raw temperature reading, 20 bits */
00159         int raw_baro ;       /*!< raw pressure reading, 20 bits */
00160         int raw_hum ;        /*!< raw humidity reading, 16 bits */
00161         int corr_temp ;      /*!< corrected temperature as integer*/
00162         int64_t corr_baro ;  /*!< corrected pressure as integer */
00163         uint32_t corr_hum ;  /*!< corrected humidity as integer */
00164         double bme_temp ;    /*!< corrected temperature as a float */
00165         double bme_baro ;    /*!< corrected pressure as a float */
00166         double bme_hum ;     /*!< corrected humidity as a float */
00167     } bme_data;
00168 
00169     /** 
00170      * Public enum for selecting polarity of I2C address pin.
00171      *
00172      * - CSB pin = 0, user set I2C address to 0xEC
00173      * - CSB pin = 1, user set I2C address to 0xEE
00174      * 
00175     **/    
00176     enum CSBpolarity {
00177         CSBpin_0,  //CSB pin is grounded, I2C address is 0xEC and 0xED
00178         CSBpin_1,  //CSB pin is tied to Vdd, I2C address is 0xEE and 0xEF
00179     };
00180 
00181     /** 
00182      * Create a BME280 object using the specified I2C object
00183      *   - User defined use of the CSB pin
00184      *   - CSB pin = 0, user set I2C address to 0xEC
00185      *   - CSB pin = 1, user set I2C address to 0xEE 
00186      * @param sda - mbed I2C interface pin
00187      * @param scl - mbed I2C interface pin
00188      * @param bme280::CSBpin_0 - CSB pin tied to ground
00189      * @param bme280::CSBpin_1 - CSB pin tied to VDD
00190      */
00191     bme280(PinName sda, PinName scl, CSBpolarity CSBpin);
00192 
00193     /**
00194      * Destructor
00195      *
00196      * @param --none--
00197      */
00198     ~bme280();
00199 
00200     /** 
00201      * Get BME280 ID Register
00202      * 
00203      * @param --none--
00204      *
00205      * @return ID Register value
00206     */ 
00207     uint8_t getBmeID();
00208 
00209     /** 
00210      * Soft Reset the BME280
00211      * 
00212      * @param --none--
00213      *
00214      * @return ACK/NACK status
00215     */
00216     uint8_t resetBme();
00217     
00218     /** 
00219      * Get BME280 Status Register
00220      * 
00221      * @param --none--
00222      *
00223      * @return Status Register value
00224     */ 
00225     uint8_t getBmeStatus();
00226 
00227     /** 
00228      * Get BME280 Raw Data
00229      * 
00230      * @param pointer to struct bme_data
00231      *
00232      * @return Status Register value. Pass-thru from getBmeStatus()
00233      * @return raw data put into struct bme_data
00234     */ 
00235     uint8_t getBmeRawData(bme_data& bmed);
00236 
00237     /** 
00238      * Convert BME280 Raw Data using integer based routines
00239      * 
00240      * @param pointer to struct bme_data
00241      * @param pointer to struct bme_cal
00242      *
00243      * @return converted data put into struct bme_data
00244     */ 
00245     void convertBmeRawData(bme_data& bmed, bme_cal& bmec);
00246 
00247     /** 
00248      * Convert BME280 Raw Data using all floating routines
00249      * 
00250      * @param pointer to struct bme_data
00251      * @param pointer to struct bme_cal
00252      *
00253      * @return converted data put into struct bme_data
00254     */ 
00255     void convertBmeRawDataFloat(bme_data& bmed, bme_cal& bmec);
00256     
00257     /** 
00258      * Initialize the BME280. 
00259      * - Sets up the command registers
00260      * - Loads up the calibration data
00261      * 
00262      * @param pointer to struct bme_data
00263      *
00264      * @return 0
00265     */  
00266     uint8_t initBme(bme_cal& bmec);
00267     
00268     /** 
00269      * Corrected altitude (feet) from barometer at seal level (mB)
00270      * 
00271      * @param pointer to struct bme_data
00272      * @param pressure at sea level (in mB)
00273      *
00274      * @return corrected altimeter (in feet)
00275     */ 
00276     float getAltitudeFT(bme_data& bmed, float sea_pressure);
00277     
00278     /** 
00279      * Altitude corrected barometer (feet)
00280      * 
00281      * @param pointer to struct bme_data
00282      * @param known_alt in feet
00283      *
00284      * @return corrected barometer
00285     */
00286     float getSeaLevelBaroFT(bme_data& bmed, float known_alt);
00287     
00288     /** 
00289      * Altitude corrected barometer (meters)
00290      * 
00291      * @param pointer to struct bme_data
00292      * @param known_alt in meters
00293      *
00294      * @return corrected barometer
00295     */    
00296     float getSeaLevelBaroM(bme_data& bmed, float known_alt);
00297 
00298     /** 
00299      * Return the dew point based on T & H. Slower but more accurate.
00300      * 
00301      * @param pointer to struct bme_data
00302      *
00303      * @return dew point in degrees C
00304     */  
00305     float getDewPt(bme_data& bmed);
00306 
00307     /** 
00308      * Return the dew point based on T & H. Faster but less accurate.
00309      * 
00310      * @param pointer to struct bme_data
00311      *
00312      * @return dew point in degrees C
00313     */ 
00314     float getDewPtFast(bme_data& bmed);
00315     
00316 private:
00317 #if not defined  BMEi2cLOWLEVEL
00318     char bme280Buffer[32];
00319 #endif
00320     char i2cWAddr;
00321     char i2cRAddr;
00322     int _i2c_start(uint8_t i2c_addr);
00323     void _i2c_stop();
00324     uint8_t _i2c_write(uint8_t data);
00325     uint8_t _i2c_readACK();
00326     uint8_t _i2c_readNACK();
00327   
00328 protected:
00329     I2C     _i2c;
00330 }; 
00331 #endif