一、目的
这一节我们来学习如何使用SP32 C3, 用PCA9548 I2C总线扩展挂载多个ssd1306屏幕。
二、环境
合宙ESP32 C3开发板(icroPython v1.19.1 on 2022-06-18)+ PCA9548 I2C扩展开发板 + ssd1306显示屏3块 + Win10商业版
ESP32 C3和PCA9548接线方法:
SSD1306屏幕1接线方法:
SSD1306屏幕2接线方法:
SSD1306屏幕n接线方法和屏幕1和屏幕2依次类推。
三、示例代码
from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
from ufont import BMFont
from fonts import urequests
import ujson
import time
import network
#液晶屏I2C地址
addr = 0x3c
#创建I2C对象
i2c = I2C(0,scl = Pin(5),sda = Pin(4),freq = 400_000)
#创建old对象
#oled = SSD1306_I2C(128,64,i2c,addr)
#挂载字库
font = BMFont("fonts/unifont-14-12888-16.v3.bmf")
#print("%#x"%i2c.scan()[0]) # 打印液晶屏I2C地址
# 显示函数
def dispaly(Num):
global oled
if Num > 0xff:
print("已挂载的器件已超出上线!")
i2c.writeto(0x70,chr(Num))
oled = SSD1306_I2C(128,64,i2c,addr)
# 网络连接函数
def do_connect():
# 指定在第一路显示屏上显示,即接在SD0,SC0上的液晶屏
dispaly(0x01)
# 创建网络对象
wlan = network.WLAN(network.STA_IF)
# 激活网络对象
wlan.active(True)
# 检查网络是否连接
if not wlan.isconnected():
font.text(oled,"connecting to network...",0,0,color=1,font_size=16,reverse=False,reverse=False,clear=True,show=True,auto_wrap=True)
wlan.connect('WIFI名字', 'WIFI密码') # 换成你的WIFI信息
while not wlan.isconnected():
pass
# 显示网络相关信息
font.text(oled,"network",36,0,color=1,font_size=16,reverse=False,reverse=False,clear=True,show=True,auto_wrap=True)
font.text(oled,"{}".format(wlan.ifconfig()[0]),0,16,color=1,font_size=16,reverse=False,reverse=False,clear=False,show=True,auto_wrap=True)
font.text(oled,"{}".format(wlan.ifconfig()[1]),0,32,color=1,font_size=16,reverse=False,reverse=False,clear=False,show=True,auto_wrap=True)
font.text(oled,"{}".format(wlan.ifconfig()[2]),0,48,color=1,font_size=16,reverse=False,reverse=False,clear=False,show=True,auto_wrap=True)
time.sleep(3)
oled.clear() # 清屏
# 获取网络诗词函数
def poetry():
try:
# 指定在第1路显示屏上显示,即接在SD0,SC0上的液晶屏
dispaly(0x01)
# 调用网络诗词API
Text = urequests.get("https://v1.jinrishici.com/all.json")
# 解析获得的数据
Dic = ujson.loads(Text.text)
code = Dic["content"]
# 显示在屏幕上
font.text(oled,"%s"%(code),0,0,color=1,font_size=16,reverse=False,reverse=False,clear=True,show=True,auto_wrap=True)
# 指定在第2路显示屏上显示,即接在SD1,SC1上的液晶屏
dispaly(0x02)
# 显示获得的诗词的有多少字节
font.text(oled,"%d"%(len(code)),24,0,color=1,font_size=64,reverse=False,reverse=False,clear=True,show=True,auto_wrap=True)
time.sleep(3)
except:
# 指定在第3路显示屏上显示,即接在SD2,SC2上的液晶屏
dispaly(0x03) # 0000 0011
font.text(oled,"数据异常!",24,24,color=1,font_size=16,reverse=False,reverse=True,clear=False,show=True,auto_wrap=True)
def main():
do_connect()
while True:
poetry()
if __name__ == "__main__":
main()
演示效果:
四、示例相关库和字体
链接: https://pan.baidu.com/s/11w7MG612ndwfl3KYFTyPJg 提取码: p6mf 复制这段内容后打开百度网盘手机App,操作更方便哦
五、开发板购买链接