【Banana Pi 系列开发板试用体验】I2C控制液晶屏显示时间和...

  • wildmonkey
  • LV4工程师
  • |      2017-10-10 20:56:00
  • 浏览量 1393
  • 回复:2
前面介绍了香蕉派BPI-M2+开发板上使用I2C控制液晶屏显示文字,下面做个小改动在液晶屏上显示时间和日期 硬件连接比较简单,开发板GPIO2 GPIO3 GND 5V接到LCD1602背面的SDA SCL GND VCC 软件配置方面安装i2c-tools和python-smbus
sudo apt-get install i2c-tools python-smbus

查看I2C设备的smbus序号和地址
ls /dev
有i2c-0和i2c-1可用,分别查看 测试可知设备在smbus 0上,地址为3f,创建一个python脚本显示时间日期
vi date.py
加入下面内容后保存
import smbus

import time

import os

from time import gmtime, strftime, localtime



os.environ = 'Asia/Shanghai'

time.tzset()



bus = smbus.SMBus(0)

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)

      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)
运行
sudo python date.py
效果如图
  • 0
  • 收藏
  • 举报
  • 分享
我来回复

登录后可评论,请 登录注册

所有回答 数量:1
7943603 2018-01-14
哪里去买这种液晶
0   回复
举报
发布
wildmonkey 回复 2018-01-14
X宝 电子市场都有的 有的51单片机也有
0   回复
举报
x
收藏成功!点击 我的收藏 查看收藏的全部帖子