SPI串列通訊實作Part1 – MAX7219 8x8矩陣顯示
電路圖
程式列表
- MAX7219.py
|
# Reference: # https://github.com/adafruit/micropython-adafruit-max7219 # https://micropython-max7219.readthedocs.io/en/latest/matrix.html
REG_NO_OP = const(0x00) REG_DIGIT_BASE = const(0x01) REG_DIGIT0 = const(0x01) REG_DIGIT1 = const(0x02) REG_DIGIT2 = const(0x03) REG_DIGIT3 = const(0x04) REG_DIGIT4 = const(0x05) REG_DIGIT5 = const(0x06) REG_DIGIT6 = const(0x07) REG_DIGIT7 = const(0x08) REG_DECODE_MODE = const(0x09) #Decode-Mode Register # 0x00: No decode for digits 7¡V0 # 0x01: Code B decode for digit 0,No decode for digits 7¡V1 # 0x0F: Code B decode for digits 3¡V0, No decode for digits 7¡V4 # 0xFF: Code B decode for digits 7¡V0, REG_INTENSITY = const(0x0A) #Intensity Register Format # MAX7219 MAX7221 # 0x00: 1/16 1/32 # 0x01: 3/32 2/16 # 0x02: 5/32 3/16 # 0x03: 7/32 4/16 # 0x04: 9/32 5/16 # 0x05: 11/32 6/16 # 0x06: 13/32 7/16 # 0x07: 15/32 8/16 # 0x08: 17/32 9/16 # 0x09: 19/32 10/16 # 0x0A: 21/32 11/16 # 0x0B: 23/32 12/16 # 0x0C: 25/32 13/16 # 0x0D: 27/32 14/16 # 0x0E: 29/32 15/16 # 0x0F: 15/16 31/32 REG_SCAN_LIMIT = const(0x0B) #Scan-Limit Register Format # 0x00: Display digits 0 X X X X X X X # 0x01: Display digits 0 1 X X X X X X # 0x02: Display digits 0 1 2 X X X X X # 0x03: Display digits 0 1 2 3 X X X X # 0x04: Display digits 0 1 2 3 4 X X X # 0x05: Display digits 0 1 2 3 4 5 X X # 0x06: Display digits 0 1 2 3 4 5 6 X # 0x07: Display digits 0 1 2 3 4 5 6 7 REG_SHUTDOWN = const(0x0C) #Shutdown Register Format # 0x00 Shutdown Mode # 0x01 Normal Operation REG_DISPLAY_TEST = const(0x0F) #Display-Test Register Format # 0x00 Normal Operation # 0x01 Display Test Mode
NUM_DIGITS = 8
class Matrix8x8: def __init__(self, spi, intensity=7): self.spi = spi self.buffer = bytearray(NUM_DIGITS) self.intensity = intensity self.init()
def init(self): for command, data in ( (REG_SHUTDOWN, 0), (REG_DECODE_MODE, 0), (REG_INTENSITY, self.intensity), (REG_SCAN_LIMIT, 7), (REG_DISPLAY_TEST, 0), (REG_SHUTDOWN, 1), ): self.set_register(command, data)
def set_register(self, register, value): self.spi.write(bytearray([value, register]))
def brightness(self, value): if not 0 <= value <= 15: raise ValueError("Brightness out of range") self.set_register(REG_INTENSITY, value)
def fill(self, color): data = 0xff if color else 0x00 for y in range(8): self.buffer[y] = data
def pixel(self, x, y, color=None): if color is None: return bool(self.buffer[y] & 1 << x) elif color: self.buffer[y] |= 1 << x else: self.buffer[y] &= ~(1 << x)
def show(self): for y in range(0,NUM_DIGITS): self.set_register(REG_DIGIT_BASE + y, self.buffer[y]) |
- MAX7219_Matrix_Test.py
|
from machine import KEY #獲取按鍵KEY庫 from machine import SPI, PIN import MAX7219 import utime
ePyCloud = [0x18, 0x7E, 0x5A, 0xFF, 0x5A, 0x66, 0x7E, 0x40]
symbol=[ [0x18, 0x18, 0x3C, 0x5A, 0x5A, 0x24, 0x24, 0x66], [0x38, 0x30, 0x1E, 0x39, 0x48, 0x14, 0x22, 0x61], [0x38, 0x30, 0x1E, 0x39, 0x68, 0x14, 0x22, 0x61], [0x38, 0x30, 0x1C, 0x3A, 0x28, 0x14, 0x24, 0x22], [0x38, 0x30, 0x18, 0x1C, 0x18, 0x0C, 0x0C, 0x06], [0x38, 0x30, 0x1C, 0x3A, 0x28, 0x14, 0x24, 0x22]]
face = [ [0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x98, 0x42, 0x3C], [0x3C, 0x42, 0xA5, 0x81, 0xBD, 0x81, 0x42, 0x3C], [0x3C, 0x42, 0xA5, 0x81, 0x99, 0xA5, 0x42, 0x3C]]
spi_1 = None spi_1 =SPI(1, mode=SPI.MASTER, baudrate=5000000, polarity=1, phase=0) #For Hardware SPI bus
# Start Function if __name__ == '__main__': display = MAX7219.Matrix8x8(spi_1) # display.brightness(7)
display.fill(False) display.show() utime.sleep(1)
# display.fill(False) # display.pixel(4, 4, True) # display.show() # utime.sleep(1)
for y in range(8): display.buffer[y] = ePyCloud[y] display.show() utime.sleep(2)
for y in range(8): display.buffer[y] = symbol[0][y] display.show() utime.sleep(2)
for z in range(20): for x in range(1,6): for y in range(8): display.buffer[y] = symbol[x][y] display.show() utime.sleep_ms(200)
display.fill(False) display.show() utime.sleep(1)
spi_1 .deinit() |
沒有留言:
張貼留言