Get Guides,Tips,Tricks And Information Related To Raspberry Pi

Monday, December 31, 2012

Interfacing MAX7219 with Raspberry Pi

The MAX7219 is extraordinary IC from Maxim which allows us to control many LEDs utilizing only a couple Raspberry Pi pin-outs. It removes all the hassles related with multiplexing, latching, refreshing common with 7 segments. Just by sending commands  via its SPI interfaceto the MAX7219 can control 64 LEDs or eight 7 segment displays.

Even though Rpi supports hardware SPI I couldn't get it to work with MAX7219. So I abandoned the SPI and connected 3 normal digital GPIO pins to the MAX7219 directly.I connected pin 1 (data), 12 (load) and 13 (clock) to the RPi GPIO pins 17, 23 and 22. Pins 4 and 9 to RPi Ground. Pin 19 to RPi’s 5V along with pin 18 via a resistor of your choice depending on your LED.



















Now it was only a matter of sending commands to the MAX7219 via the data in, clock and load line.

For instance, to display the number 4 on digit 1 we would send the command 0×104 (0×01 : use digit 1, 0×04 : display the number 4). Which might look something like this :

Set LOAD to 1.
Send 16 bits command and data (0×104 = 0000000100000100), and for each binary digit we : set CLOCK to 0; send the binary digit, 0 or 1; set CLOCK to 1.
To finish, set LOAD to 0 then set LOAD to 1.

At this point it might be worth mentioning that the MAX7219 can work in two modes, BCD decode on or BCD off. With BCD on we can just send the number we want to appear on the 7 segment display to the MAX7219 and it will display it, as shown above. As well as digits 0 to 9, 10 to 15 correspond to -, E, H, L, P and ‘ ‘ / blank.

With BCD set to off we can control each segment directly. We’d use this mode if we were controlling a bunch of LEDs

bit  -  segment
0  -  g
1  -  f
2  -  e
3  -  d
4  -  c
5  -  b
6  -  a
7  -  dp

So binary 1110111, 0×77, would display a letter A on a 7 segment display.

Below is the code for MAX7219, using Gordon’s excellent wiringPi library to control the GPIO pins :-

/*
 max7219.c
 to compile : gcc max7219.c -o max7219 -lwiringPi
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// define our pins :
#define DATA        0 // GPIO 17 (WiringPi pin num 0)  header pin 11
#define CLOCK       3 // GPIO 22 (WiringPi pin num 3)   header pin 15
#define LOAD        4 // GPIO 23 (WiringPi pin num 4)   header pin 16
// The Max7219 Registers :
#define DECODE_MODE   0x09                       
#define INTENSITY     0x0a                        
#define SCAN_LIMIT    0x0b                        
#define SHUTDOWN      0x0c                        
#define DISPLAY_TEST  0x0f                         
static void Send16bits (unsigned short output)
{
  unsigned char i;
  for(i=16;i>0;i--) 
  {
    unsigned short mask=1<<(i-1); // calculate bitmask
    digitalWrite(CLOCK,0);  // set clock to 0
    // Send one bit on the data pin
    if (output & mask)   
      digitalWrite(DATA,1);          
    else                              
      digitalWrite(DATA,0);        
    digitalWrite(CLOCK,1);  // set clock to 1 	 
  }
}
// Take a reg numer and data and send to the max7219
static void MAX7219Send(unsigned char reg_number,unsigned char dataout)
{
  digitalWrite(LOAD,1);  // set LOAD 1 to start
  Send16bits((reg_number << 8) + dataout);   // send 16 bits ( reg number + dataout )
  digitalWrite(LOAD,0);  // LOAD 0 to latch
  digitalWrite(LOAD,1);  // set LOAD 1 to finish
}
int main(void)
{
  printf("\n\nRaspberry Pi Max7219 Test using WiringPi\n\n");
  if(wiringPiSetup()==-1) exit(1) ;
  //We need 3 output pins to control the Max7219: Data, Clock and Load
  pinMode(DATA,OUTPUT);  
  pinMode(CLOCK,OUTPUT);
  pinMode(LOAD,OUTPUT);  
  MAX7219Send(SCAN_LIMIT,7);     // set up to scan all eight digits
/* 
 BCD decode mode off : data bits correspond to the segments (A-G and DP) of the seven segment display.
 BCD mode on :  0 to 15 =  0 to 9, -, E, H, L, P, and ' '	
*/
  MAX7219Send(DECODE_MODE,1);   // Set BCD decode mode on
  MAX7219Send(DISPLAY_TEST,0);  // Disable test mode
  MAX7219Send(INTENSITY,1);     // set brightness 0 to 15	
  MAX7219Send(SHUTDOWN,1);      // come out of shutdown mode
  MAX7219Send(1,6); 		 // displays the number 6 on digit 1
  return 0;
}

No comments:

Post a Comment