TA的每日心情 | 奋斗 2019-10-1 12:54 |
---|
签到天数: 313 天 连续签到: 1 天 [LV.8]以坛为家I
|
笔者在上面一个帖子里面介绍了LCD1602显示文字的方法,接下来笔者介绍使用LCD1602显示时间和日期做一个数字日历的方法,硬件连接方法可以查看笔者上面一个帖子【BPI-M2 Berry试用】使用LCD1602显示文字
使用数字日历显示时间需要时间是准确时间,首先使用下面命令设置时区按下图设置
然后修改hosts文件将最后一行改成然后安装时间同步服务并同步时间- sudo apt-get install ntp ntpdate
- sudo ntpdate update
- sudo service ntp restart
复制代码 经过上面处理就可以自动同步网络时间了
下面介绍脚本创建方法,打开终端输入下面命令创建一个lcd1602.py脚本输入下面代码- import smbus
- import time
- import os
- from time import gmtime, strftime, localtime
- os.environ['TZ'] = 'Asia/Shanghai'
- time.tzset()
- bus = smbus.SMBus(2)
- addr = 0x3f
- def writeCommand(command):
- bus.write_byte(addr, 0b1100 | command << 4)
- time.sleep(0.005)
- bus.write_byte(addr, 0b1000 | command << 4)
- time.sleep(0.005)
- def writeWord(word):
- for i in range(0,len(word)):
- asciiCode = ord(word[i])
- bus.write_byte(addr, 0b1101 |(asciiCode >> 4 & 0x0F) << 4)
- time.sleep(0.0005)
- bus.write_byte(addr, 0b1001 |(asciiCode >> 4 & 0x0F) << 4)
- time.sleep(0.0005)
- bus.write_byte(addr, 0b1101 |(asciiCode & 0x0F) << 4)
- time.sleep(0.0005)
- bus.write_byte(addr, 0b1001 | (asciiCode & 0x0F) << 4)
- time.sleep(0.0005)
- # init
- writeCommand(0b0010)
- # 4-byte mode, 2 line code
- writeCommand(0b0010)
- writeCommand(0b1111)
- # set cursor mode
- writeCommand(0b0000)
- writeCommand(0b1100)
- # cursor shift mode
- writeCommand(0b0000)
- writeCommand(0b0110)
- writeWord("Welcome")
- clear = True
- time.sleep(1)
- while(1):
- # first line first column
- writeCommand(0b1000)
- writeCommand(0b0000)
- writeWord(strftime("%Y-%m-%d, %a ", localtime()))
- # second line first column
- writeCommand(0b1100)
- writeCommand(0b0000)
- writeWord(strftime("%H:%M:%S", localtime()))
- time.sleep(0.2)
复制代码 保存修改后退出,输入下面命令运行脚本数字日历显示效果如下图
|
|