中职扩展资源包资料:树莓派系列教程12:I2C总线控制BMP180_第1页
中职扩展资源包资料:树莓派系列教程12:I2C总线控制BMP180_第2页
中职扩展资源包资料:树莓派系列教程12:I2C总线控制BMP180_第3页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、通过上一章,相信各位对树莓派I2C编程有一定的了解了,今天我们继续使用I2C来控制BMP180压强传感器。BMP180压强传感器操作原理比较简单,开机先通过I2C读取出AC1,AC2,AC3,AC4,AC5,AC6,B1,B2,MB,MC,MD 等寄存器的值, 这些寄存器的值作为校准时使用。如何读取温度寄存器,压强寄存器的值,根据下图公式算出测得的当前温度和压强。xw« sn 玄”售由 2383 327士 sam 23言 聖呂d -32%I6BNIa6B5*1 q4WJS«_suns-JSned 4=ui- LrMIgnrtIZLstiori; Lm&snqd ss

2、孕口工 shod岁Oft5ll 口 ft黑 MHrsQa5丄戏K2AC¥ 會 1X3UX;X22«i-.5WTA l +去's-8 1.CMD;甬事器/二 s , g* XJIlJr-%B4 " to_2 -Un5_gerxllon9>3 * 32768.- - 2" Bm 呂諾 ng 应 qep ,53:(5WW $ 出二 三电 A =§0t8-NS * ss宀辛叟gd】X1H 更 yxEsi H ? “ sft- . 2*咼 U-TT33:P=*JWI1J":富 * K2gT9E 2*i i *ssv* <2M

3、MCr 去-H Kh Hm*YIH肆 *g心I<!?!IgM 龙glylAF 谭rl«aaflauhc s penMiBcl pgs* =i _* !.: ¥ 上:i 1 匕:LS8U*KLSB2苇 CUJ云卑吕ue p寵urm<K 庐* 口 .1-.K FTUJL 售念 总一dow power n-芸X2W E: is2300-§sS3 rl" 33J5F二 7 请wwM80V74醪 s& ,7B活 粵.n盂5V.-Scerli&rnl>t审gkng§2ned -0n9 unMgnc d 一 2*gFigu

4、晶护 Acraomhfn fQDC5SUE and 弓 ea5uesmes程序具体本章主要讲解 python 程序,使大家熟悉python 编程。关于bcm2835,wiringpi可参看Pioneer600 示例程序。驱动文件 bmp180.py001 import time002 import smbus003004 # BMP085 default address.005 BMP180_I2CADDR= 0x77006007 # Operati ng Modes008 BMP180_ULTRALOWPOWER = 0BMP180_STANDARD 0091010 BMP180_HIGHR

5、ES011 BMP180_ULTRAHIGHRES012013 # BMP085 Registers014 BMP180_CAL_AC1015 BMP180_CAL_AC2016 BMP180_CAL_AC3017 BMP180_CAL_AC4018 BMP180_CAL_AC5019 BMP180_CAL_AC6020 BMP180_CAL_B1=0xAA# RCalibration data (16 bits)=0xAC# RCalibration data (16 bits)=0xAE# RCalibration data (16 bits)=0xB0# RCalibration dat

6、a (16 bits)=0xB2# RCalibration data (16 bits)=0xB4# RCalibration data (16 bits)0xB6# RCalibration data (16 bits)021 BMP180_CAL_B2=0xB8 # RCalibration data (16 bits)022 BMP180_CAL_MB=0xBA # RCalibration data (16 bits)=0xBC # RCalibration data (16 bits)024 BMP180_CAL_MD=0xBE # RCalibratio n data (16 b

7、its)025 BMP180_CONTROL= 0xF4BMP180_TEMPDATA 0260xF6027 BMP180_PRESSUREDATA = 0xF6028029 # Comma nds030 BMP180_READTEMPCMD= 0x2E031 BMP180_READPRESSURECMD = 0x34032033034 class BMP180(object):def _ini t_(self, address=BMP180_I2CADDR, 035036037038039040041042043044045046047048self._mode = mode self._a

8、ddress = address self._bus = smbus.SMBus(l)# Load calibrati on values.self._load_calibrati on()def _read_byte(self,cmd):return self._bus.read_byte_data(self._address,cmd)def _read_u16(self,cmd):MSB = self._bus.read_byte_data(self._address,cmd)LSB = self._bus.read_byte_data(self._address,cmd+1) retur

9、n (MSB << 8) + LSBdef _read_s16(self,cmd): result = self._read_u16(cmd) if result > 32767:result -= 65536 retur n resultdef _write_byte(self,cmd,val):self._bus.write_byte_data(self._address,cmd,val)def _load_calibrati on( self):"load calibrati on"0490500510520530540550560570580590

10、60061# INT16# INT16self.cal_AC1 = self._read_s16(BMP180_CAL_AC1) self.cal_AC2 = self._read_s16(BMP180_CAL_AC2)062063064065066067068069070071072073# UINT16self.cal_AC4 = self._read_u16(BMP180_CAL_AC4)self.cal_AC5 = selfead_u16(BMP180_CAL_AC5)# UINT16self.cal_AC6 = selfead_u16(BMP180_CAL_AC6)# UINT16s

11、elf.cal_B1= selfead_s16(BMP180_CAL_B1)# INT16self.cal_B2= selfead_s16(BMP180_CAL_B2)# INT16self.cal_MB= selfead_s16(BMP180_CAL_MB)# INT16self.cal_MC = selfead_s16(BMP180_CAL_MC)# INT16self.cal_MD= selfead_s16(BMP180_CAL_MD)# INT16def read_raw_temp(self):""”Reads the raw (un compe nsated) t

12、emperature from thecodecs HHIIsen sor.074075076077078079080081082083084085time.sleep(0.005) # Wait 5msMSB = selfead_byte(BMP180_TEMPDATA)LSB = selfead_byte(BMP180_TEMPDATA+1) raw = (MSB << 8) + LSB return rawdef read_raw_pressure(self):""”Reads the raw (un compe nsated) pressure leve

13、l from the sensor.""”self._write_byte(BMP180_CONTROL,BMP180_READPRESSURECMD + (self._mode << 6)if self._mode = BMP180_ULTRALOWPOWER:time.sleep(0.005)elif self._mode = BMP180_HIGHRES:086087088089090091092093094095096097098time.sleep(0.014)elif self._mode = BMP180_ULTRAHIGHRES:time.sle

14、ep(0.026)else:time.sleep(0.008)MSB = selfead_byte(BMP180_PRESSUREDATA)LSB = selfead_byte(BMP180_PRESSUREDATA+1)XLSB = selfead_byte(BMP180_PRESSUREDATA+2) raw = (MSB << 16) + (LSB << 8) + XLSB) >> (8 - self._mode) return rawdef read_temperature(self):""”Gets the compe nsat

15、ed temperature in degrees celsius.""”099100101102103104105106107108109110111UT = self.read_raw_temp()X1 = (UT - self.cal_AC6) * self.cal_AC5) >> 15X2 = (self.cal_MC << 11) / (X1 + self.cal_MD)B5 = X1 + X2 temp = (B5 + 8) >> 4) / 10.0 return tempdef read_pressure(self):&qu

16、ot;"”Gets the compe nsated pressure in Pascals.""”UT = self.read_raw_temp()UP = self.read_raw_pressure()112113114115116117118119120121122123124X1 = (UT - self.cal_AC6) * self.cal_AC5) >> 15X2 = (self.cal_MC << 11) / (X1 + self.cal_MD)B5 = X1 + X2# Pressure Calculatio nsB6

17、= B5 - 4000X1 = (self.cal_B2 * (B6 * B6) >> 12) >> 11X2 = (self.cal_AC2 * B6) >> 11X3 = X1 + X2B3 = (self.cal_AC1 * 4 + X3) << self._mode) + 2) / 4X1 = (self.cal_AC3 * B6) >> 13125126127128129130131132133134135136137X2 = (self.cal_B1 * (B6 * B6) >> 12) >> 16

18、X3 = (X1 + X2) + 2) >> 2B4 = (self.cal_AC4 * (X3 + 32768) >> 15B7 = (UP - B3) * (50000 >> self._mode) if B7 < 0x80000000:p = (B7 * 2) / B4else:p = (B7 / B4) * 2X1 = (p >> 8) * (p >> 8)X1 = (X1 * 3038) >> 16X2 = (-7357 * p) >> 16138139140141142143144145146

19、147148149p = p + (X1 + X2 + 3791) >> 4)return pdef read_altitude(self, sealevel_pa=101325.0):"""Calculates the altitude in meters."""# Calculatio n take n straight from secti on 3.6 of the datasheet.pressure = float(self.read_pressure()altitude = 44330.0 * (1.0 -

20、pow(pressure / sealevel_pa,(1.0/5.255)retur n altitudedef read_sealevel_pressure(self, altitude_m=0.0):""”Calculates the pressure at sealevel whe n give n a knownaltitude in150 meters. Returns a value in Pascals.""”151 pressure = float(self.read_pressure()152 p0 = pressure / pow(

21、1.0 - altitude_m/44330.0, 5.255)153 return p0主文件 bmp180_example.py01 #!/usr/bi n/python0203 import time04 from BMP180 import BMP1800506 # Initialise the BMP085 and use STANDARD mode (default value)07 # bmp = BMP085(0x77, debug=True)08 bmp = BMP180()09# To specify a differe nt operati ng mode, un comme nt one of the 10followi ng:11 # bmp = BMP085(0x77, 0)# ULTRALOWPOWER Mode12 # bmp = BMP085(0x77, 1)# STANDARD Mode13 # bmp = BMP085(0x77, 2)# HIRES Mode14 # bmp = BMP085(0x77, 3)# ULTRAHIRES Mode15 while True:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论