樹莓派應用--LCD1602的使用
阿新 • • 發佈:2018-11-08
LCD1602的應用(python)
材料:
- 樹莓派B型
- LCD1602 液晶顯示(裝了I2C轉接版)
- 麵包板
LCD1602使用手冊
LCD1602 的接線
- Vcc 接 +5v
- Gnd 接 Gnd
- SDA 接 SDA
- SCL 接 SCL
python 程式設計
- 樹莓派裝機時已經裝了i2c-tools;python-smbus
- 執行 sudo i2cdetect -y 1 (LCD1602.py 中BUS = smbus2.SMBus(1))
[email protected]:~ $ sudo i2cdetect -y 1
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: – -- – -- – -- – --
[email protected]:~ $
- 裝置的地址27,這個記住
- LCD1602.py 中 LCD_ADDR = 0x27
- 如果沒有這個地址,需執行下列:
sudo apt-get install i2c-tools
sudo apt-get update
sudo apt-get install python-smbus
sudo nano /etc/modules 在最後增加兩行:
i2c-bcm2708
i2c-dev
sudo nano /etc/modprobe.d/raspi-blacklist.conf
用#註釋黑名單中的i2c-bcm2708
sudo reboot - 執行python程式碼(程式是LCD1602自帶的):
#!/user/bin/env python
import smbus2
import time
import sys
import LCD1602 as LCD
import logx
import logging
if __name__ == '__main__':
LCD.init_lcd()
time.sleep(2)
LCD.print_lcd(0,0,'Jason & dinosaur')
LCD.print_lcd(1,1,'local time')
logging.info('wait 2 seconds')
LCD.turn_light(1)
logging.info('turn on BLight')
time.sleep(5)
while True:
nowtime = time.strftime('%m-%d %H:%M:%S',time.localtime(time.time()))
hourtime = time.strftime('%H',time.localtime(time.time()))
mintime = time.strftime('%M',time.localtime(time.time()))
sectime = time.strftime('%S',time.localtime(time.time()))
LCD.print_lcd(1,1,nowtime)
if mintime == '59':
if sectime == '00':
LCD.turn_light(1)
elif sectime == '59':
LCD.turn_light(0)
time.sleep(1)
執行結果:顯示正常,程式的時間輸出每9-10秒跳一秒,需要調整為time.sleep(0.9),與CPU執行每條指令的速度有關,樹莓派B型速度慢。