"""
Read BMP280 using i2c
"""
#!/usr/bin/python
#-*- encoding: utf-8 -*-
from periphery import I2C
import sys
import time
ADDRESS = 0x76 # Default device I2C address
DEBUG = 0
def readChipId():
# out = [0xD0, 0x00]
# resp = spi.transfer(out)
# if(DEBUG == 1):print resp
msgs = [I2C.Message([0xD0]), I2C.Message([0],
read=True)]
i2c.transfer(ADDRESS, msgs)
if(DEBUG == 1):print msgs[1].data
ChipId = msgs[1].data[0]
print("ChipId = 0x%x " % ChipId),
if (ChipId == 0x58): print("BMP280")
if (ChipId == 0x60): print("BME280")
def calibration_T(adc_T):
global t_fine
var1 = ((((adc_T >> 3) - (dig_T1<<1)))
* (dig_T2)) >> 11
var2 = (((((adc_T >> 4) - (dig_T1)) *
((adc_T>>4) - (dig_T1))) >> 12) * (dig_T3))
>> 14
t_fine = var1 + var2
t_fine = int32(t_fine)
T = (t_fine * 5 + 128) >> 8
return T
def calibration_P(adc_P):
var1 = ((t_fine)>>1) - 64000
if (DEBUG == 1):print("var1(1) = %d " % var1)
var2 = (((var1>>2) * (var1>>2))
>> 11) * (dig_P6)
if(DEBUG == 1):print("var2(2) = %d " % var2)
var2 = var2 + ((var1*(dig_P5))<<1)
if(DEBUG == 1):print("var2(3) = %d " % var2)
var2 = (var2>>2)+((dig_P4)<<16)
if(DEBUG == 1):print("var2(4) = %d " % var2)
var1 = (((dig_P3 *
(((var1>>2)*(var1>>2)) >> 13))
>>3) + (((dig_P2) * var1)>>1))>>18
if(DEBUG == 1):print("var1(5) = %d " % var1)
var1 = ((((32768+var1))*(dig_P1))>>15)
if(DEBUG == 1):print("var1(6) = %d " % var1)
if (var1 == 0):
return 0
P = ((((1048576)-adc_P)-(var2>>12)))*3125
if(P<0x80000000):
P = (P << 1) / (var1)
if(P>=0x80000000):
P = (P / var1) * 2;
var1 = ((dig_P9) * ((((P>>3) *
(P>>3))>>13)))>>12
var2 = (((P>>2)) * (dig_P8))>>13
if(DEBUG == 1):
print("var1 = %d" % var1),
print("var2 = %d" % var2)
P = (P + ((var1 + var2 + dig_P7) >> 4))
return P
def int16(x):
if x>0xFFFF:
raise OverflowError
if x>0x7FFF:
x=int(0x10000-x)
if x<2147483648:
return -x
else:
return -2147483648
return x
def int32(x):
if x>0xFFFFFFFF:
raise OverflowError
if x>0x7FFFFFFF:
x=int(0x100000000-x)
if x<2147483648:
return -x
else:
return -2147483648
return x
if __name__=="__main__":
t_sb = 5 #stanby 1000ms
filter = 0 #filter O = off
spi3or4 = 0 #SPI 3wire or 4wire, 0=4wire, 1=3wire
osrs_t = 4 #OverSampling Temperature x8
osrs_p = 4 #OverSampling Pressure x8
Mode = 3 #Normal mode
temp_raw = 0
pres_raw = 0
t_fine = 0
argv = sys.argv
argc = len(argv)
if (argc == 1):
device = "/dev/i2c-0"
if (argc == 2):
pin = int(argv[1])
if (pin == 0):
device = "/dev/i2c-0"
if (pin == 1):
device = "/dev/i2c-1"
if (pin == 2):
device = "/dev/i2c-2"
# Open ic2
print "device=",
print device
i2c = I2C(device)
print('Read the contents of the ID register')
readChipId()
# Send a command to the control register[0xF4]
ctrl_meas_reg = (osrs_t << 5) | (osrs_p
<< 2) | Mode
if(DEBUG == 1):print("ctrl_meas_reg = %x" %
ctrl_meas_reg)
# spi.transfer([0xF4,ctrl_meas_reg])
msgs = [I2C.Message([0xF4, ctrl_meas_reg]),
I2C.Message([0], read=False)]
i2c.transfer(ADDRESS, msgs)
# Send a command to the config register[0xF5]
config_reg = (t_sb << 5) |
(filter << 2) | spi3or4
if(DEBUG == 1):print("config_reg = %x " %
config_reg)
# spi.transfer([0xF5,config_reg])
msgs = [I2C.Message([0xF5, config_reg]),
I2C.Message([0], read=False)]
i2c.transfer(ADDRESS, msgs)
print('Read calibration data')
# a = []
# for x in range(0x88, 0xA0):
# a.append(x)
# a.append(0)
# resp = spi.transfer(a)
# if(DEBUG == 1):print resp
cal1 = [0 for i in range(0x88,0xA0)]
msgs = [I2C.Message([0x88]), I2C.Message(cal1,
read=True)]
i2c.transfer(ADDRESS, msgs)
if(DEBUG == 1):print msgs[1].data
resp = []
for x in range(0x88, 0xA0):
resp.append(msgs[1].data[x-0x88])
dig_T1 = resp[1] * 256 + resp[0]
dig_T2 = int16(resp[3] * 256 + resp[2])
dig_T3 = int16(resp[5] * 256 + resp[4])
if(DEBUG == 1):
print("dig_T1 = %d" % dig_T1),
print("dig_T2 = %d" % dig_T2),
print("dig_T3 = %d" % dig_T3)
dig_P1 = resp[7] * 256 + resp[6]
dig_P2 = int16(resp[9] * 256 + resp[8])
dig_P3 = int16(resp[11] * 256 + resp[10])
dig_P4 = int16(resp[13] * 256 + resp[12])
dig_P5 = int16(resp[15] * 256 + resp[14])
dig_P6 = int16(resp[17] * 256 + resp[16])
dig_P7 = int16(resp[19] * 256 + resp[18])
dig_P8 = int16(resp[21] * 256 + resp[20])
dig_P9 = int16(resp[23] * 256 + resp[22])
if(DEBUG == 1):
print("dig_P1 = %d" % dig_P1),
print("dig_P2 = %d" % dig_P2),
print("dig_P3 = %d" % dig_P3)
print("dig_P4 = %d" % dig_P4),
print("dig_P5 = %d" % dig_P5),
print("dig_P6 = %d" % dig_P6)
print("dig_P7 = %d" % dig_P7),
print("dig_P8 = %d" % dig_P8),
print("dig_P9 = %d" % dig_P9)
print('Read temp and pressure')
# a = []
# for x in range(0xF7, 0xFD):
# a.append(x)
# a.append(0)
# resp = spi.transfer(a)
# if(DEBUG == 1):print resp
data = [0 for i in range(0xF7,0xFD)]
msgs = [I2C.Message([0xF7]), I2C.Message(data,
read=True)]
i2c.transfer(ADDRESS, msgs)
if(DEBUG == 1):print msgs[1].data
resp = []
for x in range(0xF7, 0xFD):
resp.append(msgs[1].data[x-0xF7])
pres_raw = (resp[0] << 12) | (resp[1]
<< 4) | (resp[2] >> 4) #0xF7,
msb+lsb+xlsb=19bit
if(DEBUG == 1):print("pres_raw = %d " % pres_raw)
temp_raw = (resp[3] << 12) | (resp[4]
<< 4) | (resp[5] >> 4) #0xFA,
msb+lsb+xlsb=19bit
if(DEBUG == 1):print("temp_raw = %d " % temp_raw)
temp_cal = calibration_T(temp_raw)
if(DEBUG == 1):print("temp_cal = %d " % temp_cal)
press_cal = calibration_P(pres_raw)
if(DEBUG == 1):print("press_cal = %d " % press_cal)
temp_act = temp_cal / 100.0
press_act = press_cal / 100.0
print("-----------------------");
print("Temperature = %f *C" % temp_act)
print("Pressure = %f hPa" % press_act)
|