1回答

0收藏

【BPI-M2 Berry试用】使用LCD1602显示文字

其他 其他 3806 人阅读 | 1 人回复 | 2017-10-03

本帖最后由 可乐丸子 于 2017-10-9 21:55 编辑

香蕉派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固件需要输入下面命令安装组件,树莓派固件不需要
  1. sudo apt-get install i2c-tools python-smbus
复制代码

接着查看LCD1602连接的I2C的位置
  1. ls /dev
复制代码

可以看到有i2c-0 i2c-1 i2c-2 i2c-4,依次使用下面命令测试直到找到lcd1602地址码
  1. i2cdetect -y 数字
复制代码
可知LCD1602的地址码为3f

接着创建一个lcd1602显示脚本lcd1602_txt.py
  1. vi lcd1602_txt.py
复制代码
输入下面代码
  1. import smbus
  2. import time

  3. bus = smbus.SMBus(2)
  4. addr = 0x3f

  5. def writeCommand(command):
  6.    bus.write_byte(addr, 0b1100 | command << 4)
  7.    time.sleep(0.005)
  8.    bus.write_byte(addr, 0b1000 | command << 4)
  9.    time.sleep(0.005)

  10. def writeWord(word):
  11.    for i in range(0,len(word)):
  12.       asciiCode =  ord(word[i])
  13.       bus.write_byte(addr, 0b1101 |(asciiCode >> 4 & 0x0F) << 4)
  14.       time.sleep(0.0005)
  15.       bus.write_byte(addr, 0b1001 |(asciiCode >> 4 & 0x0F) << 4)
  16.       time.sleep(0.0005)
  17.       bus.write_byte(addr, 0b1101 |(asciiCode & 0x0F) << 4)
  18.       time.sleep(0.0005)
  19.       bus.write_byte(addr, 0b1001 | (asciiCode & 0x0F) << 4)
  20.       time.sleep(0.0005)

  21. # init
  22. writeCommand(0b0010)

  23. # 4-byte mode, 2 line code
  24. writeCommand(0b0010)
  25. writeCommand(0b1111)

  26. # set cursor mode
  27. writeCommand(0b0000)
  28. writeCommand(0b1100)

  29. # cursor shift mode
  30. writeCommand(0b0000)
  31. writeCommand(0b0110)

  32. writeWord("Hello World")
  33. clear = True
  34. time.sleep(1)

  35. while(1):
  36.    # first line first column
  37.    writeCommand(0b1000)
  38.    writeCommand(0b0000)
  39.    writeWord("eeboard BBS test")

  40.    # second line first column
  41.    writeCommand(0b1100)
  42.    writeCommand(0b0000)
  43.    writeWord("www.eeboard.com")
  44.    time.sleep(0.2)
复制代码
保存修改后输入下面命令运行
  1. sudo python lcd1602_txt.py
复制代码
LCD1602液晶屏显示如下





关注下面的标签,发现更多相似文章
分享到:
回复

使用道具 举报

回答|共 1 个

倒序浏览

沙发

jwdxu2009

发表于 2017-10-4 22:49:50 | 只看该作者

这么好产品,学习了
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

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