查看: 2412|回复: 0

【BPI-M2 Zero试用】LCD1602网络校时数字日历时钟

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

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2018-3-11 18:34:54 | 显示全部楼层 |阅读模式
    分享到:
    下面介绍使用BPI-M2 Zero连接LCD1602(I2C)打造网络自动对时的数字日历时钟的方法
    首先将BPI-M2 Zero开发板的SDA SCL连接到I2C接口的LCD1602的SDA SCL(下图的PIN3 PIN5),然后将LCD1602的VCC GND连接开发板的PIN2 PIN6

    从上图可知开发板的上面接口为I2C1,但是实际测试在ARMBIAN中显示为I2C0,安装如下组件
    1. sudo apt-get install i2c-tools python-smbus
    复制代码
    2018-03-11_172640.png
    输入下面命令打开I2C-0
    1. sudo armbian-config
    复制代码
    2018-03-11_172956.png
    2018-03-11_173012.png
    2018-03-11_181324.png
    保存后重启,然后输入下面命令查看I2C地址
    2018-03-11_173603.png
    创建python脚本lcd-clock.py,输入下面代码
    1. import smbus
    2. import time
    3. import os
    4. from time import gmtime, strftime, localtime

    5. os.environ['TZ'] = 'Asia/Shanghai'
    6. time.tzset()

    7. bus = smbus.SMBus(0)
    8. addr = 0x3f

    9. def writeCommand(command):
    10.    bus.write_byte(addr, 0b1100 | command << 4)
    11.    time.sleep(0.005)
    12.    bus.write_byte(addr, 0b1000 | command << 4)
    13.    time.sleep(0.005)

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

    25. # init
    26. writeCommand(0b0010)

    27. # 4-byte mode, 2 line code
    28. writeCommand(0b0010)
    29. writeCommand(0b1111)

    30. # set cursor mode
    31. writeCommand(0b0000)
    32. writeCommand(0b1100)

    33. # cursor shift mode
    34. writeCommand(0b0000)
    35. writeCommand(0b0110)

    36. writeWord("Welcome")
    37. clear = True
    38. time.sleep(1)

    39. while(1):
    40.    # first line first column
    41.    writeCommand(0b1000)
    42.    writeCommand(0b0000)
    43.    writeWord(strftime("%Y-%m-%d, %a ", localtime()))

    44.    # second line first column
    45.    writeCommand(0b1100)
    46.    writeCommand(0b0000)
    47.    writeWord(strftime("%H:%M:%S", localtime()))
    48.    time.sleep(0.2)
    复制代码
    2018-03-11_173735.png
    2018-03-11_173707.png
    运行
    1. sudo python lcd-clock.py
    复制代码
    显示效果如下
    20180311_173923.jpg
    回复

    使用道具 举报

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

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

    GMT+8, 2024-5-1 02:36 , Processed in 0.115201 second(s), 16 queries , MemCache On.

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

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.