interface to chr_6dm and Baro bmp085

Dependencies:   mbed

Committer:
belloula
Date:
Mon Oct 17 19:43:06 2011 +0000
Revision:
0:d07617f8ede9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
belloula 0:d07617f8ede9 1 /*
belloula 0:d07617f8ede9 2 * mbed library to use a Bosch Sensortec BMP085 sensor
belloula 0:d07617f8ede9 3 * Copyright (c) 2010 Hiroshi Suga
belloula 0:d07617f8ede9 4 * Released under the MIT License: http://mbed.org/license/mit
belloula 0:d07617f8ede9 5 */
belloula 0:d07617f8ede9 6
belloula 0:d07617f8ede9 7 /** @file BMP085.cpp
belloula 0:d07617f8ede9 8 * @brief mbed library to use a Bosch Sensortec BMP085 sensor
belloula 0:d07617f8ede9 9 * barometric pressure sensor BMP085 (Bosch Sensortec)
belloula 0:d07617f8ede9 10 * interface: I2C digital
belloula 0:d07617f8ede9 11 */
belloula 0:d07617f8ede9 12
belloula 0:d07617f8ede9 13 #include "mbed.h"
belloula 0:d07617f8ede9 14 #include "BMP085.h"
belloula 0:d07617f8ede9 15
belloula 0:d07617f8ede9 16 #define WEATHER_BMP085 0xee
belloula 0:d07617f8ede9 17 #define xpow(x, y) ((long)1 << y)
belloula 0:d07617f8ede9 18
belloula 0:d07617f8ede9 19 /**
belloula 0:d07617f8ede9 20 * @brief Initializes interface (private I2C)
belloula 0:d07617f8ede9 21 * @param p_sda port of I2C SDA
belloula 0:d07617f8ede9 22 * @param p_scl port of I2C SCL
belloula 0:d07617f8ede9 23 * @param p_oss parameter of OSS
belloula 0:d07617f8ede9 24 */
belloula 0:d07617f8ede9 25 BMP085::BMP085 (PinName p_sda, PinName p_scl, BMP085_oss p_oss) : i2c(p_sda, p_scl)
belloula 0:d07617f8ede9 26 {
belloula 0:d07617f8ede9 27 init(p_oss);
belloula 0:d07617f8ede9 28 ok=0;
belloula 0:d07617f8ede9 29 }
belloula 0:d07617f8ede9 30
belloula 0:d07617f8ede9 31 /**
belloula 0:d07617f8ede9 32 * @brief Initializes interface (public I2C)
belloula 0:d07617f8ede9 33 * @param p_i2c instance of I2C class
belloula 0:d07617f8ede9 34 * @param p_oss parameter of OSS
belloula 0:d07617f8ede9 35 */
belloula 0:d07617f8ede9 36 BMP085::BMP085 (I2C& p_i2c, BMP085_oss p_oss) : i2c(p_i2c)
belloula 0:d07617f8ede9 37 {
belloula 0:d07617f8ede9 38 init(p_oss);
belloula 0:d07617f8ede9 39 ok=0;
belloula 0:d07617f8ede9 40 }
belloula 0:d07617f8ede9 41
belloula 0:d07617f8ede9 42 /**
belloula 0:d07617f8ede9 43 * @brief Get temperature
belloula 0:d07617f8ede9 44 * @return temperature (`C)
belloula 0:d07617f8ede9 45 */
belloula 0:d07617f8ede9 46 float BMP085::get_temperature()
belloula 0:d07617f8ede9 47 {
belloula 0:d07617f8ede9 48 return temperature;
belloula 0:d07617f8ede9 49 }
belloula 0:d07617f8ede9 50
belloula 0:d07617f8ede9 51 /**
belloula 0:d07617f8ede9 52 * @brief Get pressure
belloula 0:d07617f8ede9 53 * @return pressure (hPa)
belloula 0:d07617f8ede9 54 */
belloula 0:d07617f8ede9 55 float BMP085::get_pressure()
belloula 0:d07617f8ede9 56 {
belloula 0:d07617f8ede9 57 return pressure;
belloula 0:d07617f8ede9 58 }
belloula 0:d07617f8ede9 59 /**
belloula 0:d07617f8ede9 60 * @brief Get altitude e
belloula 0:d07617f8ede9 61 * @return altitude (m)
belloula 0:d07617f8ede9 62 */
belloula 0:d07617f8ede9 63 float BMP085::get_altitude()
belloula 0:d07617f8ede9 64 {
belloula 0:d07617f8ede9 65 return altitude;
belloula 0:d07617f8ede9 66 }
belloula 0:d07617f8ede9 67
belloula 0:d07617f8ede9 68
belloula 0:d07617f8ede9 69 /**
belloula 0:d07617f8ede9 70 * @brief Update results
belloula 0:d07617f8ede9 71 */
belloula 0:d07617f8ede9 72 void BMP085::update ()
belloula 0:d07617f8ede9 73 {
belloula 0:d07617f8ede9 74 static long t, p, ut, up, x1, x2, x3, b3, b5, b6;
belloula 0:d07617f8ede9 75 static unsigned long b4, b7;
belloula 0:d07617f8ede9 76 static unsigned int nbtick=0;
belloula 0:d07617f8ede9 77
belloula 0:d07617f8ede9 78 switch(nbtick){
belloula 0:d07617f8ede9 79 case 0 : twi_writechar(WEATHER_BMP085, 0xf4, 0x2e);
belloula 0:d07617f8ede9 80 nbtick=1;
belloula 0:d07617f8ede9 81 break;
belloula 0:d07617f8ede9 82 case 1 : ut = twi_readshort(WEATHER_BMP085, 0xf6);
belloula 0:d07617f8ede9 83 twi_writechar(WEATHER_BMP085, 0xf4, 0x34 | (oss << 6));
belloula 0:d07617f8ede9 84 nbtick=2;
belloula 0:d07617f8ede9 85 break;
belloula 0:d07617f8ede9 86 case 2 :
belloula 0:d07617f8ede9 87 case 3 :
belloula 0:d07617f8ede9 88 case 4 :
belloula 0:d07617f8ede9 89 case 5 :
belloula 0:d07617f8ede9 90 case 6 :
belloula 0:d07617f8ede9 91 //case 7 :
belloula 0:d07617f8ede9 92 nbtick++;
belloula 0:d07617f8ede9 93 break;
belloula 0:d07617f8ede9 94 case 7 : up = twi_readlong(WEATHER_BMP085, 0xf6) >> (8 - oss);
belloula 0:d07617f8ede9 95 x1 = (ut - ac6) * ac5 / xpow(2, 15);
belloula 0:d07617f8ede9 96 x2 = (long)mc * xpow(2, 11) / (x1 + md);
belloula 0:d07617f8ede9 97 b5 = x1 + x2;
belloula 0:d07617f8ede9 98 t = (b5 + 8) / xpow(2, 4);
belloula 0:d07617f8ede9 99 temperature = (float)t / 10.0;
belloula 0:d07617f8ede9 100 b6 = b5 - 4000;
belloula 0:d07617f8ede9 101 x1 = (b2 * (b6 * b6 / xpow(2, 12))) / xpow(2, 11);
belloula 0:d07617f8ede9 102 x2 = ac2 * b6 / xpow(2, 11);
belloula 0:d07617f8ede9 103 x3 = x1 + x2;
belloula 0:d07617f8ede9 104 b3 = ((((unsigned long)ac1 * 4 + x3) << oss) + 2) / 4;
belloula 0:d07617f8ede9 105 x1 = ac3 * b6 / xpow(2, 13);
belloula 0:d07617f8ede9 106 x2 = (b1 * (b6 * b6 / xpow(2, 12))) / xpow(2, 16);
belloula 0:d07617f8ede9 107 x3 = ((x1 + x2) + 2) / xpow(2, 2);
belloula 0:d07617f8ede9 108 b4 = ac4 * (unsigned long)(x3 + 32768) / xpow(2, 15);
belloula 0:d07617f8ede9 109 b7 = ((unsigned long)up - b3) * (50000 >> oss);
belloula 0:d07617f8ede9 110 if (b7 < (unsigned long)0x80000000) {
belloula 0:d07617f8ede9 111 p = (b7 * 2) / b4;
belloula 0:d07617f8ede9 112 } else {
belloula 0:d07617f8ede9 113 p = (b7 / b4) * 2;
belloula 0:d07617f8ede9 114 }
belloula 0:d07617f8ede9 115 x1 = (p / xpow(2, 8)) * (p / xpow(2, 8));
belloula 0:d07617f8ede9 116 x1 = (x1 * 3038) / xpow(2, 16);
belloula 0:d07617f8ede9 117 x2 = (-7357 * p) / xpow(2, 16);
belloula 0:d07617f8ede9 118 p = p + (x1 + x2 + 3791) / xpow(2, 4);
belloula 0:d07617f8ede9 119 pressure = (float)p / 100.0;
belloula 0:d07617f8ede9 120 ok=1;
belloula 0:d07617f8ede9 121 nbtick=0;
belloula 0:d07617f8ede9 122 break;
belloula 0:d07617f8ede9 123 default: nbtick=0;
belloula 0:d07617f8ede9 124 break;
belloula 0:d07617f8ede9 125 }
belloula 0:d07617f8ede9 126 /*float To=231; // 298 273,15 + 25
belloula 0:d07617f8ede9 127 float ho=0;
belloula 0:d07617f8ede9 128 float Po=102400; //101325
belloula 0:d07617f8ede9 129 //ecuacion
belloula 0:d07617f8ede9 130 float c=(To-0.0065*ho);
belloula 0:d07617f8ede9 131 float e=(p/Po);
belloula 0:d07617f8ede9 132 float d=exp(0.19082*log(e));
belloula 0:d07617f8ede9 133 float b=c*d;
belloula 0:d07617f8ede9 134 altitude = 153.84615*(To-b);
belloula 0:d07617f8ede9 135
belloula 0:d07617f8ede9 136 /*altitude = (float)44330 * (1 - pow(((float) pressure/Po), 0.190295));
belloula 0:d07617f8ede9 137 float A = pressure/1024.00;
belloula 0:d07617f8ede9 138 float B = 1/5.25588;
belloula 0:d07617f8ede9 139 altitude = pow(A,B);
belloula 0:d07617f8ede9 140 altitude = 1 - altitude;
belloula 0:d07617f8ede9 141 altitude = altitude /0.0000225577;*/
belloula 0:d07617f8ede9 142
belloula 0:d07617f8ede9 143
belloula 0:d07617f8ede9 144
belloula 0:d07617f8ede9 145
belloula 0:d07617f8ede9 146
belloula 0:d07617f8ede9 147 }
belloula 0:d07617f8ede9 148
belloula 0:d07617f8ede9 149 void BMP085::init (BMP085_oss p_oss) {
belloula 0:d07617f8ede9 150 ac1 = twi_readshort(WEATHER_BMP085, 0xaa);
belloula 0:d07617f8ede9 151 ac2 = twi_readshort(WEATHER_BMP085, 0xac);
belloula 0:d07617f8ede9 152 ac3 = twi_readshort(WEATHER_BMP085, 0xae);
belloula 0:d07617f8ede9 153 ac4 = twi_readshort(WEATHER_BMP085, 0xb0);
belloula 0:d07617f8ede9 154 ac5 = twi_readshort(WEATHER_BMP085, 0xb2);
belloula 0:d07617f8ede9 155 ac6 = twi_readshort(WEATHER_BMP085, 0xb4);
belloula 0:d07617f8ede9 156 b1 = twi_readshort(WEATHER_BMP085, 0xb6);
belloula 0:d07617f8ede9 157 b2 = twi_readshort(WEATHER_BMP085, 0xb8);
belloula 0:d07617f8ede9 158 mb = twi_readshort(WEATHER_BMP085, 0xba);
belloula 0:d07617f8ede9 159 mc = twi_readshort(WEATHER_BMP085, 0xbc);
belloula 0:d07617f8ede9 160 md = twi_readshort(WEATHER_BMP085, 0xbe);
belloula 0:d07617f8ede9 161 oss = p_oss;
belloula 0:d07617f8ede9 162 }
belloula 0:d07617f8ede9 163
belloula 0:d07617f8ede9 164 unsigned short BMP085::twi_readshort (int id, int addr) {
belloula 0:d07617f8ede9 165 unsigned short i;
belloula 0:d07617f8ede9 166
belloula 0:d07617f8ede9 167 i2c.start();
belloula 0:d07617f8ede9 168 i2c.write(id);
belloula 0:d07617f8ede9 169 i2c.write(addr);
belloula 0:d07617f8ede9 170
belloula 0:d07617f8ede9 171 i2c.start();
belloula 0:d07617f8ede9 172 i2c.write(id | 1);
belloula 0:d07617f8ede9 173 i = i2c.read(1) << 8;
belloula 0:d07617f8ede9 174 i |= i2c.read(0);
belloula 0:d07617f8ede9 175 i2c.stop();
belloula 0:d07617f8ede9 176
belloula 0:d07617f8ede9 177 return i;
belloula 0:d07617f8ede9 178 }
belloula 0:d07617f8ede9 179
belloula 0:d07617f8ede9 180 unsigned long BMP085::twi_readlong (int id, int addr) {
belloula 0:d07617f8ede9 181 unsigned long i;
belloula 0:d07617f8ede9 182
belloula 0:d07617f8ede9 183 i2c.start();
belloula 0:d07617f8ede9 184 i2c.write(id);
belloula 0:d07617f8ede9 185 i2c.write(addr);
belloula 0:d07617f8ede9 186
belloula 0:d07617f8ede9 187 i2c.start();
belloula 0:d07617f8ede9 188 i2c.write(id | 1);
belloula 0:d07617f8ede9 189 i = i2c.read(1) << 16;
belloula 0:d07617f8ede9 190 i |= i2c.read(1) << 8;
belloula 0:d07617f8ede9 191 i |= i2c.read(0);
belloula 0:d07617f8ede9 192 i2c.stop();
belloula 0:d07617f8ede9 193
belloula 0:d07617f8ede9 194 return i;
belloula 0:d07617f8ede9 195 }
belloula 0:d07617f8ede9 196
belloula 0:d07617f8ede9 197 void BMP085::twi_writechar (int id, int addr, int dat)
belloula 0:d07617f8ede9 198 {
belloula 0:d07617f8ede9 199
belloula 0:d07617f8ede9 200 i2c.start();
belloula 0:d07617f8ede9 201 i2c.write(id);
belloula 0:d07617f8ede9 202 i2c.write(addr);
belloula 0:d07617f8ede9 203 i2c.write(dat);
belloula 0:d07617f8ede9 204 i2c.stop();
belloula 0:d07617f8ede9 205 }
belloula 0:d07617f8ede9 206