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.
Dependencies: BLE_API mbed-src nRF51822
BMP280.h
00001 #ifndef BMP280_H 00002 #define BMP280_H 00003 00004 //#include "mbed.h" 00005 00006 // BMP280 registers 00007 #define BMP280_TEMP_XLSB 0xFC 00008 #define BMP280_TEMP_LSB 0xFB 00009 #define BMP280_TEMP_MSB 0xFA 00010 #define BMP280_PRESS_XLSB 0xF9 00011 #define BMP280_PRESS_LSB 0xF8 00012 #define BMP280_PRESS_MSB 0xF7 00013 #define BMP280_CONFIG 0xF5 00014 #define BMP280_CTRL_MEAS 0xF4 00015 #define BMP280_STATUS 0xF3 00016 #define BMP280_RESET 0xE0 00017 #define BMP280_ID 0xD0 // should be 0x58 00018 #define BMP280_CALIB00 0x88 00019 #define BMP280_ADDRESS 0x77<<1 00020 00021 // Set initial input parameters 00022 00023 enum Posr { 00024 P_OSR_00 = 0, // no op 00025 P_OSR_01, 00026 P_OSR_02, 00027 P_OSR_04, 00028 P_OSR_08, 00029 P_OSR_16 00030 }; 00031 00032 enum Tosr { 00033 T_OSR_00 = 0, // no op 00034 T_OSR_01, 00035 T_OSR_02, 00036 T_OSR_04, 00037 T_OSR_08, 00038 T_OSR_16 00039 }; 00040 00041 enum IIRFilter { 00042 full = 0, // bandwidth at full sample rate 00043 BW0_223ODR, 00044 BW0_092ODR, 00045 BW0_042ODR, 00046 BW0_021ODR // bandwidth at 0.021 x sample rate 00047 }; 00048 00049 enum Mode { 00050 BMP280Sleep = 0, 00051 forced, 00052 forced2, 00053 normal 00054 }; 00055 00056 enum SBy { 00057 t_00_5ms = 0, 00058 t_62_5ms, 00059 t_125ms, 00060 t_250ms, 00061 t_500ms, 00062 t_1000ms, 00063 t_2000ms, 00064 t_4000ms, 00065 }; 00066 00067 // Specify BMP280 configuration 00068 uint8_t Posr = P_OSR_16, Tosr = T_OSR_02, Mode = normal, IIRFilter = BW0_042ODR, SBy = t_62_5ms; // set pressure amd temperature output data rate 00069 // t_fine carries fine temperature as global value for BMP280 00070 int32_t t_fine; 00071 00072 //Set up I2C, (SDA,SCL) 00073 //I2C i2c(P0_0, P0_1); 00074 00075 // BMP280 compensation parameters 00076 uint16_t dig_T1, dig_P1; 00077 int16_t dig_T2, dig_T3, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9; 00078 00079 class BMP280 { 00080 00081 protected: 00082 00083 public: 00084 //=================================================================================================================== 00085 //====== Set of useful function to access pressure and temperature data 00086 //=================================================================================================================== 00087 00088 00089 void writeByte(uint8_t address, uint8_t subAddress, uint8_t data) 00090 { 00091 char data_write[2]; 00092 data_write[0] = subAddress; 00093 data_write[1] = data; 00094 i2c.write(address, data_write, 2, 0); 00095 } 00096 00097 char readByte(uint8_t address, uint8_t subAddress) 00098 { 00099 char data[1]; // `data` will store the register data 00100 char data_write[1]; 00101 data_write[0] = subAddress; 00102 i2c.write(address, data_write, 1, 1); // no stop 00103 i2c.read(address, data, 1, 0); 00104 return data[0]; 00105 } 00106 00107 void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest) 00108 { 00109 char data[24]; 00110 char data_write[1]; 00111 data_write[0] = subAddress; 00112 i2c.write(address, data_write, 1, 1); // no stop 00113 i2c.read(address, data, count, 0); 00114 for(int ii = 0; ii < count; ii++) { 00115 dest[ii] = data[ii]; 00116 } 00117 } 00118 00119 00120 int32_t readBMP280Temperature() 00121 { 00122 uint8_t rawData[3]; // 20-bit pressure register data stored here 00123 readBytes(BMP280_ADDRESS, BMP280_TEMP_MSB, 3, &rawData[0]); 00124 return (int32_t) (((int32_t) rawData[0] << 16 | (int32_t) rawData[1] << 8 | rawData[2]) >> 4); 00125 } 00126 00127 int32_t readBMP280Pressure() 00128 { 00129 uint8_t rawData[3]; // 20-bit pressure register data stored here 00130 readBytes(BMP280_ADDRESS, BMP280_PRESS_MSB, 3, &rawData[0]); 00131 return (int32_t) (((int32_t) rawData[0] << 16 | (int32_t) rawData[1] << 8 | rawData[2]) >> 4); 00132 } 00133 00134 void BMP280Init() 00135 { 00136 // Configure the BMP280 00137 // Set T and P oversampling rates and sensor mode 00138 writeByte(BMP280_ADDRESS, BMP280_CTRL_MEAS, Tosr << 5 | Posr << 2 | Mode); 00139 // Set standby time interval in normal mode and bandwidth 00140 writeByte(BMP280_ADDRESS, BMP280_CONFIG, SBy << 5 | IIRFilter << 2); 00141 // Read and store calibration data 00142 uint8_t calib[24]; 00143 readBytes(BMP280_ADDRESS, BMP280_CALIB00, 24, &calib[0]); 00144 dig_T1 = (uint16_t)(((uint16_t) calib[1] << 8) | calib[0]); 00145 dig_T2 = ( int16_t)((( int16_t) calib[3] << 8) | calib[2]); 00146 dig_T3 = ( int16_t)((( int16_t) calib[5] << 8) | calib[4]); 00147 dig_P1 = (uint16_t)(((uint16_t) calib[7] << 8) | calib[6]); 00148 dig_P2 = ( int16_t)((( int16_t) calib[9] << 8) | calib[8]); 00149 dig_P3 = ( int16_t)((( int16_t) calib[11] << 8) | calib[10]); 00150 dig_P4 = ( int16_t)((( int16_t) calib[13] << 8) | calib[12]); 00151 dig_P5 = ( int16_t)((( int16_t) calib[15] << 8) | calib[14]); 00152 dig_P6 = ( int16_t)((( int16_t) calib[17] << 8) | calib[16]); 00153 dig_P7 = ( int16_t)((( int16_t) calib[19] << 8) | calib[18]); 00154 dig_P8 = ( int16_t)((( int16_t) calib[21] << 8) | calib[20]); 00155 dig_P9 = ( int16_t)((( int16_t) calib[23] << 8) | calib[22]); 00156 } 00157 00158 // Returns temperature in DegC, resolution is 0.01 DegC. Output value of 00159 // “5123” equals 51.23 DegC. 00160 int32_t bmp280_compensate_T(int32_t adc_T) 00161 { 00162 int32_t var1, var2, T; 00163 var1 = ((((adc_T >> 3) - ((int32_t)dig_T1 << 1))) * ((int32_t)dig_T2)) >> 11; 00164 var2 = (((((adc_T >> 4) - ((int32_t)dig_T1)) * ((adc_T >> 4) - ((int32_t)dig_T1))) >> 12) * ((int32_t)dig_T3)) >> 14; 00165 t_fine = var1 + var2; 00166 T = (t_fine * 5 + 128) >> 8; 00167 return T; 00168 } 00169 00170 // Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 00171 //fractional bits). 00172 //Output value of “24674867” represents 24674867/256 = 96386.2 Pa = 963.862 hPa 00173 uint32_t bmp280_compensate_P(int32_t adc_P) 00174 { 00175 long long var1, var2, p; 00176 var1 = ((long long)t_fine) - 128000; 00177 var2 = var1 * var1 * (long long)dig_P6; 00178 var2 = var2 + ((var1*(long long)dig_P5)<<17); 00179 var2 = var2 + (((long long)dig_P4)<<35); 00180 var1 = ((var1 * var1 * (long long)dig_P3)>>8) + ((var1 * (long long)dig_P2)<<12); 00181 var1 = (((((long long)1)<<47)+var1))*((long long)dig_P1)>>33; 00182 if(var1 == 0) 00183 { 00184 return 0; 00185 // avoid exception caused by division by zero 00186 } 00187 p = 1048576 - adc_P; 00188 p = (((p<<31) - var2)*3125)/var1; 00189 var1 = (((long long)dig_P9) * (p>>13) * (p>>13)) >> 25; 00190 var2 = (((long long)dig_P8) * p)>> 19; 00191 p = ((p + var1 + var2) >> 8) + (((long long)dig_P7)<<4); 00192 return (uint32_t)p; 00193 } 00194 00195 00196 00197 00198 }; 00199 #endif
Generated on Sat Jul 16 2022 19:46:00 by
