forked from ropod7/pyboard_drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMP180.py
executable file
·156 lines (121 loc) · 3.91 KB
/
BMP180.py
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
# BMP180.py -- controlling barometer BMP180
import struct
import pyb, micropython
from pyb import I2C, Switch
pyb.freq(64000000)
micropython.alloc_emergency_exception_buf(100)
i2c = I2C(2, I2C.MASTER, baudrate=4500000)
sw = Switch()
# oversampling setting (short 0..3)
oss = 3
# slave (device) address that scanned by I2C.scan():
sladdr = 0x77
# standard sea level at Zero point in hPa
#hPaZero = 1013.25
def get_I2C_calib(msb, lsb, unsigned=False):
# 'H' = unsigned short, 'h' = signed short
tp = '<H' if unsigned else '<h'
MSB = struct.unpack(tp, i2c.mem_read(1, sladdr, msb))[0]
LSB = struct.unpack(tp, i2c.mem_read(1, sladdr, lsb))[0]
data = struct.unpack(tp, struct.pack(tp, (MSB<<8 | LSB)))[0]
return data
# function returns calibration data
def read_E2PROM_regs():
data = dict()
data['AC1'] = get_I2C_calib(0xAA, 0xAB)
data['AC2'] = get_I2C_calib(0xAC, 0xAD)
data['AC3'] = get_I2C_calib(0xAE, 0xAF)
data['AC4'] = get_I2C_calib(0xB0, 0xB1, unsigned=True)
data['AC5'] = get_I2C_calib(0xB2, 0xB3, unsigned=True)
data['AC6'] = get_I2C_calib(0xB4, 0xB5, unsigned=True)
data['B1'] = get_I2C_calib(0xB6, 0xB7)
data['B2'] = get_I2C_calib(0xB8, 0xB9)
data['MB'] = get_I2C_calib(0xBA, 0xBB)
data['MC'] = get_I2C_calib(0xBC, 0xBD)
data['MD'] = get_I2C_calib(0xBE, 0xBF)
return data
def read_BMP180_temp():
UT = 0
for i in range(3):
# writing read temp register 0x2E
i2c.mem_write(0x2E, sladdr, 0xF4)
pyb.udelay(4500)
MSB = struct.unpack("<h", i2c.mem_read(1, sladdr, 0xF6))[0]
LSB = struct.unpack("<h", i2c.mem_read(1, sladdr, 0xF7))[0]
UT += (MSB<<8) | LSB
return int(UT/3)
def read_BMP180_pressure():
data = 0x34 + (oss<<6)
UP = 0
for i in range(3):
# writing read pressure register
# with oversampling reg setting: 0x34 + (oss<<6)
i2c.mem_write(data, sladdr, 0xF4)
pyb.delay(2 + (3<<oss))
MSB = struct.unpack("<L", i2c.mem_read(1, sladdr, 0xF6))[0]
LSB = struct.unpack("<L", i2c.mem_read(1, sladdr, 0xF7))[0]
XLSB = struct.unpack("<L", i2c.mem_read(1, sladdr, 0xF8))[0]
UP += (MSB<<16 | LSB<<8 | XLSB) >> (8 - oss)
return UP/3
def calc_true_temp():
UT = read_BMP180_temp()
x1 = (UT - data['AC6']) * data['AC5']>>15 # data>>15 == data / 2**15
x2 = (data['MC']<<11) / (x1 + data['MD']) # data<<11 == data * 2**11
b5 = x1 + x2
tempC = round(((b5 + 8) / pow(2, 4)) * 0.1, 1)
return tempC, b5
def calc_true_pressure():
UP = read_BMP180_pressure()
b5 = calc_true_temp()[1]
b6 = b5 - 4000
# calculate b3
x1 = int(b6 * b6)>>12
x1 *= data['B2']
x1 >>= 11
x2 = int(data['AC2'] * b6)>>11
x3 = x1 + x2
b3 = (((data['AC1'] * 4 + x3) * 2 ** oss) + 2) / 4
# calculate b4
x1 = int(data['AC3'] * b6)>>13
x2 = (data['B1'] *(int(b6 * b6)>>12))>>16
x3 = ((x1 + x2) + 2)>>2
b4 = data['AC4'] * (x3 + 32768) / 2**15
b7 = (UP - b3) * (50000>>oss)
if b7 < 0x80000000:
p = (b7*2) / b4
else:
p = (b7 / b4) * 2
x1 = p / 2**8
x1 *= x1
x1 = (x1 * 3038) / 2**16
x2 = (-7357 * p) / 2**16
# pressure in Pa
Pa = p + (x1 + x2 + 3791) / 2**4
return Pa
def calc_meters_hpa():
Pa = calc_true_pressure()
hPa = Pa/100
try:
altitude = 44330 * (1 - (hPa/hPaZero)**(1/5.255) )
return round(altitude, 1), round(hPa, 2)
except NameError:
return hPa
def print_temp_meters():
t = calc_true_temp()[0]
altitude, hPa = calc_meters_hpa()
string = """
temperature = {0}C
Value from Zero point:
altitude = {1}m
pressure = {2}hPa
\n
"""
return string.format(t, altitude, hPa)
# get calibration regs
data = read_E2PROM_regs()
# get Zero point in hPa on system start
hPaZero = calc_meters_hpa()
while True:
while sw():
print(print_temp_meters())
pyb.delay(500)