Rev 2194 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2194 | Rev 2198 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | bmp085 lib 0x01 |
2 | bmp085 lib 0x01 |
3 | 3 | ||
4 | copyright (c) Davide Gironi, 2012 |
4 | copyright (c) Davide Gironi, 2012 |
5 | 5 | ||
6 | Released under GPLv3. |
6 | Released under GPLv3. |
7 | Please refer to LICENSE file for licensing information. |
7 | Please refer to LICENSE file for licensing information. |
8 | */ |
8 | */ |
- | 9 | ||
- | 10 | //############################################################################ |
|
- | 11 | //# HISTORY bmp085.c |
|
- | 12 | //# |
|
- | 13 | //# 19.09.2015 cebra |
|
- | 14 | //# - add: Library für BMP085/BMP180 Luftdrucksensor, GY-87 Sensorboard |
|
- | 15 | //# |
|
- | 16 | //############################################################################ |
|
- | 17 | ||
- | 18 | ||
- | 19 | ||
- | 20 | ||
- | 21 | ||
- | 22 | ||
- | 23 | ||
- | 24 | ||
- | 25 | ||
9 | 26 | #ifdef USE_KOMPASS |
|
10 | 27 | ||
11 | #include <stdio.h> |
28 | #include <stdio.h> |
12 | #include <stdlib.h> |
29 | #include <stdlib.h> |
13 | #include <string.h> |
30 | #include <string.h> |
14 | #include <math.h> |
31 | #include <math.h> |
15 | #include <avr/io.h> |
32 | #include <avr/io.h> |
16 | #include <util/delay.h> |
33 | #include <util/delay.h> |
17 | 34 | ||
18 | #include "bmp085.h" |
35 | #include "bmp085.h" |
19 | 36 | ||
20 | //path to i2c fleury lib |
37 | //path to i2c fleury lib |
21 | #include BMP085_I2CFLEURYPATH |
38 | #include BMP085_I2CFLEURYPATH |
22 | 39 | ||
23 | /* |
40 | /* |
24 | * i2c write |
41 | * i2c write |
25 | */ |
42 | */ |
26 | void bmp085_writemem(uint8_t reg, uint8_t value) { |
43 | void bmp085_writemem(uint8_t reg, uint8_t value) { |
27 | i2c_start_wait(BMP085_ADDR | I2C_WRITE); |
44 | i2c_start_wait(BMP085_ADDR | I2C_WRITE); |
28 | i2c_write(reg); |
45 | i2c_write(reg); |
29 | i2c_write(value); |
46 | i2c_write(value); |
30 | i2c_stop(); |
47 | i2c_stop(); |
31 | } |
48 | } |
32 | 49 | ||
33 | /* |
50 | /* |
34 | * i2c read |
51 | * i2c read |
35 | */ |
52 | */ |
36 | void bmp085_readmem(uint8_t reg, uint8_t buff[], uint8_t bytes) { |
53 | void bmp085_readmem(uint8_t reg, uint8_t buff[], uint8_t bytes) { |
37 | uint8_t i =0; |
54 | uint8_t i =0; |
38 | i2c_start_wait(BMP085_ADDR | I2C_WRITE); |
55 | i2c_start_wait(BMP085_ADDR | I2C_WRITE); |
39 | i2c_write(reg); |
56 | i2c_write(reg); |
40 | i2c_rep_start(BMP085_ADDR | I2C_READ); |
57 | i2c_rep_start(BMP085_ADDR | I2C_READ); |
41 | for(i=0; i<bytes; i++) { |
58 | for(i=0; i<bytes; i++) { |
42 | if(i==bytes-1) |
59 | if(i==bytes-1) |
43 | buff[i] = i2c_readNak(); |
60 | buff[i] = i2c_readNak(); |
44 | else |
61 | else |
45 | buff[i] = i2c_readAck(); |
62 | buff[i] = i2c_readAck(); |
46 | } |
63 | } |
47 | i2c_stop(); |
64 | i2c_stop(); |
48 | } |
65 | } |
49 | 66 | ||
50 | 67 | ||
51 | #if BMP085_FILTERPRESSURE == 1 |
68 | #if BMP085_FILTERPRESSURE == 1 |
52 | #define BMP085_AVARAGECOEF 21 |
69 | #define BMP085_AVARAGECOEF 21 |
53 | static long k[BMP085_AVARAGECOEF]; |
70 | static long k[BMP085_AVARAGECOEF]; |
54 | long bmp085_avaragefilter(long input) { |
71 | long bmp085_avaragefilter(long input) { |
55 | uint8_t i=0; |
72 | uint8_t i=0; |
56 | long sum=0; |
73 | long sum=0; |
57 | for (i=0; i<BMP085_AVARAGECOEF; i++) { |
74 | for (i=0; i<BMP085_AVARAGECOEF; i++) { |
58 | k[i] = k[i+1]; |
75 | k[i] = k[i+1]; |
59 | } |
76 | } |
60 | k[BMP085_AVARAGECOEF-1] = input; |
77 | k[BMP085_AVARAGECOEF-1] = input; |
61 | for (i=0; i<BMP085_AVARAGECOEF; i++) { |
78 | for (i=0; i<BMP085_AVARAGECOEF; i++) { |
62 | sum += k[i]; |
79 | sum += k[i]; |
63 | } |
80 | } |
64 | return (sum /BMP085_AVARAGECOEF) ; |
81 | return (sum /BMP085_AVARAGECOEF) ; |
65 | } |
82 | } |
66 | #endif |
83 | #endif |
67 | 84 | ||
68 | /* |
85 | /* |
69 | * read calibration registers |
86 | * read calibration registers |
70 | */ |
87 | */ |
71 | void bmp085_getcalibration() { |
88 | void bmp085_getcalibration() { |
72 | uint8_t buff[2]; |
89 | uint8_t buff[2]; |
73 | memset(buff, 0, sizeof(buff)); |
90 | memset(buff, 0, sizeof(buff)); |
74 | 91 | ||
75 | bmp085_readmem(BMP085_REGAC1, buff, 2); |
92 | bmp085_readmem(BMP085_REGAC1, buff, 2); |
76 | bmp085_regac1 = ((int)buff[0] <<8 | ((int)buff[1])); |
93 | bmp085_regac1 = ((int)buff[0] <<8 | ((int)buff[1])); |
77 | bmp085_readmem(BMP085_REGAC2, buff, 2); |
94 | bmp085_readmem(BMP085_REGAC2, buff, 2); |
78 | bmp085_regac2 = ((int)buff[0] <<8 | ((int)buff[1])); |
95 | bmp085_regac2 = ((int)buff[0] <<8 | ((int)buff[1])); |
79 | bmp085_readmem(BMP085_REGAC3, buff, 2); |
96 | bmp085_readmem(BMP085_REGAC3, buff, 2); |
80 | bmp085_regac3 = ((int)buff[0] <<8 | ((int)buff[1])); |
97 | bmp085_regac3 = ((int)buff[0] <<8 | ((int)buff[1])); |
81 | bmp085_readmem(BMP085_REGAC4, buff, 2); |
98 | bmp085_readmem(BMP085_REGAC4, buff, 2); |
82 | bmp085_regac4 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1])); |
99 | bmp085_regac4 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1])); |
83 | bmp085_readmem(BMP085_REGAC5, buff, 2); |
100 | bmp085_readmem(BMP085_REGAC5, buff, 2); |
84 | bmp085_regac5 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1])); |
101 | bmp085_regac5 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1])); |
85 | bmp085_readmem(BMP085_REGAC6, buff, 2); |
102 | bmp085_readmem(BMP085_REGAC6, buff, 2); |
86 | bmp085_regac6 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1])); |
103 | bmp085_regac6 = ((unsigned int)buff[0] <<8 | ((unsigned int)buff[1])); |
87 | bmp085_readmem(BMP085_REGB1, buff, 2); |
104 | bmp085_readmem(BMP085_REGB1, buff, 2); |
88 | bmp085_regb1 = ((int)buff[0] <<8 | ((int)buff[1])); |
105 | bmp085_regb1 = ((int)buff[0] <<8 | ((int)buff[1])); |
89 | bmp085_readmem(BMP085_REGB2, buff, 2); |
106 | bmp085_readmem(BMP085_REGB2, buff, 2); |
90 | bmp085_regb2 = ((int)buff[0] <<8 | ((int)buff[1])); |
107 | bmp085_regb2 = ((int)buff[0] <<8 | ((int)buff[1])); |
91 | bmp085_readmem(BMP085_REGMB, buff, 2); |
108 | bmp085_readmem(BMP085_REGMB, buff, 2); |
92 | bmp085_regmb = ((int)buff[0] <<8 | ((int)buff[1])); |
109 | bmp085_regmb = ((int)buff[0] <<8 | ((int)buff[1])); |
93 | bmp085_readmem(BMP085_REGMC, buff, 2); |
110 | bmp085_readmem(BMP085_REGMC, buff, 2); |
94 | bmp085_regmc = ((int)buff[0] <<8 | ((int)buff[1])); |
111 | bmp085_regmc = ((int)buff[0] <<8 | ((int)buff[1])); |
95 | bmp085_readmem(BMP085_REGMD, buff, 2); |
112 | bmp085_readmem(BMP085_REGMD, buff, 2); |
96 | bmp085_regmd = ((int)buff[0] <<8 | ((int)buff[1])); |
113 | bmp085_regmd = ((int)buff[0] <<8 | ((int)buff[1])); |
97 | } |
114 | } |
98 | 115 | ||
99 | /* |
116 | /* |
100 | * get raw temperature as read by registers, and do some calculation to convert it |
117 | * get raw temperature as read by registers, and do some calculation to convert it |
101 | */ |
118 | */ |
102 | void bmp085_getrawtemperature() { |
119 | void bmp085_getrawtemperature() { |
103 | uint8_t buff[2]; |
120 | uint8_t buff[2]; |
104 | memset(buff, 0, sizeof(buff)); |
121 | memset(buff, 0, sizeof(buff)); |
105 | long ut,x1,x2; |
122 | long ut,x1,x2; |
106 | 123 | ||
107 | //read raw temperature |
124 | //read raw temperature |
108 | bmp085_writemem(BMP085_REGCONTROL, BMP085_REGREADTEMPERATURE); |
125 | bmp085_writemem(BMP085_REGCONTROL, BMP085_REGREADTEMPERATURE); |
109 | _delay_ms(5); // min. 4.5ms read Temp delay |
126 | _delay_ms(5); // min. 4.5ms read Temp delay |
110 | bmp085_readmem(BMP085_REGCONTROLOUTPUT, buff, 2); |
127 | bmp085_readmem(BMP085_REGCONTROLOUTPUT, buff, 2); |
111 | ut = ((long)buff[0] << 8 | ((long)buff[1])); //uncompensated temperature value |
128 | ut = ((long)buff[0] << 8 | ((long)buff[1])); //uncompensated temperature value |
112 | 129 | ||
113 | //calculate raw temperature |
130 | //calculate raw temperature |
114 | x1 = ((long)ut - bmp085_regac6) * bmp085_regac5 >> 15; |
131 | x1 = ((long)ut - bmp085_regac6) * bmp085_regac5 >> 15; |
115 | x2 = ((long)bmp085_regmc << 11) / (x1 + bmp085_regmd); |
132 | x2 = ((long)bmp085_regmc << 11) / (x1 + bmp085_regmd); |
116 | bmp085_rawtemperature = x1 + x2; |
133 | bmp085_rawtemperature = x1 + x2; |
117 | } |
134 | } |
118 | 135 | ||
119 | /* |
136 | /* |
120 | * get raw pressure as read by registers, and do some calculation to convert it |
137 | * get raw pressure as read by registers, and do some calculation to convert it |
121 | */ |
138 | */ |
122 | void bmp085_getrawpressure() { |
139 | void bmp085_getrawpressure() { |
123 | uint8_t buff[3]; |
140 | uint8_t buff[3]; |
124 | memset(buff, 0, sizeof(buff)); |
141 | memset(buff, 0, sizeof(buff)); |
125 | long up,x1,x2,x3,b3,b6,p; |
142 | long up,x1,x2,x3,b3,b6,p; |
126 | unsigned long b4,b7; |
143 | unsigned long b4,b7; |
127 | 144 | ||
128 | #if BMP085_AUTOUPDATETEMP == 1 |
145 | #if BMP085_AUTOUPDATETEMP == 1 |
129 | bmp085_getrawtemperature(); |
146 | bmp085_getrawtemperature(); |
130 | #endif |
147 | #endif |
131 | 148 | ||
132 | //read raw pressure |
149 | //read raw pressure |
133 | bmp085_writemem(BMP085_REGCONTROL, BMP085_REGREADPRESSURE+(BMP085_MODE << 6)); |
150 | bmp085_writemem(BMP085_REGCONTROL, BMP085_REGREADPRESSURE+(BMP085_MODE << 6)); |
134 | _delay_ms(2 + (3<<BMP085_MODE)); |
151 | _delay_ms(2 + (3<<BMP085_MODE)); |
135 | bmp085_readmem(BMP085_REGCONTROLOUTPUT, buff, 3); |
152 | bmp085_readmem(BMP085_REGCONTROLOUTPUT, buff, 3); |
136 | up = ((((long)buff[0] <<16) | ((long)buff[1] <<8) | ((long)buff[2])) >> (8-BMP085_MODE)); // uncompensated pressure value |
153 | up = ((((long)buff[0] <<16) | ((long)buff[1] <<8) | ((long)buff[2])) >> (8-BMP085_MODE)); // uncompensated pressure value |
137 | 154 | ||
138 | //calculate raw pressure |
155 | //calculate raw pressure |
139 | b6 = bmp085_rawtemperature - 4000; |
156 | b6 = bmp085_rawtemperature - 4000; |
140 | x1 = (bmp085_regb2* (b6 * b6) >> 12) >> 11; |
157 | x1 = (bmp085_regb2* (b6 * b6) >> 12) >> 11; |
141 | x2 = (bmp085_regac2 * b6) >> 11; |
158 | x2 = (bmp085_regac2 * b6) >> 11; |
142 | x3 = x1 + x2; |
159 | x3 = x1 + x2; |
143 | b3 = (((((long)bmp085_regac1) * 4 + x3) << BMP085_MODE) + 2) >> 2; |
160 | b3 = (((((long)bmp085_regac1) * 4 + x3) << BMP085_MODE) + 2) >> 2; |
144 | x1 = (bmp085_regac3 * b6) >> 13; |
161 | x1 = (bmp085_regac3 * b6) >> 13; |
145 | x2 = (bmp085_regb1 * ((b6 * b6) >> 12)) >> 16; |
162 | x2 = (bmp085_regb1 * ((b6 * b6) >> 12)) >> 16; |
146 | x3 = ((x1 + x2) + 2) >> 2; |
163 | x3 = ((x1 + x2) + 2) >> 2; |
147 | b4 = (bmp085_regac4 * (uint32_t)(x3 + 32768)) >> 15; |
164 | b4 = (bmp085_regac4 * (uint32_t)(x3 + 32768)) >> 15; |
148 | b7 = ((uint32_t)up - b3) * (50000 >> BMP085_MODE); |
165 | b7 = ((uint32_t)up - b3) * (50000 >> BMP085_MODE); |
149 | p = b7 < 0x80000000 ? (b7 << 1) / b4 : (b7 / b4) << 1; |
166 | p = b7 < 0x80000000 ? (b7 << 1) / b4 : (b7 / b4) << 1; |
150 | x1 = (p >> 8) * (p >> 8); |
167 | x1 = (p >> 8) * (p >> 8); |
151 | x1 = (x1 * 3038) >> 16; |
168 | x1 = (x1 * 3038) >> 16; |
152 | x2 = (-7357 * p) >> 16; |
169 | x2 = (-7357 * p) >> 16; |
153 | bmp085_rawpressure = p + ((x1 + x2 + 3791) >> 4); |
170 | bmp085_rawpressure = p + ((x1 + x2 + 3791) >> 4); |
154 | 171 | ||
155 | #if BMP085_FILTERPRESSURE == 1 |
172 | #if BMP085_FILTERPRESSURE == 1 |
156 | bmp085_rawpressure = bmp085_avaragefilter(bmp085_rawpressure); |
173 | bmp085_rawpressure = bmp085_avaragefilter(bmp085_rawpressure); |
157 | #endif |
174 | #endif |
158 | } |
175 | } |
159 | 176 | ||
160 | /* |
177 | /* |
161 | * get celsius temperature |
178 | * get celsius temperature |
162 | */ |
179 | */ |
163 | double bmp085_gettemperature() { |
180 | double bmp085_gettemperature() { |
164 | bmp085_getrawtemperature(); |
181 | bmp085_getrawtemperature(); |
165 | double temperature = ((bmp085_rawtemperature + 8)>>4); |
182 | double temperature = ((bmp085_rawtemperature + 8)>>4); |
166 | temperature = temperature /10; |
183 | temperature = temperature /10; |
167 | return temperature; |
184 | return temperature; |
168 | } |
185 | } |
169 | 186 | ||
170 | /* |
187 | /* |
171 | * get pressure |
188 | * get pressure |
172 | */ |
189 | */ |
173 | int32_t bmp085_getpressure() { |
190 | int32_t bmp085_getpressure() { |
174 | bmp085_getrawpressure(); |
191 | bmp085_getrawpressure(); |
175 | return bmp085_rawpressure + BMP085_UNITPAOFFSET; |
192 | return bmp085_rawpressure + BMP085_UNITPAOFFSET; |
176 | } |
193 | } |
177 | 194 | ||
178 | /* |
195 | /* |
179 | * get altitude |
196 | * get altitude |
180 | */ |
197 | */ |
181 | double bmp085_getaltitude() { |
198 | double bmp085_getaltitude() { |
182 | bmp085_getrawpressure(); |
199 | bmp085_getrawpressure(); |
183 | return ((1 - pow(bmp085_rawpressure/(double)101325, 0.1903 )) / 0.0000225577) + BMP085_UNITMOFFSET; |
200 | return ((1 - pow(bmp085_rawpressure/(double)101325, 0.1903 )) / 0.0000225577) + BMP085_UNITMOFFSET; |
184 | } |
201 | } |
185 | 202 | ||
186 | /* |
203 | /* |
187 | * init bmp085 |
204 | * init bmp085 |
188 | */ |
205 | */ |
189 | void bmp085_init() { |
206 | void bmp085_init() { |
190 | #if BMP085_I2CINIT == 1 |
207 | #if BMP085_I2CINIT == 1 |
191 | //init i2c |
208 | //init i2c |
192 | i2c_init(); |
209 | i2c_init(); |
193 | _delay_us(10); |
210 | _delay_us(10); |
194 | #endif |
211 | #endif |
195 | 212 | ||
196 | bmp085_getcalibration(); //get calibration data |
213 | bmp085_getcalibration(); //get calibration data |
197 | bmp085_getrawtemperature(); //update raw temperature, at least the first time |
214 | bmp085_getrawtemperature(); //update raw temperature, at least the first time |
198 | 215 | ||
199 | #if BMP085_FILTERPRESSURE == 1 |
216 | #if BMP085_FILTERPRESSURE == 1 |
200 | //initialize the avarage filter |
217 | //initialize the avarage filter |
201 | uint8_t i=0; |
218 | uint8_t i=0; |
202 | for (i=0; i<BMP085_AVARAGECOEF; i++) { |
219 | for (i=0; i<BMP085_AVARAGECOEF; i++) { |
203 | bmp085_getrawpressure(); |
220 | bmp085_getrawpressure(); |
204 | } |
221 | } |
205 | #endif |
222 | #endif |
206 | } |
223 | } |
- | 224 | #endif |
|
207 | 225 |