"""
Read BMP280 using Hardware SPI
"""
#!/usr/bin/python
#-*- encoding: utf-8 -*-
from periphery import SPI
import sys
import time
import os.path
DEBUG = 0
def readChipId():
out = [0xD0, 0x00]
resp = spi.transfer(out)
if(DEBUG == 1):print resp
ChipId = resp[1]
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
# Open spidev with mode 0 and max speed 1MHz
if os.path.exists("/dev/spidev0.0"):
print "open /dev/spidev0.0"
spi = SPI("/dev/spidev0.0", 0, 1000000)
if os.path.exists("/dev/spidev1.0"):
print "open /dev/spidev1.0"
spi = SPI("/dev/spidev1.0", 0, 1000000)
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])
# 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])
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
dig_T1 = resp[2] * 256 + resp[1]
dig_T2 = int16(resp[4] * 256 + resp[3])
dig_T3 = int16(resp[6] * 256 + resp[5])
if(DEBUG == 1):
print("dig_T1 = %d" % dig_T1),
print("dig_T2 = %d" % dig_T2),
print("dig_T3 = %d" % dig_T3)
dig_P1 = resp[8] * 256 + resp[7]
dig_P2 = int16(resp[10] * 256 + resp[9])
dig_P3 = int16(resp[12] * 256 + resp[11])
dig_P4 = int16(resp[14] * 256 + resp[13])
dig_P5 = int16(resp[16] * 256 + resp[15])
dig_P6 = int16(resp[18] * 256 + resp[17])
dig_P7 = int16(resp[20] * 256 + resp[19])
dig_P8 = int16(resp[22] * 256 + resp[21])
dig_P9 = int16(resp[24] * 256 + resp[23])
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
pres_raw = (resp[1] << 12) | (resp[2]
<< 4) | (resp[3] >> 4) #0xF7,
msb+lsb+xlsb=19bit
if(DEBUG == 1):print("pres_raw = %d " % pres_raw)
temp_raw = (resp[4] << 12) | (resp[5]
<< 4) | (resp[6] >> 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)
|