-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp085.c
195 lines (159 loc) · 4.85 KB
/
bmp085.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "bmp085.h"
#include "hardware/i2c.h"
// Calibration data registers
#define BMP085_CAL_AC1 0xAA //!< R Calibration data (16 bits)
#define BMP085_CAL_AC2 0xAC //!< R Calibration data (16 bits)
#define BMP085_CAL_AC3 0xAE //!< R Calibration data (16 bits)
#define BMP085_CAL_AC4 0xB0 //!< R Calibration data (16 bits)
#define BMP085_CAL_AC5 0xB2 //!< R Calibration data (16 bits)
#define BMP085_CAL_AC6 0xB4 //!< R Calibration data (16 bits)
#define BMP085_CAL_B1 0xB6 //!< R Calibration data (16 bits)
#define BMP085_CAL_B2 0xB8 //!< R Calibration data (16 bits)
#define BMP085_CAL_MB 0xBA //!< R Calibration data (16 bits)
#define BMP085_CAL_MC 0xBC //!< R Calibration data (16 bits)
#define BMP085_CAL_MD 0xBE //!< R Calibration data (16 bits)
// Commands
#define BMP085_CONTROL 0xF4 //!< Control register
#define BMP085_TEMPDATA 0xF6 //!< Temperature data register
#define BMP085_PRESSUREDATA 0xF6 //!< Pressure data register
#define BMP085_READTEMPCMD 0x2E //!< Read temperature control register value
#define BMP085_READPRESSURECMD 0x34 //!< Read pressure control register value
// Calibration data (will be read from sensor)
int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
uint16_t ac4, ac5, ac6;
uint8_t oversampling;
uint8_t BMP085_ADDR = 0x77;
i2c_inst_t *bmp_i2c = i2c_default;
uint16_t bmp085_sda = PICO_DEFAULT_I2C_SDA_PIN;
uint16_t bmp085_scl = PICO_DEFAULT_I2C_SCL_PIN;
// pin select function
void BMP085_setI2C(i2c_inst_t *i, uint16_t sda, uint16_t scl, uint8_t addr)
{
bmp_i2c = i;
bmp085_sda = sda;
bmp085_scl = scl;
BMP085_ADDR = addr;
}
// I2C handling functions
uint8_t BMP085_read8(uint8_t a)
{
uint8_t r;
i2c_write_blocking(bmp_i2c, BMP085_ADDR, &a, 1, true);
i2c_read_blocking(bmp_i2c, BMP085_ADDR, &r, 1, false);
return r;
}
uint16_t BMP085_read16(uint8_t a)
{
uint8_t retbuf[2];
uint16_t r;
i2c_write_blocking(bmp_i2c, BMP085_ADDR, &a, 1, true);
i2c_read_blocking(bmp_i2c, BMP085_ADDR, retbuf, 2, false);
r = retbuf[1] | (retbuf[0] << 8);
return r;
}
void BMP085_write8(uint8_t a, uint8_t d)
{
uint8_t tBuf[2];
tBuf[0] = a;
tBuf[1] = d;
i2c_write_blocking(bmp_i2c, BMP085_ADDR, tBuf, 2, false);
}
uint8_t BMP085_init(uint8_t mode)
{
i2c_init(bmp_i2c, 800 * 1000);
gpio_set_function(bmp085_sda, GPIO_FUNC_I2C);
gpio_set_function(bmp085_scl, GPIO_FUNC_I2C);
gpio_pull_up(bmp085_sda);
gpio_pull_up(bmp085_scl);
if (mode > BMP085_ULTRAHIGHRES)
mode = BMP085_ULTRAHIGHRES;
oversampling = mode;
if (BMP085_read8(0xD0) != 0x55)
return 0;
/* read calibration data */
ac1 = BMP085_read16(BMP085_CAL_AC1);
ac2 = BMP085_read16(BMP085_CAL_AC2);
ac3 = BMP085_read16(BMP085_CAL_AC3);
ac4 = BMP085_read16(BMP085_CAL_AC4);
ac5 = BMP085_read16(BMP085_CAL_AC5);
ac6 = BMP085_read16(BMP085_CAL_AC6);
b1 = BMP085_read16(BMP085_CAL_B1);
b2 = BMP085_read16(BMP085_CAL_B2);
mb = BMP085_read16(BMP085_CAL_MB);
mc = BMP085_read16(BMP085_CAL_MC);
md = BMP085_read16(BMP085_CAL_MD);
return 1;
}
// Sensor read functions
int32_t computeB5(int32_t UT)
{
int32_t X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) >> 15;
int32_t X2 = ((int32_t)mc << 11) / (X1 + (int32_t)md);
return X1 + X2;
}
uint16_t BMP085_readRawTemperature(void)
{
BMP085_write8(BMP085_CONTROL, BMP085_READTEMPCMD);
sleep_ms(5);
return BMP085_read16(BMP085_TEMPDATA);
}
uint32_t BMP085_readRawPressure(void)
{
uint32_t raw;
BMP085_write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));
if (oversampling == BMP085_ULTRALOWPOWER)
sleep_ms(5);
else if (oversampling == BMP085_STANDARD)
sleep_ms(8);
else if (oversampling == BMP085_HIGHRES)
sleep_ms(14);
else
sleep_ms(26);
raw = BMP085_read16(BMP085_PRESSUREDATA);
raw <<= 8;
raw |= BMP085_read8(BMP085_PRESSUREDATA + 2);
raw >>= (8 - oversampling);
return raw;
}
float BMP085_readTemperature(void)
{
int32_t UT, B5; // following ds convention
float temp;
UT = BMP085_readRawTemperature();
B5 = computeB5(UT);
temp = (B5 + 8) >> 4;
temp /= 10;
return temp;
}
int32_t BMP085_readPressure(void)
{
int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
uint32_t B4, B7;
UT = BMP085_readRawTemperature();
UP = BMP085_readRawPressure();
B5 = computeB5(UT);
// do pressure calcs
B6 = B5 - 4000;
X1 = ((int32_t)b2 * ((B6 * B6) >> 12)) >> 11;
X2 = ((int32_t)ac2 * B6) >> 11;
X3 = X1 + X2;
B3 = ((((int32_t)ac1 * 4 + X3) << oversampling) + 2) / 4;
X1 = ((int32_t)ac3 * B6) >> 13;
X2 = ((int32_t)b1 * ((B6 * B6) >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = ((uint32_t)ac4 * (uint32_t)(X3 + 32768)) >> 15;
B7 = ((uint32_t)UP - B3) * (uint32_t)(50000UL >> oversampling);
if (B7 < 0x80000000)
{
p = (B7 * 2) / B4;
}
else
{
p = (B7 / B4) * 2;
}
X1 = (p >> 8) * (p >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * p) >> 16;
p = p + ((X1 + X2 + (int32_t)3791) >> 4);
return p;
}