Posted on Leave a comment

Understanding Max7219 LED Driver In C Programming

I’ve slowly been drifting away from Arduino. Even if I was making this transition full-time, it would still be slow. Real deal embedded programming is more like mountain climbing or aging than the usual skill development that happens via a few Youtube videos.

Big Things To Know About Max7219

  • The datasheet tells you most of what you know.
  • To talk to an LED matrix, we need to send an 8-bit address and an 8-bit value. This is actually only implied in the datasheet. It’s not full-blown explicit as the datasheet is more concerned with decoding for 7-segment displays. The addresses are 1-8 and correspond to a row/column (depending on which way you have your matrix turned. The 8-bit value corresponds to the value of that row. 0b11111111 turns on all the LEDs in that row/column. 0b00011000 turns on only the middle 2 leds in the that column.
  • Understanding shift registers is sorta required for cascading multiple matrices, but it turns out that they aren’t all that bad.
  • Cascading multiple led matrices is mention in the datasheet as the “No Op” feature. They mention sending a dummy byte to address 0x00 with data 0x00 to give first Max7219 something to eat and then sending the 0x03 address with 0b11111111 to turn all the leds on the third row of the second matrix.
  • Max7219 shift registers are setup so that that the first 7219 snags the first 2 bytes and passes the rest on. If your intent is to turn on rows 1 and 8 of the first matrix….
    address 0x01 data 0b11111111
    address 0x08 data 0b111111111

Sending the above psudeo code would turn on rows 1 and 8 of the first matrix and turn on row 8 of the second matrix. We need to use those No Op zeros to get what we want.

  • The trick is when to latch. Google “digital latching”. The latching is done when we set the ~SS HIGH after loading up our data.

We can’t send the SPI data to the Max7219 unless the ~SS goes low.
SPI_PORT &= ~(1<<SPI_CS); // ~SS goes LOW

Now ew are talking.

We do our sending

SPDR = address;  // give the SPI data register the address of 7219

while (!(SPSR & (1<<SPIF) ));  // wait for the data to be sent

SPDR = dataout;  // load up SPI data register with dataout

while (!(SPSR & (1<<SPIF)));    // wait for data to be sent.

Now we need to say “goodbye” to end our conversation.

SPI_PORT |= (1<<SPI_CS); // ~SS goes HIGH


In the above code, the 8-bit address is sent and then the 8-bit dataout is sent. We do this all in one latching. If we were working 8 matrices, we’d need to set the ~SS low, send the address and dataout 8 times (one for each matrix), and then set the ~SS high to end the conversation.

You’ll see that there is no direct way to communicate with the third Max7219, but we have tricks. We know the first 7219 is going to steal the first 2 bytes. The second 7219 is going to steal the next 2 bytes.

Let’s create an array that’ll handle this.

uint8_t dumb_array[6] ;
dumb_array[0] = 0x00;
dumb_array[1] = 0;
dumb_array[2] = 0x00;
dumb_array[3] = 0;
dumb_array[4] = 0x03;
dumb_array[5] = 0b11111111;

The first Max7219 eats up dumb_array[0] = 0x00 and dumb_array[1] . The array is now only 4 entries long. I looks like this:
dumb_array[0] = 0x00;
dumb_array[1] = 0;
dumb_array[2] = 0x03;
dumb_array[3] = 0b11111111;

This is sent to the seond Max7219. It chews up the first 2 bytes. The array turns into this:
dumb_array[0] = 0x03;
dumb_array[1] = 0b11111111;

This is sent to the 3rd matrix. The matrix likes this. It turns on all the LEDs in the 3rd matrix.

Leave a Reply

Your email address will not be published. Required fields are marked *