東北大学学友会準加盟団体 From The Earth の高高度ロケットFTE-06(通称:海豚)にて使用したソフトウェアです.ご自由にお使いください.このプログラムによって生じた損害について当団体は一切責任を負いません.また,各モジュールのライブラリは当団体が作成したものではないので再配布は禁止します.
Dependencies: mbed FATFileSystem
Fork of FTE-06 by
BMP085/BMP085.cpp@57:d78497a079cf, 2017-08-25 (annotated)
- Committer:
- mizuki_akaike
- Date:
- Fri Aug 25 03:42:30 2017 +0000
- Revision:
- 57:d78497a079cf
- Parent:
- 23:9be034083282
??????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mizuki_akaike | 23:9be034083282 | 1 | /* |
mizuki_akaike | 23:9be034083282 | 2 | * mbed library to use a Bosch Sensortec BMP085/BMP180 sensor |
mizuki_akaike | 23:9be034083282 | 3 | * Copyright (c) 2010 Hiroshi Suga |
mizuki_akaike | 23:9be034083282 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
mizuki_akaike | 23:9be034083282 | 5 | */ |
mizuki_akaike | 23:9be034083282 | 6 | |
mizuki_akaike | 23:9be034083282 | 7 | /** @file BMP085.cpp |
mizuki_akaike | 23:9be034083282 | 8 | * @brief mbed library to use a Bosch Sensortec BMP085/BMP180 sensor |
mizuki_akaike | 23:9be034083282 | 9 | * barometric pressure sensor BMP085/BMP180 (Bosch Sensortec) |
mizuki_akaike | 23:9be034083282 | 10 | * interface: I2C digital |
mizuki_akaike | 23:9be034083282 | 11 | */ |
mizuki_akaike | 23:9be034083282 | 12 | |
mizuki_akaike | 23:9be034083282 | 13 | #include "mbed.h" |
mizuki_akaike | 23:9be034083282 | 14 | #include "BMP085.h" |
mizuki_akaike | 23:9be034083282 | 15 | |
mizuki_akaike | 23:9be034083282 | 16 | #define WEATHER_BMP085 0xee |
mizuki_akaike | 23:9be034083282 | 17 | #define xpow(x, y) ((long)1 << y) |
mizuki_akaike | 23:9be034083282 | 18 | |
mizuki_akaike | 23:9be034083282 | 19 | /** |
mizuki_akaike | 23:9be034083282 | 20 | * @brief Initializes interface (private I2C) |
mizuki_akaike | 23:9be034083282 | 21 | * @param p_sda port of I2C SDA |
mizuki_akaike | 23:9be034083282 | 22 | * @param p_scl port of I2C SCL |
mizuki_akaike | 23:9be034083282 | 23 | * @param p_oss parameter of OSS |
mizuki_akaike | 23:9be034083282 | 24 | */ |
mizuki_akaike | 23:9be034083282 | 25 | BMP085::BMP085 (PinName p_sda, PinName p_scl, BMP085_oss p_oss) : i2c(p_sda, p_scl) { |
mizuki_akaike | 23:9be034083282 | 26 | init(p_oss); |
mizuki_akaike | 23:9be034083282 | 27 | } |
mizuki_akaike | 23:9be034083282 | 28 | |
mizuki_akaike | 23:9be034083282 | 29 | /** |
mizuki_akaike | 23:9be034083282 | 30 | * @brief Initializes interface (public I2C) |
mizuki_akaike | 23:9be034083282 | 31 | * @param p_i2c instance of I2C class |
mizuki_akaike | 23:9be034083282 | 32 | * @param p_oss parameter of OSS |
mizuki_akaike | 23:9be034083282 | 33 | */ |
mizuki_akaike | 23:9be034083282 | 34 | BMP085::BMP085 (I2C& p_i2c, BMP085_oss p_oss) : i2c(p_i2c) { |
mizuki_akaike | 23:9be034083282 | 35 | init(p_oss); |
mizuki_akaike | 23:9be034083282 | 36 | } |
mizuki_akaike | 23:9be034083282 | 37 | |
mizuki_akaike | 23:9be034083282 | 38 | /** |
mizuki_akaike | 23:9be034083282 | 39 | * @brief Get temperature |
mizuki_akaike | 23:9be034083282 | 40 | * @return temperature (`C) |
mizuki_akaike | 23:9be034083282 | 41 | */ |
mizuki_akaike | 23:9be034083282 | 42 | float BMP085::get_temperature() { |
mizuki_akaike | 23:9be034083282 | 43 | return temperature; |
mizuki_akaike | 23:9be034083282 | 44 | } |
mizuki_akaike | 23:9be034083282 | 45 | |
mizuki_akaike | 23:9be034083282 | 46 | /** |
mizuki_akaike | 23:9be034083282 | 47 | * @brief Get pressure |
mizuki_akaike | 23:9be034083282 | 48 | * @return pressure (hPa) |
mizuki_akaike | 23:9be034083282 | 49 | */ |
mizuki_akaike | 23:9be034083282 | 50 | float BMP085::get_pressure() { |
mizuki_akaike | 23:9be034083282 | 51 | return pressure; |
mizuki_akaike | 23:9be034083282 | 52 | } |
mizuki_akaike | 23:9be034083282 | 53 | |
mizuki_akaike | 23:9be034083282 | 54 | /** |
mizuki_akaike | 23:9be034083282 | 55 | * @brief Update results |
mizuki_akaike | 23:9be034083282 | 56 | */ |
mizuki_akaike | 23:9be034083282 | 57 | void BMP085::update () { |
mizuki_akaike | 23:9be034083282 | 58 | long t, p, ut, up, x1, x2, x3, b3, b5, b6; |
mizuki_akaike | 23:9be034083282 | 59 | unsigned long b4, b7; |
mizuki_akaike | 23:9be034083282 | 60 | |
mizuki_akaike | 23:9be034083282 | 61 | twi_writechar(WEATHER_BMP085, 0xf4, 0x2e); |
mizuki_akaike | 23:9be034083282 | 62 | wait(0.01); |
mizuki_akaike | 23:9be034083282 | 63 | ut = twi_readshort(WEATHER_BMP085, 0xf6); |
mizuki_akaike | 23:9be034083282 | 64 | |
mizuki_akaike | 23:9be034083282 | 65 | twi_writechar(WEATHER_BMP085, 0xf4, 0x34 | (oss << 6)); |
mizuki_akaike | 23:9be034083282 | 66 | wait(0.05); |
mizuki_akaike | 23:9be034083282 | 67 | up = twi_readlong(WEATHER_BMP085, 0xf6) >> (8 - oss); |
mizuki_akaike | 23:9be034083282 | 68 | |
mizuki_akaike | 23:9be034083282 | 69 | x1 = (ut - ac6) * ac5 / xpow(2, 15); |
mizuki_akaike | 23:9be034083282 | 70 | x2 = (long)mc * xpow(2, 11) / (x1 + md); |
mizuki_akaike | 23:9be034083282 | 71 | b5 = x1 + x2; |
mizuki_akaike | 23:9be034083282 | 72 | t = (b5 + 8) / xpow(2, 4); |
mizuki_akaike | 23:9be034083282 | 73 | temperature = (float)t / 10.0; |
mizuki_akaike | 23:9be034083282 | 74 | |
mizuki_akaike | 23:9be034083282 | 75 | b6 = b5 - 4000; |
mizuki_akaike | 23:9be034083282 | 76 | x1 = (b2 * (b6 * b6 / xpow(2, 12))) / xpow(2, 11); |
mizuki_akaike | 23:9be034083282 | 77 | x2 = ac2 * b6 / xpow(2, 11); |
mizuki_akaike | 23:9be034083282 | 78 | x3 = x1 + x2; |
mizuki_akaike | 23:9be034083282 | 79 | b3 = ((((unsigned long)ac1 * 4 + x3) << oss) + 2) / 4; |
mizuki_akaike | 23:9be034083282 | 80 | x1 = ac3 * b6 / xpow(2, 13); |
mizuki_akaike | 23:9be034083282 | 81 | x2 = (b1 * (b6 * b6 / xpow(2, 12))) / xpow(2, 16); |
mizuki_akaike | 23:9be034083282 | 82 | x3 = ((x1 + x2) + 2) / xpow(2, 2); |
mizuki_akaike | 23:9be034083282 | 83 | b4 = ac4 * (unsigned long)(x3 + 32768) / xpow(2, 15); |
mizuki_akaike | 23:9be034083282 | 84 | b7 = ((unsigned long)up - b3) * (50000 >> oss); |
mizuki_akaike | 23:9be034083282 | 85 | if (b7 < (unsigned long)0x80000000) { |
mizuki_akaike | 23:9be034083282 | 86 | p = (b7 * 2) / b4; |
mizuki_akaike | 23:9be034083282 | 87 | } else { |
mizuki_akaike | 23:9be034083282 | 88 | p = (b7 / b4) * 2; |
mizuki_akaike | 23:9be034083282 | 89 | } |
mizuki_akaike | 23:9be034083282 | 90 | x1 = (p / xpow(2, 8)) * (p / xpow(2, 8)); |
mizuki_akaike | 23:9be034083282 | 91 | x1 = (x1 * 3038) / xpow(2, 16); |
mizuki_akaike | 23:9be034083282 | 92 | x2 = (-7357 * p) / xpow(2, 16); |
mizuki_akaike | 23:9be034083282 | 93 | p = p + (x1 + x2 + 3791) / xpow(2, 4); |
mizuki_akaike | 23:9be034083282 | 94 | pressure = (float)p / 100.0; |
mizuki_akaike | 23:9be034083282 | 95 | } |
mizuki_akaike | 23:9be034083282 | 96 | |
mizuki_akaike | 23:9be034083282 | 97 | void BMP085::init (BMP085_oss p_oss) { |
mizuki_akaike | 23:9be034083282 | 98 | ac1 = twi_readshort(WEATHER_BMP085, 0xaa); |
mizuki_akaike | 23:9be034083282 | 99 | ac2 = twi_readshort(WEATHER_BMP085, 0xac); |
mizuki_akaike | 23:9be034083282 | 100 | ac3 = twi_readshort(WEATHER_BMP085, 0xae); |
mizuki_akaike | 23:9be034083282 | 101 | ac4 = twi_readshort(WEATHER_BMP085, 0xb0); |
mizuki_akaike | 23:9be034083282 | 102 | ac5 = twi_readshort(WEATHER_BMP085, 0xb2); |
mizuki_akaike | 23:9be034083282 | 103 | ac6 = twi_readshort(WEATHER_BMP085, 0xb4); |
mizuki_akaike | 23:9be034083282 | 104 | b1 = twi_readshort(WEATHER_BMP085, 0xb6); |
mizuki_akaike | 23:9be034083282 | 105 | b2 = twi_readshort(WEATHER_BMP085, 0xb8); |
mizuki_akaike | 23:9be034083282 | 106 | mb = twi_readshort(WEATHER_BMP085, 0xba); |
mizuki_akaike | 23:9be034083282 | 107 | mc = twi_readshort(WEATHER_BMP085, 0xbc); |
mizuki_akaike | 23:9be034083282 | 108 | md = twi_readshort(WEATHER_BMP085, 0xbe); |
mizuki_akaike | 23:9be034083282 | 109 | oss = p_oss; |
mizuki_akaike | 23:9be034083282 | 110 | } |
mizuki_akaike | 23:9be034083282 | 111 | |
mizuki_akaike | 23:9be034083282 | 112 | unsigned short BMP085::twi_readshort (int id, int addr) { |
mizuki_akaike | 23:9be034083282 | 113 | unsigned short i; |
mizuki_akaike | 23:9be034083282 | 114 | |
mizuki_akaike | 23:9be034083282 | 115 | i2c.start(); |
mizuki_akaike | 23:9be034083282 | 116 | i2c.write(id); |
mizuki_akaike | 23:9be034083282 | 117 | i2c.write(addr); |
mizuki_akaike | 23:9be034083282 | 118 | |
mizuki_akaike | 23:9be034083282 | 119 | i2c.start(); |
mizuki_akaike | 23:9be034083282 | 120 | i2c.write(id | 1); |
mizuki_akaike | 23:9be034083282 | 121 | i = i2c.read(1) << 8; |
mizuki_akaike | 23:9be034083282 | 122 | i |= i2c.read(0); |
mizuki_akaike | 23:9be034083282 | 123 | i2c.stop(); |
mizuki_akaike | 23:9be034083282 | 124 | |
mizuki_akaike | 23:9be034083282 | 125 | return i; |
mizuki_akaike | 23:9be034083282 | 126 | } |
mizuki_akaike | 23:9be034083282 | 127 | |
mizuki_akaike | 23:9be034083282 | 128 | unsigned long BMP085::twi_readlong (int id, int addr) { |
mizuki_akaike | 23:9be034083282 | 129 | unsigned long i; |
mizuki_akaike | 23:9be034083282 | 130 | |
mizuki_akaike | 23:9be034083282 | 131 | i2c.start(); |
mizuki_akaike | 23:9be034083282 | 132 | i2c.write(id); |
mizuki_akaike | 23:9be034083282 | 133 | i2c.write(addr); |
mizuki_akaike | 23:9be034083282 | 134 | |
mizuki_akaike | 23:9be034083282 | 135 | i2c.start(); |
mizuki_akaike | 23:9be034083282 | 136 | i2c.write(id | 1); |
mizuki_akaike | 23:9be034083282 | 137 | i = i2c.read(1) << 16; |
mizuki_akaike | 23:9be034083282 | 138 | i |= i2c.read(1) << 8; |
mizuki_akaike | 23:9be034083282 | 139 | i |= i2c.read(0); |
mizuki_akaike | 23:9be034083282 | 140 | i2c.stop(); |
mizuki_akaike | 23:9be034083282 | 141 | |
mizuki_akaike | 23:9be034083282 | 142 | return i; |
mizuki_akaike | 23:9be034083282 | 143 | } |
mizuki_akaike | 23:9be034083282 | 144 | |
mizuki_akaike | 23:9be034083282 | 145 | void BMP085::twi_writechar (int id, int addr, int dat) { |
mizuki_akaike | 23:9be034083282 | 146 | |
mizuki_akaike | 23:9be034083282 | 147 | i2c.start(); |
mizuki_akaike | 23:9be034083282 | 148 | i2c.write(id); |
mizuki_akaike | 23:9be034083282 | 149 | i2c.write(addr); |
mizuki_akaike | 23:9be034083282 | 150 | i2c.write(dat); |
mizuki_akaike | 23:9be034083282 | 151 | i2c.stop(); |
mizuki_akaike | 23:9be034083282 | 152 | } |