1602 LCD 液晶顯示器

透過 I2C 操作 LCD 16x2

接線說明:

  • 5V
  • GND
  • SDA
  • SCL

執行 Raspberry Pi 系統設置:

sudo raspi-config

操作步驟:

  • 選擇 Interfacing Options
  • 選擇 I2C
  • 點選 Yes
  • 點選 Ok
  • 選擇 Finish
sudo apt-get update
sudo apt-get install -y python-smbus i2c-tools

檢查 I2C 裝置代碼:

sudo i2cdetect -y 1

參考輸出:

$ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 3f 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

Python 程式碼:

import smbus
import time

I2C_ADDR  = 0x3f
LCD_WIDTH = 16

LCD_CHR = 1
LCD_CMD = 0

LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0
LCD_LINE_3 = 0x94
LCD_LINE_4 = 0xD4

LCD_BACKLIGHT  = 0x08
#LCD_BACKLIGHT = 0x00

ENABLE = 0b00000100

E_PULSE = 0.0005
E_DELAY = 0.0005

#bus = smbus.SMBus(0)
bus = smbus.SMBus(1)

def lcd_init():
  lcd_byte(0x33, LCD_CMD) # 110011 Initialise
  lcd_byte(0x32, LCD_CMD) # 110010 Initialise
  lcd_byte(0x06, LCD_CMD) # 000110 Cursor move direction
  lcd_byte(0x0C, LCD_CMD) # 001100 Display On,Cursor Off, Blink Off 
  lcd_byte(0x28, LCD_CMD) # 101000 Data length, number of lines, font size
  lcd_byte(0x01, LCD_CMD) # 000001 Clear display
  time.sleep(E_DELAY)

def lcd_byte(bits, mode):
  bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

  bus.write_byte(I2C_ADDR, bits_high)
  lcd_toggle_enable(bits_high)

  bus.write_byte(I2C_ADDR, bits_low)
  lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):
  time.sleep(E_DELAY)
  bus.write_byte(I2C_ADDR, (bits | ENABLE))
  time.sleep(E_PULSE)
  bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  time.sleep(E_DELAY)

def lcd_string(message,line):
  message = message.ljust(LCD_WIDTH, " ")

  lcd_byte(line, LCD_CMD)

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def main():

  lcd_init()

  while True:

    lcd_string("RPiSpy         <", LCD_LINE_1)
    lcd_string("I2C LCD        <", LCD_LINE_2)

    time.sleep(3)

    lcd_string(">         RPiSpy", LCD_LINE_1)
    lcd_string(">        I2C LCD", LCD_LINE_2)

    time.sleep(3)

if __name__ == '__main__':

  try:
    main()
  except KeyboardInterrupt:
    pass
  finally:
    lcd_byte(0x01, LCD_CMD)

直接驅動字幕顯示器的方式

安裝必須的 Python 套件:

apt-get install git
git clone git://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
cd Adafruit-Raspberry-Pi-Python-Code
cd Adafruit_CharLCD

建立 Adafruit_CharLCD程式檔:

#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime

lcd = Adafruit_CharLCD()

cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"

lcd.begin(16,1)

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

while 1:
        lcd.clear()
        ipaddr = run_cmd(cmd)
        lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))
        lcd.message('IP %s' % ( ipaddr ) )
        sleep(2)

執行程式:

chmod +x Adafruit_CharLCD.py
sudo ./Adafruit_CharLCD.py

results matching ""

    No results matching ""