這篇文章給大家分享的是有關(guān) 樹(shù)莓派怎么用Python實(shí)現(xiàn)i2c的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比衢江網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式衢江網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋衢江地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴(lài)。
首先樹(shù)莓派得安裝 python-smbus, i2c-tools,
然后修改文件:sudo nano /etc/modules,添加上 i2c-bcm2708 和i2c-dev 這兩行,Raspbian還需要在raspi-config中激活i2c.
用 sudo i2cdetect -y 1 查看設(shè)備地址,
例子1:LCD2004,設(shè)備地址 為0x27;
先寫(xiě)個(gè)驅(qū)動(dòng)調(diào)用程序 i2c_driver_lcd.py
import smbus from time import * # LCD Address ADDRESS = 0x27 # commands LCD_CLEARDISPLAY = 0x01 LCD_RETURNHOME = 0x02 LCD_ENTRYMODESET = 0x04 LCD_DISPLAYCONTROL = 0x08 LCD_CURSORSHIFT = 0x10 LCD_FUNCTIONSET = 0x20 LCD_SETCGRAMADDR = 0x40 LCD_SETDDRAMADDR = 0x80 # flags for display entry mode LCD_ENTRYRIGHT = 0x00 LCD_ENTRYLEFT = 0x02 LCD_ENTRYSHIFTINCREMENT = 0x01 LCD_ENTRYSHIFTDECREMENT = 0x00 # flags for display on/off control LCD_DISPLAYON = 0x04 LCD_DISPLAYOFF = 0x00 LCD_CURSORON = 0x02 LCD_CURSOROFF = 0x00 LCD_BLINKON = 0x01 LCD_BLINKOFF = 0x00 # flags for display/cursor shift LCD_DISPLAYMOVE = 0x08 LCD_CURSORMOVE = 0x00 LCD_MOVERIGHT = 0x04 LCD_MOVELEFT = 0x00 # flags for function set LCD_8BITMODE = 0x10 LCD_4BITMODE = 0x00 LCD_2LINE = 0x08 LCD_1LINE = 0x00 LCD_5x10DOTS = 0x04 LCD_5x8DOTS = 0x00 # flags for backlight control LCD_BACKLIGHT = 0x08 LCD_NOBACKLIGHT = 0x00 # set init LCD BACKLIGHT ON or OFF def lcd_backlight(lcdbl=1): if lcdbl == 0 : return LCD_NOBACKLIGHT return LCD_BACKLIGHT En = 0b00000100 # Enable bit Rw = 0b00000010 # Read/Write bit Rs = 0b00000001 # Register select bit class lcd(object): #initializes objects and lcd def __init__(self,lcd_bl,port=1): self.addr = ADDRESS self.bus = smbus.SMBus(port) self.lcd_bl = lcd_bl self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x02) self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) sleep(0.2) def set_lcd(self,set_lcdbl): self.lcd_bl = set_lcdbl # clocks EN to latch command def lcd_strobe(self, data): self.write_cmd(data | En | lcd_backlight(self.lcd_bl)) sleep(.0005) self.write_cmd(((data & ~En) | lcd_backlight(self.lcd_bl))) sleep(.0001) def lcd_write_four_bits(self, data): self.write_cmd(data | lcd_backlight(self.lcd_bl)) self.lcd_strobe(data) # write a command to lcd def lcd_write(self, cmd, mode=0): self.lcd_write_four_bits(mode | (cmd & 0xF0)) self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0)) # put string function def lcd_display_string(self, string, raw=1, col=1): if raw == 1: self.lcd_write(0x80+col-1) if raw == 2: self.lcd_write(0xC0+col-1) if raw == 3: self.lcd_write(0x94+col-1) if raw == 4: self.lcd_write(0xD4+col-1) for char in string: self.lcd_write(ord(char), Rs) # clear lcd and set to home def lcd_clear(self): self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_RETURNHOME) # Write a single command def write_cmd(self, cmd): self.bus.write_byte(self.addr, cmd) sleep(0.0001)
然后寫(xiě)個(gè)lcd_main.py
import i2c_driver_lcd as lcd_driver from time import * lcd = lcd_driver.lcd(0,1)#設(shè)置背光開(kāi)關(guān),port=1 lcd.lcd_display_string("hello raspberry", 1,3) s= raw_input("input:") lcd.set_lcd(1)#設(shè)置背光開(kāi)關(guān) lcd.lcd_display_string(s, 2, 2)
例子2:hmc5883l ; 設(shè)備地址為 0x1e
先寫(xiě)個(gè)驅(qū)動(dòng)調(diào)用程序 i2c_driver_hmc5883l.py
#!/usr/bin/env python # vim: set fileencoding=UTF-8 : # HMC5888L Magnetometer (Digital Compass) wrapper class # Based on https://bitbucket.org/thinkbowl/i2clibraries/src/14683feb0f96, # but uses smbus rather than quick2wire and sets some different init # params. import smbus import math class hmc5883l: __scales = { 0.88: [0, 0.73], 1.30: [1, 0.92], 1.90: [2, 1.22], 2.50: [3, 1.52], 4.00: [4, 2.27], 4.70: [5, 2.56], 5.60: [6, 3.03], 8.10: [7, 4.35], } def __init__(self, port=1, address=0x1E, gauss=1.3, declination=(0,0)): self.bus = smbus.SMBus(port) self.address = address (degrees, minutes) = declination self.__declDegrees = degrees self.__declMinutes = minutes self.__declination = (degrees + minutes / 60) * math.pi / 180 (reg, self.__scale) = self.__scales[gauss] self.bus.write_byte_data(self.address, 0x00, 0x70) # 8 Average, 15 Hz, normal measurement self.bus.write_byte_data(self.address, 0x01, reg << 5) # Scale self.bus.write_byte_data(self.address, 0x02, 0x00) # Continuous measurement def declination(self): return (self.__declDegrees, self.__declMinutes) def twos_complement(self, val, len): # Convert twos compliment to integer if (val & (1 << len - 1)): val = val - (1<2 * math.pi: headingRad -= 2 * math.pi # Convert to degrees from radians headingDeg = headingRad * 180 / math.pi return headingDeg def degrees(self, headingDeg): degrees = math.floor(headingDeg) minutes = round((headingDeg - degrees) * 60) return (degrees, minutes) def __str__(self): (x, y, z) = self.axes() return "Axis X: " + str(x) + "\n" \ "Axis Y: " + str(y) + "\n" \ "Axis Z: " + str(z) + "\n" \ "Declination: " + self.degrees(self.declination()) + "\n" \ "Heading: " + self.degrees(self.heading()) + "\n"
再寫(xiě)個(gè)gy273_main.py
import i2c_driver_hmc5883l as hmc5883l import time import sys # http://magnetic-declination.com/Great%20Britain%20(UK)/Harrogate# compass = hmc5883l.hmc5883l(gauss = 4.7, declination = (-2,5)) while True: sys.stdout.write("\rHeading: " + str(compass.degrees(compass.heading())) + " ") #sys.stdout.write("\rAxis X: " + str(str(x)) sys.stdout.flush() time.sleep(0.5)
i2c設(shè)備容易共用總線,比如上述兩個(gè)i2c設(shè)備,可以接在同一個(gè)i2c總線上,但要注意LCD可能需要5V供電,而模塊gy273供電VCC為3.3V。
感謝各位的閱讀!關(guān)于“ 樹(shù)莓派怎么用Python實(shí)現(xiàn)i2c”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!