I'm working on a project that uses lots of 7-segment LED displays, and I'm using the MAX7219 IC for its wonderful multiplexing and constant current features. Since the displays I found were common anode (not the common cathode the MAX7219 datasheet specifies), I had to modify the LedControl library a bit to accommodate them. I will share my methods and code here to get feedback before I upload any updated libraries.
The 7219 can be easily used with common anode displays, as long as things are wired up "backwards." In other words, since the pins for DIG-0 through DIG-7 are current sinks and the pins for SEG-DP through SEG-G are current sources, they can be swapped to provide appropriate power to a common anode display. Wiring should be:
Wiring for Common anode display
MAX7219 | Display
DIG-0 | SEG-DP
DIG-1 | SEG-A
DIG-2 | SEG-B
... | ...
DIG-7 | SEG-G
SEG-DP | Anode of Digit 0
SEG-A | Anode of Digit 1
... | ...
SEG-G | Anode of Digit 7
Now since the 7219 scans digit-by-digit, and we've swapped digits with segments, it's now going to scan segment-by-segment on the common anode displays, unless we modify the data we're sending to it. The data for the entire display is represented by an 8-byte array, with each byte corresponding to one digit and each bit corresponding to one segment. To get it to work to our wiring scheme, we have to flip it: we transpose the bit matrix.
Here are the two matrices for displaying "01234567" with no decimal points on an 8-digit display (in NO DECODE mode, which LedControl uses). Each row of the matrix is stored as a byte in an array (in LedControl, it's in the status[] array).
Common cathode (standard) Common anode (transposed)
DP A B C D E F G DP A B C D E F G
+---------------- +----------------
0 | 0 1 1 1 1 1 1 0 0 | 0 0 0 0 0 0 0 0
1 | 0 0 1 1 0 0 0 0 1 | 1 0 1 1 0 1 1 1
2 | 0 1 1 0 1 1 0 1 2 | 1 1 1 1 1 0 0 1
3 | 0 1 1 1 1 0 0 1 3 | 1 1 0 1 1 1 1 1
4 | 0 0 1 1 0 0 1 1 4 | 1 0 1 1 0 1 1 0
5 | 0 1 0 1 1 0 1 1 5 | 1 0 1 0 0 0 1 0
6 | 0 1 0 1 1 1 1 1 6 | 1 0 0 0 1 1 1 0
7 | 0 1 1 1 0 0 0 0 7 | 0 0 1 1 1 1 1 0
My patch for LedControl is attached. It's completely backwards compatible with existing code. To use it with common anode displays, you only have to add a boolean option to the object creation. The transposition is handled completely by the library.
#include "LedControl.h"
LedControl lc=LedControl(12,11,10,1,true); // lc is our object
// pin 12 is connected to the MAX7219 pin 1
// pin 11 is connected to the CLK pin 13
// pin 10 is connected to LOAD pin 12
// 1 as we are only using 1 MAX7219
// true since this is a common anode display setup
void setup() {
lc.shutdown(0,false); // turn off power saving, enables display
lc.setIntensity(0,5); // sets brightness (0~15 possible values)
lc.clearDisplay(0); // clear screen
}
void loop() {
// display 0-7, cascading on the 8 displays
for(int i=0; i<8; i++) {
lc.setDigit(0,i,i,false);
delay(250);
lc.clearDisplay(0);
}
}
Comments, questions, and critiques are welcome.
common-anode.patch (7.12 KB)