Library for BMP085 digital pressure sensor.
BMP085.cpp@2:34a32898cd23, 2017-08-25 (annotated)
- Committer:
- mcm
- Date:
- Fri Aug 25 14:16:15 2017 +0000
- Revision:
- 2:34a32898cd23
- Parent:
- 1:01aeefb5f4cf
The library is ready, it was tested successfully and it works as expected.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mcm | 1:01aeefb5f4cf | 1 | /** |
mcm | 1:01aeefb5f4cf | 2 | * @brief BMP085.c |
mcm | 1:01aeefb5f4cf | 3 | * @details Digital pressure sensor. |
mcm | 1:01aeefb5f4cf | 4 | * Functions file. |
mcm | 1:01aeefb5f4cf | 5 | * |
mcm | 1:01aeefb5f4cf | 6 | * |
mcm | 1:01aeefb5f4cf | 7 | * @return NA |
mcm | 1:01aeefb5f4cf | 8 | * |
mcm | 1:01aeefb5f4cf | 9 | * @author Manuel Caballero |
mcm | 1:01aeefb5f4cf | 10 | * @date 25/August/2017 |
mcm | 1:01aeefb5f4cf | 11 | * @version 25/August/2017 The ORIGIN |
mcm | 1:01aeefb5f4cf | 12 | * @pre NaN. |
mcm | 1:01aeefb5f4cf | 13 | * @warning NaN |
mcm | 1:01aeefb5f4cf | 14 | * @pre This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ). |
mcm | 1:01aeefb5f4cf | 15 | */ |
mcm | 1:01aeefb5f4cf | 16 | |
mcm | 1:01aeefb5f4cf | 17 | #include "BMP085.h" |
mcm | 1:01aeefb5f4cf | 18 | |
mcm | 1:01aeefb5f4cf | 19 | |
mcm | 1:01aeefb5f4cf | 20 | BMP085::BMP085 ( PinName sda, PinName scl, uint32_t addr, uint32_t freq ) |
mcm | 1:01aeefb5f4cf | 21 | : i2c ( sda, scl ) |
mcm | 1:01aeefb5f4cf | 22 | , BMP085_Addr ( addr ) |
mcm | 1:01aeefb5f4cf | 23 | { |
mcm | 1:01aeefb5f4cf | 24 | i2c.frequency( freq ); |
mcm | 1:01aeefb5f4cf | 25 | } |
mcm | 1:01aeefb5f4cf | 26 | |
mcm | 1:01aeefb5f4cf | 27 | |
mcm | 1:01aeefb5f4cf | 28 | BMP085::~BMP085(){ |
mcm | 1:01aeefb5f4cf | 29 | } |
mcm | 1:01aeefb5f4cf | 30 | |
mcm | 1:01aeefb5f4cf | 31 | |
mcm | 1:01aeefb5f4cf | 32 | |
mcm | 1:01aeefb5f4cf | 33 | /** |
mcm | 1:01aeefb5f4cf | 34 | * @brief BMP085_GetCalibrationCoefficients ( Vector_cal_coeff_t* ) |
mcm | 1:01aeefb5f4cf | 35 | * |
mcm | 1:01aeefb5f4cf | 36 | * @details It gets the calibration coefficients. |
mcm | 1:01aeefb5f4cf | 37 | * |
mcm | 1:01aeefb5f4cf | 38 | * @param[in] NaN |
mcm | 1:01aeefb5f4cf | 39 | * |
mcm | 1:01aeefb5f4cf | 40 | * @param[out] Vector_cal_coeff_t: Calibration coefficients. |
mcm | 1:01aeefb5f4cf | 41 | * |
mcm | 1:01aeefb5f4cf | 42 | * |
mcm | 1:01aeefb5f4cf | 43 | * @return Status of BMP085_GetCalibrationCoefficients. |
mcm | 1:01aeefb5f4cf | 44 | * |
mcm | 1:01aeefb5f4cf | 45 | * |
mcm | 1:01aeefb5f4cf | 46 | * @author Manuel Caballero |
mcm | 1:01aeefb5f4cf | 47 | * @date 25/August/2017 |
mcm | 1:01aeefb5f4cf | 48 | * @version 25/August/2017 The ORIGIN |
mcm | 1:01aeefb5f4cf | 49 | * @pre NaN |
mcm | 1:01aeefb5f4cf | 50 | * @warning NaN. |
mcm | 1:01aeefb5f4cf | 51 | */ |
mcm | 1:01aeefb5f4cf | 52 | BMP085::BMP085_status_t BMP085::BMP085_GetCalibrationCoefficients ( Vector_cal_coeff_t* myCalCoeff ) |
mcm | 1:01aeefb5f4cf | 53 | { |
mcm | 1:01aeefb5f4cf | 54 | char cmd[22] = { 0 }; |
mcm | 1:01aeefb5f4cf | 55 | uint32_t aux = 0; |
mcm | 1:01aeefb5f4cf | 56 | |
mcm | 1:01aeefb5f4cf | 57 | cmd[0] = BMP085_AC1_MSB; |
mcm | 1:01aeefb5f4cf | 58 | |
mcm | 1:01aeefb5f4cf | 59 | |
mcm | 1:01aeefb5f4cf | 60 | aux = i2c.write ( BMP085_Addr, &cmd[0], 1 ); |
mcm | 1:01aeefb5f4cf | 61 | aux = i2c.read ( BMP085_Addr, &cmd[0], 22 ); |
mcm | 1:01aeefb5f4cf | 62 | |
mcm | 1:01aeefb5f4cf | 63 | // Parse the data |
mcm | 1:01aeefb5f4cf | 64 | myCalCoeff->AC1 = ( cmd[0] << 8 ) | cmd[1]; |
mcm | 1:01aeefb5f4cf | 65 | myCalCoeff->AC2 = ( cmd[2] << 8 ) | cmd[3]; |
mcm | 1:01aeefb5f4cf | 66 | myCalCoeff->AC3 = ( cmd[4] << 8 ) | cmd[5]; |
mcm | 1:01aeefb5f4cf | 67 | myCalCoeff->AC4 = ( cmd[6] << 8 ) | cmd[7]; |
mcm | 1:01aeefb5f4cf | 68 | myCalCoeff->AC5 = ( cmd[8] << 8 ) | cmd[9]; |
mcm | 1:01aeefb5f4cf | 69 | myCalCoeff->AC6 = ( cmd[10] << 8 ) | cmd[11]; |
mcm | 1:01aeefb5f4cf | 70 | myCalCoeff->B1 = ( cmd[12] << 8 ) | cmd[13]; |
mcm | 1:01aeefb5f4cf | 71 | myCalCoeff->B2 = ( cmd[14] << 8 ) | cmd[15]; |
mcm | 1:01aeefb5f4cf | 72 | myCalCoeff->MB = ( cmd[16] << 8 ) | cmd[17]; |
mcm | 1:01aeefb5f4cf | 73 | myCalCoeff->MC = ( cmd[18] << 8 ) | cmd[19]; |
mcm | 1:01aeefb5f4cf | 74 | myCalCoeff->MD = ( cmd[20] << 8 ) | cmd[21]; |
mcm | 1:01aeefb5f4cf | 75 | |
mcm | 1:01aeefb5f4cf | 76 | |
mcm | 1:01aeefb5f4cf | 77 | |
mcm | 1:01aeefb5f4cf | 78 | if ( aux == I2C_SUCCESS ) |
mcm | 1:01aeefb5f4cf | 79 | return BMP085_SUCCESS; |
mcm | 1:01aeefb5f4cf | 80 | else |
mcm | 1:01aeefb5f4cf | 81 | return BMP085_FAILURE; |
mcm | 1:01aeefb5f4cf | 82 | } |
mcm | 1:01aeefb5f4cf | 83 | |
mcm | 1:01aeefb5f4cf | 84 | |
mcm | 1:01aeefb5f4cf | 85 | /** |
mcm | 1:01aeefb5f4cf | 86 | * @brief BMP085_TriggerTemperature ( void ) |
mcm | 1:01aeefb5f4cf | 87 | * |
mcm | 1:01aeefb5f4cf | 88 | * @details It triggers a new temperature measurement. |
mcm | 1:01aeefb5f4cf | 89 | * |
mcm | 1:01aeefb5f4cf | 90 | * @param[in] NaN. |
mcm | 1:01aeefb5f4cf | 91 | * |
mcm | 1:01aeefb5f4cf | 92 | * @param[out] NaN. |
mcm | 1:01aeefb5f4cf | 93 | * |
mcm | 1:01aeefb5f4cf | 94 | * |
mcm | 1:01aeefb5f4cf | 95 | * @return Status of BMP085_TriggerTemperature. |
mcm | 1:01aeefb5f4cf | 96 | * |
mcm | 1:01aeefb5f4cf | 97 | * |
mcm | 1:01aeefb5f4cf | 98 | * @author Manuel Caballero |
mcm | 1:01aeefb5f4cf | 99 | * @date 25/August/2017 |
mcm | 1:01aeefb5f4cf | 100 | * @version 25/August/2017 The ORIGIN |
mcm | 1:01aeefb5f4cf | 101 | * @pre NaN |
mcm | 1:01aeefb5f4cf | 102 | * @warning The user MUST respect the maximum temperature conversion time, in this |
mcm | 1:01aeefb5f4cf | 103 | * case, that value is 4.5ms. |
mcm | 1:01aeefb5f4cf | 104 | */ |
mcm | 1:01aeefb5f4cf | 105 | BMP085::BMP085_status_t BMP085::BMP085_TriggerTemperature ( void ) |
mcm | 1:01aeefb5f4cf | 106 | { |
mcm | 1:01aeefb5f4cf | 107 | char cmd[] = { BMP085_CONTROL, BMP085_TRIGGER_TEMPERATURE }; |
mcm | 1:01aeefb5f4cf | 108 | uint32_t aux = 0; |
mcm | 1:01aeefb5f4cf | 109 | |
mcm | 1:01aeefb5f4cf | 110 | |
mcm | 1:01aeefb5f4cf | 111 | aux = i2c.write ( BMP085_Addr, &cmd[0], 2 ); |
mcm | 1:01aeefb5f4cf | 112 | |
mcm | 1:01aeefb5f4cf | 113 | |
mcm | 1:01aeefb5f4cf | 114 | |
mcm | 1:01aeefb5f4cf | 115 | if ( aux == I2C_SUCCESS ) |
mcm | 1:01aeefb5f4cf | 116 | return BMP085_SUCCESS; |
mcm | 1:01aeefb5f4cf | 117 | else |
mcm | 1:01aeefb5f4cf | 118 | return BMP085_FAILURE; |
mcm | 1:01aeefb5f4cf | 119 | } |
mcm | 1:01aeefb5f4cf | 120 | |
mcm | 1:01aeefb5f4cf | 121 | |
mcm | 1:01aeefb5f4cf | 122 | /** |
mcm | 1:01aeefb5f4cf | 123 | * @brief BMP085_ReadRawTemperature ( Vector_temp_f* ) |
mcm | 1:01aeefb5f4cf | 124 | * |
mcm | 1:01aeefb5f4cf | 125 | * @details It reads an uncompensated temperature. |
mcm | 1:01aeefb5f4cf | 126 | * |
mcm | 1:01aeefb5f4cf | 127 | * @param[in] NaN. |
mcm | 1:01aeefb5f4cf | 128 | * |
mcm | 1:01aeefb5f4cf | 129 | * @param[out] myRawTemperature: Uncompensated temperature. |
mcm | 1:01aeefb5f4cf | 130 | * |
mcm | 1:01aeefb5f4cf | 131 | * |
mcm | 1:01aeefb5f4cf | 132 | * @return Status of BMP085_ReadRawTemperature. |
mcm | 1:01aeefb5f4cf | 133 | * |
mcm | 1:01aeefb5f4cf | 134 | * |
mcm | 1:01aeefb5f4cf | 135 | * @author Manuel Caballero |
mcm | 1:01aeefb5f4cf | 136 | * @date 25/August/2017 |
mcm | 1:01aeefb5f4cf | 137 | * @version 25/August/2017 The ORIGIN |
mcm | 1:01aeefb5f4cf | 138 | * @pre BMP085_TriggerTemperature function MUST be called before calling this one. |
mcm | 1:01aeefb5f4cf | 139 | * @warning The user MUST respect the maximum temperature conversion time, in this |
mcm | 1:01aeefb5f4cf | 140 | * case, that value is 4.5ms. |
mcm | 1:01aeefb5f4cf | 141 | */ |
mcm | 1:01aeefb5f4cf | 142 | BMP085::BMP085_status_t BMP085::BMP085_ReadRawTemperature ( Vector_temp_f* myRawTemperature ) |
mcm | 1:01aeefb5f4cf | 143 | { |
mcm | 1:01aeefb5f4cf | 144 | char cmd[] = { BMP085_READ_TEMPERATURE, 0 }; |
mcm | 1:01aeefb5f4cf | 145 | uint32_t aux = 0; |
mcm | 1:01aeefb5f4cf | 146 | |
mcm | 1:01aeefb5f4cf | 147 | |
mcm | 1:01aeefb5f4cf | 148 | aux = i2c.write ( BMP085_Addr, &cmd[0], 1 ); |
mcm | 1:01aeefb5f4cf | 149 | aux = i2c.read ( BMP085_Addr, &cmd[0], 2 ); |
mcm | 1:01aeefb5f4cf | 150 | |
mcm | 1:01aeefb5f4cf | 151 | |
mcm | 1:01aeefb5f4cf | 152 | // Parse the data |
mcm | 1:01aeefb5f4cf | 153 | myRawTemperature->UT_Temperature = ( cmd[0] << 8 ) | cmd[1]; |
mcm | 1:01aeefb5f4cf | 154 | |
mcm | 1:01aeefb5f4cf | 155 | |
mcm | 1:01aeefb5f4cf | 156 | |
mcm | 1:01aeefb5f4cf | 157 | if ( aux == I2C_SUCCESS ) |
mcm | 1:01aeefb5f4cf | 158 | return BMP085_SUCCESS; |
mcm | 1:01aeefb5f4cf | 159 | else |
mcm | 1:01aeefb5f4cf | 160 | return BMP085_FAILURE; |
mcm | 1:01aeefb5f4cf | 161 | } |
mcm | 1:01aeefb5f4cf | 162 | |
mcm | 1:01aeefb5f4cf | 163 | |
mcm | 1:01aeefb5f4cf | 164 | /** |
mcm | 1:01aeefb5f4cf | 165 | * @brief BMP085_ReadCompensatedTemperature ( Vector_temp_f*, Vector_cal_coeff_t ) |
mcm | 1:01aeefb5f4cf | 166 | * |
mcm | 1:01aeefb5f4cf | 167 | * @details It reads an compensated/true temperature. |
mcm | 1:01aeefb5f4cf | 168 | * |
mcm | 1:01aeefb5f4cf | 169 | * @param[in] myCalCoeff: Calibration coefficients. |
mcm | 1:01aeefb5f4cf | 170 | * |
mcm | 1:01aeefb5f4cf | 171 | * @param[out] myTrueTemperature: Compensated/True temperature. |
mcm | 1:01aeefb5f4cf | 172 | * |
mcm | 1:01aeefb5f4cf | 173 | * |
mcm | 1:01aeefb5f4cf | 174 | * @return Status of BMP085_ReadCompensatedTemperature. |
mcm | 1:01aeefb5f4cf | 175 | * |
mcm | 1:01aeefb5f4cf | 176 | * |
mcm | 1:01aeefb5f4cf | 177 | * @author Manuel Caballero |
mcm | 1:01aeefb5f4cf | 178 | * @date 25/August/2017 |
mcm | 1:01aeefb5f4cf | 179 | * @version 25/August/2017 The ORIGIN |
mcm | 1:01aeefb5f4cf | 180 | * @pre Both BMP085_TriggerTemperature and BMP085_GetCalibrationCoefficients functions MUST be called before calling this one. |
mcm | 1:01aeefb5f4cf | 181 | * @warning The user MUST respect the maximum temperature conversion time, in this |
mcm | 1:01aeefb5f4cf | 182 | * case, that value is 4.5ms. |
mcm | 1:01aeefb5f4cf | 183 | */ |
mcm | 1:01aeefb5f4cf | 184 | BMP085::BMP085_status_t BMP085::BMP085_ReadCompensatedTemperature ( Vector_temp_f* myTrueTemperature, Vector_cal_coeff_t myCalCoeff ) |
mcm | 1:01aeefb5f4cf | 185 | { |
mcm | 1:01aeefb5f4cf | 186 | uint32_t aux = 0; |
mcm | 1:01aeefb5f4cf | 187 | int32_t X1, X2, B5; |
mcm | 1:01aeefb5f4cf | 188 | |
mcm | 1:01aeefb5f4cf | 189 | Vector_temp_f myRawTemperature; |
mcm | 1:01aeefb5f4cf | 190 | |
mcm | 1:01aeefb5f4cf | 191 | |
mcm | 1:01aeefb5f4cf | 192 | aux = BMP085_ReadRawTemperature ( &myRawTemperature ); |
mcm | 1:01aeefb5f4cf | 193 | |
mcm | 1:01aeefb5f4cf | 194 | |
mcm | 1:01aeefb5f4cf | 195 | // Parse the data |
mcm | 1:01aeefb5f4cf | 196 | X1 = ( ( myRawTemperature.UT_Temperature - myCalCoeff.AC6 ) * myCalCoeff.AC5 ) / 32768; |
mcm | 1:01aeefb5f4cf | 197 | X2 = ( myCalCoeff.MC * 2048 ) / ( X1 + myCalCoeff.MD ); |
mcm | 1:01aeefb5f4cf | 198 | B5 = X1 + X2; |
mcm | 1:01aeefb5f4cf | 199 | |
mcm | 1:01aeefb5f4cf | 200 | myTrueTemperature->UT_Temperature = ( B5 + 8 ) / 16; |
mcm | 1:01aeefb5f4cf | 201 | |
mcm | 1:01aeefb5f4cf | 202 | |
mcm | 1:01aeefb5f4cf | 203 | if ( aux == I2C_SUCCESS ) |
mcm | 1:01aeefb5f4cf | 204 | return BMP085_SUCCESS; |
mcm | 1:01aeefb5f4cf | 205 | else |
mcm | 1:01aeefb5f4cf | 206 | return BMP085_FAILURE; |
mcm | 1:01aeefb5f4cf | 207 | } |
mcm | 1:01aeefb5f4cf | 208 | |
mcm | 1:01aeefb5f4cf | 209 | |
mcm | 1:01aeefb5f4cf | 210 | |
mcm | 1:01aeefb5f4cf | 211 | /** |
mcm | 1:01aeefb5f4cf | 212 | * @brief BMP085_TriggerPressure ( BMP085_pressure_osrs_t ) |
mcm | 1:01aeefb5f4cf | 213 | * |
mcm | 1:01aeefb5f4cf | 214 | * @details It triggers a new pressure measurement. |
mcm | 1:01aeefb5f4cf | 215 | * |
mcm | 1:01aeefb5f4cf | 216 | * @param[in] myResolution: Pressure resolution. |
mcm | 1:01aeefb5f4cf | 217 | * |
mcm | 1:01aeefb5f4cf | 218 | * @param[out] NaN. |
mcm | 1:01aeefb5f4cf | 219 | * |
mcm | 1:01aeefb5f4cf | 220 | * |
mcm | 1:01aeefb5f4cf | 221 | * @return Status of BMP085_TriggerPressure. |
mcm | 1:01aeefb5f4cf | 222 | * |
mcm | 1:01aeefb5f4cf | 223 | * |
mcm | 1:01aeefb5f4cf | 224 | * @author Manuel Caballero |
mcm | 1:01aeefb5f4cf | 225 | * @date 25/August/2017 |
mcm | 1:01aeefb5f4cf | 226 | * @version 25/August/2017 The ORIGIN |
mcm | 1:01aeefb5f4cf | 227 | * @pre NaN |
mcm | 1:01aeefb5f4cf | 228 | * @warning The user MUST respect the maximum pressure conversion time: |
mcm | 1:01aeefb5f4cf | 229 | * Ultra Low Power Mode: 4.5ms. |
mcm | 1:01aeefb5f4cf | 230 | * Standard Mode: 7.5ms. |
mcm | 1:01aeefb5f4cf | 231 | * High Resolution Mode: 13.5ms. |
mcm | 1:01aeefb5f4cf | 232 | * Ultra High Res. Mode: 25.5ms. |
mcm | 1:01aeefb5f4cf | 233 | */ |
mcm | 1:01aeefb5f4cf | 234 | BMP085::BMP085_status_t BMP085::BMP085_TriggerPressure ( BMP085_pressure_osrs_t myResolution ) |
mcm | 1:01aeefb5f4cf | 235 | { |
mcm | 1:01aeefb5f4cf | 236 | char cmd[] = { BMP085_CONTROL, BMP085_TRIGGER_PRESSURE }; |
mcm | 1:01aeefb5f4cf | 237 | uint32_t aux = 0; |
mcm | 1:01aeefb5f4cf | 238 | |
mcm | 1:01aeefb5f4cf | 239 | // adjust the pressure resolution |
mcm | 1:01aeefb5f4cf | 240 | cmd[1] |= ( myResolution << 6 ); |
mcm | 1:01aeefb5f4cf | 241 | |
mcm | 1:01aeefb5f4cf | 242 | |
mcm | 1:01aeefb5f4cf | 243 | aux = i2c.write ( BMP085_Addr, &cmd[0], 2 ); |
mcm | 1:01aeefb5f4cf | 244 | |
mcm | 1:01aeefb5f4cf | 245 | |
mcm | 1:01aeefb5f4cf | 246 | |
mcm | 1:01aeefb5f4cf | 247 | if ( aux == I2C_SUCCESS ) |
mcm | 1:01aeefb5f4cf | 248 | return BMP085_SUCCESS; |
mcm | 1:01aeefb5f4cf | 249 | else |
mcm | 1:01aeefb5f4cf | 250 | return BMP085_FAILURE; |
mcm | 1:01aeefb5f4cf | 251 | } |
mcm | 1:01aeefb5f4cf | 252 | |
mcm | 1:01aeefb5f4cf | 253 | |
mcm | 1:01aeefb5f4cf | 254 | /** |
mcm | 1:01aeefb5f4cf | 255 | * @brief BMP085_ReadRawPressure ( Vector_pressure_f* ) |
mcm | 1:01aeefb5f4cf | 256 | * |
mcm | 1:01aeefb5f4cf | 257 | * @details It reads an uncompensated temperature. |
mcm | 1:01aeefb5f4cf | 258 | * |
mcm | 1:01aeefb5f4cf | 259 | * @param[in] NaN. |
mcm | 1:01aeefb5f4cf | 260 | * |
mcm | 1:01aeefb5f4cf | 261 | * @param[out] myRawPressure: Uncompensated temperature. |
mcm | 1:01aeefb5f4cf | 262 | * |
mcm | 1:01aeefb5f4cf | 263 | * |
mcm | 1:01aeefb5f4cf | 264 | * @return Status of BMP085_ReadRawPressure. |
mcm | 1:01aeefb5f4cf | 265 | * |
mcm | 1:01aeefb5f4cf | 266 | * |
mcm | 1:01aeefb5f4cf | 267 | * @author Manuel Caballero |
mcm | 1:01aeefb5f4cf | 268 | * @date 25/August/2017 |
mcm | 1:01aeefb5f4cf | 269 | * @version 25/August/2017 The ORIGIN |
mcm | 1:01aeefb5f4cf | 270 | * @pre BMP085_TriggerPressure function MUST be called before calling this one. |
mcm | 1:01aeefb5f4cf | 271 | * @warning The user MUST respect the maximum pressure conversion time: |
mcm | 1:01aeefb5f4cf | 272 | * Ultra Low Power Mode: 4.5ms. |
mcm | 1:01aeefb5f4cf | 273 | * Standard Mode: 7.5ms. |
mcm | 1:01aeefb5f4cf | 274 | * High Resolution Mode: 13.5ms. |
mcm | 1:01aeefb5f4cf | 275 | * Ultra High Res. Mode: 25.5ms. |
mcm | 1:01aeefb5f4cf | 276 | */ |
mcm | 1:01aeefb5f4cf | 277 | BMP085::BMP085_status_t BMP085::BMP085_ReadRawPressure ( Vector_pressure_f* myRawPressure ) |
mcm | 1:01aeefb5f4cf | 278 | { |
mcm | 1:01aeefb5f4cf | 279 | char cmd[] = { BMP085_READ_PRESSURE, 0, 0 }; |
mcm | 1:01aeefb5f4cf | 280 | uint32_t aux = 0; |
mcm | 1:01aeefb5f4cf | 281 | |
mcm | 1:01aeefb5f4cf | 282 | |
mcm | 1:01aeefb5f4cf | 283 | aux = i2c.write ( BMP085_Addr, &cmd[0], 1 ); |
mcm | 1:01aeefb5f4cf | 284 | aux = i2c.read ( BMP085_Addr, &cmd[0], 3 ); |
mcm | 1:01aeefb5f4cf | 285 | |
mcm | 1:01aeefb5f4cf | 286 | |
mcm | 1:01aeefb5f4cf | 287 | // Parse the data |
mcm | 1:01aeefb5f4cf | 288 | myRawPressure->UP_Pressure = ( cmd[0] << 16 ) | ( cmd[1] << 8 ) | cmd[2]; |
mcm | 1:01aeefb5f4cf | 289 | |
mcm | 1:01aeefb5f4cf | 290 | |
mcm | 1:01aeefb5f4cf | 291 | |
mcm | 1:01aeefb5f4cf | 292 | if ( aux == I2C_SUCCESS ) |
mcm | 1:01aeefb5f4cf | 293 | return BMP085_SUCCESS; |
mcm | 1:01aeefb5f4cf | 294 | else |
mcm | 1:01aeefb5f4cf | 295 | return BMP085_FAILURE; |
mcm | 1:01aeefb5f4cf | 296 | } |
mcm | 1:01aeefb5f4cf | 297 | |
mcm | 1:01aeefb5f4cf | 298 | |
mcm | 1:01aeefb5f4cf | 299 | /** |
mcm | 1:01aeefb5f4cf | 300 | * @brief BMP085_CalculateCompensated_Temperature_Pressure ( Vector_cal_coeff_t , Vector_temp_f , Vector_pressure_f , BMP085_pressure_osrs_t ) |
mcm | 1:01aeefb5f4cf | 301 | * |
mcm | 1:01aeefb5f4cf | 302 | * @details It reads an compensated pressure. |
mcm | 1:01aeefb5f4cf | 303 | * |
mcm | 1:01aeefb5f4cf | 304 | * @param[in] myCalCoeff: Calibration coefficients. |
mcm | 1:01aeefb5f4cf | 305 | * @param[in] myRawTemperature: Uncompensated temperature data. |
mcm | 1:01aeefb5f4cf | 306 | * @param[in] myRawPressure: Uncompensated pressure data. |
mcm | 1:01aeefb5f4cf | 307 | * @param[in] myResolution: Pressure resolution. |
mcm | 1:01aeefb5f4cf | 308 | * |
mcm | 1:01aeefb5f4cf | 309 | * @param[out] NaN. |
mcm | 1:01aeefb5f4cf | 310 | * |
mcm | 1:01aeefb5f4cf | 311 | * |
mcm | 1:01aeefb5f4cf | 312 | * @return Compensated Temperature and Pressure. |
mcm | 1:01aeefb5f4cf | 313 | * |
mcm | 1:01aeefb5f4cf | 314 | * |
mcm | 1:01aeefb5f4cf | 315 | * @author Manuel Caballero |
mcm | 1:01aeefb5f4cf | 316 | * @date 25/August/2017 |
mcm | 1:01aeefb5f4cf | 317 | * @version 25/August/2017 The ORIGIN |
mcm | 1:01aeefb5f4cf | 318 | * @pre BMP085_GetCalibrationCoefficients, BMP085_ReadRawPressure and BMP085_ReadRawTemperature MUST be called before using this function. |
mcm | 1:01aeefb5f4cf | 319 | * @warning NaN |
mcm | 1:01aeefb5f4cf | 320 | */ |
mcm | 1:01aeefb5f4cf | 321 | BMP085::Vector_compensated_data_f BMP085::BMP085_CalculateCompensated_Temperature_Pressure ( Vector_cal_coeff_t myCalCoeff, Vector_temp_f myRawTemperature, Vector_pressure_f myRawPressure, |
mcm | 1:01aeefb5f4cf | 322 | BMP085_pressure_osrs_t myResolution ) |
mcm | 1:01aeefb5f4cf | 323 | { |
mcm | 1:01aeefb5f4cf | 324 | int32_t B6, X1, B5, X2, X3, B3; |
mcm | 1:01aeefb5f4cf | 325 | uint32_t B4, B7; |
mcm | 1:01aeefb5f4cf | 326 | |
mcm | 1:01aeefb5f4cf | 327 | Vector_compensated_data_f myTrueData; |
mcm | 1:01aeefb5f4cf | 328 | |
mcm | 1:01aeefb5f4cf | 329 | // Calculate true temperature |
mcm | 1:01aeefb5f4cf | 330 | X1 = ( ( myRawTemperature.UT_Temperature - myCalCoeff.AC6 ) * myCalCoeff.AC5 ) / 32768; |
mcm | 1:01aeefb5f4cf | 331 | X2 = ( myCalCoeff.MC * 2048 ) / ( X1 + myCalCoeff.MD ); |
mcm | 1:01aeefb5f4cf | 332 | B5 = X1 + X2; |
mcm | 1:01aeefb5f4cf | 333 | |
mcm | 1:01aeefb5f4cf | 334 | myTrueData.Temperature = ( B5 + 8 ) / 16; |
mcm | 1:01aeefb5f4cf | 335 | |
mcm | 1:01aeefb5f4cf | 336 | |
mcm | 1:01aeefb5f4cf | 337 | // Calculate true pressure |
mcm | 1:01aeefb5f4cf | 338 | B6 = B5 - 4000; |
mcm | 1:01aeefb5f4cf | 339 | X1 = ( myCalCoeff.B2 * ( B6 * B6 / 4096 ) ) /2048; |
mcm | 1:01aeefb5f4cf | 340 | X2 = myCalCoeff.AC2 * B6 / 2048; |
mcm | 1:01aeefb5f4cf | 341 | X3 = X1 + X2; |
mcm | 1:01aeefb5f4cf | 342 | B3 = ( ( ( myCalCoeff.AC1 * 4 + X3 ) << myResolution ) + 2 ) / 4; |
mcm | 1:01aeefb5f4cf | 343 | X1 = myCalCoeff.AC3 * B6 / 8192; |
mcm | 1:01aeefb5f4cf | 344 | X2 = ( myCalCoeff.B1 * ( B6 * B6 / 4096 ) ) / 65536; |
mcm | 1:01aeefb5f4cf | 345 | X3 = ( ( X1 + X2 ) + 2 ) / 4; |
mcm | 1:01aeefb5f4cf | 346 | B4 = myCalCoeff.AC4 * ( uint32_t )( X3 + 32768 ) / 32768; |
mcm | 1:01aeefb5f4cf | 347 | B7 = ( ( uint32_t )myRawPressure.UP_Pressure - B3 ) * ( 50000 >> myResolution ); |
mcm | 1:01aeefb5f4cf | 348 | |
mcm | 1:01aeefb5f4cf | 349 | if ( B7 < 0x80000000 ) |
mcm | 1:01aeefb5f4cf | 350 | myTrueData.Pressure = ( B7 * 2 ) / B4; |
mcm | 1:01aeefb5f4cf | 351 | else |
mcm | 1:01aeefb5f4cf | 352 | myTrueData.Pressure = ( B7 / B4 ) * 2; |
mcm | 1:01aeefb5f4cf | 353 | |
mcm | 1:01aeefb5f4cf | 354 | X1 = ( myTrueData.Pressure / 256 ) * ( myTrueData.Pressure / 256 ); |
mcm | 1:01aeefb5f4cf | 355 | X1 = ( X1 * 3038 ) / 65536; |
mcm | 1:01aeefb5f4cf | 356 | X2 = ( -7357 * myTrueData.Pressure ) / 65536; |
mcm | 1:01aeefb5f4cf | 357 | |
mcm | 1:01aeefb5f4cf | 358 | myTrueData.Pressure = myTrueData.Pressure + ( X1 + X2 + 3791 ) / 16; |
mcm | 1:01aeefb5f4cf | 359 | |
mcm | 1:01aeefb5f4cf | 360 | |
mcm | 1:01aeefb5f4cf | 361 | return myTrueData; |
mcm | 1:01aeefb5f4cf | 362 | } |