中職擴(kuò)展資源包資料:樹莓派系列教程12:I2C總線控制BMP180_第1頁(yè)
中職擴(kuò)展資源包資料:樹莓派系列教程12:I2C總線控制BMP180_第2頁(yè)
中職擴(kuò)展資源包資料:樹莓派系列教程12:I2C總線控制BMP180_第3頁(yè)
已閱讀5頁(yè),還剩16頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、通過上一章,相信各位對(duì)樹莓派I2C編程有一定的了解了,今天我們繼續(xù)使用I2C來控制BMP180壓強(qiáng)傳感器。BMP180壓強(qiáng)傳感器操作原理比較簡(jiǎn)單,開機(jī)先通過I2C讀取出AC1,AC2,AC3,AC4,AC5,AC6,B1,B2,MB,MC,MD 等寄存器的值, 這些寄存器的值作為校準(zhǔn)時(shí)使用。如何讀取溫度寄存器,壓強(qiáng)寄存器的值,根據(jù)下圖公式算出測(cè)得的當(dāng)前溫度和壓強(qiáng)。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¥ 會(huì) 1X3UX;X22«i-.5WTA l +去's-8 1.CMD;甬事器/二 s , g* XJIlJr-%B4 " to_2 -Un5_gerxllon9>3 * 32768.- - 2" Bm 呂諾 ng 應(yī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 請(qǐng)wwM80V74醪 s& ,7B活 粵.n盂5V.-Scerli&rnl>t審gkng§2ned -0n9 unMgnc d 一 2*gFigu

4、晶護(hù) Acraomhfn fQDC5SUE and 弓 ea5uesmes程序具體本章主要講解 python 程序,使大家熟悉python 編程。關(guān)于bcm2835,wiringpi可參看Pioneer600 示例程序。驅(qū)動(dòng)文件 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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論