-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp085.c
415 lines (363 loc) · 10.8 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<<<<<<< HEAD
/*
bmp085 lib 0x01
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <avr/io.h>
#include <util/delay.h>
#include "bmp085.h"
//path to i2c fleury lib
#include BMP085_I2CFLEURYPATH
/*
* i2c write
*/
void bmp085_writemem(uint8_t reg, uint8_t value) {
i2c_start_wait(BMP085_ADDR | I2C_WRITE);
i2c_write(reg);
i2c_write(value);
i2c_stop();
}
/*
* i2c read
*/
void bmp085_readmem(uint8_t reg, uint8_t buff[], uint8_t bytes) {
uint8_t i =0;
i2c_start_wait(BMP085_ADDR | I2C_WRITE);
i2c_write(reg);
i2c_rep_start(BMP085_ADDR | I2C_READ);
for(i=0; i<bytes; i++) {
if(i==bytes-1)
buff[i] = i2c_readNak();
else
buff[i] = i2c_readAck();
}
i2c_stop();
}
#if BMP085_FILTERPRESSURE == 1
#define BMP085_AVARAGECOEF 21
static long k[BMP085_AVARAGECOEF];
long bmp085_avaragefilter(long input) {
uint8_t i=0;
long sum=0;
for (i=0; i<BMP085_AVARAGECOEF; i++) {
k[i] = k[i+1];
}
k[BMP085_AVARAGECOEF-1] = input;
for (i=0; i<BMP085_AVARAGECOEF; i++) {
sum += k[i];
}
return (sum /BMP085_AVARAGECOEF) ;
}
#endif
/*
* read calibration registers
*/
void bmp085_getcalibration() {
uint8_t buff[2];
memset(buff, 0, sizeof(buff));
bmp085_readmem(BMP085_REGAC1, buff, 2);
bmp085_regac1 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGAC2, buff, 2);
bmp085_regac2 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGAC3, buff, 2);
bmp085_regac3 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGAC4, buff, 2);
bmp085_regac4 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1]));
bmp085_readmem(BMP085_REGAC5, buff, 2);
bmp085_regac5 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1]));
bmp085_readmem(BMP085_REGAC6, buff, 2);
bmp085_regac6 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1]));
bmp085_readmem(BMP085_REGB1, buff, 2);
bmp085_regb1 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGB2, buff, 2);
bmp085_regb2 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGMB, buff, 2);
bmp085_regmb = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGMC, buff, 2);
bmp085_regmc = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGMD, buff, 2);
bmp085_regmd = ((int)buff[0] <<8 | ((int)buff[1]));
}
/*
* get raw temperature as read by registers, and do some calculation to convert it
*/
void bmp085_getrawtemperature() {
uint8_t buff[2];
memset(buff, 0, sizeof(buff));
long ut,x1,x2;
//read raw temperature
bmp085_writemem(BMP085_REGCONTROL, BMP085_REGREADTEMPERATURE);
_delay_ms(5); // min. 4.5ms read Temp delay
bmp085_readmem(BMP085_REGCONTROLOUTPUT, buff, 2);
ut = ((long)buff[0] << 8 | ((long)buff[1])); //uncompensated temperature value
//calculate raw temperature
x1 = ((long)ut - bmp085_regac6) * bmp085_regac5 >> 15;
x2 = ((long)bmp085_regmc << 11) / (x1 + bmp085_regmd);
bmp085_rawtemperature = (x1 + x2);
}
/*
* get raw pressure as read by registers, and do some calculation to convert it
*/
void bmp085_getrawpressure() {
uint8_t buff[3];
memset(buff, 0, sizeof(buff));
long up,x1,x2,x3,b3,b6,p;
unsigned long b4,b7;
#if BMP085_AUTOUPDATETEMP == 1
bmp085_getrawtemperature();
#endif
//read raw pressure
bmp085_writemem(BMP085_REGCONTROL, BMP085_REGREADPRESSURE+(BMP085_MODE << 6));
_delay_ms(2 + (3<<BMP085_MODE));
bmp085_readmem(BMP085_REGCONTROLOUTPUT, buff, 3);
up = ((((long)buff[0] <<16) | ((long)buff[1] <<8) | ((long)buff[2])) >> (8-BMP085_MODE)); // uncompensated pressure value
//calculate raw pressure
b6 = bmp085_rawtemperature - 4000;
x1 = (bmp085_regb2* (b6 * b6) >> 12) >> 11;
x2 = (bmp085_regac2 * b6) >> 11;
x3 = x1 + x2;
b3 = (((((long)bmp085_regac1) * 4 + x3) << BMP085_MODE) + 2) >> 2;
x1 = (bmp085_regac3 * b6) >> 13;
x2 = (bmp085_regb1 * ((b6 * b6) >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (bmp085_regac4 * (uint32_t)(x3 + 32768)) >> 15;
b7 = ((uint32_t)up - b3) * (50000 >> BMP085_MODE);
p = b7 < 0x80000000 ? (b7 << 1) / b4 : (b7 / b4) << 1;
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
bmp085_rawpressure = p + ((x1 + x2 + 3791) >> 4);
#if BMP085_FILTERPRESSURE == 1
bmp085_rawpressure = bmp085_avaragefilter(bmp085_rawpressure);
#endif
}
/*
* get celsius temperature
*/
double bmp085_gettemperature() {
bmp085_getrawtemperature();
double temperature = ((bmp085_rawtemperature + 8)>>4);
//temperature = temperature /10;
return temperature;
}
/*
* get pressure with offset in BMP085.h and user pressure corection
*/
int32_t bmp085_getpressure(int32_t gPressCorr) {
bmp085_getrawpressure();
return bmp085_rawpressure + BMP085_UNITPAOFFSET + gPressCorr;
}
/*
* get altitude
*/
double bmp085_getaltitude() {
bmp085_getrawpressure();
return ((1 - pow(bmp085_rawpressure/(double)101325, 0.1903 )) / 0.0000225577) + BMP085_UNITMOFFSET;
}
/*
* init bmp085
*/
void bmp085_init() {
#if BMP085_I2CINIT == 1
//init i2c
i2c_init();
_delay_us(10);
#endif
bmp085_getcalibration(); //get calibration data
bmp085_getrawtemperature(); //update raw temperature, at least the first time
#if BMP085_FILTERPRESSURE == 1
//initialize the avarage filter
uint8_t i=0;
for (i=0; i<BMP085_AVARAGECOEF; i++) {
bmp085_getrawpressure();
}
#endif
}
=======
/*
bmp085 lib 0x01
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <avr/io.h>
#include <util/delay.h>
#include "bmp085.h"
//path to i2c fleury lib
#include BMP085_I2CFLEURYPATH
/*
* i2c write
*/
void bmp085_writemem(uint8_t reg, uint8_t value) {
i2c_start_wait(BMP085_ADDR | I2C_WRITE);
i2c_write(reg);
i2c_write(value);
i2c_stop();
}
/*
* i2c read
*/
void bmp085_readmem(uint8_t reg, uint8_t buff[], uint8_t bytes) {
uint8_t i =0;
i2c_start_wait(BMP085_ADDR | I2C_WRITE);
i2c_write(reg);
i2c_rep_start(BMP085_ADDR | I2C_READ);
for(i=0; i<bytes; i++) {
if(i==bytes-1)
buff[i] = i2c_readNak();
else
buff[i] = i2c_readAck();
}
i2c_stop();
}
#if BMP085_FILTERPRESSURE == 1
#define BMP085_AVARAGECOEF 21
static long k[BMP085_AVARAGECOEF];
long bmp085_avaragefilter(long input) {
uint8_t i=0;
long sum=0;
for (i=0; i<BMP085_AVARAGECOEF; i++) {
k[i] = k[i+1];
}
k[BMP085_AVARAGECOEF-1] = input;
for (i=0; i<BMP085_AVARAGECOEF; i++) {
sum += k[i];
}
return (sum /BMP085_AVARAGECOEF) ;
}
#endif
/*
* read calibration registers
*/
void bmp085_getcalibration() {
uint8_t buff[2];
memset(buff, 0, sizeof(buff));
bmp085_readmem(BMP085_REGAC1, buff, 2);
bmp085_regac1 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGAC2, buff, 2);
bmp085_regac2 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGAC3, buff, 2);
bmp085_regac3 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGAC4, buff, 2);
bmp085_regac4 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1]));
bmp085_readmem(BMP085_REGAC5, buff, 2);
bmp085_regac5 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1]));
bmp085_readmem(BMP085_REGAC6, buff, 2);
bmp085_regac6 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1]));
bmp085_readmem(BMP085_REGB1, buff, 2);
bmp085_regb1 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGB2, buff, 2);
bmp085_regb2 = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGMB, buff, 2);
bmp085_regmb = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGMC, buff, 2);
bmp085_regmc = ((int)buff[0] <<8 | ((int)buff[1]));
bmp085_readmem(BMP085_REGMD, buff, 2);
bmp085_regmd = ((int)buff[0] <<8 | ((int)buff[1]));
}
/*
* get raw temperature as read by registers, and do some calculation to convert it
*/
void bmp085_getrawtemperature() {
uint8_t buff[2];
memset(buff, 0, sizeof(buff));
long ut,x1,x2;
//read raw temperature
bmp085_writemem(BMP085_REGCONTROL, BMP085_REGREADTEMPERATURE);
_delay_ms(5); // min. 4.5ms read Temp delay
bmp085_readmem(BMP085_REGCONTROLOUTPUT, buff, 2);
ut = ((long)buff[0] << 8 | ((long)buff[1])); //uncompensated temperature value
//calculate raw temperature
x1 = ((long)ut - bmp085_regac6) * bmp085_regac5 >> 15;
x2 = ((long)bmp085_regmc << 11) / (x1 + bmp085_regmd);
bmp085_rawtemperature = (x1 + x2);
}
/*
* get raw pressure as read by registers, and do some calculation to convert it
*/
void bmp085_getrawpressure() {
uint8_t buff[3];
memset(buff, 0, sizeof(buff));
long up,x1,x2,x3,b3,b6,p;
unsigned long b4,b7;
#if BMP085_AUTOUPDATETEMP == 1
bmp085_getrawtemperature();
#endif
//read raw pressure
bmp085_writemem(BMP085_REGCONTROL, BMP085_REGREADPRESSURE+(BMP085_MODE << 6));
_delay_ms(2 + (3<<BMP085_MODE));
bmp085_readmem(BMP085_REGCONTROLOUTPUT, buff, 3);
up = ((((long)buff[0] <<16) | ((long)buff[1] <<8) | ((long)buff[2])) >> (8-BMP085_MODE)); // uncompensated pressure value
//calculate raw pressure
b6 = bmp085_rawtemperature - 4000;
x1 = (bmp085_regb2* (b6 * b6) >> 12) >> 11;
x2 = (bmp085_regac2 * b6) >> 11;
x3 = x1 + x2;
b3 = (((((long)bmp085_regac1) * 4 + x3) << BMP085_MODE) + 2) >> 2;
x1 = (bmp085_regac3 * b6) >> 13;
x2 = (bmp085_regb1 * ((b6 * b6) >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (bmp085_regac4 * (uint32_t)(x3 + 32768)) >> 15;
b7 = ((uint32_t)up - b3) * (50000 >> BMP085_MODE);
p = b7 < 0x80000000 ? (b7 << 1) / b4 : (b7 / b4) << 1;
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
bmp085_rawpressure = p + ((x1 + x2 + 3791) >> 4);
#if BMP085_FILTERPRESSURE == 1
bmp085_rawpressure = bmp085_avaragefilter(bmp085_rawpressure);
#endif
}
/*
* get celsius temperature
*/
double bmp085_gettemperature() {
bmp085_getrawtemperature();
double temperature = ((bmp085_rawtemperature + 8)>>4);
//temperature = temperature /10;
return temperature;
}
/*
* get pressure with offset in BMP085.h and user pressure corection
*/
int32_t bmp085_getpressure(int32_t gPressCorr) {
bmp085_getrawpressure();
return bmp085_rawpressure + BMP085_UNITPAOFFSET + gPressCorr;
}
/*
* get altitude
*/
double bmp085_getaltitude() {
bmp085_getrawpressure();
return ((1 - pow(bmp085_rawpressure/(double)101325, 0.1903 )) / 0.0000225577) + BMP085_UNITMOFFSET;
}
/*
* init bmp085
*/
void bmp085_init() {
#if BMP085_I2CINIT == 1
//init i2c
i2c_init();
_delay_us(10);
#endif
bmp085_getcalibration(); //get calibration data
bmp085_getrawtemperature(); //update raw temperature, at least the first time
#if BMP085_FILTERPRESSURE == 1
//initialize the avarage filter
uint8_t i=0;
for (i=0; i<BMP085_AVARAGECOEF; i++) {
bmp085_getrawpressure();
}
#endif
}
>>>>>>> origin/master