回答

收藏

BPI-M2 Berry使用LCD1602显示文字

#其他 #其他 1789 人阅读 | 0 人回复 | 2017-10-10

香蕉派BPI-M2 Berry开发板配备的40针类似于树莓派的GPIO扩展接口,扩展接口里面有I2C接口(SDA SCL)支持,我们可以使用I2C通讯接口来驱动LCD1602显示屏加IIC转换板,方法如下
首先是LCD1602的硬件连接方法

LCD1602显示屏和转换板按如下图焊接

然后吧转换板的SDA SCL GND VCC连接到BPI-M2 Berry的SDA SCL GND VCC,接着给开发板上电,登陆后打开终端,ubuntu固件需要输入下面命令安装组件,树莓派固件不需要

  • sudo apt-get install i2c-tools python-smbus

接着查看LCD1602连接的I2C的位置

  • ls /dev

可以看到有i2c-0 i2c-1 i2c-2 i2c-4,依次使用下面命令测试直到找到lcd1602地址码

  • i2cdetect -y 数字
可知LCD1602的地址码为3f

接着创建一个lcd1602显示脚本lcd1602_txt.py

  • vi lcd1602_txt.py
输入下面代码

  • import smbus
  • import time
  • bus = smbus.SMBus(2)
  • addr = 0x3f
  • def writeCommand(command):
  •    bus.write_byte(addr, 0b1100 | command << 4)
  •    time.sleep(0.005)
  •    bus.write_byte(addr, 0b1000 | command << 4)
  •    time.sleep(0.005)
  • def writeWord(word):
  •    for i in range(0,len(word)):
  •      asciiCode = ord(word)
  •      bus.write_byte(addr, 0b1101 |(asciiCode >> 4 & 0x0F) << 4)
  •      time.sleep(0.0005)
  •      bus.write_byte(addr, 0b1001 |(asciiCode >> 4 & 0x0F) << 4)
  •      time.sleep(0.0005)
  •      bus.write_byte(addr, 0b1101 |(asciiCode & 0x0F) << 4)
  •      time.sleep(0.0005)
  •      bus.write_byte(addr, 0b1001 | (asciiCode & 0x0F) << 4)
  •      time.sleep(0.0005)
  • # init
  • writeCommand(0b0010)
  • # 4-byte mode, 2 line code
  • writeCommand(0b0010)
  • writeCommand(0b1111)
  • # set cursor mode
  • writeCommand(0b0000)
  • writeCommand(0b1100)
  • # cursor shift mode
  • writeCommand(0b0000)
  • writeCommand(0b0110)
  • writeWord("Hello World")
  • clear = True
  • time.sleep(1)
  • while(1):
  •    # first line first column
  •    writeCommand(0b1000)
  •    writeCommand(0b0000)
  •    writeWord("eeboard BBS test")
  •    # second line first column
  •    writeCommand(0b1100)
  •    writeCommand(0b0000)
  •    writeWord("www.eeboard.com")
  •    time.sleep(0.2)
保存修改后输入下面命令运行

  • sudo python lcd1602_txt.py
复制代码LCD1602液晶屏显示如下
  
关注下面的标签,发现更多相似文章
分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条