Statistics
| Revision:

root / Ports / PortsBMP085.cpp

History | View | Annotate | Download (2 KB)

1
//>>> The latest version of this code can be found at https://github.com/jcw/ !!
2
3
// Port library interface to BMP085 sensors connected via I2C
4
// 2009-02-17 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
5
// $Id: PortsBMP085.cpp 7763 2011-12-11 01:28:16Z jcw $
6
7
// computation and algorithm taken from the Bosch Sensortec BMP085 data sheet
8
// (IIRC, I had to cast to an int32_t for the b3 calculation to work above 25C)
9
10
#include <Ports.h>
11
#include "PortsBMP085.h"
12
#include <WProgram.h>
13
14
uint8_t BMP085::startMeas(uint8_t type) const {
15
    send();
16
    write(0xF4);
17
    write(type == TEMP ? 0x2E : 0x34 | (oss << 6));
18
    stop();
19
    return oss == 0 ? 5 : oss == 1 ? 8 : oss == 2 ? 14 : 26;
20
}
21
22
int32_t BMP085::getResult(uint8_t type) {
23
    readFromReg(0xF6);
24
    if (type == TEMP) 
25
        meas[TEMP] = readWord(1);
26
    else {
27
        meas[PRES] = readWord(0);
28
        meas[PRES] <<= oss;
29
        meas[PRES] |= read(1) >> (8-oss);
30
    }
31
    return meas[type];
32
}
33
34
void BMP085::getCalibData() {
35
    readFromReg(0xAA);
36
    ac1 = readWord(0);
37
    ac2 = readWord(0);
38
    ac3 = readWord(0);
39
    ac4 = readWord(0);
40
    ac5 = readWord(0);
41
    ac6 = readWord(0);
42
    b1 = readWord(0);
43
    b2 = readWord(0);
44
    mb = readWord(0);
45
    mc = readWord(0);
46
    md = readWord(1);
47
}
48
49
void BMP085::calculate(int16_t& tval, int32_t& pval) const {
50
    int32_t ut = meas[TEMP], up = meas[PRES];
51
    int32_t x1, x2, x3, b3, b5, b6, p;
52
    uint32_t b4, b7;
53
54
    x1 = (ut - ac6) * ac5 >> 15;
55
    x2 = ((int32_t) mc << 11) / (x1 + md);
56
    b5 = x1 + x2;
57
    tval = (b5 + 8) >> 4;
58
    
59
    b6 = b5 - 4000;
60
    x1 = (b2 * (b6 * b6 >> 12)) >> 11; 
61
    x2 = ac2 * b6 >> 11;
62
    x3 = x1 + x2;
63
    b3 = ((((int32_t) ac1 * 4 + x3) << oss) + 2) >> 2;
64
    x1 = ac3 * b6 >> 13;
65
    x2 = (b1 * (b6 * b6 >> 12)) >> 16;
66
    x3 = ((x1 + x2) + 2) >> 2;
67
    b4 = (ac4 * (uint32_t) (x3 + 32768)) >> 15;
68
    b7 = ((uint32_t) up - b3) * (50000 >> oss);
69
    p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
70
    
71
    x1 = (p >> 8) * (p >> 8);
72
    x1 = (x1 * 3038) >> 16;
73
    x2 = (-7357 * p) >> 16;
74
    pval = p + ((x1 + x2 + 3791) >> 4);
75
}