回答

收藏

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

其他 其他 3598 人阅读 | 0 人回复 | 2018-03-11

下面介绍使用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
复制代码
显示效果如下

分享到:
回复

使用道具 举报

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

本版积分规则

关闭

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