MAX7219是一种集成化的串行输入/输出共阴极显示驱动器,它连接微处理器与8位数字的7段数字LED显示,也可以连接条线图显示器或者64个独立的LED。MAX7219上包括一个片上的B型BCD编码器、多路扫描回路,段字驱动器,而且还有一个8*8的静态RAM用来存储每一个数据。只有一个外部寄存器用来设置各个LED的段电流。MAX7219和微处理器只需三根导线连接,每位显示数字有一个地址由微处理器写入。允许设计者选择每位是BCD译码或不译码。设计者还可选择停机模式、数字亮度控制、从1~8选择扫描位数和对所有LED 显示器的测试模式。
MAX7219直插式芯片引脚分布图如下:
各引脚功能如下:
DIN:串行数据输入端;
DIG0~DIG7:LED 位线;
LOAD:数据装载信号输入端;
SEGA~SEGG,SEGDp:段码输出端;
ISET:硬件亮度调节端;
DOUT:串行数据输出端;
CLK:移位脉冲输入端;
V+:正电源;
GND:地。
MAX7219和MCU连接有三条引线(DIN、CLK、LOAD),采用16位数据串行移位接收方式。即单片机将16位二进制数逐位发送到DIN端,在CLK上升沿到来前准备就绪,CLK的每个上升沿将一位数据移入MAX7219内移位寄存器,当16位数据移入完,在LOAD引脚信号上升沿将16位数据装入MAX7219内的相应位置,在MAX7219内部硬件动态扫描显示控制电路作用下实现动态显示。以下是MAX7219模块的电路原理图。
本次实验Pico与MAX7219模块的实物连线如下:
VBUS----->VCC
GND------>GND
GPIO2---->CLK
GPIO3---->DIN
GPIO5---->CS
接下来我们还是使用Thonny软件来编写点阵屏动态显示程序,当然关于这个模块的驱动源码在github上开源化了,足见其应用广泛性。
max7219.py库文件源码如下,编写完成后保存到pico根目录下的"lib"文件夹下。
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)
用户测试主程序如下,编写完成后保存到pico设备的根目录下即可。
import max7219 from machine import Pin, SPI from time import sleep spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3)) ss = Pin(5, Pin.OUT) msg = 'EEPW.COM.CN' length = len(msg) length = ((length-3)*8) display = max7219.Matrix8x8(spi, ss, 4) display.brightness(5) # adjust brightness 1 to 15 display.fill(0) display.show() sleep(0.1) while True: for x in range(32, -length, -1): display.text(msg ,x,0,1) display.show() sleep(0.1) display.fill(0)
实验的效果就是8*8点阵屏动态循环显示“EEPW.COM.CN”字符。源码中主要部分是SPI在MicroPython编程中的实现方法。这里的库文件好比MCU下的外设驱动源码,至关重要。