最近入手了一個新玩具,沒錯,就是樹莓派了,這里我使用的是樹莓派 3B+。但是在玩兒的時候遇到了一些問題,比如樹莓派開機有時候特別慢,且 IP 地址什么的記不住,于是就買了一塊便宜的 LCD1602 顯示屏(5V).
先上效果圖

這顯示屏可以顯示 16個 * 2行 (32)字符,且只標準 ASCII 碼字符和日文希臘文字符,LCD1602 一共有 16 個針腳,如果直接將其連接在樹莓派上的話非常占用資源,所以我們買的是和 IIC(I2C)模塊集成在一起的板子。IIC 只有四個針腳,這樣就可以大幅度節(jié)約樹莓派針腳去干其他事,I2C接口引腳如下:
GND --- GND
VCC --- 電源 (接樹莓派5V)
SDA --- I2C 數據
SCL --- I2C 時鐘
將四個引腳接到樹莓派同名 GPIO 引腳即可,VCC接5V,樹莓派引腳如圖:

接好了 LCD1602 后,我們就要登入樹莓派了,首先安裝 i2c-tools,和 Python 需要用到的 smbus , 然后查看 I2C 設備地址,這個地址將在后面用到。
pi@raspberrypi:~ $ sudo apt-get install i2c-tools? ?
pi@raspberrypi:~ $ sudo apt-get install python-smbus? ?
pi@raspberrypi:~ $ sudo i2cdetect -y 1?
I2C 設備地址
? ? ? ? 0? 1? 2? 3? 4? 5? 6? 7? 8? 9? a? b? c? d? e? f
? ? 00:? ? ? ? ? -- -- -- -- -- -- -- -- -- -- -- -- --
? ? 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
? ? 20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
? ? 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
? ? 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
? ? 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
? ? 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
? ? 70: -- -- -- -- -- -- -- --?
看到地址為 0x27 .這說明 已經成功連接了 LCD1602 。接下來就可以用 Python 控制 LCD1602 顯示信息了。
由于對 LCD1602 的指令不了解,這里從網上找了一個很好例子,并優(yōu)化了以下,保存在 /home/lcd/lcd1602.py
, 運行該 腳本,即可看到效果。
? ? import time
? ? import smbus
? ? import logx
? ? import logging
? ? BUS = smbus.SMBus(1)
? ? LCD_ADDR = 0x27
? ? BLEN = 1 #turn on/off background light
? ? def turn_light(key):
? ? ? ? global BLEN
? ? ? ? BLEN = key
? ? ? ? if key ==1 :
? ? ? ? ? ? BUS.write_byte(LCD_ADDR ,0x08)
? ? ? ? ? ? logging.info('LCD executed turn on BLight')
? ? ? ? else:
? ? ? ? ? ? BUS.write_byte(LCD_ADDR ,0x00)
? ? ? ? ? ? logging.info('LCD executed turn off BLight')
? ? def write_word(addr, data):
? ? ? ? global BLEN
? ? ? ? temp = data
? ? ? ? if BLEN == 1:
? ? ? ? ? ? temp |= 0x08
? ? ? ? else:
? ? ? ? ? ? temp &= 0xF7
? ? ? ? BUS.write_byte(addr ,temp)
? ? def send_command(comm):
? ? ? ? # Send bit7-4 firstly
? ? ? ? buf = comm & 0xF0
? ? ? ? buf |= 0x04? ? ? ? ? ? ? # RS = 0, RW = 0, EN = 1
? ? ? ? write_word(LCD_ADDR ,buf)
? ? ? ? time.sleep(0.002)
? ? ? ? buf &= 0xFB? ? ? ? ? ? ? # Make EN = 0
? ? ? ? write_word(LCD_ADDR ,buf)
? ? ? ? # Send bit3-0 secondly
? ? ? ? buf = (comm & 0x0F) << 4
? ? ? ? buf |= 0x04? ? ? ? ? ? ? # RS = 0, RW = 0, EN = 1
? ? ? ? write_word(LCD_ADDR ,buf)
? ? ? ? time.sleep(0.002)
? ? ? ? buf &= 0xFB? ? ? ? ? ? ? # Make EN = 0
? ? ? ? write_word(LCD_ADDR ,buf)
? ? def send_data(data):
? ? ? ? # Send bit7-4 firstly
? ? ? ? buf = data & 0xF0
? ? ? ? buf |= 0x05? ? ? ? ? ? ? # RS = 1, RW = 0, EN = 1
? ? ? ? write_word(LCD_ADDR ,buf)
? ? ? ? time.sleep(0.002)
? ? ? ? buf &= 0xFB? ? ? ? ? ? ? # Make EN = 0
? ? ? ? write_word(LCD_ADDR ,buf)
? ? ? ? # Send bit3-0 secondly
? ? ? ? buf = (data & 0x0F) << 4
? ? ? ? buf |= 0x05? ? ? ? ? ? ? # RS = 1, RW = 0, EN = 1
? ? ? ? write_word(LCD_ADDR ,buf)
? ? ? ? time.sleep(0.002)
? ? ? ? buf &= 0xFB? ? ? ? ? ? ? # Make EN = 0
? ? ? ? write_word(LCD_ADDR ,buf)
? ? def init_lcd():
? ? ? ? try:
? ? ? ? ? ? send_command(0x33) # Must initialize to 8-line mode at first
? ? ? ? ? ? time.sleep(0.005)
? ? ? ? ? ? send_command(0x32) # Then initialize to 4-line mode
? ? ? ? ? ? time.sleep(0.005)
? ? ? ? ? ? send_command(0x28) # 2 Lines & 5*7 dots
? ? ? ? ? ? time.sleep(0.005)
? ? ? ? ? ? send_command(0x0C) # Enable display without cursor
? ? ? ? ? ? time.sleep(0.005)
? ? ? ? ? ? send_command(0x01) # Clear Screen
? ? ? ? ? ? logging.info('LCD init over')
? ? ? ? ? ? BUS.write_byte(LCD_ADDR ,0x08)
? ? ? ? ? ? logging.info('LCD turning on BLight')
? ? ? ? except:
? ? ? ? ? ? return False
? ? ? ? else:
? ? ? ? ? ? return True
? ? def clear_lcd():
? ? ? ? send_command(0x01) # Clear Screen
? ? def print_lcd(x, y, str):
? ? ? ? if x < 0:
? ? ? ? ? ? x = 0
? ? ? ? if x > 15:
? ? ? ? ? ? x = 15
? ? ? ? if y <0:
? ? ? ? ? ? y = 0
? ? ? ? if y > 1:
? ? ? ? ? ? y = 1
? ? ? ? # Move cursor
? ? ? ? addr = 0x80 + 0x40 * y + x
? ? ? ? send_command(addr)
? ? ? ? for chr in str:
? ? ? ? ? ? send_data(ord(chr))
? ? if __name__ == '__main__':
? ? ? ? init_lcd()
? ? ? ? print_lcd(0, 0, 'Hello, world!')
? ? ? ? print_lcd(8, 1, 'by Jerry')
在這個基礎上,我們還需要添加一些功能,在每次開啟自動啟動為守護進程,在 LCD 上顯示主機 ip 和已運行時間。
deamon.py : 守護進程父類
host_status.py : 樹莓派 IP ,可以添加更多內容,比如獲取藍牙狀態(tài)顯示到LCD
LCD1602.py : LCD1602 工具,上面的代碼,用于在LCD上打印字符
pi_status.py : 主腳本,這里會開啟的一個名為 pi-status 的守護進程,不斷刷新 LCD 內容
先將代碼下載到樹莓派的 /home/lcd/ 目錄下,使用命令:
啟動
? ? pi@raspberrypi:/home/lcd $ sudo python pi_status.py
? ? ready to starting ......
查看進程狀態(tài), PID
? ? pi@raspberrypi:/home/lcd $ sudo python pi_status.py status?
? ? is running ... PID 1116
重啟進程
? ? pi@raspberrypi:/home/lcd $ sudo python pi_status.py restart
? ? stopping ...
? ? Stopped!
? ? ready to starting ......
停止
? ? pi@raspberrypi:/home/lcd $ sudo python pi_status.py stop
? ? stopping ...
? ? Stopped!
我們還需要將這個腳本加入到開機啟動,每次開啟將顯示狀態(tài)。
? ? vi /etc/rc.local
在 exit 0 之前插入一下內容,其中路徑根據實際情況更改。
? ? sudo python /home/lcd/pi-status.py
(完)