/
bmp085.c
195 lines (144 loc) · 3.47 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
#include "bmp085.h"
Status BMP085_RawTemperarure(uint8_t *buf)
{
uint8_t i2cbuf[2];
Status res;
i2cbuf[0] = 0xF4;
i2cbuf[1] = 0x2E;
res = I2C_Write(I2Cx, i2cbuf, 2, 0xEE);
if (!res) return Error;
Delay(5);
i2cbuf[0] = 0xF6;
res = I2C_Write(I2Cx, i2cbuf, 1, 0xEE);
if (!res) return Error;
res = I2C_Read(I2Cx, i2cbuf, 1, 0xEF);
if (!res) return Error;
buf[0] = i2cbuf[0];
res = I2C_Read(I2Cx, i2cbuf, 1, 0xEF);
if (!res) return Error;
buf[1] = i2cbuf[0];
return Success;
}
Status BMP085_RawPreasure(uint8_t *buf)
{
uint8_t i2cbuf[2];
Status res;
i2cbuf[0] = 0xF4;
i2cbuf[1] = 0x34 + (BMP085_OSRS << 6);
res = I2C_Write(I2Cx, i2cbuf, 2, 0xEE);
if (!res) return Error;
Delay(25);
i2cbuf[0] = 0xF6;
res = I2C_Write(I2Cx, i2cbuf, 1, 0xEE);
if (!res) return Error;
res = I2C_Read(I2Cx, i2cbuf, 1, 0xEF);
if (!res) return Error;
buf[2] = i2cbuf[0];
res = I2C_Read(I2Cx, i2cbuf, 1, 0xEF);
if (!res) return Error;
buf[3] = i2cbuf[0];
res = I2C_Read(I2Cx, i2cbuf, 1, 0xEF);
if (!res) return Error;
buf[4] = i2cbuf[0];
return Success;
}
Status BMP085_RawCalibData(uint16_t *buf)
{
uint8_t i2cbuf[2];
Status res;
uint8_t i;
i2cbuf[0] = 0xAA;
res = I2C_Write(I2Cx, i2cbuf, 1, 0xEE);
if (!res) return Error;
for (i = 0; i<11; i++){
res = I2C_Read(I2Cx, i2cbuf, 1, 0xEF);
if (!res) return Error;
buf[i] = i2cbuf[0]<<8;
res = I2C_Read(I2Cx, i2cbuf, 1, 0xEF);
if (!res) return Error;
buf[i] |= i2cbuf[0];
}
return Success;
}
float BMP085_Temperarure(struct bmp085_type *bmp085){
int32_t X1, X2, T;
//X1 = (UT-AC6)*AC5/0x8000
X1 = (bmp085->UT - bmp085->calib.AC6);
X1 *= bmp085->calib.AC5;
X1 >>= 15;
//X2 = MC * 0x800/(X1 + MD)
X2 = bmp085->calib.MC;
X2 <<= 11;
X2 /= (X1 + bmp085->calib.MD);
//B5 = X1 + X2
bmp085->B5 = X1 + X2;
//T = (B5 + 8) / 0x10
T = (bmp085->B5 + 8) >> 4;
//T = (bmp085->B5 + 8);
return (float) T/10;
}
int32_t BMP085_Preasure(struct bmp085_type *bmp085){
int32_t X1, X2, X3, B6, B3, P;
uint32_t B4, B7;
//B6 = B5 - 4000
B6 = bmp085->B5 - 4000;
//X1 = (B2 * (B6 * B6 / 0x1000)) / 0x800
X1 = B6 * B6;
X1 >>= 12;
X1 *= bmp085->calib.B2;
X1 >>= 11;
//X2 = B6 * AC2 / 0x800
X2 = B6 * bmp085->calib.AC2;
X2 >>= 11;
//X3 = X1 + X2
X3 = X1 + X2;
//B3 = ((AC1 * 4 + X3) + 2) / 4
B3 = (bmp085->calib.AC1 * 4) + X3;
B3 <<= BMP085_OSRS;
B3 += 2;
B3 /= 4;
//bmp085->B3 = B3;
//X1 = (AC3 * B6) / 0x2000
X1 = bmp085->calib.AC3 * B6;
X1 >>= 13;
// X2 = ((B6 * B6)/0x1000) * B1 / 0x10000
X2 = B6 * B6;
X2 >>= 12;
X2 *= bmp085->calib.B1;
X2 >>= 16;
//X3 = (X1 + X2 + 2)/4
X3 = X1 + X2;
X3 += 2;
//X3 >>= 2;
X3 /= 2;
//B4 = (X4 + 32768) * AC4
B4 = (uint32_t) (X3 + 32768) * bmp085->calib.AC4;
B4 >>= 15;
//bmp085->B4 = B4;
//B7 = (UP - B3) * 50000
B7 = (uint32_t) (bmp085->UP - B3) * (50000 >> BMP085_OSRS);
//bmp085->B7 = B7;
//P = B7 * 2 / B4
if (B7 < 0x80000000) P = B7 * 2 / B4;
else P = B7 / B4 * 2;
//X1 = (P / 80 * P / 80 * 3038) / 0x10000
X1 = (P >> 8);
X1 *= X1 * 3038;
X1 >>= 16;
//bmp085->X1 = X1;
//X2 = (-7357 * P) / 0x10000
X2 = -7357 * P;
X2 >>= 16;
//bmp085->X2 = X2;
//P = P + (X1 + X2 + 3791) / 16
P = P + (X1 + X2 + 3791) / 16;
return P;
}
float BMP085_Preasure_mm(int32_t P){
//return P * 0.0075;
return (float) P * 3 / 400;
}
void BMP085_UTUP(uint8_t *buf, struct bmp085_type *bmp085){
bmp085->UT = (buf[0] << 8) | buf[1];
bmp085->UP = ((buf[2] << 16) | (buf[3] << 8) | buf[4]) >> (8 - BMP085_OSRS);
}