ok
BMP085.cpp@0:b9870f4bc956, 2014-06-05 (annotated)
- Committer:
- stepJun
- Date:
- Thu Jun 05 12:02:46 2014 +0000
- Revision:
- 0:b9870f4bc956
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stepJun | 0:b9870f4bc956 | 1 | /* |
stepJun | 0:b9870f4bc956 | 2 | * mbed library to use a Bosch Sensortec BMP085 sensor |
stepJun | 0:b9870f4bc956 | 3 | * Copyright (c) 2010 Hiroshi Suga |
stepJun | 0:b9870f4bc956 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
stepJun | 0:b9870f4bc956 | 5 | */ |
stepJun | 0:b9870f4bc956 | 6 | |
stepJun | 0:b9870f4bc956 | 7 | #include "mbed.h" |
stepJun | 0:b9870f4bc956 | 8 | #include "BMP085.h" |
stepJun | 0:b9870f4bc956 | 9 | |
stepJun | 0:b9870f4bc956 | 10 | #define WEATHER_BMP085 0xee |
stepJun | 0:b9870f4bc956 | 11 | #define xpow(x, y) ((long)1 << y) |
stepJun | 0:b9870f4bc956 | 12 | |
stepJun | 0:b9870f4bc956 | 13 | |
stepJun | 0:b9870f4bc956 | 14 | BMP085::BMP085 (PinName p_sda, PinName p_scl, BMP085_oss p_oss) : i2c(p_sda, p_scl) { |
stepJun | 0:b9870f4bc956 | 15 | init(p_oss); |
stepJun | 0:b9870f4bc956 | 16 | } |
stepJun | 0:b9870f4bc956 | 17 | |
stepJun | 0:b9870f4bc956 | 18 | BMP085::BMP085 (I2C& p_i2c, BMP085_oss p_oss) : i2c(p_i2c) { |
stepJun | 0:b9870f4bc956 | 19 | init(p_oss); |
stepJun | 0:b9870f4bc956 | 20 | } |
stepJun | 0:b9870f4bc956 | 21 | |
stepJun | 0:b9870f4bc956 | 22 | float BMP085::get_temperature() { |
stepJun | 0:b9870f4bc956 | 23 | return temperature; |
stepJun | 0:b9870f4bc956 | 24 | } |
stepJun | 0:b9870f4bc956 | 25 | |
stepJun | 0:b9870f4bc956 | 26 | long BMP085::get_pressure() { |
stepJun | 0:b9870f4bc956 | 27 | return pressure; |
stepJun | 0:b9870f4bc956 | 28 | } |
stepJun | 0:b9870f4bc956 | 29 | |
stepJun | 0:b9870f4bc956 | 30 | void BMP085::update () { |
stepJun | 0:b9870f4bc956 | 31 | long t, p, ut, up, x1, x2, x3, b3, b5, b6; |
stepJun | 0:b9870f4bc956 | 32 | unsigned long b4, b7; |
stepJun | 0:b9870f4bc956 | 33 | |
stepJun | 0:b9870f4bc956 | 34 | twi_writechar(WEATHER_BMP085, 0xf4, 0x2e); |
stepJun | 0:b9870f4bc956 | 35 | wait(0.0045); |
stepJun | 0:b9870f4bc956 | 36 | ut = twi_readshort(WEATHER_BMP085, 0xf6); |
stepJun | 0:b9870f4bc956 | 37 | |
stepJun | 0:b9870f4bc956 | 38 | twi_writechar(WEATHER_BMP085, 0xf4, 0x34 | (oss << 6)); |
stepJun | 0:b9870f4bc956 | 39 | wait(0.0075); |
stepJun | 0:b9870f4bc956 | 40 | up = twi_readlong(WEATHER_BMP085, 0xf6) >> (8 - oss); |
stepJun | 0:b9870f4bc956 | 41 | |
stepJun | 0:b9870f4bc956 | 42 | x1 = (ut - ac6) * ac5 / xpow(2, 15); |
stepJun | 0:b9870f4bc956 | 43 | x2 = (long)mc * xpow(2, 11) / (x1 + md); |
stepJun | 0:b9870f4bc956 | 44 | b5 = x1 + x2; |
stepJun | 0:b9870f4bc956 | 45 | t = (b5 + 8) / xpow(2, 4); |
stepJun | 0:b9870f4bc956 | 46 | temperature = (float)t / 10.0; |
stepJun | 0:b9870f4bc956 | 47 | |
stepJun | 0:b9870f4bc956 | 48 | b6 = b5 - 4000; |
stepJun | 0:b9870f4bc956 | 49 | x1 = (b2 * (b6 * b6 / xpow(2, 12))) / xpow(2, 11); |
stepJun | 0:b9870f4bc956 | 50 | x2 = ac2 * b6 / xpow(2, 11); |
stepJun | 0:b9870f4bc956 | 51 | x3 = x1 + x2; |
stepJun | 0:b9870f4bc956 | 52 | b3 = ((((unsigned long)ac1 * 4 + x3) << oss) + 2) / 4; |
stepJun | 0:b9870f4bc956 | 53 | x1 = ac3 * b6 / xpow(2, 13); |
stepJun | 0:b9870f4bc956 | 54 | x2 = (b1 * (b6 * b6 / xpow(2, 12))) / xpow(2, 16); |
stepJun | 0:b9870f4bc956 | 55 | x3 = ((x1 + x2) + 2) / xpow(2, 2); |
stepJun | 0:b9870f4bc956 | 56 | b4 = ac4 * (unsigned long)(x3 + 32768) / xpow(2, 15); |
stepJun | 0:b9870f4bc956 | 57 | b7 = ((unsigned long)up - b3) * (50000 >> oss); |
stepJun | 0:b9870f4bc956 | 58 | if (b7 < (unsigned long)0x80000000) { |
stepJun | 0:b9870f4bc956 | 59 | p = (b7 * 2) / b4; |
stepJun | 0:b9870f4bc956 | 60 | } else { |
stepJun | 0:b9870f4bc956 | 61 | p = (b7 / b4) * 2; |
stepJun | 0:b9870f4bc956 | 62 | } |
stepJun | 0:b9870f4bc956 | 63 | x1 = (p / xpow(2, 8)) * (p / xpow(2, 8)); |
stepJun | 0:b9870f4bc956 | 64 | x1 = (x1 * 3038) / xpow(2, 16); |
stepJun | 0:b9870f4bc956 | 65 | x2 = (-7357 * p) / xpow(2, 16); |
stepJun | 0:b9870f4bc956 | 66 | p = p + (x1 + x2 + 3791) / xpow(2, 4); |
stepJun | 0:b9870f4bc956 | 67 | pressure = (long)p; |
stepJun | 0:b9870f4bc956 | 68 | } |
stepJun | 0:b9870f4bc956 | 69 | |
stepJun | 0:b9870f4bc956 | 70 | void BMP085::init (BMP085_oss p_oss) { |
stepJun | 0:b9870f4bc956 | 71 | ac1 = twi_readshort(WEATHER_BMP085, 0xaa); |
stepJun | 0:b9870f4bc956 | 72 | ac2 = twi_readshort(WEATHER_BMP085, 0xac); |
stepJun | 0:b9870f4bc956 | 73 | ac3 = twi_readshort(WEATHER_BMP085, 0xae); |
stepJun | 0:b9870f4bc956 | 74 | ac4 = twi_readshort(WEATHER_BMP085, 0xb0); |
stepJun | 0:b9870f4bc956 | 75 | ac5 = twi_readshort(WEATHER_BMP085, 0xb2); |
stepJun | 0:b9870f4bc956 | 76 | ac6 = twi_readshort(WEATHER_BMP085, 0xb4); |
stepJun | 0:b9870f4bc956 | 77 | b1 = twi_readshort(WEATHER_BMP085, 0xb6); |
stepJun | 0:b9870f4bc956 | 78 | b2 = twi_readshort(WEATHER_BMP085, 0xb8); |
stepJun | 0:b9870f4bc956 | 79 | mb = twi_readshort(WEATHER_BMP085, 0xba); |
stepJun | 0:b9870f4bc956 | 80 | mc = twi_readshort(WEATHER_BMP085, 0xbc); |
stepJun | 0:b9870f4bc956 | 81 | md = twi_readshort(WEATHER_BMP085, 0xbe); |
stepJun | 0:b9870f4bc956 | 82 | oss = p_oss; |
stepJun | 0:b9870f4bc956 | 83 | } |
stepJun | 0:b9870f4bc956 | 84 | |
stepJun | 0:b9870f4bc956 | 85 | unsigned short BMP085::twi_readshort (int id, int addr) { |
stepJun | 0:b9870f4bc956 | 86 | unsigned short i; |
stepJun | 0:b9870f4bc956 | 87 | |
stepJun | 0:b9870f4bc956 | 88 | i2c.start(); |
stepJun | 0:b9870f4bc956 | 89 | i2c.write(id); |
stepJun | 0:b9870f4bc956 | 90 | i2c.write(addr); |
stepJun | 0:b9870f4bc956 | 91 | |
stepJun | 0:b9870f4bc956 | 92 | i2c.start(); |
stepJun | 0:b9870f4bc956 | 93 | i2c.write(id | 1); |
stepJun | 0:b9870f4bc956 | 94 | i = i2c.read(1) << 8; |
stepJun | 0:b9870f4bc956 | 95 | i |= i2c.read(0); |
stepJun | 0:b9870f4bc956 | 96 | i2c.stop(); |
stepJun | 0:b9870f4bc956 | 97 | |
stepJun | 0:b9870f4bc956 | 98 | return i; |
stepJun | 0:b9870f4bc956 | 99 | } |
stepJun | 0:b9870f4bc956 | 100 | |
stepJun | 0:b9870f4bc956 | 101 | unsigned long BMP085::twi_readlong (int id, int addr) { |
stepJun | 0:b9870f4bc956 | 102 | unsigned long i; |
stepJun | 0:b9870f4bc956 | 103 | |
stepJun | 0:b9870f4bc956 | 104 | i2c.start(); |
stepJun | 0:b9870f4bc956 | 105 | i2c.write(id); |
stepJun | 0:b9870f4bc956 | 106 | i2c.write(addr); |
stepJun | 0:b9870f4bc956 | 107 | |
stepJun | 0:b9870f4bc956 | 108 | i2c.start(); |
stepJun | 0:b9870f4bc956 | 109 | i2c.write(id | 1); |
stepJun | 0:b9870f4bc956 | 110 | i = i2c.read(1) << 16; |
stepJun | 0:b9870f4bc956 | 111 | i |= i2c.read(1) << 8; |
stepJun | 0:b9870f4bc956 | 112 | i |= i2c.read(0); |
stepJun | 0:b9870f4bc956 | 113 | i2c.stop(); |
stepJun | 0:b9870f4bc956 | 114 | |
stepJun | 0:b9870f4bc956 | 115 | return i; |
stepJun | 0:b9870f4bc956 | 116 | } |
stepJun | 0:b9870f4bc956 | 117 | |
stepJun | 0:b9870f4bc956 | 118 | void BMP085::twi_writechar (int id, int addr, int dat) { |
stepJun | 0:b9870f4bc956 | 119 | |
stepJun | 0:b9870f4bc956 | 120 | i2c.start(); |
stepJun | 0:b9870f4bc956 | 121 | i2c.write(id); |
stepJun | 0:b9870f4bc956 | 122 | i2c.write(addr); |
stepJun | 0:b9870f4bc956 | 123 | i2c.write(dat); |
stepJun | 0:b9870f4bc956 | 124 | i2c.stop(); |
stepJun | 0:b9870f4bc956 | 125 | } |