Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit baf2204

Browse files
author
raspberry@adafruit.com
committed
start with i2c and bmp085 code
0 parents  commit baf2204

File tree

5 files changed

+346
-0
lines changed

5 files changed

+346
-0
lines changed

Adafruit_BMP085/Adafruit_BMP085.py

+241
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
from Adafruit_I2C import Adafruit_I2C
5+
6+
# ===========================================================================
7+
# BMP085 Class
8+
# ===========================================================================
9+
10+
class BMP085 :
11+
i2c = None
12+
13+
# Operating Modes
14+
__BMP085_ULTRALOWPOWER = 0
15+
__BMP085_STANDARD = 1
16+
__BMP085_HIGHRES = 2
17+
__BMP085_ULTRAHIGHRES = 3
18+
19+
# BMP085 Registers
20+
__BMP085_CAL_AC1 = 0xAA # R Calibration data (16 bits)
21+
__BMP085_CAL_AC2 = 0xAC # R Calibration data (16 bits)
22+
__BMP085_CAL_AC3 = 0xAE # R Calibration data (16 bits)
23+
__BMP085_CAL_AC4 = 0xB0 # R Calibration data (16 bits)
24+
__BMP085_CAL_AC5 = 0xB2 # R Calibration data (16 bits)
25+
__BMP085_CAL_AC6 = 0xB4 # R Calibration data (16 bits)
26+
__BMP085_CAL_B1 = 0xB6 # R Calibration data (16 bits)
27+
__BMP085_CAL_B2 = 0xB8 # R Calibration data (16 bits)
28+
__BMP085_CAL_MB = 0xBA # R Calibration data (16 bits)
29+
__BMP085_CAL_MC = 0xBC # R Calibration data (16 bits)
30+
__BMP085_CAL_MD = 0xBE # R Calibration data (16 bits)
31+
__BMP085_CONTROL = 0xF4
32+
__BMP085_TEMPDATA = 0xF6
33+
__BMP085_PRESSUREDATA = 0xF6
34+
__BMP085_READTEMPCMD = 0x2E
35+
__BMP085_READPRESSURECMD = 0x34
36+
37+
# Private Fields
38+
_cal_AC1 = 0
39+
_cal_AC2 = 0
40+
_cal_AC3 = 0
41+
_cal_AC4 = 0
42+
_cal_AC5 = 0
43+
_cal_AC6 = 0
44+
_cal_B1 = 0
45+
_cal_B2 = 0
46+
_cal_MB = 0
47+
_cal_MC = 0
48+
_cal_MD = 0
49+
50+
# Constructor
51+
def __init__(self, address=0x77, mode=1, debug=False):
52+
self.i2c = Adafruit_I2C(address)
53+
54+
self.address = address
55+
self.debug = debug
56+
# Make sure the specified mode is in the appropriate range
57+
if ((mode < 0) | (mode > 3)):
58+
if (self.debug):
59+
print "Invalid Mode: Using STANDARD by default"
60+
self.mode = self.__BMP085_STANDARD
61+
else:
62+
self.mode = mode
63+
# Read the calibration data
64+
self.readCalibrationData()
65+
66+
def readCalibrationData(self):
67+
"Reads the calibration data from the IC"
68+
self._cal_AC1 = self.i2c.readS16(self.__BMP085_CAL_AC1) # INT16
69+
self._cal_AC2 = self.i2c.readS16(self.__BMP085_CAL_AC2) # INT16
70+
self._cal_AC3 = self.i2c.readS16(self.__BMP085_CAL_AC3) # INT16
71+
self._cal_AC4 = self.i2c.readU16(self.__BMP085_CAL_AC4) # UINT16
72+
self._cal_AC5 = self.i2c.readU16(self.__BMP085_CAL_AC5) # UINT16
73+
self._cal_AC6 = self.i2c.readU16(self.__BMP085_CAL_AC6) # UINT16
74+
self._cal_B1 = self.i2c.readS16(self.__BMP085_CAL_B1) # INT16
75+
self._cal_B2 = self.i2c.readS16(self.__BMP085_CAL_B2) # INT16
76+
self._cal_MB = self.i2c.readS16(self.__BMP085_CAL_MB) # INT16
77+
self._cal_MC = self.i2c.readS16(self.__BMP085_CAL_MC) # INT16
78+
self._cal_MD = self.i2c.readS16(self.__BMP085_CAL_MD) # INT16
79+
if (self.debug):
80+
self.showCalibrationData()
81+
82+
def showCalibrationData(self):
83+
"Displays the calibration values for debugging purposes"
84+
print "DBG: AC1 = %6d" % (self._cal_AC1)
85+
print "DBG: AC2 = %6d" % (self._cal_AC2)
86+
print "DBG: AC3 = %6d" % (self._cal_AC3)
87+
print "DBG: AC4 = %6d" % (self._cal_AC4)
88+
print "DBG: AC5 = %6d" % (self._cal_AC5)
89+
print "DBG: AC6 = %6d" % (self._cal_AC6)
90+
print "DBG: B1 = %6d" % (self._cal_B1)
91+
print "DBG: B2 = %6d" % (self._cal_B2)
92+
print "DBG: MB = %6d" % (self._cal_MB)
93+
print "DBG: MC = %6d" % (self._cal_MC)
94+
print "DBG: MD = %6d" % (self._cal_MD)
95+
96+
def readRawTemp(self):
97+
"Reads the raw (uncompensated) temperature from the sensor"
98+
self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READTEMPCMD)
99+
time.sleep(0.005) # Wait 5ms
100+
raw = self.i2c.readU16(self.__BMP085_TEMPDATA)
101+
if (self.debug):
102+
print "DBG: Raw Temp: 0x%04X (%d)" % (raw & 0xFFFF, raw)
103+
return raw
104+
105+
def readRawPressure(self):
106+
"Reads the raw (uncompensated) pressure level from the sensor"
107+
self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READPRESSURECMD + (self.mode << 6))
108+
if (self.mode == self.__BMP085_ULTRALOWPOWER):
109+
time.sleep(0.005)
110+
elif (self.mode == self.__BMP085_HIGHRES):
111+
time.sleep(0.014)
112+
elif (self.mode == self.__BMP085_ULTRAHIGHRES):
113+
time.sleep(0.026)
114+
else:
115+
time.sleep(0.008)
116+
msb = self.i2c.readU8(self.__BMP085_PRESSUREDATA)
117+
lsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+1)
118+
xlsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+2)
119+
raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode)
120+
if (self.debug):
121+
print "DBG: Raw Pressure: 0x%04X (%d)" % (raw & 0xFFFF, raw)
122+
return raw
123+
124+
def readTemperature(self):
125+
"Gets the compensated temperature in degrees celcius"
126+
UT = 0
127+
X1 = 0
128+
X2 = 0
129+
B5 = 0
130+
temp = 0.0
131+
132+
# Read raw temp before aligning it with the calibration values
133+
UT = self.readRawTemp()
134+
X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
135+
X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
136+
B5 = X1 + X2
137+
temp = ((B5 + 8) >> 4) / 10.0
138+
if (self.debug):
139+
print "DBG: Calibrated temperature = %f C" % temp
140+
return temp
141+
142+
def readPressure(self):
143+
"Gets the compensated pressure in pascal"
144+
UT = 0
145+
UP = 0
146+
B3 = 0
147+
B5 = 0
148+
B6 = 0
149+
X1 = 0
150+
X2 = 0
151+
X3 = 0
152+
p = 0
153+
B4 = 0
154+
B7 = 0
155+
156+
UT = self.readRawTemp()
157+
UP = self.readRawPressure()
158+
159+
# You can use the datasheet values to test the conversion results
160+
# dsValues = True
161+
dsValues = False
162+
163+
if (dsValues):
164+
UT = 27898
165+
UP = 23843
166+
self._cal_AC6 = 23153
167+
self._cal_AC5 = 32757
168+
self._cal_MC = -8711
169+
self._cal_MD = 2868
170+
self._cal_B1 = 6190
171+
self._cal_B2 = 4
172+
self._cal_AC3 = -14383
173+
self._cal_AC2 = -72
174+
self._cal_AC1 = 408
175+
self._cal_AC4 = 32741
176+
self.mode = self.__BMP085_ULTRALOWPOWER
177+
if (self.debug):
178+
self.showCalibrationData()
179+
180+
# True Temperature Calculations
181+
X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
182+
X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
183+
B5 = X1 + X2
184+
if (self.debug):
185+
print "DBG: X1 = %d" % (X1)
186+
print "DBG: X2 = %d" % (X2)
187+
print "DBG: B5 = %d" % (B5)
188+
print "DBG: True Temperature = %.2f C" % (((B5 + 8) >> 4) / 10.0)
189+
190+
# Pressure Calculations
191+
B6 = B5 - 4000
192+
X1 = (self._cal_B2 * (B6 * B6) >> 12) >> 11
193+
X2 = (self._cal_AC2 * B6) >> 11
194+
X3 = X1 + X2
195+
B3 = (((self._cal_AC1 * 4 + X3) << self.mode) + 2) / 4
196+
if (self.debug):
197+
print "DBG: B6 = %d" % (B6)
198+
print "DBG: X1 = %d" % (X1)
199+
print "DBG: X2 = %d" % (X2)
200+
print "DBG: B3 = %d" % (B3)
201+
202+
X1 = (self._cal_AC3 * B6) >> 13
203+
X2 = (self._cal_B1 * ((B6 * B6) >> 12)) >> 16
204+
X3 = ((X1 + X2) + 2) >> 2
205+
B4 = (self._cal_AC4 * (X3 + 32768)) >> 15
206+
B7 = (UP - B3) * (50000 >> self.mode)
207+
if (self.debug):
208+
print "DBG: X1 = %d" % (X1)
209+
print "DBG: X2 = %d" % (X2)
210+
print "DBG: B4 = %d" % (B4)
211+
print "DBG: B7 = %d" % (B7)
212+
213+
if (B7 < 0x80000000):
214+
p = (B7 * 2) / B4
215+
else:
216+
p = (B7 / B4) * 2
217+
218+
X1 = (p >> 8) * (p >> 8)
219+
X1 = (X1 * 3038) >> 16
220+
X2 = (-7375 * p) >> 16
221+
if (self.debug):
222+
print "DBG: p = %d" % (p)
223+
print "DBG: X1 = %d" % (X1)
224+
print "DBG: X2 = %d" % (X2)
225+
226+
p = p + ((X1 + X2 + 3791) >> 4)
227+
if (self.debug):
228+
print "DBG: Pressure = %d Pa" % (p)
229+
230+
return p
231+
232+
def readAltitude(self, seaLevelPressure=101325):
233+
"Calculates the altitude in meters"
234+
altitude = 0.0
235+
pressure = float(self.readPressure())
236+
altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903))
237+
if (self.debug):
238+
print "DBG: Altitude = %d" % (altitude)
239+
return altitude
240+
241+
return 0
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
3+
from Adafruit_BMP085 import BMP085
4+
5+
# ===========================================================================
6+
# Example Code
7+
# ===========================================================================
8+
9+
# Initialise the BMP085 and use STANDARD mode (default value)
10+
# bmp = BMP085(0x77, debug=True)
11+
bmp = BMP085(0x77)
12+
13+
# To specify a different operating mode, uncomment one of the following:
14+
# bmp = BMP085(0x77, 0) # ULTRALOWPOWER Mode
15+
# bmp = BMP085(0x77, 1) # STANDARD Mode
16+
# bmp = BMP085(0x77, 2) # HIRES Mode
17+
# bmp = BMP085(0x77, 3) # ULTRAHIRES Mode
18+
19+
temp = bmp.readTemperature()
20+
pressure = bmp.readPressure()
21+
altitude = bmp.readAltitude()
22+
23+
print "Temperature: %.2f C" % temp
24+
print "Pressure: %.2f hPa" % (pressure / 100.0)
25+
print "Altitude: %.2f" % altitude

Adafruit_BMP085/Adafruit_I2C.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Adafruit_I2C/Adafruit_I2C.py

Adafruit_I2C/Adafruit_I2C.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/python
2+
3+
import smbus
4+
5+
# ===========================================================================
6+
# Adafruit_I2C Base Class
7+
# ===========================================================================
8+
9+
class Adafruit_I2C :
10+
11+
def __init__(self, address, bus=smbus.SMBus(0), debug=False):
12+
self.address = address
13+
self.bus = bus
14+
self.debug = debug
15+
16+
def write8(self, reg, value):
17+
"Writes an 8-bit value to the specified register/address"
18+
try:
19+
self.bus.write_byte_data(self.address, reg, value)
20+
if (self.debug):
21+
print("I2C: Wrote 0x%02X to device 0x%02X" % (self.address, reg))
22+
except IOError, err:
23+
print "Error accessing 0x%02X: Check your I2C address" % self.address
24+
return -1
25+
26+
def readU8(self, reg):
27+
"Read an unsigned byte from the I2C device"
28+
try:
29+
result = self.bus.read_byte_data(self.address, reg)
30+
if (self.debug):
31+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFF, reg)
32+
return result
33+
except IOError, err:
34+
print "Error accessing 0x%02X: Check your I2C address" % self.address
35+
return -1
36+
37+
def readS8(self, reg):
38+
"Reads a signed byte from the I2C device"
39+
try:
40+
result = self.bus.read_byte_data(self.address, reg)
41+
if (self.debug):
42+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFF, reg)
43+
if (result > 127):
44+
return result - 256
45+
else:
46+
return result
47+
except IOError, err:
48+
print "Error accessing 0x%02X: Check your I2C address" % self.address
49+
return -1
50+
51+
def readU16(self, reg):
52+
"Reads an unsigned 16-bit value from the I2C device"
53+
try:
54+
hibyte = self.bus.read_byte_data(self.address, reg)
55+
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
56+
if (self.debug):
57+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
58+
return result
59+
except IOError, err:
60+
print "Error accessing 0x%02X: Check your I2C address" % self.address
61+
return -1
62+
63+
def readS16(self, reg):
64+
"Reads a signed 16-bit value from the I2C device"
65+
try:
66+
hibyte = self.bus.read_byte_data(self.address, reg)
67+
if (hibyte > 127):
68+
hibyte -= 256
69+
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
70+
if (self.debug):
71+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
72+
return result
73+
except IOError, err:
74+
print "Error accessing 0x%02X: Check your I2C address" % self.address
75+
return -1

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Raspberry-Pi
2+
============
3+
4+
my pi stuff

0 commit comments

Comments
 (0)