Eight digit Max7219 Seven-Segment Displays

Eight-digit seven-segment display driven by Uno SPI.

I have been experimenting with these 8 digit displays driven by the Max7219 driver chip. The driver chip allows you to control the segments with persistent serial data commands, rather than having to drive the segments yourself regularly from your main microcontroller.

They are driven over an SPI bus, which requires only three lines, not including vcc and gnd. SPI is built into the 328P microcontroller on the UNO. One line is for the serial data out, one is a synchronizing clock, and one line is the Chip Select line. (SPI uses another line for serial data in, but I don’t need that in this case.) The Max7219 doesn’t actually have full SPI compatibility, as it does not have a Chip Select line, but rather a LOAD line, which is a little different, and is problematic if you wanted to put more than one device on your SPI bus. I ordered some Max7222 chips also, which have full SPI compatibility; but in this use case it doesn’t really matter as the Max7219 drivers support daisy chaining of the shift registers, meaning I can hook up multiple 8 digital displays using only one SPI device slot.

The arduino reference material on the SPI library is a little confusing, so it took some time for me to figure out how to program the SPI communication correctly. Also, I was confused for a while because the display kept showing up as randomly garbled. It turned out that there wasn’t really anything wrong fundamentally with my code, but just when the Max7219 boots up, the segment registered are pre-filled with seemingly random data, so I needed to blank or overwrite the digits first to see only what you want to see.

Here is the test code.

/*
  Copyright 2020 Christopher Howard

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include <SPI.h>

#define CH_H 0b00110111
#define CH_I 0b00010000
#define CH_BLK 0b00000000
#define CH_0 0b01111110
#define CH_1 0b00110000
#define CH_2 0b01101101
#define CH_3 0b01111001
#define CH_4 0b00110011
#define CH_POINT 0b10000000

void setup ()
{
	pinMode(8, OUTPUT);
	SPI.begin();
	SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
	digitalWrite(8, LOW);
	/* Normal Mode */
	SPI.transfer(0b00001100);
	SPI.transfer(0b00000001);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	/* Scan limit: all digits */
	SPI.transfer(0b00001011);
	SPI.transfer(0b00000111);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	/* 25/32 intensity */
	SPI.transfer(0b00001010);
	SPI.transfer(0b00001100);
	digitalWrite(8, HIGH);
	/* Write some characters */
	digitalWrite(8, LOW);
	SPI.transfer(0b00000001);
	SPI.transfer(CH_0);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	SPI.transfer(0b00000010);
	SPI.transfer(CH_1);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	SPI.transfer(0b00000011);
	SPI.transfer(CH_2 | CH_POINT);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	SPI.transfer(0b00000100);
	SPI.transfer(CH_3);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	SPI.transfer(0b00000101);
	SPI.transfer(CH_4 | CH_POINT);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	SPI.transfer(0b00000110);
	SPI.transfer(CH_BLK);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	SPI.transfer(0b00000111);
	SPI.transfer(CH_I);
	digitalWrite(8, HIGH);
	digitalWrite(8, LOW);
	SPI.transfer(0b00001000);
	SPI.transfer(CH_H);
	digitalWrite(8, HIGH);
	SPI.endTransaction();
	SPI.end();
}

void loop ()
{
}

1 thought on “Eight digit Max7219 Seven-Segment Displays”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s