Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os-test by
BMP280.h
00001 /******************************************************************************* 00002 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00020 * OTHER DEALINGS IN THE SOFTWARE. 00021 * 00022 * Except as contained in this notice, the name of Maxim Integrated 00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00024 * Products, Inc. Branding Policy. 00025 * 00026 * The mere transfer of this software does not imply any licenses 00027 * of trade secrets, proprietary technology, copyrights, patents, 00028 * trademarks, maskwork rights, or any other form of intellectual 00029 * property whatsoever. Maxim Integrated Products, Inc. retains all 00030 * ownership rights. 00031 *******************************************************************************/ 00032 /** 00033 * Bosch BMP280 Digital Pressure Sensor 00034 * 00035 * @code 00036 * #include <stdio.h> 00037 * #include "mbed.h" 00038 * #include "xxx.h" 00039 * 00040 * I2C i2c(I2C_SDA, I2C_SCL); 00041 * xxx xxx(&i2c); 00042 * 00043 * int main(void) { 00044 * printf("Initialized xxx\n"); 00045 * while(1) { 00046 * if (xxx.init() != 0) { 00047 * printf("Error communicating with xxx\n"); 00048 * } else { 00049 * printf("Initialized xxx\n"); 00050 * break; 00051 * } 00052 * wait(1); 00053 * } 00054 * 00055 * while(1) { 00056 * printf(""); 00057 * wait(1); 00058 * } 00059 * } 00060 * @endcode 00061 */ 00062 00063 #ifndef BMP280_H_ 00064 #define BMP280_H_ 00065 00066 #include "mbed.h" 00067 00068 #define BMP280_READID (0x58) 00069 00070 #define BMP280_TEMP_XLSB (0xFC) 00071 #define BMP280_TEMP_LSB (0xFB) 00072 #define BMP280_TEMP_MSB (0xFA) 00073 00074 #define BMP280_PRESS_XLSB (0xF9) 00075 #define BMP280_PRESS_LSB (0xF8) 00076 #define BMP280_PRESS_MSB (0xF7) 00077 00078 #define BMP280_CONFIG (0xF5) 00079 #define BMP280_CTRL_MEAS (0xF4) 00080 #define BMP280_STATUS (0xF3) 00081 #define BMP280_RESET (0xE0) 00082 #define BMP280_ID (0xD0) 00083 00084 // calib25-calib00: 0xA1-0x88 00085 #define BMP280_CALIB25 (0xA1) // Beginning address 00086 #define BMP280_CALIB00 (0x88) // Ending address 00087 00088 #define BMP280_REGISTER_CHIPID (0xD0) 00089 #define BMP280_REGISTER_VERSION (0xD1) 00090 #define BMP280_REGISTER_SOFTRESET (0xE0) 00091 00092 #define BMP280_REGISTER_CAL26 (0xE1) // R calibration stored in 0xE1-0xF0 00093 00094 #define BMP280_REGISTER_CONTROL (0xF4) 00095 #define BMP280_REGISTER_CONFIG (0xF5) 00096 #define BMP280_REGISTER_PRESSUREDATA (0xF7) 00097 #define BMP280_REGISTER_TEMPDATA (0xFA) 00098 00099 /** 00100 * @brief Bosch BMP280 Digital Pressure Sensor 00101 */ 00102 class BMP280 { 00103 00104 public: 00105 /** 00106 * BMP280 constructor. 00107 * 00108 * @param sda mbed pin to use for SDA line of I2C interface. 00109 * @param scl mbed pin to use for SCL line of I2C interface. 00110 * @param slaveAddress Slave Address of the device. 00111 */ 00112 BMP280(PinName sda, PinName scl, int slaveAddress); 00113 00114 /** 00115 * BMP280 constructor. 00116 * 00117 * @param i2c I2C object to use. 00118 * @param slaveAddress Slave Address of the device. 00119 */ 00120 BMP280(I2C *i2c, int slaveAddress); 00121 00122 char loggingEnabled; 00123 char loggingSampleRate; 00124 00125 /** 00126 * Write a device register 00127 */ 00128 int writeReg(char reg, char value); 00129 /** 00130 * Read a device register 00131 */ 00132 int readReg(char reg, char *value); 00133 00134 /// @brief TEMP_XLSB (0xFC) 00135 char bmp280_temp_xlsb; // Lower 4-bits are all 0's 00136 00137 /// @brief TEMP_LSB (0xFB) 00138 char bmp280_temp_lsb; 00139 00140 /// @brief TEMP_MSB (0xFA) 00141 char bmp280_temp_msb; 00142 00143 /// @brief PRESS_XLSB (0xF9) 00144 char bmp280_press_xlsb; // Lower 4-bits are all 0's 00145 00146 /// @brief PRESS_LSB (0xF8) 00147 char bmp280_press_lsb; 00148 00149 /// @brief PRESS_MSB (0xF7) 00150 char bmp280_press_msb; 00151 00152 /// @brief CONFIG_REG (0xF5) 00153 union bmp280_config_reg { 00154 char all; 00155 struct { 00156 char spi3w_en : 1; 00157 char reserved : 1; 00158 char filter : 3; 00159 char t_sb : 3; 00160 } bit; 00161 } bmp280_config; 00162 00163 /// @brief CTRL_MEAS (0xF4) 00164 union bmp280_ctrl_meas { 00165 char all; 00166 struct { 00167 char mode : 2; 00168 char osrs_p : 3; 00169 char osrs_t : 3; 00170 } bit; 00171 } bmp280_ctrl_meas; 00172 00173 /// @brief STATUS (0xF3) 00174 union bmp280_status { 00175 char all; 00176 struct { 00177 char im_update : 1; 00178 char reserved1 : 2; 00179 char measuring : 1; 00180 char reserved2 : 4; 00181 } bit; 00182 } bmp280_status; 00183 00184 /// @brief RESET (0xE0) 00185 char bmp280_reset; 00186 00187 /// @brief ID (0xD0) 00188 char bmp280_id; 00189 00190 typedef enum { 00191 SKIPPED_P = 0, 00192 OVERSAMPLING_X1_P = 1, 00193 OVERSAMPLING_X2_P = 2, 00194 OVERSAMPLING_X4_P = 3, 00195 OVERSAMPLING_X8_P = 4, 00196 OVERSAMPLING_X16_P = 5 00197 } bmp280_osrs_P_t; 00198 00199 typedef enum { 00200 SKIPPED_T = 0, 00201 OVERSAMPLING_X1_T = 1, 00202 OVERSAMPLING_X2_T = 2, 00203 OVERSAMPLING_X4_T = 3, 00204 OVERSAMPLING_X8_T = 4, 00205 OVERSAMPLING_X16_T = 5 00206 } bmp280_osrs_T_t; 00207 00208 typedef enum { 00209 FILT_OFF = 1, 00210 FILT_2 = 2, 00211 FILT_3 = 4, 00212 FILT_4 = 8, 00213 FILT_5 = 16 00214 } bmp280_FILT_t; 00215 00216 typedef enum { 00217 SLEEP_MODE = 0, 00218 FORCED_MODE = 1, 00219 NORMAL_MODE = 3 00220 } bmp280_MODE_t; 00221 00222 typedef enum { 00223 T_0_5 = 0, 00224 T_62_5 = 1, 00225 T_125 = 2, 00226 T_250 = 3, 00227 T_500 = 4, 00228 T_1000 = 5, 00229 T_2000 = 6, 00230 T_4000 = 7 00231 } bmp280_TSB_t; 00232 00233 /// @brief calib25... calib00 (0xA1...0x88) 00234 char bmp280_Calib[26]; 00235 00236 uint16_t dig_T1; 00237 int16_t dig_T2; 00238 int16_t dig_T3; 00239 00240 uint16_t dig_P1; 00241 int16_t dig_P2; 00242 int16_t dig_P3; 00243 int16_t dig_P4; 00244 int16_t dig_P5; 00245 int16_t dig_P6; 00246 int16_t dig_P7; 00247 int16_t dig_P8; 00248 int16_t dig_P9; 00249 00250 int32_t t_fine; // This is calculated int the temperature to be used by the 00251 // pressure 00252 00253 int32_t bmp280_rawPress; 00254 int32_t bmp280_rawTemp; 00255 00256 float Temp_degC; // Deg C. 00257 float Press_Pa; // Pascal. 00258 00259 /** 00260 * BMP280 constructor. 00261 * 00262 * @param sda mbed pin to use for SDA line of I2C interface. 00263 * @param scl mbed pin to use for SCL line of I2C interface. 00264 */ 00265 BMP280(PinName sda, PinName scl); 00266 00267 /** 00268 * BMP280 constructor. 00269 * 00270 * @param i2c I2C object to use. 00271 */ 00272 BMP280(I2C *i2c); 00273 00274 /** 00275 * BMP280 destructor. 00276 */ 00277 ~BMP280(void); 00278 00279 // Function Prototypes 00280 00281 /** 00282 * @brief This initializes the BMP280 00283 * @brief The BMP280 has 2 modes. FORCED mode and NORMAL mode. FORCED Mode gives more 00284 * @brief control to the processor as the processor sends out the Mode to initiate a conversion 00285 * @brief and a data is sent out then. NORMAL mode is initialized once and it just runs and sends 00286 * @brief out data at a programmed timed interval. (In this example the main() will set this to Normal 00287 * @brief function) 00288 * @param Osrs_p- Pressure oversampling 00289 * @param Osrs_t- Temperature oversampling 00290 * @param Filter- Filter Settings 00291 * @param Mode- Power Modes 00292 * @param T_sb- Standby time (used with Normal mode) 00293 * @param dig_T1, dig_T2, dig_T3- Coeffs used for temp conversion - GLOBAL variables (output) 00294 * @param dig_P1, .... , dig_P9- Coeffs used for press conversion - GLOBAL variables (output) 00295 * @returns 0-if no error. A non-zero value indicates an error. 00296 */ 00297 int init(bmp280_osrs_P_t Osrs_p, bmp280_osrs_T_t Osrs_t, bmp280_FILT_t Filter, 00298 bmp280_MODE_t Mode, bmp280_TSB_t T_sb); 00299 00300 /** 00301 * @brief The BMP280 has 2 modes. FORCED mode and NORMAL mode. FORCED Mode 00302 * gives more 00303 * @brief control to the processor as the processor sends out the Mode to 00304 * initiate a conversion 00305 * @brief and a data is sent out then. NORMAL mode is initialized once and it 00306 * just runs and sends 00307 * @brief out data at a programmed timed interval. (In this example the 00308 * main() will set this to Normal 00309 * @brief function) 00310 * @param *Temp_degC - Pointer to temperature (result in deg C) 00311 * @param *Press_Pa - Pointer to pressure (resul in Pascal) 00312 * @returns 0-if no error. A non-zero value indicates an error. 00313 */ 00314 int ReadCompData(float *Temp_degC, float *Press_Pa); 00315 00316 /** 00317 * @brief This function allows writing to a register. 00318 * @param reg- Address of the register to write to 00319 * @param value- Data written to the register 00320 * @returns 0-if no error. A non-zero value indicates an error. 00321 */ 00322 int reg_write(char reg, char value); 00323 00324 /** 00325 * @brief This function allows writing to a register. 00326 * @params reg- Address of the register to read from (input) 00327 * @params *value- Pointer to the value read from the register (output) 00328 * @returns 0-if no error. A non-zero value indicates an error. 00329 */ 00330 int reg_read(char reg, char *value, char number); 00331 00332 /** 00333 * @brief Performs a soft reset on the BMP280 00334 * @param none 00335 * @returns none 00336 */ 00337 void Reset(void); 00338 00339 /** 00340 * @brief Detects if the BMP280 is present 00341 * @param none 00342 * @returns 1 for found, 0 for not found, -1 for comm error 00343 */ 00344 int Detect(void); 00345 00346 /** 00347 * @brief Performs calculations on the raw temperature data to convert to 00348 * @brief temperature in deg C, based on Bosch's algorithm 00349 * @param Raw Temp ADC value, Global dig_T1, dig_T2, dig_T3 00350 * @returns The Temperature in deg C 00351 */ 00352 float compensate_T_float(int32_t adc_T); // returned value Deg C. 00353 00354 /** 00355 * @brief Performs calculations on the raw pressure data to convert to 00356 * @brief pressure in Pascal, based on Bosch's algorithm 00357 * @param adc_P Raw Press ADC value, Global dig_P1, dig_P2,..., dig_P9 00358 * @returns The Pressure in Pascals 00359 */ 00360 float compensate_P_float(int32_t adc_P); // returned value Pascal. 00361 00362 /** 00363 * @brief Puts the BMP280 in low power Sleep mode 00364 * @param none 00365 * @returns 0 if no errors, -1 if error. 00366 */ 00367 int Sleep(void); 00368 00369 /** 00370 * @brief This reads the raw BMP280 data 00371 * @param *bmp280_rawData- array of raw output data 00372 * @returns 0-if no error. A non-zero value indicates an error. 00373 * 00374 */ 00375 int ReadCompDataRaw(char *bmp280_rawData); 00376 /** 00377 * @brief This reads the raw BMP280 data uses the Bosch algorithm to get the 00378 * data 00379 * @brief in float, then the float gets converted to an String 00380 * @param *bmp280_rawData- array of raw output data 00381 * @returns 0-if no error. A non-zero value indicates an error. 00382 * 00383 */ 00384 int ReadCompDataRaw2(char *bmp280_rawData); 00385 /** 00386 * @brief This converts the raw BMP280 data to couble based on Bosch's 00387 * algorithm 00388 * @param *bmp280_rawData- array of raw input data 00389 * @param *Temp_degC- pointer to output, Temp value in deg C 00390 * @param *Press_Pa- pointer to output, Press value in Pascals 00391 * @returns 0-if no error. A non-zero value indicates an error. 00392 * 00393 */ 00394 void ToFloat(char *bmp280_rawData, float *Temp_degC, float *Press_Pa); 00395 /** 00396 * @brief converts to Farenhite from Centigrade 00397 * @param temperature in Centigrade 00398 * @returns temperature value in Farenhite 00399 */ 00400 float ToFahrenheit(float temperature); 00401 00402 /** 00403 * @brief Reads a unique ID from the register 00404 * @param none 00405 * @returns The correct id value which is 0x58 00406 */ 00407 int ReadId(void); 00408 00409 private: 00410 /// I2C pointer 00411 I2C *i2c; 00412 /// Is this object the owner of the I2C object 00413 bool isOwner; 00414 /// Device slave address 00415 int slaveAddress; 00416 }; 00417 00418 #endif // BMP280_H_
Generated on Wed Jul 13 2022 17:00:34 by
1.7.2
