查看: 2585|回复: 1

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

[复制链接]
  • TA的每日心情
    奋斗
    2019-10-1 12:54
  • 签到天数: 313 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2017-10-3 23:30:38 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 可乐丸子 于 2017-10-9 21:55 编辑

    香蕉派BPI-M2 Berry开发板配备的40针类似于树莓派的GPIO扩展接口,扩展接口里面有I2C接口(SDA SCL)支持,我们可以使用I2C通讯接口来驱动LCD1602显示屏加IIC转换板,方法如下
    首先是LCD1602的硬件连接方法
    timg.jpg
    LCD1602显示屏和转换板按如下图焊接
    77.jpg
    然后吧转换板的SDA SCL GND VCC连接到BPI-M2 Berry的SDA SCL GND VCC,接着给开发板上电,登陆后打开终端,ubuntu固件需要输入下面命令安装组件,树莓派固件不需要
    1. sudo apt-get install i2c-tools python-smbus
    复制代码
    2017-10-03_193035.png
    接着查看LCD1602连接的I2C的位置
    1. ls /dev
    复制代码
    2017-10-03_193054.png
    可以看到有i2c-0 i2c-1 i2c-2 i2c-4,依次使用下面命令测试直到找到lcd1602地址码
    1. i2cdetect -y 数字
    复制代码
    可知LCD1602的地址码为3f
    2017-10-03_193129.png
    接着创建一个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液晶屏显示如下
    IMG_9202.JPG




    回复

    使用道具 举报

  • TA的每日心情

    2021-12-7 12:35
  • 签到天数: 1354 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2017-10-4 22:49:50 | 显示全部楼层
    这么好产品,学习了
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

    GMT+8, 2024-3-19 19:07 , Processed in 0.148201 second(s), 21 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.