物联网开发笔记(77)- 使用Micropython开发ESP32开发板之使用MAX7219驱动控制8x8LED点阵模块(续)

一、目的

        这一节我们继续学习如何使用我们的ESP32开发板控制带MAX7219驱动的8x8LED点阵模。我们使用库来显示,更加方便。

二、环境

        ESP32 + MAX7219驱动的8x8LED点阵模块 + Thonny IDE + 几根杜邦线 + Win10

接线方法:     

 

 三、max7219 8*8点阵屏驱动:

raspberrypi-pico/max7219.py at main · stechiez/raspberrypi-pico · GitHub

max7219.py 

"""
MicroPython max7219 cascadable 8x8 LED matrix driver
https://github.com/mcauser/micropython-max7219
MIT License
Copyright (c) 2017 Mike Causer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from micropython import const
import framebuf

_NOOP = const(0)
_DIGIT0 = const(1)
_DECODEMODE = const(9)
_INTENSITY = const(10)
_SCANLIMIT = const(11)
_SHUTDOWN = const(12)
_DISPLAYTEST = const(15)

class Matrix8x8:
    def __init__(self, spi, cs, num):
        """
        Driver for cascading MAX7219 8x8 LED matrices.
        >>> import max7219
        >>> from machine import Pin, SPI
        >>> spi = SPI(1)
        >>> display = max7219.Matrix8x8(spi, Pin('X5'), 4)
        >>> display.text('1234',0,0,1)
        >>> display.show()
        """
        self.spi = spi
        self.cs = cs
        self.cs.init(cs.OUT, True)
        self.buffer = bytearray(8 * num)
        self.num = num
        fb = framebuf.FrameBuffer(self.buffer, 8 * num, 8, framebuf.MONO_HLSB)
        self.framebuf = fb
        # Provide methods for accessing FrameBuffer graphics primitives. This is a workround
        # because inheritance from a native class is currently unsupported.
        # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
        self.fill = fb.fill  # (col)
        self.pixel = fb.pixel # (x, y[, c])
        self.hline = fb.hline  # (x, y, w, col)
        self.vline = fb.vline  # (x, y, h, col)
        self.line = fb.line  # (x1, y1, x2, y2, col)
        self.rect = fb.rect  # (x, y, w, h, col)
        self.fill_rect = fb.fill_rect  # (x, y, w, h, col)
        self.text = fb.text  # (string, x, y, col=1)
        self.scroll = fb.scroll  # (dx, dy)
        self.blit = fb.blit  # (fbuf, x, y[, key])
        self.init()

    def _write(self, command, data):
        self.cs(0)
        for m in range(self.num):
            self.spi.write(bytearray([command, data]))
        self.cs(1)

    def init(self):
        for command, data in (
            (_SHUTDOWN, 0),
            (_DISPLAYTEST, 0),
            (_SCANLIMIT, 7),
            (_DECODEMODE, 0),
            (_SHUTDOWN, 1),
        ):
            self._write(command, data)

    def brightness(self, value):
        if not 0 <= value <= 15:
            raise ValueError("Brightness out of range")
        self._write(_INTENSITY, value)

    def show(self):
        for y in range(8):
            self.cs(0)
            for m in range(self.num):
                self.spi.write(bytearray([_DIGIT0 + y, self.buffer[(y * self.num) + m]]))
            self.cs(1)
            

四、演示代码1

from machine import Pin,SPI
import max7219,time

spi=SPI(1,1000000,sck=Pin(14),mosi=Pin(13),miso=Pin(19))
MAX=max7219.Matrix8x8(spi,Pin(15),1) #CS-pin15;1代表一块8*8,如果有4块就是4;
MAX.init() #初始化

def main():
    MAX.brightness(7) #亮度0-15
    MAX.fill(0)  #1全部点亮;0全部灭
    MAX.show()  #更新显示
    
    while True:
        for i in range(8):
            '''
            # 画点
            MAX.pixel(0,0,1) #行;列;1亮0灭
            MAX.show()
            # 画横线
            MAX.hline(1,2,3,1)
            MAX.show()
            # 画竖线
            MAX.vline(1,2,3,1)
            MAX.show()
            # 画线
            MAX.line(1,2,3,1,1)
            MAX.show()
            # 画矩形
            MAX.rect(1,1,6,6,1)
            MAX.show()
            
            # 画方块
            MAX.fill_rect(1,1,5,5,1)
            MAX.show()
            
            # 显示文本
            MAX.text("A",0,0,1)
            MAX.show()
            '''
            # 动态显示文本
            MAX.fill(0)
            MAX.text("%.1d"%i,0,1,1) 
            MAX.show()
            time.sleep_ms(500)
            
            
if __name__=="__main__":
    main()

五、演示效果1

      

 六、演示代码2


'''from machine import Pin,SPI
import max7219,time

spi=SPI(1,1000000,sck=Pin(14),mosi=Pin(13),miso=Pin(19))
MAX=max7219.Matrix8x8(spi,Pin(15),1) #CS-pin15;1代表一块8*8,如果有4块就是4;
MAX.init() #初始化
'''
import max7219
from machine import Pin,SPI
from time import sleep

spi = SPI(1, baudrate=1000000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13))
ss = Pin(15, Pin.OUT)
 
msg = 'STechiezDIY'
length = len(msg)
#length = length*8
display = max7219.Matrix8x8(spi, ss, 1)
display.brightness(1)   # adjust brightness 1 to 15
display.fill(0)
display.show()
sleep(0.5)
 
while True:
    '''
    # 向左滚动
    for x in range(8, -length, -1):
        display.text(msg,x,0,1)
        display.show()
        sleep(0.10)
        display.fill(0)
        
    # 向右滚动
    for x in range(-length,-1,1):
        display.text(msg,x,0,1)
        display.show()
        sleep(0.10)
        display.fill(0)
    
    
    # 向下移动
    for j in range(length):
        for y in range(8,-1,-1):
            display.text(msg[j],0,-y,1)
            display.show()
            sleep(0.10)
            display.fill(0)
    '''

    # 向上移动
    for j in range(length):
        for y in range(8,-1,-1):
            display.text(msg[j],0,y,1)
            display.show()
            sleep(0.10)
            display.fill(0)

七、演示效果2

这个大家自己去实验哈。。。

八、参考资料

Raspberry Pi Pico使用MicroPython(7)---用Max7219驱动点阵屏显示图案_leotzf的博客-CSDN博客_micropython max7219蓝牙遥控Max7219点阵屏显示图案和文字.https://blog.csdn.net/weixin_45616112/article/details/122038561九、点阵屏购买

请看上一节文末,谢谢关注!

物联网开发笔记(76)- 使用Micropython开发ESP32开发板之使用MAX7219驱动控制8x8LED点阵模块_魔都飘雪的博客-CSDN博客使用Micropython开发ESP32开发板之使用MAX7219驱动控制8x8LED点阵模块https://blog.csdn.net/zhusongziye/article/details/128960872?spm=1001.2014.3001.5502

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

魔都飘雪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值