树莓派连接LCD1602液晶显示屏,并显示内容
一、效果图
【树莓派|(七)树莓派系列教程(树莓派4B连接LCD1602液晶显示屏并且显示内容)】所需要硬件:
- LCD1602液晶显示屏
- IIC转接LCD1602模块(转成IIC通讯)
文章图片
文章图片
二、设置树莓派,启动IIC通讯功能
- 因为树莓派刚烧录完系统后IIC功能是没有启动的,在命令行分别输入以下命令:
raspi-config ;
进入树莓派设置界面
>>>Interfacing Options;
设置
>>>P5 I2C;
选择I2C选项
>>>enable;
启动I2C功能
- 下载对应缺的包,例如:
sudo apt-get install i2c-tools
sudo apt-get update
sudo apt-get install python3-smbus
- 设置好了,重启一次树莓派。
文章图片
- 树莓派-------液晶显示屏
- VCC----------3.3V
- GND-----------地
- SDA----------SDA
- SCL-----------SCL
'''
1602/1602A
author:ZengXiaojie
description:
直接实例化 My1602 对象
然后调用对象的 print_lcd(param1, param2, str) 方法
ex:print_lcd(0, 0, 'Hello, world!')
param1:从第几位开始,共16位(0-15),超出不显示,除非移动屏幕(其实1602一行有40位)
param2:第几行,共有两行,0为第一行,1位第二行。
str:要显示的数据
每次打印字符时,建议适当清屏。
'''import time
import smbus
import sysclass My1602(object):
BUS = smbus.SMBus(1)
LCD_ADDR = 0x27
BLEN = 1# '''
# 开关灯
def turn_light(self, key):
self.BLEN = key
if key == 1:
self.BUS.write_byte(self.LCD_ADDR, 0x08)
else:
self.BUS.write_byte(self.LCD_ADDR, 0x00)
# '''def write_word(self, addr, data):
temp = data
if self.BLEN == 1:
temp |= 0x08
else:
temp &= 0xF7
self.BUS.write_byte(addr, temp) # 写命令
def send_command(self, comm):
# 发送7-4位数据
buf = comm & 0xF0
buf |= 0x04# RS = 0, RW = 0, EN = 1
self.write_word(self.LCD_ADDR, buf)
time.sleep(0.002)
buf &= 0xFB
self.write_word(self.LCD_ADDR, buf)# 发送3-0位数据
buf = (comm & 0x0F) << 4
buf |= 0x04# RS = 0, RW = 0, EN = 1
self.write_word(self.LCD_ADDR, buf)
time.sleep(0.002)
buf &= 0xFB
self.write_word(self.LCD_ADDR, buf) # 写数据
def send_data(self, data):
# 发送7-4位数据
buf = data & 0xF0
buf |= 0x05# RS = 1, RW = 0, EN = 1
self.write_word(self.LCD_ADDR, buf)
time.sleep(0.002)
buf &= 0xFB
self.write_word(self.LCD_ADDR, buf)# 发送3-0位数据
buf = (data & 0x0F) << 4
buf |= 0x05# RS = 1, RW = 0, EN = 1
self.write_word(self.LCD_ADDR, buf)
time.sleep(0.002)
buf &= 0xFB
self.write_word(self.LCD_ADDR, buf)
# 初始化
def __init__(self):
try:
self.send_command(0x33)
time.sleep(0.005)
self.send_command(0x32)
time.sleep(0.005)
self.send_command(0x28)
time.sleep(0.005)
self.send_command(0x0C)
time.sleep(0.005)
self.send_command(0x01)
self.BUS.write_byte(self.LCD_ADDR, 0x08)
except:
return None
else:
return None # 清屏
def clear_lcd(self):
self.send_command(0x01)# 清屏 # 显示字符
def print_lcd(self, x, y, str):
if x < 0:
x = 0
if x > 15:
x = 15
if y < 0:
y = 0
if y > 1:
y = 1addr = 0x80 + 0x40 * y + x
self.send_command(addr)for chr in str:
self.send_data(ord(chr))if __name__ == '__main__':
my1602 = My1602()
# turn_light(0)# 关闭背景灯光
my1602.print_lcd(0, 0, 'Hello, world!')
my1602.print_lcd(8, 1, 'by zxj')
方案二:(两个python程序:1.LCD1602驱动程序,2.调用驱动程序)
程序1—LCD1602.py(LCD主控制程序):
import LCD1602 as LCD
import timeLCD.init_lcd()
time.sleep(2)
LCD.print_lcd(0,0,'start')
程序2—LCD1602.py(LCD驱动程序1):
import time
import smbus
import logx
import logging
BUS = smbus.SMBus(1)
LCD_ADDR = 0x27
BLEN = 1 #turn on/off background lightdef 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 Truedef clear_lcd():
send_command(0x01) # Clear Screendef 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')
程序3—logx.py(LCD驱动程序2):
import logging
#print on log file
logging.basicConfig(level=logging.INFO,
format='%(asctime)s <%(levelname)s> [line:%(lineno)d] %(filename)s : %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename='trace.log',
filemode='a')#w a
#print on screem
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s <%(levelname)s> [line:%(lineno)d] %(filename)s : %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
if __name__ == '__main__':
#CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET
logging.critical('This is critical message')
logging.error('This is error message')
logging.warning('This is warning message')
logging.info('This is info message')
logging.debug('This is debug message')
#logging.notset('This is notset message')
五、运行即可,运行成功如下:
文章图片
推荐阅读
- web端|web全栈开发(web前后端数据交互。html5+jquery作为前端、Python+flask作为后端)
- 快来试试用 Python 将你的照片转化为“速写”
- 大型Python项目依赖树很烦(教你一键理清)
- Python 超简单一键美化你的文章
- libtorch|YOLOv4 libtorch推理【附代码】
- 数据库|谷歌、斯坦福联合发文(我们为什么一定要用大模型())
- 机器学习|【Python】逻辑回归(softmax多分类)
- TensorFlow实战|TensorFlow实战之softmax多分类
- python|Python 有哪些好玩的语法糖()