比派科技banana pi专区
直播中

jinglixixi

8年用户 1592经验值
擅长:嵌入式技术 光电显示 接口/总线/驱动 控制/MCU
私信 关注
[经验]

【Banana PI Leaf S3开发板试用体验】8X8点阵模块显示测试

zf

8X8点阵模块是一款基于MAX7219显示器件,该模块共有5****个引脚,其功能如下:

VCC ---- 5V电源

GND ---- 接电源地

DIN ---- 数据引脚

CLK ---- 时钟引脚

CS ---- 片选引脚

image.png

图1 8X8点阵模块

要驱动该显示模块,要使用到2个文件,即05.SPI.py max7219.py

在运行 05.SPI.py 之前,要先将 max7219.py 文件已上传至开发板,因为它是MAX7219的驱动。

**在完成2个文件的上传后,点阵模块会以0.5秒的时间间隔循环显示大写英文字母 A至Z ,其效果如图2至图4所示。
image.png
图2 显示字符“O”
image.png
图3显示字符“K
image.png
图4 显示字符“Q

文件max7219.py的内容如下:

from machine import Pin
from micropython import const
import framebuf
"""
Driver for MAX7219 8x8 LED matrices
Inheritance for FrameBuffer:
self.fill  # (col)
self.pixel # (x, y[, c])
self.hline  # (x, y, w, col)
self.vline  # (x, y, h, col)
self.line  # (x1, y1, x2, y2, col)
self.rect  # (x, y, w, h, col)
self.fill_rect  # (x, y, w, h, col)
self.text  # (string, x, y, col=1)
self.scroll  # (dx, dy)
self.blit  # (fbuf, x, y[, key])
Example for ESP8266 with 2x4 matrices (one on top, one on bottom),
so we have a 32x16 display area:
>>>from machine import Pin, SPI
>>>from max7219 import Max7219
>>>spi = SPI(1, baudrate=10000000)
>>>screen = Max7219(32, 16, spi, Pin(15))
>>>screen.rect(0, 0, 32, 16, 1)  # Draws a frame
>>>screen.text('Hi!', 4, 4, 1)
>>>screen.show()
On some matrices, the display is inverted (rotated 180°), in this case
you can use `rotate_180=True` in the class constructor.
"""

_DIGIT_0 = const(0x1)

_DECODE_MODE = const(0x9)
_NO_DECODE = const(0x0)

_INTENSITY = const(0xA)
_INTENSITY_MIN = const(0x0)

_SCAN_LIMIT = const(0xB)
_DISPLAY_ALL_DIGITS = const(0x7)

_SHUTDOWN = const(0xC)
_SHUTDOWN_MODE = const(0x0)
_NORMAL_OPERATION = const(0x1)

_DISPLAY_TEST = const(0xF)
_DISPLAY_TEST_NORMAL_OPERATION = const(0x0)

_MATRIX_SIZE = const(8)

class Max7219(framebuf.FrameBuffer):

def __init__(self, width, height, spi, cs, rotate_180=False):
        # Pins setup
        self.spi = spi
        self.cs = cs
self.cs.init(Pin.OUT, True)

        # Dimensions
        self.width = width
        self.height = height
        # Guess matrices disposition
        self.cols = width // _MATRIX_SIZE
        self.rows = height // _MATRIX_SIZE
        self.nb_matrices = self.cols * self.rows
        self.rotate_180 = rotate_180
        # 1 bit per pixel (on / off) -> 8 bytes per matrix
        self.buffer = bytearray(width * height // 8)
format = framebuf.MONO_HLSB if not self.rotate_180 else framebuf.MONO_HMSB
super().__init__(self.buffer, width, height, format)

        # Init display
        self.init_display()

def _write_command(self, command, data):
        """Write command on SPI"""
cmd = bytearray([command, data])
self.cs(0)
for matrix in range(self.nb_matrices):
self.spi.write(cmd)
self.cs(1)

def init_display(self):
        """Init hardware"""
for command, data in (
            (_SHUTDOWN, _SHUTDOWN_MODE),  # Prevent flash during init
            (_DECODE_MODE, _NO_DECODE),
            (_DISPLAY_TEST, _DISPLAY_TEST_NORMAL_OPERATION),
            (_INTENSITY, _INTENSITY_MIN),
            (_SCAN_LIMIT, _DISPLAY_ALL_DIGITS),
            (_SHUTDOWN, _NORMAL_OPERATION),  # Let's go
        ):
            self._write_command(command, data)

self.fill(0)
self.show()

def brightness(self, value):
        """Set display brightness (0 to 15)"""
if not 0 <= value < 16:
raise ValueError("Brightness must be between 0 and 15")
        self._write_command(_INTENSITY, value)

def show(self):
        """Update display"""
        # Write line per line on the matrices
for line in range(8):
self.cs(0)

for matrix in range(self.nb_matrices):
                # Guess where the matrix is placed
row, col = divmod(matrix, self.cols)
                # Compute where the data starts
if not self.rotate_180:
offset = row * 8 * self.cols
index = col + line * self.cols + offset
else:
offset = 8 * self.cols - row * (8 - line) * self.cols
index = (7 - line) * self.cols + col - offset

self.spi.write(bytearray([_DIGIT_0 + line, self.buffer[index]]))

self.cs(1)

文件05.SPI.py的内容如下:

from machine import SoftSPI,Pin
from m import Max7219
import time

spi = SoftSPI(baudrate=10000000, #SCK时钟频率
              polarity=1, #时钟线空闲时高电平,有效时低电平
              phase=0, #相位,在第一时钟沿采样数据
              sck=Pin(12), #连接CLK
              mosi=Pin(10),#连接DIN
              miso=Pin(9)) #max7219三线即可控制,miso可不接
screen = Max7219(width=8,#LED点阵宽
                 height=8,#LED点阵高
                 spi=spi, #使用spi设置
                 cs=Pin(11),#连接CS
                 rotate_180=False) #不进行180度翻转
text= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
list1 = []
for x in text:
  list1.append(x) #将text中的字符串逐一取出放入list1列表
screen.brightness(1)#设置亮度,最高15

while True:
    for i in range(len(list1)):#获取list1列表内元素的总数并以此数进行for循环
        screen.fill(0)#清屏
        screen.text(list1[i], 0, 0, 1)#设置将要显示的内容及位置
        screen.show()#显示输出
       time.sleep(0.5)

通过对程序的分析可知,相关引脚的连接关系如下:

DIN----GPIO10

CLK----GPIO12

CS----GPIO11

其实,这也说明其引脚连接关系在未被它用的情况下是可以自行选择的。

此外,如果将text= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
改为text= '0123456789',也是可以显示数字的。

sz

更多回帖

发帖
×
20
完善资料,
赚取积分