Driving 7-segment Displays with MAX7219

In a previous tutorial, we introduced the basic ideas about a seven-segment display and how to drive it with a 74HC595 shift register. In this tutorial, we'll show another LED driving IC: MAX7219, which is designed to drive 7-segment displays (up to 8 digits are supported), or 64 LEDs equivalently. By modifying the example used in the 74HC595 tutorial, we will go through major functionalities of MAX7219 by using LinkIt 7697 with a 8-digit 7-segment display module (with MAX7219 built in).

Table of Contents

Command Format

Before going into details of MAX7219, let's first look at how to communicate with it. All operations on MAX7219 are triggered by commands to update the values of its registers. And all commands share a unified format: a 16-bit "register address / data value" pair format (X stands for "don't care"):

By latching the LOAD pin and sending a 16-bit command with DIN pin in 16 clocks on CLK pin, a command is thus sent into the MAX7219. The timing diagram of a command sequence is:

This operation can be coded as below in Arduino. The shiftOut() API is used to send bits with clock pulses:

// define pins used on LinkIt 7697
// pin 13 of MAX7219 (CLK)
const int clock_pin = 15;
// pin 12 of MAX7219 (LOAD)
const int data_latch_pin = 16;
// pin 1 of MAX7219 (DIN)
const int data_input_pin = 17;
 
// update the register value of MAX7219
void set_register(byte address, byte value)  
{
  digitalWrite(data_latch_pin, LOW);
  shiftOut(data_input_pin, clock_pin, MSBFIRST, address);
  shiftOut(data_input_pin, clock_pin, MSBFIRST, value);
  digitalWrite(data_latch_pin, HIGH);
}

Hardware Setup

5 pins are needed from LinkIt 7697 to communicate with the 8-digit display module:

  • 5V

  • GND

  • P15

  • P16

  • P17

And they are connected to:

  • VCC

  • GND

  • CLK

  • CS (i.e. LOAD pin of MAX7219)

  • DIN

pins of the display module, respectively:

5V power is needed for the display module. If 3.3V is supplied, there would be a decrease in the display brightness of the module.

Register Map

For controlling the display, several registers of MAX7219 need to be set correctly. In the example of this tutorial, six types of registers will be accessed:

Name / FunctionalityAddressDescription

Decode Mode

0x09

Turn on / off the decode mode

Intensity

0x0A

Adjust the brightness of the display

Scan Limit

0x0B

Determine how many digits are enabled

Shutdown

0x0C

Turn on / off the display module

Display Test

0x0F

Enter the test mode (all LED segments light on) or not

Digit 0 ~ Digit 7 data

0x01 ~ 0x08

The address to control digit 0 ~ digit 7

And they are defined in the Arduino code as:

// the MAX7219 address map (datasheet table 2)
#define MAX7219_DECODE_REG      (0x09)
#define MAX7219_DIGIT_REG(pos)  ((pos) + 1)
#define MAX7219_INTENSITY_REG   (0x0A)
#define MAX7219_SCANLIMIT_REG   (0x0B)
#define MAX7219_SHUTDOWN_REG    (0X0C)
#define MAX7219_DISPLAYTEST_REG (0x0F)

For each of those registers, here we show some examples of applications:

Decode Mode (0x09)

MAX7219 supports two types of input data for each digit: 1) raw data or 2) BCD Code B. And each digit can run in different modes. For example:

set_register(MAX7219_DECODE_REG, B00011010);

Because only bit 1, 3 and 4 are set to 1, this command sets Digit 1 / 3 / 4 to BCD Code B decode mode and leaves Digit 0 / 2 / 5 / 6 / 7 to non-decode (raw data) mode.

Digit 0 ~ Digit 7 data (0x01 ~ 0x08)

In the raw data mode, users control all segments of a 7-segment display by themselves as what they did with 74HC595:

For instance, if a character "7" is going to be displayed on Digit 3, the segements "A", "B" and "C" (refer to the figure above) needs to be turned on, which means bit 6, bit 5 and bit 4 for the data byte of Digit 3 register need to be 1:

set_register(MAX7219_DIGIT_REG(3), B01110000);

Although the raw mode adds complexities to describe the display data, it provides the flexibility for users to define all display patterns they need as below:

On the other hand, for the decode mode, characters defined in the BCD Code B set (see the table below) can be recognized by MAX7219 and indexed by a single value directly:

*For displaying the decimal point, set the bit 7 (D7) to 1 no matter in decode mode or in raw data mode.

That is, you can ask MAX7219 to drive the display to show 16 different characters: "0" to "9", "-", "E", "H", "L", "P" and " " (blank) directly without specifying segment data by yourself. For example to show a character "E", first configure Digit 5 to decode mode and set value 11 (B1011, see the table above) to the data byte of Digit 5, this makes it display the "E" character:

// Make Digit 5 is under BCD Code B decode mode
set_register(MAX7219_DECODE_REG, B00100000);
// Make it display character "E"
set_register(MAX7219_DIGIT_REG(5), 11);

Intensity (0x0A)

Define the LED brightness of the display. The valid range is between 0x0 ~ 0xF.

// Dimmest
set_register(MAX7219_INTENSITY_REG, 0x0);
// Brightest
set_register(MAX7219_INTENSITY_REG, 0xF);

Scan Limit (0x0B)

Use this value to decide how many digits are turned on for display. Here are some examples:

// Only Digit 0 are turned on
set_register(MAX7219_SCANLIMIT_REG, 0);
// Only Digit 0 ~ Digit 4 are turned on
set_register(MAX7219_SCANLIMIT_REG, 4);
// All digits (Digit 0 ~ Digit 7) are turned on
set_register(MAX7219_SCANLIMIT_REG, 7);

Shutdown (0x0C)

Turn on / off the display:

#define MAX7219_OFF (0x0)
#define MAX7219_ON  (0x1)

// Turn off the display
set_register(MAX7219_SHUTDOWN_REG, MAX7219_OFF);
// Turn on the display
set_register(MAX7219_SHUTDOWN_REG, MAX7219_ON);

Display Test (0x0F)

Turn on the test mode makes all segments of the display light on.

// Enter test mode. No matter what data bytes of each digit register are, all display segments are on
set_register(MAX7219_DISPLAYTEST_REG, MAX7219_ON);
// Non-test mode. Display segments according to the data byte and decode mode for each digit
set_register(MAX7219_DISPLAYTEST_REG, MAX7219_OFF);

The Counting Example

Let's put everything mentioned above together and modify the example used in the 74HC595 tutorial. A hexadecimal counting example with floating decimal point is shown in the following video and with the accompanying Arduino sketch code.

//
// Use one MAX7219 to control 8-digit seven-segment display
// the display module: http://www.icshop.com.tw/product_info.php/products_id/20686
//
// MAX7219 datasheet: https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf
//

// the MAX7219 address map (datasheet table 2)
#define MAX7219_DECODE_REG      (0x09)
#define MAX7219_INTENSITY_REG   (0x0A)
#define MAX7219_SCANLIMIT_REG   (0x0B)
#define MAX7219_SHUTDOWN_REG    (0X0C)
#define MAX7219_DISPLAYTEST_REG (0x0F)
#define MAX7219_DIGIT_REG(pos)  ((pos) + 1)

// shutdown mode (datasheet table 3)
#define MAX7219_OFF             (0x0)
#define MAX7219_ON              (0x1)

// pin 13 of MAX7219 (CLK)
const int clock_pin = 15;
// pin 12 of MAX7219 (LOAD)
const int data_latch_pin = 16;
// pin 1 of MAX7219 (DIN)
const int data_input_pin = 17;

// digit pattern for a 7-segment display. datasheet table 5
const byte digit_pattern[16] =
{
  B01111110,  // 0
  B00110000,  // 1
  B01101101,  // 2
  B01111001,  // 3
  B00110011,  // 4
  B01011011,  // 5
  B01011111,  // 6
  B01110000,  // 7
  B01111111,  // 8
  B01111011,  // 9
  B01110111,  // A
  B00011111,  // b
  B01001110,  // C
  B00111101,  // d
  B01001111,  // E
  B01000111   // F
};

#define DP_FLAG       (B10000000)
#define NUM_OF_DIGITS (8)

unsigned int counter = 0;
unsigned int digit_base = 16;

// update the register value of MAX7219
void set_register(byte address, byte value)  
{
  digitalWrite(data_latch_pin, LOW);
  shiftOut(data_input_pin, clock_pin, MSBFIRST, address);
  shiftOut(data_input_pin, clock_pin, MSBFIRST, value);
  digitalWrite(data_latch_pin, HIGH);
}

void init_max7219()
{
  // disable test mode. datasheet table 10
  set_register(MAX7219_DISPLAYTEST_REG, MAX7219_OFF);
  // set medium intensity. datasheet table 7
  set_register(MAX7219_INTENSITY_REG, 0x8);
  // turn off display. datasheet table 3
  set_register(MAX7219_SHUTDOWN_REG, MAX7219_OFF);
  // drive 8 digits. datasheet table 8
  set_register(MAX7219_SCANLIMIT_REG, 7);
  // no decode mode for all positions. datasheet table 4
  set_register(MAX7219_DECODE_REG, B00000000);
}

void setup()  
{
  // init pin states
  pinMode(clock_pin, OUTPUT);
  pinMode(data_latch_pin, OUTPUT);    
  pinMode(data_input_pin, OUTPUT);

  // init MAX2719 states
  init_max7219();
}

void loop()  
{
  int i;
  unsigned int number;
  unsigned int digit_value;
  byte byte_data;
  
  number = counter++;
  
  // turn off display first
  set_register(MAX7219_SHUTDOWN_REG, MAX7219_OFF);

  // get every values in each position of those 8 digits based on "digit_base"
  //
  // digit_base should be <= 16
  //
  // for example, if digit_base := 2, binary values will be shown. If digit_base := 16, hexidecimal values will be shown
  //
  for (i = 0; i < NUM_OF_DIGITS; i++)
  {
    digit_value = number % digit_base;
    number /= digit_base;

    byte_data = digit_pattern[digit_value];

    if (counter % NUM_OF_DIGITS == i)
    {
      byte_data |= DP_FLAG;
    }

    set_register(MAX7219_DIGIT_REG(i), byte_data);
  }

  // turn on display
  set_register(MAX7219_SHUTDOWN_REG, MAX7219_ON);
  
  delay(50);
}

The default parameters of the setup are:

  • Count in hexadecimal mode

  • Use the raw data (non-decode) mode

  • Set to medium brightness

To change the hexadecimal mode to decimal / octal / binary counting mode, you can modify Line 52 to 10, 8 or 2. Below codes change the counting mode from hexadecimal to the decimal mode:

// change the counting mode to decimal mode
unsigned int digit_base = 10;

To enable the BCD Code B decode mode, you need to modify Line 74 and Line 111 as below:

// enable decode mode for all positions. datasheet table 4
set_register(MAX7219_DECODE_REG, B11111111);

and

digit_value = number % digit_base;
number /= digit_base;
// no need to get the raw segment patterns. use the value directly to index the BCD code
byte_data = digit_value; // byte_data = digit_pattern[digit_value];

Summary

By going through this tutorial, major functionalities of MAX7219 are introduced. Although it's designed to drive 7-segment displays, it can also be used to drive LEDs in a more general scenario. For example, a 8x8 dot-matrix display is another popular application which uses MAX7219 as the driving IC. This gives a good demonstration about how to use the raw data mode on driving such matrix displays. And we will take a closer look at it with another tutorial.

Last updated