物联网开发104 - Micropython ESP32 C3用PCA9548 I2C总线扩展挂载多个ssd1306屏幕

一、目的

        这一节我们来学习如何使用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,操作更方便哦

五、开发板购买链接

https://item.taobao.com/item.htm?spm=a1z09.2.0.0.7bb82e8djoNWJg&id=611098848999&_u=vp01rchcc24https://item.taobao.com/item.htm?spm=a1z09.2.0.0.7bb82e8djoNWJg&id=611098848999&_u=vp01rchcc24

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔都飘雪

您的1毛奖励是我创作的源源动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值