max7219_8digit
max7219_8digit copied to clipboard
MAX7219 driver for 7 segment displays with ESP-07 and micropython
I have loaded the MAX7219 driver by pdwerryhouse on my ESP-07 (with 4MiB flash) and Micropython v1.20.0.
Connections are: CS => GPIO5, MOSI => GPIO2, SCLK => GPIO4.
I have used level translators (MOSFETs) to connect the 5V MAX7219 pins to the 3V ESP-07 pins.
The code is same as downloaded from the github website. I am a newbie with Micropython and ESP8266, but quite proficient with C and AVR's. So I am confident about the wiring connections.
I have used the software code as it is, except that since I am using SoftSPI, I have changed the line for import as 'from machine import Pin, SoftSPI'. Since I am not using "write_to_buffer_with_dots()" I have commented this out. All other code is same.
Please assist me in getting this display working, my further plan is to make a NTP clock using these components and pwderryhouse driver code.
<main.py> #shown as README in github `from machine import Pin, SoftSPI import max7219_8digit
the max7219 doesn't use the MISO, but unfortunately SoftSPI requires that
we specify it anyway
spi = SoftSPI(baudrate=100000, polarity=0, phase=0, sck=Pin(4), mosi=Pin(2), miso=Pin(0)) spi.init(baudrate = 100000) #initialise SoftSPI ss = Pin(5, Pin.OUT)
display = max7219_8digit.Display(spi, ss)
display.write_to_buffer('12345678')
display.display()
<max7219_8digit.py>
# G:\Microcontrollers\ESP8266\uPython_libs\MAX7219_7segLEDdriver\pdwerryhouse_github\max7219_8digit.py
Licence: GPLv3
Copyright 2017 Paul Dwerryhouse [email protected]
CHAR_MAP = { '0': 0x7e, '1': 0x30, '2': 0x6d, '3': 0x79, '4': 0x33, '5': 0x5b, '6': 0x5f, '7': 0x70, '8': 0x7f, '9': 0x7b, 'a': 0x77, 'b': 0x1f, 'c': 0x4e, 'd': 0x3d, 'e': 0x4f, 'f': 0x47, 'g': 0x7b, 'h': 0x37, 'i': 0x30, 'j': 0x3c, 'k': 0x57, 'l': 0x0e, 'm': 0x54, 'n': 0x15, 'o': 0x1d, 'p': 0x67, 'q': 0x73, 'r': 0x05, 's': 0x5b, 't': 0x0f, 'u': 0x1c, 'v': 0x3e, 'w': 0x2a, 'x': 0x37, 'y': 0x3b, 'z': 0x6d, 'A': 0x77, 'B': 0x1f, 'C': 0x4e, 'D': 0x3d, 'E': 0x4f, 'F': 0x47, 'G': 0x7b, 'H': 0x37, 'I': 0x30, 'J': 0x3c, 'K': 0x57, 'L': 0x0e, 'M': 0x54, 'N': 0x15, 'O': 0x1d, 'P': 0x67, 'Q': 0x73, 'R': 0x05, 'S': 0x5b, 'T': 0x0f, 'U': 0x1c, 'V': 0x3e, 'W': 0x2a, 'X': 0x37, 'Y': 0x3b, 'Z': 0x6d, ' ': 0x00, '-': 0x01, '\xb0': 0x63, '.': 0x80 }
REG_NO_OP = 0x00 REG_DIGIT_BASE = 0x01 REG_DECODE_MODE = 0x09 REG_INTENSITY = 0x0a REG_SCAN_LIMIT = 0x0b REG_SHUTDOWN = 0x0c REG_DISPLAY_TEST = 0x0f
class Display:
def __init__(self, spi, ss, intensity=7):
self.spi = spi
self.ss = ss
self.buffer = bytearray(8)
self.intensity = intensity
self.reset()
def reset(self):
self.set_register(REG_DECODE_MODE, 0)
self.set_register(REG_INTENSITY, self.intensity)
self.set_register(REG_SCAN_LIMIT, 7)
self.set_register(REG_DISPLAY_TEST, 0)
self.set_register(REG_SHUTDOWN, 1)
def set_register(self, register, value):
self.ss.off()
self.spi.write(bytearray([register, value]))
self.ss.on()
def decode_char(self, c):
d = CHAR_MAP.get(c)
return d if d != None else ' '
def write_to_buffer(self, s):
l = len(s)
if l < 8:
s = "%-8s" % s
for i in range(0,8):
self.buffer[7-i] = self.decode_char(s[i])
def write_to_buffer_with_dots(self, s):
len_s = len(s)
x = 0
i = 0
while i < len_s:
if x >= 8:
break
elif i < (len_s - 1) and s[i+1] == '.':
self.buffer[7-x] = self.decode_char(s[i]) | 0x80
i += 1
else:
self.buffer[7-x] = self.decode_char(s[i])
x += 1
i += 1
while x < 8:
self.buffer[7-x] = self.decode_char(' ')
x += 1
def display(self):
for i in range(0,8):
self.set_register(REG_DIGIT_BASE + i, self.buffer[i])
def set_intensity(self, i):
self.intensity = i
self.set_register(REG_INTENSITY, self.intensity)
`