![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
demo program of BMP085 pressure sensor
main.cpp@2:8c00953d4755, 2012-11-06 (annotated)
- Committer:
- tylerjw
- Date:
- Tue Nov 06 02:39:40 2012 +0000
- Revision:
- 2:8c00953d4755
- Parent:
- 1:2935199329b2
- Child:
- 3:ae2b8103e52b
library initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tylerjw | 2:8c00953d4755 | 1 | /* |
tylerjw | 2:8c00953d4755 | 2 | * |
tylerjw | 2:8c00953d4755 | 3 | * 1.8 - 3.6V (Vdd) |
tylerjw | 2:8c00953d4755 | 4 | * 1.62 - 3.6 (Vddio) |
tylerjw | 2:8c00953d4755 | 5 | * |
tylerjw | 2:8c00953d4755 | 6 | * |
tylerjw | 2:8c00953d4755 | 7 | *Altitude = 44330*(1-(p/p0)^(1/5.255)) |
tylerjw | 2:8c00953d4755 | 8 | * set p0 to sealevel pressure |
tylerjw | 2:8c00953d4755 | 9 | * delta p = 1hPa = 8.43m at sea level |
tylerjw | 2:8c00953d4755 | 10 | * |
tylerjw | 2:8c00953d4755 | 11 | */ |
tylerjw | 2:8c00953d4755 | 12 | |
tylerjw | 2:8c00953d4755 | 13 | /* |
tylerjw | 2:8c00953d4755 | 14 | *Pinout: |
tylerjw | 2:8c00953d4755 | 15 | *pin9 = SDA |
tylerjw | 2:8c00953d4755 | 16 | *pin10 = SCL |
tylerjw | 2:8c00953d4755 | 17 | *pin11 = XCLR (digital out; active low; Resets sensor) |
tylerjw | 2:8c00953d4755 | 18 | *pin12 = EOC ("end of conversation"; signal when conversion finished) |
tylerjw | 2:8c00953d4755 | 19 | * |
tylerjw | 2:8c00953d4755 | 20 | */ |
tylerjw | 2:8c00953d4755 | 21 | |
tylerjw | 2:8c00953d4755 | 22 | #include "mbed.h" |
tylerjw | 2:8c00953d4755 | 23 | #include "BMP085.h" |
tylerjw | 2:8c00953d4755 | 24 | |
tylerjw | 2:8c00953d4755 | 25 | I2C i2c(p9, p10); // sda, scl |
tylerjw | 2:8c00953d4755 | 26 | BMP085 alt_sensor(i2c); |
tylerjw | 2:8c00953d4755 | 27 | Serial pc(USBTX, USBRX); // tx, rx |
tylerjw | 2:8c00953d4755 | 28 | |
tylerjw | 2:8c00953d4755 | 29 | /* |
tylerjw | 2:8c00953d4755 | 30 | const int bmp085_address = 0xEE; //address of bmp085 |
tylerjw | 2:8c00953d4755 | 31 | const int P0 = 101325; //pressure at sea level |
tylerjw | 2:8c00953d4755 | 32 | |
tylerjw | 2:8c00953d4755 | 33 | void bmp085_calibration(void); |
tylerjw | 2:8c00953d4755 | 34 | void display_calibration(void); |
tylerjw | 2:8c00953d4755 | 35 | unsigned short read_short(int,int); |
tylerjw | 2:8c00953d4755 | 36 | |
tylerjw | 2:8c00953d4755 | 37 | void write_char(int,int,int); |
tylerjw | 2:8c00953d4755 | 38 | |
tylerjw | 2:8c00953d4755 | 39 | long get_raw_temp(void); |
tylerjw | 2:8c00953d4755 | 40 | void calculations(long); |
tylerjw | 2:8c00953d4755 | 41 | |
tylerjw | 2:8c00953d4755 | 42 | float get_temperature(void); |
tylerjw | 2:8c00953d4755 | 43 | long get_pressure(void); |
tylerjw | 2:8c00953d4755 | 44 | float get_altitude(long); |
tylerjw | 2:8c00953d4755 | 45 | */ |
tylerjw | 2:8c00953d4755 | 46 | |
tylerjw | 2:8c00953d4755 | 47 | //short AC1, AC2, AC3, B1, B2, MB, MC, MD, OSS; |
tylerjw | 2:8c00953d4755 | 48 | //unsigned short AC4, AC5, AC6; |
tylerjw | 2:8c00953d4755 | 49 | |
tylerjw | 2:8c00953d4755 | 50 | int main() |
tylerjw | 2:8c00953d4755 | 51 | { |
tylerjw | 2:8c00953d4755 | 52 | pc.baud(9600); |
tylerjw | 2:8c00953d4755 | 53 | //alt_sensor.display_cal_param(&pc); |
tylerjw | 2:8c00953d4755 | 54 | while(1) |
tylerjw | 2:8c00953d4755 | 55 | { |
tylerjw | 2:8c00953d4755 | 56 | pc.printf("Temperature: %d\r\n", alt_sensor.get_temperature()); |
tylerjw | 2:8c00953d4755 | 57 | pc.printf("Pressure: %d\r\n", alt_sensor.get_pressure()); |
tylerjw | 2:8c00953d4755 | 58 | pc.printf("Altitude: %f\r\n", alt_sensor.get_altitude_ft()); |
tylerjw | 2:8c00953d4755 | 59 | |
tylerjw | 2:8c00953d4755 | 60 | wait(0.5); |
tylerjw | 2:8c00953d4755 | 61 | } |
tylerjw | 2:8c00953d4755 | 62 | //Initialize |
tylerjw | 2:8c00953d4755 | 63 | //OSS = STANDARD; //change between enums under bmp085_oss for desired Sampling resolution |
tylerjw | 2:8c00953d4755 | 64 | |
tylerjw | 2:8c00953d4755 | 65 | //calibrate |
tylerjw | 2:8c00953d4755 | 66 | //bmp085_calibration(); |
tylerjw | 2:8c00953d4755 | 67 | //display_calibration(); |
tylerjw | 2:8c00953d4755 | 68 | |
tylerjw | 2:8c00953d4755 | 69 | //long raw_temp = get_raw_temp(); |
tylerjw | 2:8c00953d4755 | 70 | |
tylerjw | 2:8c00953d4755 | 71 | //calculations(raw_temp); |
tylerjw | 2:8c00953d4755 | 72 | |
tylerjw | 2:8c00953d4755 | 73 | //float altitude = get_altitude(pressure); |
tylerjw | 2:8c00953d4755 | 74 | |
tylerjw | 2:8c00953d4755 | 75 | //pc.printf("Temperature: %f\n",temperature); |
tylerjw | 2:8c00953d4755 | 76 | //pc.printf("Pressure: %l\n", pressure); |
tylerjw | 2:8c00953d4755 | 77 | //pc.printf("Altitude: %f\n", altitude); |
tylerjw | 2:8c00953d4755 | 78 | } |
tylerjw | 2:8c00953d4755 | 79 | /* |
tylerjw | 2:8c00953d4755 | 80 | long get_raw_temp(void) |
tylerjw | 2:8c00953d4755 | 81 | { |
tylerjw | 2:8c00953d4755 | 82 | long raw_temp; |
tylerjw | 2:8c00953d4755 | 83 | |
tylerjw | 2:8c00953d4755 | 84 | //Read raw temperature value |
tylerjw | 2:8c00953d4755 | 85 | write_char(bmp085_address, 0xF4, 0x2E); |
tylerjw | 2:8c00953d4755 | 86 | wait_ms(4.5); |
tylerjw | 2:8c00953d4755 | 87 | raw_temp = read_short(bmp085_address, 0xF6); |
tylerjw | 2:8c00953d4755 | 88 | |
tylerjw | 2:8c00953d4755 | 89 | return raw_temp; |
tylerjw | 2:8c00953d4755 | 90 | } |
tylerjw | 2:8c00953d4755 | 91 | |
tylerjw | 2:8c00953d4755 | 92 | void calculations(long raw_temp) |
tylerjw | 2:8c00953d4755 | 93 | { |
tylerjw | 2:8c00953d4755 | 94 | long X1, X2, B5; //temperature; |
tylerjw | 2:8c00953d4755 | 95 | |
tylerjw | 2:8c00953d4755 | 96 | //Start temperature calculation |
tylerjw | 2:8c00953d4755 | 97 | X1 = (((long)raw_temp - (long)AC6) * (long)AC5) >> 15; |
tylerjw | 2:8c00953d4755 | 98 | X2 = ((long)MC << 11) / (X1 + MD); |
tylerjw | 2:8c00953d4755 | 99 | B5 = X1 + X2; |
tylerjw | 2:8c00953d4755 | 100 | temperature = ((B5 + 8) >> 4); |
tylerjw | 2:8c00953d4755 | 101 | |
tylerjw | 2:8c00953d4755 | 102 | temperature = ((float)temperature / 10.0); |
tylerjw | 2:8c00953d4755 | 103 | |
tylerjw | 2:8c00953d4755 | 104 | pc.printf("Temperature: %f\n",temperature); |
tylerjw | 2:8c00953d4755 | 105 | |
tylerjw | 2:8c00953d4755 | 106 | |
tylerjw | 2:8c00953d4755 | 107 | long raw_pressure, B6, B3, X3; |
tylerjw | 2:8c00953d4755 | 108 | unsigned long B4, B7; |
tylerjw | 2:8c00953d4755 | 109 | |
tylerjw | 2:8c00953d4755 | 110 | //Read raw pressure value |
tylerjw | 2:8c00953d4755 | 111 | write_char(bmp085_address, 0xf4, 0x34 | (OSS << 6)); |
tylerjw | 2:8c00953d4755 | 112 | wait_ms(5); |
tylerjw | 2:8c00953d4755 | 113 | raw_pressure = read_short(bmp085_address, 0xF6) >> (8 - OSS); |
tylerjw | 2:8c00953d4755 | 114 | |
tylerjw | 2:8c00953d4755 | 115 | //Start Pressure calculation |
tylerjw | 2:8c00953d4755 | 116 | B6 = B5 - 4000; |
tylerjw | 2:8c00953d4755 | 117 | X1 = (B2 * (B6 * B6) >> 12) >> 11; |
tylerjw | 2:8c00953d4755 | 118 | X2 = (AC2 * B6) >> 11; |
tylerjw | 2:8c00953d4755 | 119 | X3 = X1 + X2; |
tylerjw | 2:8c00953d4755 | 120 | B3 = (((AC1 * 4 + X3) << OSS + 2) / 4); |
tylerjw | 2:8c00953d4755 | 121 | X1 = (AC3 * B6) >> 13; |
tylerjw | 2:8c00953d4755 | 122 | X2 = (B1 * ((B6 * B6) >> 12)) >> 16; |
tylerjw | 2:8c00953d4755 | 123 | X3 = ((X1 + X2) + 2) >> 2; |
tylerjw | 2:8c00953d4755 | 124 | B4 = (AC4 * (unsigned long)(X3 + 32768)) >> 15; |
tylerjw | 2:8c00953d4755 | 125 | B7 = ((unsigned long)(raw_pressure - B3) * (50000 >> OSS)); |
tylerjw | 2:8c00953d4755 | 126 | if (B7 < 0x80000000) |
tylerjw | 2:8c00953d4755 | 127 | pressure = (B7 *2) / B4; |
tylerjw | 2:8c00953d4755 | 128 | else |
tylerjw | 2:8c00953d4755 | 129 | pressure = (B7 / B4) * 2; |
tylerjw | 2:8c00953d4755 | 130 | |
tylerjw | 2:8c00953d4755 | 131 | X1 = (pressure >> 8) * (pressure >>8); |
tylerjw | 2:8c00953d4755 | 132 | X1 = (X1 * 3038) >>16; |
tylerjw | 2:8c00953d4755 | 133 | X2 = (-7357 * pressure) >>16; |
tylerjw | 2:8c00953d4755 | 134 | pressure += (X1 + X2 + 3791)>>4; |
tylerjw | 2:8c00953d4755 | 135 | |
tylerjw | 2:8c00953d4755 | 136 | pc.printf("Pressure: %f\n", pressure); |
tylerjw | 2:8c00953d4755 | 137 | |
tylerjw | 2:8c00953d4755 | 138 | } |
tylerjw | 2:8c00953d4755 | 139 | |
tylerjw | 2:8c00953d4755 | 140 | float get_temperature(void) |
tylerjw | 2:8c00953d4755 | 141 | { |
tylerjw | 2:8c00953d4755 | 142 | return temperature; |
tylerjw | 2:8c00953d4755 | 143 | } |
tylerjw | 2:8c00953d4755 | 144 | |
tylerjw | 2:8c00953d4755 | 145 | long get_pressure(void) |
tylerjw | 2:8c00953d4755 | 146 | { |
tylerjw | 2:8c00953d4755 | 147 | return pressure; |
tylerjw | 2:8c00953d4755 | 148 | } |
tylerjw | 2:8c00953d4755 | 149 | |
tylerjw | 2:8c00953d4755 | 150 | float get_altitude(long pressure) |
tylerjw | 2:8c00953d4755 | 151 | { |
tylerjw | 2:8c00953d4755 | 152 | float altitude; |
tylerjw | 2:8c00953d4755 | 153 | |
tylerjw | 2:8c00953d4755 | 154 | altitude = (float)44330 * (1 - pow(( pressure/P0), 0.190295)); |
tylerjw | 2:8c00953d4755 | 155 | |
tylerjw | 2:8c00953d4755 | 156 | pc.printf("Altitude: %f\n", altitude); |
tylerjw | 2:8c00953d4755 | 157 | return altitude; |
tylerjw | 2:8c00953d4755 | 158 | |
tylerjw | 2:8c00953d4755 | 159 | } |
tylerjw | 2:8c00953d4755 | 160 | |
tylerjw | 2:8c00953d4755 | 161 | void bmp085_calibration(void) |
tylerjw | 2:8c00953d4755 | 162 | { |
tylerjw | 2:8c00953d4755 | 163 | AC1 = read_short(bmp085_address, 0xAA); |
tylerjw | 2:8c00953d4755 | 164 | AC2 = read_short(bmp085_address, 0xAC); |
tylerjw | 2:8c00953d4755 | 165 | AC3 = read_short(bmp085_address, 0xAE); |
tylerjw | 2:8c00953d4755 | 166 | AC4 = read_short(bmp085_address, 0xB0); |
tylerjw | 2:8c00953d4755 | 167 | AC5 = read_short(bmp085_address, 0xB2); |
tylerjw | 2:8c00953d4755 | 168 | AC6 = read_short(bmp085_address, 0xB4); |
tylerjw | 2:8c00953d4755 | 169 | B1 = read_short(bmp085_address, 0xB6); |
tylerjw | 2:8c00953d4755 | 170 | B2 = read_short(bmp085_address, 0xB8); |
tylerjw | 2:8c00953d4755 | 171 | MB = read_short(bmp085_address, 0xBA); |
tylerjw | 2:8c00953d4755 | 172 | MC = read_short(bmp085_address, 0xBC); |
tylerjw | 2:8c00953d4755 | 173 | MD = read_short(bmp085_address, 0xBE); |
tylerjw | 2:8c00953d4755 | 174 | |
tylerjw | 2:8c00953d4755 | 175 | } |
tylerjw | 2:8c00953d4755 | 176 | |
tylerjw | 2:8c00953d4755 | 177 | void display_calibration(void) |
tylerjw | 2:8c00953d4755 | 178 | { |
tylerjw | 2:8c00953d4755 | 179 | pc.printf("Calibration Values:\n"); |
tylerjw | 2:8c00953d4755 | 180 | pc.printf("AC1 = %d\n",AC1); |
tylerjw | 2:8c00953d4755 | 181 | pc.printf("AC2 = %d\n",AC2); |
tylerjw | 2:8c00953d4755 | 182 | pc.printf("AC3 = %d\n",AC3); |
tylerjw | 2:8c00953d4755 | 183 | pc.printf("AC4 = %d\n",AC4); |
tylerjw | 2:8c00953d4755 | 184 | pc.printf("AC5 = %d\n",AC5); |
tylerjw | 2:8c00953d4755 | 185 | pc.printf("AC6 = %d\n",AC6); |
tylerjw | 2:8c00953d4755 | 186 | pc.printf("B1 = %d\n",B1); |
tylerjw | 2:8c00953d4755 | 187 | pc.printf("B2 = %d\n",B2); |
tylerjw | 2:8c00953d4755 | 188 | pc.printf("MB = %d\n",MB); |
tylerjw | 2:8c00953d4755 | 189 | pc.printf("MC = %d\n",MC); |
tylerjw | 2:8c00953d4755 | 190 | pc.printf("MD = %d\n",MD); |
tylerjw | 2:8c00953d4755 | 191 | }*/ |
tylerjw | 2:8c00953d4755 | 192 |