How to use MAX7221 to drive 7-segment LED set using a micro

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to use MAX7221 to drive 7-segment LED set using a micro

Post by Shane » Fri Jun 24, 2011 7:22 am

MAX7221 LED driver IC from Maxim is for driving either 64 individual Led's, or up to 8 digits of 7-segment displays. The driver implement a SPI compatible slave interface that can be controlled from the Arduino using only 3 of the digital output pins. An extensive datasheet for the IC is available from the Maxim homepage. Both MAX7219 and MAX7221 are very similar, we will use MAX7221 for the tutorial.
1.jpg
1.jpg (26.47 KiB) Viewed 4372 times
2.jpg
2.jpg (13.87 KiB) Viewed 4372 times
3.jpg
3.jpg (50.39 KiB) Viewed 4372 times

Code: Select all

//USE SPI TO CONTROL THE MAX7221 LED DRIVER
 //THIS CODE EXAMPLES IMPLEMENTS A SIMPLE 4-DIGIT COUNTER

#define DATAOUT 11//MOSI
 #define DATAIN 12//MISO - not used, but part of builtin SPI
 #define SPICLOCK 13//sck
 #define SLAVESELECT 10//ss

byte digit=1;
 byte counterNumber1=0;
 byte counterNumber10 = 0;
 byte counterNumber100 = 0;
 byte counterNumber1000 = 0;

///////////////////////////////////////////////////////////////////
 //spi transfer function (from ATmega168 datasheet)
 char spi_transfer(volatile char data)
 {
 SPDR = data; // Start the transmission
 while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
 {
 };
 return SPDR; // return the received byte
 }

///////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////
 byte write_7seg(int digAddress, int displayValue) //dig pot data transfer function
 {
 digitalWrite(SLAVESELECT,LOW); //digital pot chip select is active low
 //2 byte data transfer to digital pot
 spi_transfer(digAddress);
 spi_transfer(displayValue);
 digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
 }

///////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////
 void setup()
 {
 byte i;
 byte clr;
 pinMode(DATAOUT, OUTPUT);
 pinMode(DATAIN, INPUT);
 pinMode(SPICLOCK,OUTPUT);
 pinMode(SLAVESELECT,OUTPUT);
 digitalWrite(SLAVESELECT,HIGH); //disable device

 ///////////////////////////////////////////////////////////////////
 // SPCR = 01010000
 //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
 //sample on leading edge of clk,system clock/4 (fastest)
 SPCR = (1<<SPE)|(1<<MSTR);
 clr=SPSR;
 clr=SPDR;
 delay(10);
 ///////////////////////////////////////////////////////////////////

//clear 7221 and format to receive data
 write_7seg(0x0C,1);
 write_7seg(0x09,0xFF);
 write_7seg(0x0A,0x0F);
 write_7seg(0x0B,0x04);
 }

///////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////
 void loop()
 {
 do{
 write_7seg(1, counterNumber1000);
 counterNumber1000++;
 do{
 write_7seg(2, counterNumber100);
 counterNumber100++; 
 do{
 write_7seg(3, counterNumber10);
 counterNumber10++;
 do{
 write_7seg(4, counterNumber1);
 delay (100);
 counterNumber1++;
 }while (counterNumber1<10);
 counterNumber1 = 0;
 }while (counterNumber10<10);
 counterNumber10 = 0;
 }while (counterNumber100<10);
 counterNumber100 = 0;
 }while (counterNumber1000<10);
 counterNumber1000 = 0;
 }
4.jpg
4.jpg (213.6 KiB) Viewed 4372 times
5.jpg
5.jpg (210.08 KiB) Viewed 4372 times
Matrix LED wiring diagram
6.jpg
6.jpg (92.81 KiB) Viewed 4372 times
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”