TA的每日心情 | 奋斗 2019-10-1 12:54 |
---|
签到天数: 313 天 连续签到: 1 天 [LV.8]以坛为家I
|
下面介绍在树莓派3B上使用LCD1602液晶屏打造数字日历的方法
树莓派3B的GPIO排针定义如下
LCD1602使用I2C连接方式连接到树莓派3B,SDA SCL VCC GND分布连接到树莓派3B的Pin3 Pin5 Pin3 Pin6
然后给树莓派3B上电,输入下面命令安装所需组件- sudo apt-get install i2c-tools python-smbus
复制代码
然后使用查看I2C设备是否显示
可知没有发现I2C设备,但是硬件连接又正确,原因是Raspbian固件默认关闭了I2C接口,输入下面命令按下面选择打开I2C接口
再次查看i2c设备可以看到i2c-1设备了
查看地址码可知为3f
创建一个python脚本lcd-clock.py加入下面代码- import smbus
- import time
- import os
- from time import gmtime, strftime, localtime
- os.environ['TZ'] = 'Asia/Shanghai'
- time.tzset()
- bus = smbus.SMBus(1)
- 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)
复制代码
保存后运行
数字日历效果如下
|
|