树莓派应用--LCD1602的使用

LCD1602的应用(python)

材料:

  1. 树莓派B型
  2. LCD1602 液晶显示(装了I2C转接版)
  3. 面包板

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))

pi@raspberrypi:~ $ 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: – -- – -- – -- – --
pi@raspberrypi:~ $

  • 设备的地址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型速度慢。

猜你喜欢

转载自blog.csdn.net/qq_43143240/article/details/83829979