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
spiridion 0:9a0671b6009f 34 #include "BMP180.h"
helloqi 2:5b3e84563dcb 35
helloqi 2:5b3e84563dcb 36 /***** Definitions *****/
helloqi 2:5b3e84563dcb 37 #define I2C_ADDR (0xEE) // 1110111x
spiridion 0:9a0671b6009f 38
helloqi 2:5b3e84563dcb 39 #define REG_ADDR_RESET (0xE0)
helloqi 2:5b3e84563dcb 40 #define REG_ADDR_ID (0xD0)
helloqi 2:5b3e84563dcb 41 #define REG_ADDR_CTRL (0xF4)
helloqi 2:5b3e84563dcb 42 #define REG_ADDR_DATA (0xF6)
helloqi 2:5b3e84563dcb 43 #define REG_ADDR_AC1 (0xAA)
spiridion 0:9a0671b6009f 44
helloqi 2:5b3e84563dcb 45 #define CTRL_REG_TEMP (0x2E)
helloqi 2:5b3e84563dcb 46 #define CTRL_REG_PRESS_0 (0x34)
helloqi 2:5b3e84563dcb 47 #define CTRL_REG_PRESS_1 (0x74)
helloqi 2:5b3e84563dcb 48 #define CTRL_REG_PRESS_2 (0xB4)
helloqi 2:5b3e84563dcb 49 #define CTRL_REG_PRESS_3 (0xF4)
helloqi 2:5b3e84563dcb 50
helloqi 2:5b3e84563dcb 51 //******************************************************************************
helloqi 2:5b3e84563dcb 52 BMP180::BMP180(PinName sda, PinName scl)
spiridion 0:9a0671b6009f 53 {
helloqi 2:5b3e84563dcb 54 i2c_ = new I2C(sda, scl);
helloqi 2:5b3e84563dcb 55 i2c_owner = true;
spiridion 0:9a0671b6009f 56
helloqi 2:5b3e84563dcb 57 i2c_->frequency(400000); //400000
spiridion 0:9a0671b6009f 58 }
spiridion 0:9a0671b6009f 59
helloqi 2:5b3e84563dcb 60 //******************************************************************************
helloqi 2:5b3e84563dcb 61 BMP180::BMP180(I2C *i2c) :
helloqi 2:5b3e84563dcb 62 i2c_(i2c)
spiridion 0:9a0671b6009f 63 {
helloqi 2:5b3e84563dcb 64 i2c_owner = false;
helloqi 2:5b3e84563dcb 65 }
spiridion 0:9a0671b6009f 66
helloqi 2:5b3e84563dcb 67 //******************************************************************************
helloqi 2:5b3e84563dcb 68 BMP180::~BMP180()
helloqi 2:5b3e84563dcb 69 {
helloqi 2:5b3e84563dcb 70 if(i2c_owner) {
helloqi 2:5b3e84563dcb 71 delete i2c_;
helloqi 2:5b3e84563dcb 72 }
spiridion 0:9a0671b6009f 73 }
spiridion 0:9a0671b6009f 74
helloqi 2:5b3e84563dcb 75 //******************************************************************************
helloqi 2:5b3e84563dcb 76 int BMP180::init(void)
spiridion 0:9a0671b6009f 77 {
helloqi 2:5b3e84563dcb 78 char addr;
helloqi 2:5b3e84563dcb 79 char data[22];
helloqi 2:5b3e84563dcb 80 int i;
spiridion 0:9a0671b6009f 81
helloqi 2:5b3e84563dcb 82 if (checkId() != 0) {
helloqi 2:5b3e84563dcb 83 return -1;
helloqi 2:5b3e84563dcb 84 }
helloqi 2:5b3e84563dcb 85
helloqi 2:5b3e84563dcb 86 addr = REG_ADDR_AC1;
helloqi 2:5b3e84563dcb 87 if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
helloqi 2:5b3e84563dcb 88 return -1;
helloqi 2:5b3e84563dcb 89 }
helloqi 2:5b3e84563dcb 90
helloqi 2:5b3e84563dcb 91 if (i2c_->read(I2C_ADDR, data, 22) != 0) {
helloqi 2:5b3e84563dcb 92 return -1;
spiridion 0:9a0671b6009f 93 }
spiridion 0:9a0671b6009f 94
helloqi 2:5b3e84563dcb 95 for (i = 0; i < 11; i++) {
helloqi 2:5b3e84563dcb 96 calib.value[i] = (data[2*i] << 8) | data[(2*i)+1];
helloqi 2:5b3e84563dcb 97 }
helloqi 2:5b3e84563dcb 98
helloqi 2:5b3e84563dcb 99 return 0;
helloqi 2:5b3e84563dcb 100 }
helloqi 2:5b3e84563dcb 101
helloqi 2:5b3e84563dcb 102 //******************************************************************************
helloqi 2:5b3e84563dcb 103 int BMP180::reset(void)
helloqi 2:5b3e84563dcb 104 {
helloqi 2:5b3e84563dcb 105 char data;
spiridion 0:9a0671b6009f 106
helloqi 2:5b3e84563dcb 107 data = REG_ADDR_RESET;
helloqi 2:5b3e84563dcb 108 if (i2c_->write(I2C_ADDR, &data, 1) != 0) {
helloqi 2:5b3e84563dcb 109 return -1;
helloqi 2:5b3e84563dcb 110 }
spiridion 0:9a0671b6009f 111
helloqi 2:5b3e84563dcb 112 data = 0xB6;
helloqi 2:5b3e84563dcb 113 if (i2c_->write(I2C_ADDR, &data, 1) != 0) {
helloqi 2:5b3e84563dcb 114 return -1;
helloqi 2:5b3e84563dcb 115 }
helloqi 2:5b3e84563dcb 116
helloqi 2:5b3e84563dcb 117 return 0;
spiridion 0:9a0671b6009f 118 }
spiridion 0:9a0671b6009f 119
helloqi 2:5b3e84563dcb 120 //******************************************************************************
helloqi 2:5b3e84563dcb 121 int BMP180::checkId(void)
spiridion 0:9a0671b6009f 122 {
helloqi 2:5b3e84563dcb 123 char addr;
helloqi 2:5b3e84563dcb 124 char data;
spiridion 0:9a0671b6009f 125
helloqi 2:5b3e84563dcb 126 addr = REG_ADDR_ID;
helloqi 2:5b3e84563dcb 127 if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
helloqi 2:5b3e84563dcb 128 return -1;
helloqi 2:5b3e84563dcb 129 }
spiridion 0:9a0671b6009f 130
helloqi 2:5b3e84563dcb 131 if (i2c_->read(I2C_ADDR, &data, 1) != 0) {
helloqi 2:5b3e84563dcb 132 return -1;
helloqi 2:5b3e84563dcb 133 }
spiridion 0:9a0671b6009f 134
helloqi 2:5b3e84563dcb 135 if (data != 0x55) {
helloqi 2:5b3e84563dcb 136 return -1;
helloqi 2:5b3e84563dcb 137 }
helloqi 2:5b3e84563dcb 138
helloqi 2:5b3e84563dcb 139 return 0;
spiridion 0:9a0671b6009f 140 }
spiridion 0:9a0671b6009f 141
helloqi 2:5b3e84563dcb 142 //******************************************************************************
helloqi 2:5b3e84563dcb 143 int BMP180::startPressure(BMP180::oversampling_t oss)
spiridion 0:9a0671b6009f 144 {
spiridion 0:9a0671b6009f 145 char data[2];
helloqi 2:5b3e84563dcb 146
helloqi 2:5b3e84563dcb 147 data[0] = REG_ADDR_CTRL;
helloqi 2:5b3e84563dcb 148 data[1] = CTRL_REG_PRESS_0 | ((oss & 0x3) << 6);
helloqi 2:5b3e84563dcb 149 oss_ = oss;
helloqi 2:5b3e84563dcb 150
helloqi 2:5b3e84563dcb 151 if (i2c_->write(I2C_ADDR, data, 2) != 0) {
helloqi 2:5b3e84563dcb 152 return -1;
helloqi 2:5b3e84563dcb 153 }
helloqi 2:5b3e84563dcb 154
helloqi 2:5b3e84563dcb 155 return 0;
helloqi 2:5b3e84563dcb 156 }
spiridion 0:9a0671b6009f 157
helloqi 2:5b3e84563dcb 158 //******************************************************************************
helloqi 2:5b3e84563dcb 159 int BMP180::getPressure(int *pressure)
helloqi 2:5b3e84563dcb 160 {
helloqi 2:5b3e84563dcb 161 char addr, byte[3];
helloqi 2:5b3e84563dcb 162 uint32_t up;
helloqi 2:5b3e84563dcb 163 int32_t b6, x1, x2, x3, b3, p;
helloqi 2:5b3e84563dcb 164 uint32_t b4, b7;
helloqi 2:5b3e84563dcb 165
helloqi 2:5b3e84563dcb 166 addr = REG_ADDR_DATA;
helloqi 2:5b3e84563dcb 167 if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
helloqi 2:5b3e84563dcb 168 return -1;
helloqi 2:5b3e84563dcb 169 }
helloqi 2:5b3e84563dcb 170
helloqi 2:5b3e84563dcb 171 if (i2c_->read(I2C_ADDR, byte, 3) != 0) {
helloqi 2:5b3e84563dcb 172 return -1;
spiridion 0:9a0671b6009f 173 }
spiridion 0:9a0671b6009f 174
helloqi 2:5b3e84563dcb 175 up = ((byte[0] << 16) | (byte[1] << 8) | byte[2]) >> (8 - oss_);
helloqi 2:5b3e84563dcb 176
helloqi 2:5b3e84563dcb 177 b6 = b5 - 4000;
helloqi 2:5b3e84563dcb 178 x1 = (b6 * b6) >> 12;
helloqi 2:5b3e84563dcb 179 x1 *= calib.b2;
helloqi 2:5b3e84563dcb 180 x1 >>= 11;
helloqi 2:5b3e84563dcb 181 x2 = calib.ac2 * b6;
helloqi 2:5b3e84563dcb 182 x2 >>= 11;
helloqi 2:5b3e84563dcb 183 x3 = x1 + x2;
helloqi 2:5b3e84563dcb 184 b3 = (((((int32_t)calib.ac1) * 4 + x3) << oss_) + 2);
helloqi 2:5b3e84563dcb 185 b3 >>= 2;
spiridion 0:9a0671b6009f 186
helloqi 2:5b3e84563dcb 187 x1 = (calib.ac3 * b6) >> 13;
helloqi 2:5b3e84563dcb 188 x2 = (calib.b1 * ((b6 * b6) >> 12)) >> 16;
helloqi 2:5b3e84563dcb 189 x3 = (x1 + x2 + 2) >> 2;
helloqi 2:5b3e84563dcb 190 b4 = (calib.ac4 * (uint32_t)(x3 + 32768)) >> 15;
helloqi 2:5b3e84563dcb 191 b7 = ((uint32_t)up - b3) * (50000 >> oss_);
helloqi 2:5b3e84563dcb 192 p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));
helloqi 2:5b3e84563dcb 193 x1 = p >> 8;
helloqi 2:5b3e84563dcb 194 x1 *= x1;
helloqi 2:5b3e84563dcb 195 x1 = (x1 * 3038) >> 16;
helloqi 2:5b3e84563dcb 196 x2 = (-7357 * p) >> 16;
helloqi 2:5b3e84563dcb 197 p += (x1 + x2 + 3791) >> 4;
helloqi 2:5b3e84563dcb 198
helloqi 2:5b3e84563dcb 199 *pressure = p;
spiridion 0:9a0671b6009f 200
helloqi 2:5b3e84563dcb 201 return 0;
helloqi 2:5b3e84563dcb 202 }
helloqi 2:5b3e84563dcb 203
helloqi 2:5b3e84563dcb 204 //******************************************************************************
helloqi 2:5b3e84563dcb 205 int BMP180::startTemperature(void)
helloqi 2:5b3e84563dcb 206 {
helloqi 2:5b3e84563dcb 207 char data[2] = { REG_ADDR_CTRL, CTRL_REG_TEMP };
helloqi 2:5b3e84563dcb 208
helloqi 2:5b3e84563dcb 209 if (i2c_->write(I2C_ADDR, data, 2) != 0) {
helloqi 2:5b3e84563dcb 210 return -1;
helloqi 2:5b3e84563dcb 211 }
helloqi 2:5b3e84563dcb 212
helloqi 2:5b3e84563dcb 213 return 0;
spiridion 0:9a0671b6009f 214 }
spiridion 0:9a0671b6009f 215
helloqi 2:5b3e84563dcb 216 //******************************************************************************
helloqi 2:5b3e84563dcb 217 int BMP180::getTemperature(float *tempC)
spiridion 0:9a0671b6009f 218 {
helloqi 2:5b3e84563dcb 219 char addr, byte[2];
helloqi 2:5b3e84563dcb 220 uint16_t ut;
helloqi 2:5b3e84563dcb 221 int32_t x1, x2;
helloqi 2:5b3e84563dcb 222
helloqi 2:5b3e84563dcb 223 addr = REG_ADDR_DATA;
helloqi 2:5b3e84563dcb 224 if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
helloqi 2:5b3e84563dcb 225 return -1;
helloqi 2:5b3e84563dcb 226 }
helloqi 2:5b3e84563dcb 227
helloqi 2:5b3e84563dcb 228 if (i2c_->read(I2C_ADDR, byte, 2) != 0) {
helloqi 2:5b3e84563dcb 229 return -1;
helloqi 2:5b3e84563dcb 230 }
helloqi 2:5b3e84563dcb 231
helloqi 2:5b3e84563dcb 232 ut = (byte[0] << 8) | byte[1];
helloqi 2:5b3e84563dcb 233
helloqi 2:5b3e84563dcb 234 x1 = ((ut - calib.ac6) * calib.ac5) >> 15;
helloqi 2:5b3e84563dcb 235 x2 = (calib.mc << 11) / (x1 + calib.md);
spiridion 0:9a0671b6009f 236 b5 = x1 + x2;
spiridion 0:9a0671b6009f 237
helloqi 2:5b3e84563dcb 238 *tempC = (float)(b5 + 8) / 160;
helloqi 2:5b3e84563dcb 239
helloqi 2:5b3e84563dcb 240 return 0;
spiridion 0:9a0671b6009f 241 }
spiridion 0:9a0671b6009f 242
helloqi 2:5b3e84563dcb 243 //******************************************************************************
helloqi 2:5b3e84563dcb 244 int BMP180::getTemperature(int16_t *tempCx10)
spiridion 0:9a0671b6009f 245 {
helloqi 2:5b3e84563dcb 246 char addr, byte[2];
helloqi 2:5b3e84563dcb 247 uint16_t ut;
helloqi 2:5b3e84563dcb 248 int32_t x1, x2;
helloqi 2:5b3e84563dcb 249
helloqi 2:5b3e84563dcb 250 addr = REG_ADDR_DATA;
helloqi 2:5b3e84563dcb 251 if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
helloqi 2:5b3e84563dcb 252 return -1;
helloqi 2:5b3e84563dcb 253 }
spiridion 0:9a0671b6009f 254
helloqi 2:5b3e84563dcb 255 if (i2c_->read(I2C_ADDR, byte, 2) != 0) {
helloqi 2:5b3e84563dcb 256 return -1;
helloqi 2:5b3e84563dcb 257 }
helloqi 2:5b3e84563dcb 258
helloqi 2:5b3e84563dcb 259 ut = (byte[0] << 8) | byte[1];
helloqi 2:5b3e84563dcb 260
helloqi 2:5b3e84563dcb 261 x1 = ((ut - calib.ac6) * calib.ac5) >> 15;
helloqi 2:5b3e84563dcb 262 x2 = (calib.mc << 11) / (x1 + calib.md);
helloqi 2:5b3e84563dcb 263 b5 = x1 + x2;
helloqi 2:5b3e84563dcb 264
helloqi 2:5b3e84563dcb 265 *tempCx10 = (b5 + 8) >> 4;
helloqi 2:5b3e84563dcb 266
helloqi 2:5b3e84563dcb 267 return 0;
helloqi 2:5b3e84563dcb 268 }