硬件:
一般LCD1602需要11个GPIO,但是树莓派的GPIO口有限,所以需要串转并的方式来节省GPIO口。
接线方式:
:---PCF85741---: :---RASP---:
GND-------------06 GROUND
VCC-------------02 5V
SDA-------------03 SDA1
好了,完成上面的接线工作,就可以通电了。
连接电源打开树莓派,显示屏就会亮,同时在第一行显示一排黑方块。如果看不到黑方块或黑方块不明显,请调节可调电阻,直到黑方块清晰显示。如果调节可调电阻还看不到方块,则可能你的连接有问题了,请检查连接,包括检查显示屏的引脚有没有虚焊。
下面我们需要修改树莓派的配置文件使能I2C,通过nano编辑器修改raspi-blacklist.conf 文件内容
pi@raspberrypi ~ $ sudo nano /etc/modprobe.d/raspi-blacklist.conf
写入保存
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
blacklist i2c-bcm2708
打开/etc/modules ,在文件结尾加上 i2c-dev、i2c-bcm2708
pi@raspberrypi ~ $ sudo nano /etc/modules
写入保存
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
i2c-bcm2708
i2c-dev
更新一次包列表
pi@raspberrypi ~ $ sudo apt-get update
安装 i2c-tools工具与python-smbus
pi@raspberrypi ~ $ sudo apt-get install i2c-tools python-smbus
之后reboot,重启后千万记得打开i2c接口
否则无法找到i2c地址,出现如下错误:
Could not open file /dev/i2c-1' or
/dev/i2c/1’: No such file or directory
寻址:
pi@raspberrypi ~ $ sudo i2cdetect -y 1
完成!
测试代码:
#!/user/bin/env python
import smbus
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,'NUPT bear BLight')
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)
资料来源: