一、目的
这一节我们继续学习如何使用我们的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
这个大家自己去实验哈。。。
八、参考资料
请看上一节文末,谢谢关注!