美文网首页
树莓派+LCD1602液晶屏(或LCD2004)+I2C PCF

树莓派+LCD1602液晶屏(或LCD2004)+I2C PCF

作者: 我开心0536 | 来源:发表于2020-09-09 16:52 被阅读0次

【硬件】:
树莓派
LCD1602屏幕 ( 能显示2行,每行16个字母)
或 LCD2004屏幕 ( 能显示4行,每行20个字母)
自带i2c转LCD1602的转接板( PCF85741),就是后面只有四根针的屏幕

image.png

【接线方式】:
屏幕上的针 ------------- 树莓派上的针
GND-------------06针 GROUND
VCC-------------02针 5V
SDA-------------03针 SDA1
SCL-------------05针 SCL1

好了,完成上面的接线工作,就可以通电了。
连接电源打开树莓派,显示屏就会亮,同时在第一行显示一排黑方块。
如果看不到黑方块或黑方块不明显,请调节可调电阻,直到黑方块清晰显示。如果调节可调电阻还看不到方块,则可能你的连接有问题了,请检查连接,包括检查显示屏的引脚有没有虚焊。

【开始修改配置】:
下面我们需要修改树莓派的配置文件使能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

之后重启树莓派,重启后千万记得打开i2c接口:
pi@raspberrypi ~ $ sudo raspi-config

微信截图_20200909170101.png

否则无法找到i2c地址,出现如下错误:
Could not open file /dev/i2c-1' or/dev/i2c/1’: No such file or directory

寻址:
pi@raspberrypi ~ $ sudo i2cdetect -y 1

你的I2c设备地址就是0x27,即转接板上的I2C地址。

到此,准备工作完成!

【下面是代码部分】:

IMG_20200909_164324.jpg

1、将以下代码保存为随便一个文件名,如 1602_test.py

#!/user/bin/env python 
import time
import sys
import LCD1602 as LCD

# LCD.print_lcd ( 0-19列、0-3行、'文字内容' ),
# 如果屏幕是1602,则最多能显示16列(0-15列),2行(0-1行)。
# 如果屏幕是2004,则最多能显示20列(0-19列)、4行(0-3行) 
if __name__ == '__main__':  
    LCD.init_lcd()
    LCD.print_lcd(1,0,' LCD1602 demo')    
    LCD.print_lcd(1,1,'Li Jiahao 2020')
    LCD.turn_light(1)
    time.sleep(2)
    LCD.clear_lcd()    
    while True:
        nowtime  = time.strftime('%m-%d %H:%M:%S',time.localtime(time.time()))
        LCD.print_lcd(0,0,nowtime)
        LCD.print_lcd(0,1,"12345678ABCDEFGH")
        #LCD.print_lcd(0,2,"33333333333333333333")  # 如果屏幕是2004,能显示20列4行的,可以把本行前面的注释去掉
        #LCD.print_lcd(0,3,"44444444444444444444")  # 如果屏幕是2004,能显示20列4行的,可以把本行前面的注释去掉
        time.sleep(1)   

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 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 > 19:
        x = 19
    if y <0:
        y = 0
    if y > 3:
        y = 3

    # Move cursor
    # addr = 0x80 + 0x40 * y + x
    if   y == 0:
        addr = 0x80 + x
    elif y == 1:
        addr = 0xc0 + x
    elif y == 2:
        addr = 0x94 + x
    elif y == 3:
        addr = 0xd4 + x
    send_command(addr)
    
    for chr in str:
        send_data(ord(chr))

# LCD.print_lcd( 0-3列、0-3行、'文字内容'),
# 如果屏幕是1602,则最多能显示16列(0-15列),2行(0-1行)。
# 如果屏幕是2004,则最多能显示20列(0-19列)、4行(0-3行)
if __name__ == '__main__':
    init_lcd()
    print_lcd(0,0,'LCD1602/LCD2004 Demo')
    print_lcd(0,1,'-222222222BBBBBBBBB-')
    print_lcd(0,2,'-333333333CCCCCCCCC-')
    print_lcd(0,3,'-444444444DDDDDDDDD-')

相关文章

网友评论

      本文标题:树莓派+LCD1602液晶屏(或LCD2004)+I2C PCF

      本文链接:https://www.haomeiwen.com/subject/ffaeektx.html