BMP180 Pressure/Temperature Sensor library

Dependents:   LinkNode_TemperatureAdvertising

Fork of BMP180 by Spiridion Mbed

Committer:
helloqi
Date:
Wed Mar 02 05:45:36 2016 +0000
Revision:
2:5b3e84563dcb
Parent:
1:072073c79cfd
temperature advertising

Who changed what in which revision?

UserRevisionLine numberNew contents of line
helloqi 2:5b3e84563dcb 1 /*******************************************************************************
helloqi 2:5b3e84563dcb 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
helloqi 2:5b3e84563dcb 3 *
helloqi 2:5b3e84563dcb 4 * Permission is hereby granted, free of charge, to any person obtaining a
helloqi 2:5b3e84563dcb 5 * copy of this software and associated documentation files (the "Software"),
helloqi 2:5b3e84563dcb 6 * to deal in the Software without restriction, including without limitation
helloqi 2:5b3e84563dcb 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
helloqi 2:5b3e84563dcb 8 * and/or sell copies of the Software, and to permit persons to whom the
helloqi 2:5b3e84563dcb 9 * Software is furnished to do so, subject to the following conditions:
helloqi 2:5b3e84563dcb 10 *
helloqi 2:5b3e84563dcb 11 * The above copyright notice and this permission notice shall be included
helloqi 2:5b3e84563dcb 12 * in all copies or substantial portions of the Software.
helloqi 2:5b3e84563dcb 13 *
helloqi 2:5b3e84563dcb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
helloqi 2:5b3e84563dcb 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
helloqi 2:5b3e84563dcb 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
helloqi 2:5b3e84563dcb 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
helloqi 2:5b3e84563dcb 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
helloqi 2:5b3e84563dcb 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
helloqi 2:5b3e84563dcb 20 * OTHER DEALINGS IN THE SOFTWARE.
helloqi 2:5b3e84563dcb 21 *
helloqi 2:5b3e84563dcb 22 * Except as contained in this notice, the name of Maxim Integrated
helloqi 2:5b3e84563dcb 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
helloqi 2:5b3e84563dcb 24 * Products, Inc. Branding Policy.
helloqi 2:5b3e84563dcb 25 *
helloqi 2:5b3e84563dcb 26 * The mere transfer of this software does not imply any licenses
helloqi 2:5b3e84563dcb 27 * of trade secrets, proprietary technology, copyrights, patents,
helloqi 2:5b3e84563dcb 28 * trademarks, maskwork rights, or any other form of intellectual
helloqi 2:5b3e84563dcb 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
helloqi 2:5b3e84563dcb 30 * ownership rights.
helloqi 2:5b3e84563dcb 31 *******************************************************************************
helloqi 2:5b3e84563dcb 32 */
spiridion 0:9a0671b6009f 33
helloqi 2:5b3e84563dcb 34 #ifndef _BMP180_H_
helloqi 2:5b3e84563dcb 35 #define _BMP180_H_
spiridion 0:9a0671b6009f 36
spiridion 0:9a0671b6009f 37 #include "mbed.h"
spiridion 0:9a0671b6009f 38
helloqi 2:5b3e84563dcb 39 /**
helloqi 2:5b3e84563dcb 40 * Bosch BMP180 Digital Pressure Sensor
spiridion 0:9a0671b6009f 41 *
spiridion 0:9a0671b6009f 42 * @code
helloqi 2:5b3e84563dcb 43 * #include <stdio.h>
spiridion 0:9a0671b6009f 44 * #include "mbed.h"
spiridion 0:9a0671b6009f 45 * #include "BMP180.h"
helloqi 2:5b3e84563dcb 46 *
helloqi 2:5b3e84563dcb 47 * I2C i2c(I2C_SDA, I2C_SCL);
helloqi 2:5b3e84563dcb 48 * BMP180 bmp180(&i2c);
helloqi 2:5b3e84563dcb 49 *
helloqi 2:5b3e84563dcb 50 * int main(void) {
helloqi 2:5b3e84563dcb 51 *
helloqi 2:5b3e84563dcb 52 * while(1) {
helloqi 2:5b3e84563dcb 53 * if (bmp180.init() != 0) {
helloqi 2:5b3e84563dcb 54 * printf("Error communicating with BMP180\n");
helloqi 2:5b3e84563dcb 55 * } else {
helloqi 2:5b3e84563dcb 56 * printf("Initialized BMP180\n");
helloqi 2:5b3e84563dcb 57 * break;
helloqi 2:5b3e84563dcb 58 * }
helloqi 2:5b3e84563dcb 59 * wait(1);
helloqi 2:5b3e84563dcb 60 * }
helloqi 2:5b3e84563dcb 61 *
helloqi 2:5b3e84563dcb 62 * while(1) {
helloqi 2:5b3e84563dcb 63 * bmp180.startTemperature();
helloqi 2:5b3e84563dcb 64 * wait_ms(5); // Wait for conversion to complete
helloqi 2:5b3e84563dcb 65 * float temp;
helloqi 2:5b3e84563dcb 66 * if(bmp180.getTemperature(&temp) != 0) {
helloqi 2:5b3e84563dcb 67 * printf("Error getting temperature\n");
helloqi 2:5b3e84563dcb 68 * continue;
helloqi 2:5b3e84563dcb 69 * }
helloqi 2:5b3e84563dcb 70 *
helloqi 2:5b3e84563dcb 71 * bmp180.startPressure(BMP180::ULTRA_LOW_POWER);
helloqi 2:5b3e84563dcb 72 * wait_ms(10); // Wait for conversion to complete
helloqi 2:5b3e84563dcb 73 * int pressure;
helloqi 2:5b3e84563dcb 74 * if(bmp180.getPressure(&pressure) != 0) {
helloqi 2:5b3e84563dcb 75 * printf("Error getting pressure\n");
helloqi 2:5b3e84563dcb 76 * continue;
helloqi 2:5b3e84563dcb 77 * }
helloqi 2:5b3e84563dcb 78 *
helloqi 2:5b3e84563dcb 79 * printf("Pressure = %d Pa Temperature = %f C\n", pressure, temp);
spiridion 0:9a0671b6009f 80 * wait(1);
spiridion 0:9a0671b6009f 81 * }
spiridion 0:9a0671b6009f 82 * }
spiridion 0:9a0671b6009f 83 * @endcode
spiridion 0:9a0671b6009f 84 */
helloqi 2:5b3e84563dcb 85 class BMP180
spiridion 0:9a0671b6009f 86 {
spiridion 0:9a0671b6009f 87
spiridion 0:9a0671b6009f 88 public:
spiridion 0:9a0671b6009f 89
helloqi 2:5b3e84563dcb 90 /**
helloqi 2:5b3e84563dcb 91 * @brief Oversampling ratio.
helloqi 2:5b3e84563dcb 92 * @details Dictates how many pressure samples to take. Conversion time varies
helloqi 2:5b3e84563dcb 93 * depending on the number of samples taken. Refer to data sheet
helloqi 2:5b3e84563dcb 94 * for timing specifications.
spiridion 0:9a0671b6009f 95 */
helloqi 2:5b3e84563dcb 96 typedef enum {
helloqi 2:5b3e84563dcb 97 ULTRA_LOW_POWER = 0, ///< 1 pressure sample
helloqi 2:5b3e84563dcb 98 STANDARD = 1, ///< 2 pressure samples
helloqi 2:5b3e84563dcb 99 HIGH_RESOLUTION = 2, ///< 4 pressure samples
helloqi 2:5b3e84563dcb 100 ULTRA_HIGH_RESOLUTION = 3, ///< 8 pressure samples
helloqi 2:5b3e84563dcb 101 } oversampling_t;
spiridion 0:9a0671b6009f 102
helloqi 2:5b3e84563dcb 103 /**
helloqi 2:5b3e84563dcb 104 * BMP180 constructor.
helloqi 2:5b3e84563dcb 105 *
helloqi 2:5b3e84563dcb 106 * @param sda mbed pin to use for SDA line of I2C interface.
helloqi 2:5b3e84563dcb 107 * @param scl mbed pin to use for SCL line of I2C interface.
spiridion 0:9a0671b6009f 108 */
helloqi 2:5b3e84563dcb 109 BMP180(PinName sda, PinName scl);
spiridion 0:9a0671b6009f 110
helloqi 2:5b3e84563dcb 111 /**
helloqi 2:5b3e84563dcb 112 * BMP180 constructor.
helloqi 2:5b3e84563dcb 113 *
helloqi 2:5b3e84563dcb 114 * @param i2c I2C object to use.
helloqi 2:5b3e84563dcb 115 */
helloqi 2:5b3e84563dcb 116 BMP180(I2C *i2c);
helloqi 2:5b3e84563dcb 117
helloqi 2:5b3e84563dcb 118 /**
helloqi 2:5b3e84563dcb 119 * BMP180 destructor.
spiridion 0:9a0671b6009f 120 */
helloqi 2:5b3e84563dcb 121 ~BMP180();
helloqi 2:5b3e84563dcb 122
helloqi 2:5b3e84563dcb 123 /**
helloqi 2:5b3e84563dcb 124 * @brief Initialize BMP180.
helloqi 2:5b3e84563dcb 125 * @details Gets the device ID and saves the calibration values.
helloqi 2:5b3e84563dcb 126 * @returns 0 if no errors, -1 if error.
helloqi 2:5b3e84563dcb 127 */
helloqi 2:5b3e84563dcb 128 int init(void);
helloqi 2:5b3e84563dcb 129
helloqi 2:5b3e84563dcb 130 /**
helloqi 2:5b3e84563dcb 131 * @brief Reset BMP180.
helloqi 2:5b3e84563dcb 132 * @details Performs a soft reset of the device. Same sequence as power on reset.
helloqi 2:5b3e84563dcb 133 * @returns 0 if no errors, -1 if error.
helloqi 2:5b3e84563dcb 134 */
helloqi 2:5b3e84563dcb 135 int reset(void);
spiridion 0:9a0671b6009f 136
helloqi 2:5b3e84563dcb 137 /**
helloqi 2:5b3e84563dcb 138 * @brief Check ID.
helloqi 2:5b3e84563dcb 139 * @details Checks the device ID, should be 0x55 on reset.
helloqi 2:5b3e84563dcb 140 * @returns 0 if no errors, -1 if error.
helloqi 2:5b3e84563dcb 141 */
helloqi 2:5b3e84563dcb 142 int checkId(void);
spiridion 0:9a0671b6009f 143
helloqi 2:5b3e84563dcb 144 /**
helloqi 2:5b3e84563dcb 145 * @brief Start pressure conversion.
helloqi 2:5b3e84563dcb 146 * @details Initiates the pressure conversion sequence. Refer to data sheet
helloqi 2:5b3e84563dcb 147 * for timing specifications.
helloqi 2:5b3e84563dcb 148 *
helloqi 2:5b3e84563dcb 149 * @param oss Number of samples to take.
helloqi 2:5b3e84563dcb 150 * @returns 0 if no errors, -1 if error.
helloqi 2:5b3e84563dcb 151 */
helloqi 2:5b3e84563dcb 152 int startPressure(BMP180::oversampling_t oss);
spiridion 0:9a0671b6009f 153
helloqi 2:5b3e84563dcb 154 /**
helloqi 2:5b3e84563dcb 155 * @brief Get pressure reading.
helloqi 2:5b3e84563dcb 156 * @details Calculates the pressure using the data calibration data and formula.
helloqi 2:5b3e84563dcb 157 * Pressure is reported in Pascals.
helloqi 2:5b3e84563dcb 158 * @note This function should be called after calling startPressure().
helloqi 2:5b3e84563dcb 159 * Refer to the data sheet for the timing requirements. Calling this
helloqi 2:5b3e84563dcb 160 * function too soon can result in oversampling.
helloqi 2:5b3e84563dcb 161 *
helloqi 2:5b3e84563dcb 162 * @param pressure Pointer to store pressure reading.
helloqi 2:5b3e84563dcb 163 * @returns 0 if no errors, -1 if error.
helloqi 2:5b3e84563dcb 164 */
helloqi 2:5b3e84563dcb 165 int getPressure(int *pressure);
spiridion 0:9a0671b6009f 166
helloqi 2:5b3e84563dcb 167 /**
helloqi 2:5b3e84563dcb 168 * @brief Start temperature conversion.
helloqi 2:5b3e84563dcb 169 * @details Initiates the temperature conversion sequence. Refer to data
helloqi 2:5b3e84563dcb 170 * sheet for timing specifications.
helloqi 2:5b3e84563dcb 171 * @returns 0 if no errors, -1 if error.
helloqi 2:5b3e84563dcb 172 */
helloqi 2:5b3e84563dcb 173 int startTemperature(void);
spiridion 0:9a0671b6009f 174
helloqi 2:5b3e84563dcb 175 /**
helloqi 2:5b3e84563dcb 176 * @brief Get temperature reading.
helloqi 2:5b3e84563dcb 177 * @details Calculates the temperature using the data calibration data and formula.
helloqi 2:5b3e84563dcb 178 * Temperature is reported in degrees Celcius.
helloqi 2:5b3e84563dcb 179 *
helloqi 2:5b3e84563dcb 180 * @note This function should be called after calling startTemperature().
helloqi 2:5b3e84563dcb 181 * Refer to the data sheet for the timing requirements. Calling this
helloqi 2:5b3e84563dcb 182 * function too soon can result in oversampling.
helloqi 2:5b3e84563dcb 183 *
helloqi 2:5b3e84563dcb 184 * @param tempC Pointer to store temperature reading.
helloqi 2:5b3e84563dcb 185 * @returns 0 if no errors, -1 if error.
helloqi 2:5b3e84563dcb 186 */
helloqi 2:5b3e84563dcb 187 int getTemperature(float *tempC);
spiridion 0:9a0671b6009f 188
helloqi 2:5b3e84563dcb 189 /**
helloqi 2:5b3e84563dcb 190 * @brief Get temperature reading.
helloqi 2:5b3e84563dcb 191 * @details Calculates the temperature using the data calibration data and formula.
helloqi 2:5b3e84563dcb 192 * Temperature is reported in 1/10ths degrees Celcius.
helloqi 2:5b3e84563dcb 193 *
helloqi 2:5b3e84563dcb 194 * @note This function should be called after calling startTemperature().
helloqi 2:5b3e84563dcb 195 * Refer to the data sheet for the timing requirements. Calling this
helloqi 2:5b3e84563dcb 196 * function too soon can result in oversampling.
helloqi 2:5b3e84563dcb 197 *
helloqi 2:5b3e84563dcb 198 * @param tempCx10 Pointer to store temperature reading.
helloqi 2:5b3e84563dcb 199 * @returns 0 if no errors, -1 if error.
helloqi 2:5b3e84563dcb 200 */
helloqi 2:5b3e84563dcb 201 int getTemperature(int16_t *tempCx10);
spiridion 0:9a0671b6009f 202
helloqi 2:5b3e84563dcb 203 private:
spiridion 0:9a0671b6009f 204
helloqi 2:5b3e84563dcb 205 typedef union {
helloqi 2:5b3e84563dcb 206 uint16_t value[11];
helloqi 2:5b3e84563dcb 207 struct {
helloqi 2:5b3e84563dcb 208 int16_t ac1;
helloqi 2:5b3e84563dcb 209 int16_t ac2;
helloqi 2:5b3e84563dcb 210 int16_t ac3;
helloqi 2:5b3e84563dcb 211 uint16_t ac4;
helloqi 2:5b3e84563dcb 212 uint16_t ac5;
helloqi 2:5b3e84563dcb 213 uint16_t ac6;
helloqi 2:5b3e84563dcb 214 int16_t b1;
helloqi 2:5b3e84563dcb 215 int16_t b2;
helloqi 2:5b3e84563dcb 216 int16_t mb;
helloqi 2:5b3e84563dcb 217 int16_t mc;
helloqi 2:5b3e84563dcb 218 int16_t md;
helloqi 2:5b3e84563dcb 219 };
helloqi 2:5b3e84563dcb 220 } calibration_t;
spiridion 0:9a0671b6009f 221
helloqi 2:5b3e84563dcb 222 I2C *i2c_;
helloqi 2:5b3e84563dcb 223 bool i2c_owner;
spiridion 0:9a0671b6009f 224
helloqi 2:5b3e84563dcb 225 BMP180::calibration_t calib;
helloqi 2:5b3e84563dcb 226 int32_t b5;
helloqi 2:5b3e84563dcb 227 BMP180::oversampling_t oss_;
spiridion 0:9a0671b6009f 228 };
spiridion 0:9a0671b6009f 229
helloqi 2:5b3e84563dcb 230 #endif /* _BMP180_H_ */