查看: 2965|回复: 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
    复制代码

    输入下面命令打开I2C-0
    1. sudo armbian-config
    复制代码



    保存后重启,然后输入下面命令查看I2C地址

    创建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)
    复制代码


    运行
    1. sudo python lcd-clock.py
    复制代码
    显示效果如下

    回复

    举报

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

    本版积分规则

    关闭

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

    「工聚四方·创见未来」欧时RS入驻四方维创新中心线下沙龙
    5月9日四方维创新中心将携手欧时RS,以技术创新为起点,开启开放实验室新篇章!作为硬科技的创新基地,四方维创新中心秉持为技术赋能,为企业助力的原则,为企业及工程师用户打造开放实验室。本次欧时RS入驻沙龙将为大家带来实用的设备工具和专业的主题分享,在这里您可以上手试用所有工具,并与技术专家交流学习。探索电子领域的前沿应用,从实用工具到前沿技术,共同交流探讨领域未来。

    查看 »



    手机版|小黑屋|与非网

    GMT+8, 2025-4-29 06:16 , Processed in 0.113095 second(s), 18 queries , MemCache On.

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

    苏公网安备 32059002001037号

    Powered by Discuz! X3.5

    Copyright © 2001-2024, Tencent Cloud.