查看: 2039|回复: 0

【BPI-M2 Berry试用】网络时间同步与使用LCD1602显示时间和日期

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

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2017-10-3 23:44:41 | 显示全部楼层 |阅读模式
    分享到:
    笔者在上面一个帖子里面介绍了LCD1602显示文字的方法,接下来笔者介绍使用LCD1602显示时间和日期做一个数字日历的方法,硬件连接方法可以查看笔者上面一个帖子【BPI-M2 Berry试用】使用LCD1602显示文字
    使用数字日历显示时间需要时间是准确时间,首先使用下面命令设置时区
    1. tzselect
    复制代码
    按下图设置
    2017-10-03_193559.png
    2017-10-03_193609.png
    然后修改hosts文件
    1. sudo vi /etc/hosts
    复制代码
    将最后一行改成
    1. 127.0.1.1 pi
    复制代码
    然后安装时间同步服务并同步时间
    1. sudo apt-get install ntp ntpdate
    2. sudo ntpdate update
    3. sudo service ntp restart
    复制代码
    经过上面处理就可以自动同步网络时间了
    下面介绍脚本创建方法,打开终端输入下面命令创建一个lcd1602.py脚本
    1. vi lcd1602.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(2)
    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 lcd1602.py
    复制代码
    数字日历显示效果如下图
    ANIMATION_5.GIF


    回复

    使用道具 举报

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

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

    GMT+8, 2024-5-5 12:56 , Processed in 0.118912 second(s), 19 queries , MemCache On.

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

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.