-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMS5611.py
111 lines (87 loc) · 2.71 KB
/
MS5611.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
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# MS5611
# This code is designed to work with the MS5611.
from OmegaExpansion import onionI2C
import time
#Device Address = 0x77
addr = 0x77
seaPressure = 1013.25
# Get I2C bus
i2c = onionI2C.OnionI2C()
# MS5611_01BXXX address, 0x77(119)
# 0x1E(30) Reset command
data = [0x1E]
i2c.write(addr, data)
time.sleep(0.5)
# Read 12 bytes of calibration data
# Read pressure sensitivity
data = i2c.readBytes(addr, 0xA2, 2)
C1 = data[0] * 256 + data[1]
# Read pressure offset
data = i2c.readBytes(addr, 0xA4, 2)
C2 = data[0] * 256 + data[1]
# Read temperature coefficient of pressure sensitivity
data = i2c.readBytes(addr, 0xA6, 2)
C3 = data[0] * 256 + data[1]
# Read temperature coefficient of pressure offset
data = i2c.readBytes(addr, 0xA8, 2)
C4 = data[0] * 256 + data[1]
# Read reference temperature
data = i2c.readBytes(addr, 0xAA, 2)
C5 = data[0] * 256 + data[1]
# Read temperature coefficient of the temperature
data = i2c.readBytes(addr, 0xAC, 2)
C6 = data[0] * 256 + data[1]
# MS5611_01BXXX address, 0x77(119)
# 0x40(64) Pressure conversion(OSR = 256) command
data = [0x40]
i2c.write(addr, data)
time.sleep(0.5)
# Read digital pressure value
# Read data back from 0x00(0), 3 bytes
# D1 MSB2, D1 MSB1, D1 LSB
value = i2c.readBytes(addr, 0x00, 3)
D1 = value[0] * 65536 + value[1] * 256 + value[2]
# MS5611_01BXXX address, 0x77(119)
# 0x50(64) Temperature conversion(OSR = 256) command
data = [0x50]
i2c.write(addr, data)
time.sleep(0.5)
# Read digital temperature value
# Read data back from 0x00(0), 3 bytes
# D2 MSB2, D2 MSB1, D2 LSB
value = i2c.readBytes(addr, 0x00, 3)
D2 = value[0] * 65536 + value[1] * 256 + value[2]
dT = D2 - C5 * 256
TEMP = 2000 + dT * C6 / 8388608
OFF = C2 * 65536 + (C4 * dT) / 128
SENS = C1 * 32768 + (C3 * dT ) / 256
T2 = 0
OFF2 = 0
SENS2 = 0
if TEMP >= 2000 :
T2 = 0
OFF2 = 0
SENS2 = 0
elif TEMP < 2000 :
T2 = (dT * dT) / 2147483648
OFF2 = 5 * ((TEMP - 2000) * (TEMP - 2000)) / 2
SENS2 = 5 * ((TEMP - 2000) * (TEMP - 2000)) / 4
if TEMP < -1500 :
OFF2 = OFF2 + 7 * ((TEMP + 1500) * (TEMP + 1500))
SENS2 = SENS2 + 11 * ((TEMP + 1500) * (TEMP + 1500)) / 2
TEMP = TEMP - T2
OFF = OFF - OFF2
SENS = SENS - SENS2
pressure = ((((D1 * SENS) / 2097152) - OFF) / 32768.0) / 100.0
cTemp = TEMP / 100.0
fTemp = cTemp * 1.8 + 32
#Get Altitude
altitude = ((pow((seaPress / pressure), 1/5.257) - 1.0) * (cTemp + 273.15)) / 0.0065
# Output data to screen
print("Sea Pressure : %.2f mbar" %seaPressure)
print("Pressure : %.2f mbar" %pressure)
print("Temperature in Celsius : %.2f C" %cTemp)
print("Temperature in Fahrenheit : %.2f F" %fTemp)
print("Altitude in meters : %.2f m" %altitude)