# 8*8 LED矩正 動畫程式
# coding by 30409高俊霖
from machine import Pin, SPI
import time
# 影格設定
Num =4 #幾個影格
speed =9 # 1~9
# 每一個影格 一行
# 可使用 LED to 16進位 工具
# https://xantorohara.github.io/led-matrix-editor/
sprite = (
(0xFF,0x81,0xA5,0x81,0xA5,0x99,0x81,0xFF),
(0xFF,0x81,0xA5,0x81,0x81,0xBD,0x81,0xFF),
(0xFF,0x81,0xA5,0x81,0x99,0xA5,0x81,0xFF),
(0xFF,0x81,0xA5,0x81,0x81,0xBD,0x81,0xFF),
)
# 腳位設定
DECODEMODE = const(9)
INTENSITY = const(10)
SCANLIMIT = const(11)
SHUTDOWN = const(12)
DISPLAYTEST = const(15)
cs = Pin(5, Pin.OUT)
spi = SPI(1,
baudrate=10000000,
polarity=1,
phase=0,
sck=Pin(14, Pin.OUT), # clk
mosi=Pin(13, Pin.OUT)
)
def max7219(reg, data):
cs.value(0)
spi.write(bytes([reg, data]))
cs.value(1)
def init():
for reg, data in (
(DISPLAYTEST, 0),
(SCANLIMIT, 7),
(INTENSITY, 1),
(DECODEMODE, 0),
(SHUTDOWN, 1)
):
max7219(reg, data)
def clear():
for i in range(8):
max7219(i + 1, 0)
def show():
for i in range(Num):
for j in range(8):
max7219(j+1, sprite[i][j])
time.sleep(1/speed) # 等待 n秒
try:
init()
while True: # 無窮迴圈
show()
except:
clear()