In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how raspberry pie uses Python to implement i2C. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First of all, raspberry pie has to install python-smbus, i2c-tools.
Then modify the file: sudo nano / etc/modules, add the lines i2c-bcm2708 and i2c-dev, and Raspbian needs to activate i2c in raspi-config.
Check the device address with sudo i2cdetect-y 1
Example 1:LCD2004, device address is 0x27
First write a driver caller i2c_driver_lcd.py
Import smbusfrom time import * # LCD AddressADDRESS = 0x27# commandsLCD_CLEARDISPLAY = 0x01LCD_RETURNHOME = 0x02LCD_ENTRYMODESET = 0x04LCD_DISPLAYCONTROL = 0x08LCD_CURSORSHIFT = 0x10LCD_FUNCTIONSET = 0x20LCD_SETCGRAMADDR = 0x40LCD_SETDDRAMADDR = 0x80# flags for display entry modeLCD_ENTRYRIGHT = 0x00LCD_ENTRYLEFT = 0x02LCD_ENTRYSHIFTINCREMENT = 0x01LCD_ENTRYSHIFTDECREMENT = 0x00# flags for display on/off controlLCD_DISPLAYON = 0x04LCD_DISPLAYOFF = 0x00LCD_CURSORON = 0x02LCD_CURSOROFF = 0x00LCD_BLINKON = 0x01LCD_BLINKOFF = 0x00# flags for display/cursor shiftLCD_DISPLAYMOVE = 0x08LCD_CURSORMOVE = 0x00LCD_MOVERIGHT = 0x04LCD_MOVELEFT = 0x00# flags for function setLCD_8BITMODE = 0x10LCD_4BITMODE = 0x00LCD_2LINE = 0x08LCD_1LINE = 0x00LCD_5x10DOTS = 0x04LCD_5x8DOTS = 0x00# flags for backlight controlLCD_BACKLIGHT = 0x08LCD_NOBACKLIGHT = 0x00# set init LCD BACKLIGHT ON or OFFdef lcd_backlight (lcdbl=1): if lcdbl= = 0: return LCD_NOBACKLIGHT return LCD_BACKLIGHTEn = 0b00000100 # Enable bitRw = 0b00000010 # Read/Write bitRs = 0b00000001 # Register select bitclass 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 (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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.