[rasp.] 기압 센서 BMP085 설정

기압 센서를 이용해 현재 온도와 기압, 해발고도를 측정할 수 있다.

 

#!/usr/bin/python 

 

import smbus

import time

from ctypes import c_short

from ctypes import c_uint32

 

bus = smbus.SMBus(1)

 

DEV_ADDR = 0x77

 

#CALIBRATION DATA ADDR

AC1M = 0xAA

AC1L = 0xAB

AC2M = 0xAC

AC2L = 0xAD

AC3M = 0xAE

AC3L = 0xAF

AC4M = 0xB0

AC4L = 0xB1

AC5M = 0xB2

AC5L = 0xB3

AC6M = 0xB4

AC6L = 0xB5

B1M = 0xB6

B1L = 0xB7

B2M = 0xB8

B2L = 0xB9

MBM = 0xBA

MBL = 0xBB

MCM = 0xBC

MCL = 0xBD

MDM = 0xBE

MDL = 0xBF

 

# get calibraion data

AC1_m = bus.read_byte_data(DEV_ADDR,AC1M)

AC1_l = bus.read_byte_data(DEV_ADDR,AC1L)

AC1 = (AC1_m << 8) + AC1_l

AC1 = c_short(AC1).value

AC2_m = bus.read_byte_data(DEV_ADDR,AC2M)

AC2_l = bus.read_byte_data(DEV_ADDR,AC2L)

AC2 = (AC2_m << 8) + AC2_l

AC2 = c_short(AC2).value

AC3_m = bus.read_byte_data(DEV_ADDR,AC3M)

AC3_l = bus.read_byte_data(DEV_ADDR,AC3L)

AC3 =( AC3_m << 8 )+ AC3_l

AC3 = c_short(AC3).value

AC4_m = bus.read_byte_data(DEV_ADDR,AC4M)

AC4_l = bus.read_byte_data(DEV_ADDR,AC4L)

AC4 =( AC4_m << 8) + AC4_l

AC4 = c_short(AC4).value

AC5_m = bus.read_byte_data(DEV_ADDR,AC5M)

AC5_l = bus.read_byte_data(DEV_ADDR,AC5L)

AC5 =( AC5_m << 8) + AC5_l

AC5 = c_short(AC5).value

AC6_m = bus.read_byte_data(DEV_ADDR,AC6M)

AC6_l = bus.read_byte_data(DEV_ADDR,AC6L)

AC6 =( AC6_m << 8) + AC6_l

AC6 = c_short(AC6).value

B1_m = bus.read_byte_data(DEV_ADDR,B1M)

B1_l = bus.read_byte_data(DEV_ADDR,B1L)

B1 = (B1_m << 8) + B1_l

B1 = c_short(B1).value

B2_m = bus.read_byte_data(DEV_ADDR,B2M)

B2_l = bus.read_byte_data(DEV_ADDR,B2L)

B2 = (B2_m << 8) + B2_l

B2 = c_short(B2).value

MB_m = bus.read_byte_data(DEV_ADDR,MBM)

MB_l = bus.read_byte_data(DEV_ADDR,MBL)

MB = (MB_m << 8) + MB_l

MB = c_short(MB).value

MC_m = bus.read_byte_data(DEV_ADDR,MCM)

MC_l = bus.read_byte_data(DEV_ADDR,MCL)

MC = (MC_m << 8) + MC_l

MC = c_short(MC).value

MD_m = bus.read_byte_data(DEV_ADDR,MDM)

MD_l = bus.read_byte_data(DEV_ADDR,MDL)

MD = (MD_m  <<8)+ MD_l

MD = c_short(MD).value

 

print "AC1 : ", AC1

print "AC2 : ", AC2

print "AC3 : ", AC3

print "AC4 : ", AC4

print "AC5 : ", AC5

print "AC6 : ", AC6

print "B1  : ", B1

print "B2  : ", B2

print "MB  : ", MB

print "MC  : ", MC

print "MD  : ", MD

 

 

# READ Sensor

 

bus.write_byte_data(DEV_ADDR,0xF4,0x2E)

time.sleep(.01)

UT_m = bus.read_byte_data(DEV_ADDR,0xF6)

UT_l = bus.read_byte_data(DEV_ADDR,0xF7)

UT = (UT_m << 8) + UT_l

 

print "UT : ", UT

 

oss = 3

bus.write_byte_data(DEV_ADDR,0xF4,(0x34+(oss<<6)))

time.sleep(.018)

UP_m = bus.read_byte_data(DEV_ADDR,0xF6)

UP_l = bus.read_byte_data(DEV_ADDR,0xF7)

UP_x = bus.read_byte_data(DEV_ADDR,0xF8)

UP = ((UP_m << 16) + (UP_l << 8) + UP_x) >> (8-oss)

 

print "UP : ", UP

 

X1 = (UT-AC6)*AC5>>15

X2 = (MC << 11) / (X1+MD)

B5 = X1 + X2

T = (B5 + 8) >> 4

 

print "T : ", T # temperature

 

B6 = B5 – 4000

X1 = (B2*(B6*(B6>>12)))>>11

X2 = AC2 * B6>>11

X3 = X1 + X2

B3 = ((AC1*4+X3)<<oss+2)>>2

X1 = AC3 * B6 >> 13

X2 = (B1*(B6*B6>>12))>>16

X3 = ((X1+X2)+2)>>2

B4 = AC4 * (c_uint32(X3+32768).value)>>15

B7 = (c_uint32(UP).value – B3)*(50000>>oss)

if B7<0x80000000 :

        p = (B7*2)/B4

else :

        p = (B7/B4)*2

 

X1 = (p>>8) * (p>>8)

X1 = (X1 * 3038) >> 16

X2 = (-7357 * p) >> 16

 

p = p + ((X1 + X2 + 3791)>>4)

 

print "p : ",p # pressure

 

p0 = 101325 # 일반적으로 쓰이는 해수면 기준 hpa

k = float(float(p)/float(p0))

kk = float(1 – pow(k,0.190295))

altitude = float(44330) * kk

 

print "Altitude : ",altitude # 해발고도

 

 

 

Leave a Reply