使用micropython编写max7219模块的控制程序行列互换
以下是使用micropython编写的max7219模块的控制程序,行列互换:
from machine import Pin, SPI
import time
spi = SPI(1, baudrate=10000000, polarity=0, phase=0)
ss = Pin(15, Pin.OUT)
def send_byte(addr, data):
ss.value(0)
spi.write(bytearray([addr, data]))
ss.value(1)
def init():
send_byte(0x09, 0x00) # decode mode off
send_byte(0x0a, 0x03) # brightness
send_byte(0x0b, 0x07) # scan limit
send_byte(0x0c, 0x01) # power up
send_byte(0x0f, 0x00) # test mode off
def set_pixel(row, col, value):
if row < 0 or row > 7 or col < 0 or col > 7:
return
if value:
send_byte(row + 1, 1 << col)
else:
send_byte(row + 1, ~(1 << col))
def set_row(row, data):
if row < 0 or row > 7:
return
send_byte(row + 1, data)
def set_col(col, data):
if col < 0 or col > 7:
return
for i in range(8):
set_pixel(i, col, (data >> i) & 1)
def transpose(matrix):
return [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
init()
# set all pixels to on
for row in range(8):
for col in range(8):
set_pixel(row, col, 1)
# wait for 1 second
time.sleep(1)
# set all pixels to off
for row in range(8):
for col in range(8):
set_pixel(row, col, 0)
# wait for 1 second
time.sleep(1)
# set rows and columns in a checkerboard pattern
for i in range(8):
if i % 2 == 0:
set_row(i, 0x55)
set_col(i, 0xaa)
else:
set_row(i, 0xaa)
set_col(i, 0x55)
# wait for 1 second
time.sleep(1)
# transpose the matrix and display it
matrix = [[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]]
matrix = transpose(matrix)
for row in range(8):
for col in range(8):
set_pixel(row, col, matrix[row][col])
# wait for 1 second
time.sleep(1)
# set all pixels to on
for row in range(8):
for col in range(8):
set_pixel(row, col, 1)
# wait for 1 second
time.sleep(1)
# set all pixels to off
for row in range(8):
for col in range(8):
set_pixel(row, col, 0)
``
原文地址: https://www.cveoy.top/t/topic/cqbF 著作权归作者所有。请勿转载和采集!