User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

MAX7219 and 7 segments 4-digit Display

Thu Jan 23, 2014 11:12 am

Hi there,

i'm trying to put a 7 segments 4 digit display with a max7219 controller but i cannot find a way to wire correctly the max7219 to the display. The raspberry wiring to the max7219 is already done.
Does anyone know how to put them working?

thanks a lot
“The best way to predict your future is to create it”

User avatar
Douglas6
Posts: 4473
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: MAX7219 and 7 segments 4-digit Display

Thu Jan 23, 2014 5:16 pm

Yes, I've done this, and I even have a pretty Fritzing diagram of the wiring. Unfortumately I won't be able to get to it until this weekend, but I'll post it here then. Ping me again if I forget.

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Thu Jan 23, 2014 6:18 pm

Douglas6 wrote:Yes, I've done this, and I even have a pretty Fritzing diagram of the wiring. Unfortumately I won't be able to get to it until this weekend, but I'll post it here then. Ping me again if I forget.
Man, certainly you'll help a lot doing this! I'm desperate =/
“The best way to predict your future is to create it”

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Fri Jan 24, 2014 6:03 pm

I've made the following wiring (save the image or drag and drop to another tab to view in large size):
Image

Where do you read "arduino's pins, you should read:

Arduino Pin 12 = Raspberry Pin 3 (gpio 02)
Arduino Pin 10 = Raspberry Pin 8 (gpio 15)
Arduino Pin 11 = Raspberry Pin 23 (gpio 11)

In the max7219 i've also grounded the pin 4 and 9 and put 5V through a 10k resistor in the pin 18. 5V directly to pin 19.


And here's the raspberry diagram that i'm basing-me on:
Image



I also compiled the code below, but NOTHING happens to display when i use it:

Code: Select all

/*

 max7219.c
 
 Raspberry Pi driving the Max7219

 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        8       // GPIO 2 (WiringPi pin num 8)     header pin 3
#define CLOCK       14  // GPIO 11 (WiringPi pin num 14)  header pin 23
#define LOAD        15   // GPIO 14 (WiringPi pin num 15)   header pin 8


// 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	/ turn on the digits
  	
  MAX7219Send(1,6); 		 // displays the number 6 on digit 1

  return 0;
}
I don't know if the problem is in the wiring or in the code. (I guess it's in the code). :(
Suggestions?
Last edited by neurom4nc3r on Wed Jan 29, 2014 1:15 am, edited 2 times in total.
“The best way to predict your future is to create it”

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Fri Jan 24, 2014 6:36 pm

The diagram show no power or brightness setting resistor for a start
  1. Are you using a MAX7219 or MAX7221 the interfaces are different (slightly)
  2. What 7 segment display are you using? The device is meant to drive a Common CATHODE display
  3. You appear to have missed off the Rset resistor between V+ and Iset (pins 19 and 18 respectively, see datasheet page 8) start with a 10k resistor.
  4. What power is connected to pin 19?
    Are all the GNDs (0V) connected between devices
  5. Beware that with all segments on you may draw too much power from the Pi as 8 segments draws 320mA, Pi spec is around 200mA available on 5V from Pi
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Fri Jan 24, 2014 6:51 pm

techpaul wrote:The diagram show no power or brightness setting resistor for a start
  1. Are you using a MAX7219 or MAX7221 the interfaces are different (slightly)
  2. What 7 segment display are you using? The device is meant to drive a Common CATHODE display
  3. You appear to have missed off the Rset resistor between V+ and Iset (pins 19 and 18 respectively, see datasheet page 8) start with a 10k resistor.
  4. What power is connected to pin 19?
    Are all the GNDs (0V) connected between devices
  5. Beware that with all segments on you may draw too much power from the Pi as 8 segments draws 320mA, Pi spec is around 200mA available on 5V from Pi
Thanks for you reply sir,

Answering you questions, i'm using Max7219 and a 7-segments 4-display (Common Cathode):
Image

I've connected 5V to the Max7219's pin 19 (directly), and to the pin 18, 5V passing through a 10K resistor.
The pins 4 and 9 are to the GND! Is that correct?
“The best way to predict your future is to create it”

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Fri Jan 24, 2014 8:11 pm

neurom4nc3r wrote:Thanks for you reply sir,

Answering you questions, i'm using Max7219 and a 7-segments 4-display (Common Cathode):
.....

I've connected 5V to the Max7219's pin 19 (directly), and to the pin 18, 5V passing through a 10K resistor.
The pins 4 and 9 are to the GND! Is that correct?
I assume you have connected the 5V from the Pi, and the GND to the Pi as well. (Some folks forget these wires)

First increase Rset to about 20 to 30k to reduce power consumption for testing. You can always put several 10k resistors in series to achieve this.

Change your code to send ONE command ONLY send Hex 0x0F01 to put the MAX7219 in display test mode, when that works move onto other functions.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Fri Jan 24, 2014 9:19 pm

Hi there again,

I've disconnected almost everything in the protoboard leaving the 7 segments display alone. Connected 5V through 10k + 10k resistors to the pin 18 of max7219. Sent the command to put this IC in the test mode (0x0F).
Unfortunately without success.

I start to think that the max7219 doesn't work. :x

Maybe i need to use RPi GPIO specific pins (e.g: MOSI, SCLK and CE0)? Or "which" pins doesn't matter? I also noted that the IC start counting its digits from "0" and not from "1". But i supposed that this last fact is irrelevant because the "test mode" must turn ALL digits in ON state.
Last edited by neurom4nc3r on Tue Jan 28, 2014 4:27 pm, edited 1 time in total.
“The best way to predict your future is to create it”

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Fri Jan 24, 2014 10:28 pm

neurom4nc3r wrote:Hi there again,

I've disconnected almost everything in the protoboard leaving the 7 segments display alone. Connected 5V through 10k + 10k resistors to the pin 18 of max7219. Sent the command to put the CI in the test mode (0x0F).
Unfortunately without success.
The command is 0x0F01 a 16 bit value.
I start to think that the max7219 doesn't work.
Not so sure about that yet
Maybe i need to use RPi GPIO specific pins (e.g: MOSI, SCLK and CE0)? Or "which" pins doesn't matter? I also noted that the CI start counting its digits from "0" and not from "1". But i supposed that this last fact is irrelevant because the "test mode" must turn ALL digits in ON state.
The MAX7219 is a shift device NOT SPI, whilst the MAX7221 is SPI.

To use those pins in SPI mode means rewriting software to use te SPI device correctly.

Check one thing at a time with a voltmeter (multimeter in voltage mode) in test mode you should see nearly 4V on every segment drive when in test mode

With a single LED and no display attached, connect the LED Cathode to a digit drive and try the LED Anode on every segment, then try a segment drive being fixed and move the LED Cathode along the digit drives. When in test mode the LED should always be ON when everything is working.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Sun Jan 26, 2014 2:32 am

hi techpaul,

sorry but i really don't know how to send the "0x0F01" command to the IC. I just know that 0x0F represents the "test mode" register of max7219, so, I send to this register the 1 (high) value. I'll be glad if you or someone post a example code here.

Nevertheless I've tested the DC voltage of the max7219 output pins (that goes to the led segments and digits), it seems that are no voltage, or something insignificant just below 0.10v (maybe because my voltmeter is a cheap one).

I checked that the RPi is sending voltage to the DIN, CLK, and DATA of IC as expected. The single led i've tested individually, worked fine in parallel with their positions. But, this led doesn't worked when i put it in the same segments/digits lines (that was expected because there's no voltage, as i said early). So, the test you've recommended had no effect.

Also tried to put a 30k resistor in pin 18. (I also tested with the voltmeter the pin 18 and pin 19. they're receiving 5V correctly)

I've swapped both the IC and 7 segments display to another new ones (i bought 2 pieces of each).
Last edited by neurom4nc3r on Thu Jan 30, 2014 12:08 pm, edited 2 times in total.
“The best way to predict your future is to create it”

User avatar
Douglas6
Posts: 4473
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: MAX7219 and 7 segments 4-digit Display

Sun Jan 26, 2014 5:48 pm

As promised, here's the Fritzing diagram that includes wiring up a MAX7219CNG with (2) 4 digit seven segment displays. I'm not sure how helpful this will be, as I connected the MAX7219 to an Arduino Uno to make a power meter with an INA219 (a 'UnoMeter'), so the code is all Arduino, (and the voltages are all 5V), and I'm guessing your problems are mostly code-related. But for what it's worth, you can get a zip of the Fritzing file and Arduino code here.

Also double-check the pinouts of your displays, these are the Adafruit ones I used.

Image

Agent 57
Posts: 5
Joined: Wed Jan 09, 2013 5:39 pm

Re: MAX7219 and 7 segments 4-digit Display

Sun Jan 26, 2014 6:55 pm

Hello,

I'm the guy behind that code you found. I can report its working fine here. My Raspberry Pi has been sitting on my desk displaying the time (as well as doing other things) for about a year now.

To turn test mode on using that code : MAX7219Send(15,1); My clock just lit up!

It's been a year since I created it. To be sure it still works with a newer version wiringpi, I've just downloaded the latest wiringPi and compiled against that and it still works ok.

I'm not sure what your problem is, it could be absolutely anything to be honest. I would suggest taking it back to basics, forget the max7219 for now. Are you able to turn a single LED on and off in C with wiringpi?

Something like... (off the top of my head, it might be wrong)

Code: Select all

#include <wiringPi.h>
#include <stdlib.h>

#define PIN  0 // GPIO 17 (WiringPi pin num 0)  header pin 11

int main (void)
{
  if (wiringPiSetup () == -1) exit (1) ;

  pinMode(PIN, OUTPUT);  
  digitalWrite(PIN, 1);
  sleep(10);
  digitalWrite(PIN, 0);
}

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Tue Jan 28, 2014 4:12 am

Agent 57 wrote:Hello,

I'm the guy behind that code you found. I can report its working fine here. My Raspberry Pi has been sitting on my desk displaying the time (as well as doing other things) for about a year now.

To turn test mode on using that code : MAX7219Send(15,1); My clock just lit up!

It's been a year since I created it. To be sure it still works with a newer version wiringpi, I've just downloaded the latest wiringPi and compiled against that and it still works ok.

I'm not sure what your problem is, it could be absolutely anything to be honest. I would suggest taking it back to basics, forget the max7219 for now. Are you able to turn a single LED on and off in C with wiringpi?

Something like... (off the top of my head, it might be wrong)

Code: Select all

#include <wiringPi.h>
#include <stdlib.h>

#define PIN  0 // GPIO 17 (WiringPi pin num 0)  header pin 11

int main (void)
{
  if (wiringPiSetup () == -1) exit (1) ;

  pinMode(PIN, OUTPUT);  
  digitalWrite(PIN, 1);
  sleep(10);
  digitalWrite(PIN, 0);
}
Thanks a lot for replying the topic and entering in the discussion @Agent 57,

Yes i did what you suggested me. The single LED has lit normally.
(Also, in the previous code, I've tested the GPIO of DIN, CLK and DATA. They turn to 1 (high) without problems.)
Last edited by neurom4nc3r on Tue Jan 28, 2014 4:26 pm, edited 1 time in total.
“The best way to predict your future is to create it”

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Tue Jan 28, 2014 9:03 am

I have gone back over things and your code may have problems as well as your GPIO choice, potential problems I see are
  1. Using header Pin 3 (SDA) for your data may be clashing with pull up and other operations of system at start up of Pi
  2. Using header Pin 8 (TXD) for you clock unless you have disabled ttyAMA0 from getty will cause lots of startup problems as the Pi startup outputs startup messages on this port causing all sorts of random clocking of random data into the device
  3. Using Header pin 23 (SCLK) for your load pin means you could get erronous loads at startup with default SPI configuration on Pi startup
  4. Your initialisation does not put 7219 into a known state
  5. I think your shift routine has danger of only have one mask value and hence only shifting all ones or all zeros to 7219
  6. Your software pulsing write 1 then 0 maybe too fast with the speed of your Pi for the 7219 which is only a 10MHz device
I suspect that during startup before running your programme you are sending garbage to the device either latching it up due to out of spec (too fast) pulses or putting in strange modes as it access register D and E not listed in register list, you could also be sending other invalid data.

Here is some suggested and UNTESTED code to try using header pins 11, 13 and 15

Code: Select all

/*
 max7219.c
 
 Raspberry Pi driving the Max7219

 to compile : gcc max7219.c -o max7219 -lwiringPi

 modified and untested Paul Carpenter, PC Services

  Exit codes
	1 - No wiring Pi library or invalid Initialisation call
	2 - CLOCK pin failed to go high
	3 - LOAD pin failed to go high
*/
 
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// define our pins :
#define DATA        0   // GPIO 0 (WiringPi pin num 0)   header pin 11
#define CLOCK       2   // GPIO 2 (WiringPi pin num 2)   header pin 13
#define LOAD        3   // GPIO 3 (WiringPi pin num 3)   header pin 15

// define max display digits (actual digits -1)
#define DIGITS		0x03

// The Max7219 Registers :
#define NO_OP	      0x00
#define DECODE_MODE   0x09                       
#define INTENSITY     0x0a                        
#define SCAN_LIMIT    0x0b                        
#define SHUTDOWN      0x0c                        
#define DISPLAY_TEST  0x0f                         

// Register Values
#define MODE_B		0x01
#define MODE_7SEG	0x01

// Initialise 7219
//
// To try and ensure device initialised correctly always
// 
// Leaves device in Test mode
//
// Pass in
//	digits - number of digits to display - 1 (1 digit pass in 0)
//	mode   - display mode 7 segment (MODE_7SEG) control
//		   Segment Data	bit 0 = seg A to bit 6 = seg G and bit 7 = DP
//		 or decode special set (MODE_B) as below
//		   Segment Data	bits 0 to 3: 0 to 15 =  0 to 9, -, E, H, L, P, and ' ' (blank)
//			note top 4 bits ignored
//	level  - intensity level (1-15)
//
//  Exits program if no wiringPi installed or invalid parameters or LOAD fails to go high
//
void Init7219( int digits, int mode, int level )
{
// Initial checks
if( wiringPiSetup () == -1 )
  exit( 1 );
if( digits < 0 || digits > 7 )
  {
  printf( "\nError Init7219 called with invalid number of digits of %d (should be 0 to 7)\n", digits );
  exit( 1 );
  }
if( mode != MODE_B || mode != MODE_7SEG || level < 0 || level > 15 )
  {
  printf( "\nError Init7219 called with invalid parameters mode of %d and level of %d\n", mode, level );
  exit( 1 );
  }

//We need 3 output pins to control the Max7219: Data, Clock and Load
pinMode( DATA, OUTPUT );  
pinMode( CLOCK, OUTPUT );
pinMode( LOAD, OUTPUT );  

// Initial states of pins
digitalWrite( LOAD, 1 );  	// set LOAD 1 to start
digitalWrite( CLOCK, 0 );  	// set CLOCK to 0
digitalWrite( DATA, 0 );  	// set DATA to 0

// Quick test check LOAD pin actually HIGH as test
if( digitalRead( LOAD ) != 1 )
  {
  printf( "\nError Max7219 LOAD pin %u failed to go high\n", LOAD );
  exit( 2 );
  }

// Ensure shift refister is all zeros no random clocks occured
// and loaded into device to synchronise shift register
Max7219Send( NO_OP, 0 )

Max7219Send( SHUTDOWN, 1 )	// Ensure in Normal mode of operation

// while waiting for out of shutdown
MAX7219Send(DISPLAY_TEST, 1);  // Enable test mode

// Set modes of display
MAX7219Send(SCAN_LIMIT, digits );	// set up to scan number of digits
MAX7219Send( DECODE_MODE, mode );	// Set BCD decode mode
MAX7219Send( INTENSITY, level );	// set brightness 0 to 15
}


// Shifts out 'n' bits MSB first, number of shifts depends on mask initial value
//
// Will cause exit from programme if CLOCK pin will not go high
// This test also extends clock pulse timing
//
static void Send16bits( unsigned short output )
{
  unsigned int mask, test;

  mask = 0x8000;  		//Initialise mask
  do
    {
    // Send one bit on the data pin
    if (output & mask)   
      digitalWrite(DATA, 1);          
    else                              
      digitalWrite(DATA, 0);  
        
    digitalWrite(CLOCK, 1);  	// set clock to 1
    mask >>= 1;   		// Shift mask and extend clock high time
    // Delay in function to ensure CLOCK pulse is long enough not a glitch
    test = digitalRead( CLOCK ); // Check CLOCK pin actually HIGH as test and delay
    if( test != 1 )
      {
      printf( "\nError Max7219 CLOCK pin %u failed to go high\n", CLOCK );
      exit( 2 );
      }
    digitalWrite(CLOCK, 0);  	// set clock to 0 (and leave in that state on exit)
    }
  while( mask != 0 )		// After shifts mask == 0
}


// Take a reg numer and data and send to the max7219
//
// By setting LOAD low before clocking data and high after last data bit
// This shopuld mean works with MAX7221 as well as MAX7219
//
// Will cause exit from programme if LOAD pin will not go high
static void MAX7219Send (unsigned char reg_number, unsigned char dataout)
{
int test;

  digitalWrite( LOAD, 0 );  	// set LOAD 0 to start data shifting
  Send16bits((reg_number << 8) + dataout);   // send 16 bits ( reg number + dataout )
  digitalWrite( LOAD, 1 );  	// LOAD 1 to latch
  // Delay in function to ensure LOAD pulse is long enough not a glitch
  test = digitalRead( LOAD ); 	// Check LOAD pin actually HIGH as test and delay
  if( test != 1 )
    {
    printf( "\nError Max7219 LOAD pin %u failed to go high\n", LOAD );
    exit( 2 );
    }
}


// Actually body of program here
//
int main (void)
{
   
  printf ("\n\nRaspberry Pi Max7219 Test using WiringPi\n\n");
  Init7219( DIGITS, MODE_B, 1 );   // Initialise interface with digits, decode mode B and Intensity 1
  MAX7219Send( DISPLAY_TEST, 0 );  // Disable test mode
     
  MAX7219Send( 1, 6 );  // displays the number 6 on digit 1

  return 0;
}
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Agent 57
Posts: 5
Joined: Wed Jan 09, 2013 5:39 pm

Re: MAX7219 and 7 segments 4-digit Display

Tue Jan 28, 2014 10:16 am

@neurom4nc3r

Ok, so we know a single LED works. Next thing I'd do is try to control that same single LED via the max7219. Ignore the seven segment display for now.

On the max7219 :

Code: Select all

max7219 pin  1(data)   --> pi gpio 0
max7219 pin  2         --> LED negative
max7219 pin  4 / 9      -->  to pi gnd
max7219 pin 12(load)   --> pi gpio 3
max7219 pin 19         --> pi 5v
max7219 pin 18         --> resistor you used earlier --> 5v
max7219 pin 13 (clock) --> pi gpio 2
max7219 pin 14         --> LED positive

@techpaul Thanks for the improved code. I made a few tweaks to get it to compile and run as expected :

Code: Select all

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// define our pins :

#define DATA        0   // GPIO 0 (WiringPi pin num 0)   header pin 11
#define CLOCK       2   // GPIO 2 (WiringPi pin num 2)   header pin 13
#define LOAD        3   // GPIO 3 (WiringPi pin num 3)   header pin 15

// define max display digits (actual digits -1)
#define DIGITS      0x03

// The Max7219 Registers :
#define NO_OP         0x00
#define DECODE_MODE   0x09                       
#define INTENSITY     0x0a                        
#define SCAN_LIMIT    0x0b                        
#define SHUTDOWN      0x0c                        
#define DISPLAY_TEST  0x0f                         

// Register Values
#define MODE_B      0x01
#define MODE_7SEG   0x01



// Shifts out 'n' bits MSB first, number of shifts depends on mask initial value
//
// Will cause exit from programme if CLOCK pin will not go high
// This test also extends clock pulse timing
//
static void Send16bits( unsigned short output )
{
  unsigned int mask, test;

  mask = 0x8000;        //Initialise mask
  do
    {
    // Send one bit on the data pin
    if (output & mask)   
      digitalWrite(DATA, 1);          
    else                              
      digitalWrite(DATA, 0);  
        
    digitalWrite(CLOCK, 1);     // set clock to 1
    mask >>= 1;         // Shift mask and extend clock high time
    // Delay in function to ensure CLOCK pulse is long enough not a glitch
    test = digitalRead( CLOCK ); // Check CLOCK pin actually HIGH as test and delay
    if( test != 1 )
      {
      printf( "\nError Max7219 CLOCK pin %u failed to go high\n", CLOCK );
      exit( 2 );
      }
    digitalWrite(CLOCK, 0);     // set clock to 0 (and leave in that state on exit)
    }
  while( mask != 0 );      // After shifts mask == 0
}

// Take a reg numer and data and send to the max7219
//
// By setting LOAD low before clocking data and high after last data bit
// This shopuld mean works with MAX7221 as well as MAX7219
//
// Will cause exit from programme if LOAD pin will not go high
static void MAX7219Send (unsigned char reg_number, unsigned char dataout)
{
int test;

  digitalWrite( LOAD, 0 );     // set LOAD 0 to start data shifting
  Send16bits((reg_number << 8) + dataout);   // send 16 bits ( reg number + dataout )
  digitalWrite( LOAD, 1 );     // LOAD 1 to latch
  // Delay in function to ensure LOAD pulse is long enough not a glitch
  test = digitalRead( LOAD );    // Check LOAD pin actually HIGH as test and delay
  if( test != 1 )
    {
    printf( "\nError Max7219 LOAD pin %u failed to go high\n", LOAD );
    exit( 2 );
    }
}


// Initialise 7219
//
// To try and ensure device initialised correctly always
// 
// Leaves device in Test mode
//
// Pass in
//   digits - number of digits to display - 1 (1 digit pass in 0)
//   mode   - display mode 7 segment (MODE_7SEG) control
//         Segment Data   bit 0 = seg A to bit 6 = seg G and bit 7 = DP
//       or decode special set (MODE_B) as below
//         Segment Data   bits 0 to 3: 0 to 15 =  0 to 9, -, E, H, L, P, and ' ' (blank)
//         note top 4 bits ignored
//   level  - intensity level (1-15)
//
//  Exits program if no wiringPi installed or invalid parameters or LOAD fails to go high
//
void Init7219( int digits, int mode, int level )
{
// Initial checks
if( wiringPiSetup () == -1 )
  exit( 1 );
if( digits < 0 || digits > 7 )
  {
  printf( "\nError Init7219 called with invalid number of digits of %d (should be 0 to 7)\n", digits );
  exit( 1 );
  }
if( mode != MODE_B || mode != MODE_7SEG || level < 0 || level > 15 )
  {
  printf( "\nError Init7219 called with invalid parameters mode of %d and level of %d\n", mode, level );
  exit( 1 );
  }

//We need 3 output pins to control the Max7219: Data, Clock and Load
pinMode( DATA, OUTPUT );  
pinMode( CLOCK, OUTPUT );
pinMode( LOAD, OUTPUT );  

// Initial states of pins
digitalWrite( LOAD, 1 );     // set LOAD 1 to start
digitalWrite( CLOCK, 0 );     // set CLOCK to 0
digitalWrite( DATA, 0 );     // set DATA to 0

// Quick test check LOAD pin actually HIGH as test
if( digitalRead( LOAD ) != 1 )
  {
  printf( "\nError Max7219 LOAD pin %u failed to go high\n", LOAD );
  exit( 2 );
  }

// Ensure shift refister is all zeros no random clocks occured
// and loaded into device to synchronise shift register
MAX7219Send( NO_OP, 0 );

MAX7219Send( SCAN_LIMIT, digits );   // set up to scan number of digits
MAX7219Send( DECODE_MODE, mode );   // Set BCD decode mode
MAX7219Send( DISPLAY_TEST, 0);  // disable test mode
MAX7219Send( INTENSITY, level );   // set brightness 0 to 15
MAX7219Send( SHUTDOWN, 1 );   // Ensure in Normal mode of operation
}

int main (void)
{
  printf ("\n\nRaspberry Pi Max7219 Test using WiringPi\n\n");

	Init7219( DIGITS, MODE_B, 1 );
	
  MAX7219Send(DISPLAY_TEST,1); 		 // test all on!

  return 0;
}


techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Tue Jan 28, 2014 10:45 am

Agent 57 wrote:@neurom4nc3r

Ok, so we know a single LED works. Next thing I'd do is try to control that same single LED via the max7219. Ignore the seven segment display for now.
Agreed
On the max7219 :

Code: Select all

max7219 pin 18         --> resistor you used earlier --> 5v
Suggest starts with 20k to 30k for 4 digits drive to be dimmer and not overload anything
@techpaul Thanks for the improved code. I made a few tweaks to get it to compile and run as expected :
Well I did say 'untested', yes Init7219 should have been moved, the rest is more personal preferences
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Agent 57
Posts: 5
Joined: Wed Jan 09, 2013 5:39 pm

Re: MAX7219 and 7 segments 4-digit Display

Tue Jan 28, 2014 1:02 pm

techpaul wrote:[Well I did say 'untested', yes Init7219 should have been moved, the rest is more personal preferences
Er, ok. Apologies if I offended you.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Tue Jan 28, 2014 2:24 pm

Agent 57 wrote:
techpaul wrote:[Well I did say 'untested', yes Init7219 should have been moved, the rest is more personal preferences
Er, ok. Apologies if I offended you.
No offence forgot smiley ;)

Was early morning when I did code
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Tue Jan 28, 2014 4:01 pm

Agent 57 wrote:@neurom4nc3r

Ok, so we know a single LED works. Next thing I'd do is try to control that same single LED via the max7219. Ignore the seven segment display for now.

On the max7219 :

Code: Select all

max7219 pin  1(data)   --> pi gpio 0
max7219 pin  2         --> LED negative
max7219 pin  4 / 9      -->  to pi gnd
max7219 pin 12(load)   --> pi gpio 3
max7219 pin 19         --> pi 5v
max7219 pin 18         --> resistor you used earlier --> 5v
max7219 pin 13 (clock) --> pi gpio 2
max7219 pin 14         --> LED positive

@techpaul Thanks for the improved code. I made a few tweaks to get it to compile and run as expected :

Code: Select all

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// define our pins :

#define DATA        0   // GPIO 0 (WiringPi pin num 0)   header pin 11
#define CLOCK       2   // GPIO 2 (WiringPi pin num 2)   header pin 13
#define LOAD        3   // GPIO 3 (WiringPi pin num 3)   header pin 15

// define max display digits (actual digits -1)
#define DIGITS      0x03

// The Max7219 Registers :
#define NO_OP         0x00
#define DECODE_MODE   0x09                       
#define INTENSITY     0x0a                        
#define SCAN_LIMIT    0x0b                        
#define SHUTDOWN      0x0c                        
#define DISPLAY_TEST  0x0f                         

// Register Values
#define MODE_B      0x01
#define MODE_7SEG   0x01



// Shifts out 'n' bits MSB first, number of shifts depends on mask initial value
//
// Will cause exit from programme if CLOCK pin will not go high
// This test also extends clock pulse timing
//
static void Send16bits( unsigned short output )
{
  unsigned int mask, test;

  mask = 0x8000;        //Initialise mask
  do
    {
    // Send one bit on the data pin
    if (output & mask)   
      digitalWrite(DATA, 1);          
    else                              
      digitalWrite(DATA, 0);  
        
    digitalWrite(CLOCK, 1);     // set clock to 1
    mask >>= 1;         // Shift mask and extend clock high time
    // Delay in function to ensure CLOCK pulse is long enough not a glitch
    test = digitalRead( CLOCK ); // Check CLOCK pin actually HIGH as test and delay
    if( test != 1 )
      {
      printf( "\nError Max7219 CLOCK pin %u failed to go high\n", CLOCK );
      exit( 2 );
      }
    digitalWrite(CLOCK, 0);     // set clock to 0 (and leave in that state on exit)
    }
  while( mask != 0 );      // After shifts mask == 0
}

// Take a reg numer and data and send to the max7219
//
// By setting LOAD low before clocking data and high after last data bit
// This shopuld mean works with MAX7221 as well as MAX7219
//
// Will cause exit from programme if LOAD pin will not go high
static void MAX7219Send (unsigned char reg_number, unsigned char dataout)
{
int test;

  digitalWrite( LOAD, 0 );     // set LOAD 0 to start data shifting
  Send16bits((reg_number << 8) + dataout);   // send 16 bits ( reg number + dataout )
  digitalWrite( LOAD, 1 );     // LOAD 1 to latch
  // Delay in function to ensure LOAD pulse is long enough not a glitch
  test = digitalRead( LOAD );    // Check LOAD pin actually HIGH as test and delay
  if( test != 1 )
    {
    printf( "\nError Max7219 LOAD pin %u failed to go high\n", LOAD );
    exit( 2 );
    }
}


// Initialise 7219
//
// To try and ensure device initialised correctly always
// 
// Leaves device in Test mode
//
// Pass in
//   digits - number of digits to display - 1 (1 digit pass in 0)
//   mode   - display mode 7 segment (MODE_7SEG) control
//         Segment Data   bit 0 = seg A to bit 6 = seg G and bit 7 = DP
//       or decode special set (MODE_B) as below
//         Segment Data   bits 0 to 3: 0 to 15 =  0 to 9, -, E, H, L, P, and ' ' (blank)
//         note top 4 bits ignored
//   level  - intensity level (1-15)
//
//  Exits program if no wiringPi installed or invalid parameters or LOAD fails to go high
//
void Init7219( int digits, int mode, int level )
{
// Initial checks
if( wiringPiSetup () == -1 )
  exit( 1 );
if( digits < 0 || digits > 7 )
  {
  printf( "\nError Init7219 called with invalid number of digits of %d (should be 0 to 7)\n", digits );
  exit( 1 );
  }
if( mode != MODE_B || mode != MODE_7SEG || level < 0 || level > 15 )
  {
  printf( "\nError Init7219 called with invalid parameters mode of %d and level of %d\n", mode, level );
  exit( 1 );
  }

//We need 3 output pins to control the Max7219: Data, Clock and Load
pinMode( DATA, OUTPUT );  
pinMode( CLOCK, OUTPUT );
pinMode( LOAD, OUTPUT );  

// Initial states of pins
digitalWrite( LOAD, 1 );     // set LOAD 1 to start
digitalWrite( CLOCK, 0 );     // set CLOCK to 0
digitalWrite( DATA, 0 );     // set DATA to 0

// Quick test check LOAD pin actually HIGH as test
if( digitalRead( LOAD ) != 1 )
  {
  printf( "\nError Max7219 LOAD pin %u failed to go high\n", LOAD );
  exit( 2 );
  }

// Ensure shift refister is all zeros no random clocks occured
// and loaded into device to synchronise shift register
MAX7219Send( NO_OP, 0 );

MAX7219Send( SCAN_LIMIT, digits );   // set up to scan number of digits
MAX7219Send( DECODE_MODE, mode );   // Set BCD decode mode
MAX7219Send( DISPLAY_TEST, 0);  // disable test mode
MAX7219Send( INTENSITY, level );   // set brightness 0 to 15
MAX7219Send( SHUTDOWN, 1 );   // Ensure in Normal mode of operation
}

int main (void)
{
  printf ("\n\nRaspberry Pi Max7219 Test using WiringPi\n\n");

	Init7219( DIGITS, MODE_B, 1 );
	
  MAX7219Send(DISPLAY_TEST,1); 		 // test all on!

  return 0;
}

@Agent 57

This time, the single LED don't worked.

I'm using the Wiring Pi GPIOs 0 (DATA), 2 (CLOCK) and 3 (LOAD) as @techpaul said. Adapted the GPIO's with the improved code.

One hour ago that i've made a backup of my SD card and formated it. Started mounting everything from begining in the breadboard. Installed raspbian, wiringPi and tried to do everything again without success. Perhaps i've a bad chip? I've bought 2 pieces with the same batch number (+1208), and already swapped'em.
DSC04388-.jpg
wiring breadboard
DSC04388-.jpg (48.27 KiB) Viewed 15270 times
I also ran the new code, sometimes the output is "Error Max7219 CLOCK pin 2 failed to go high". Sometimes, "Error Max7219 LOAD pin 3 failed to go high". Sometimes, there's no errors and the code run successfully but nothing happens to display or leds!

I've used the "gpio readall" command (and the voltmeter in the breadboard), i saw that these positions are ever receiving 3v3 (1/High status) normally. I've tested the single LED in the same breadboard positions of DATA, CLOCK, and LOAD (max7219 pins 1, 13 and 12 respectively) - it lights up - so, these three pins are going to high status, aren't?

When i got the errors, i've printed the value that eventually is received in "test" variable that goes before the if condition, and the result different from 1, instead zero, was: "7.534783e-310st" (when testing CLOCK status) or "7.540724e-310st" (when testing LOAD status) :? But i guess that isn't the reason of the problem.
“The best way to predict your future is to create it”

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Wed Jan 29, 2014 4:21 pm

Try running the software with NOTHING connected to GPIO. The software will run fine that way as it is driving outputs.

Tell us if you get errors that way after running several times and what speed your Pi is set to.

I will try the software on a Pi later and scoping the outputs to see what is happening on GPIO and prove it is correct without a 7219 attached.

I suspect wiring problem or similar like wrong display type or different pin out to what you expect.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Wed Jan 29, 2014 4:27 pm

Further point in all tests unless told otherwise REMOVE all wires to 7 SEG display until the 7219 can be tested as working with a SINGLE LED.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Wed Jan 29, 2014 5:57 pm

Try running the software with NOTHING connected to GPIO. The software will run fine that way as it is driving outputs.

Tell us if you get errors that way after running several times and what speed your Pi is set to.
@techpaul,

My Pi is set to 700mhz speed (default)
I already disconnected all the GPIOs and executed the software again several times. As before, sometimes i get the same previous errors. Sometimes it runs fine:

Code: Select all

[email protected]:/home/pi/testing# ./code-out


Raspberry Pi Max7219 Test using WiringPi

[email protected]:/home/pi/testing# ./code-out


Raspberry Pi Max7219 Test using WiringPi


Error Max7219 LOAD pin 3 failed to go high. Value is 7.540724e-310st
[email protected]:/home/pi/testing# ./code-out


Raspberry Pi Max7219 Test using WiringPi


Error Max7219 CLOCK pin 2 failed to go high. Value is 7.527143e-310st
[email protected]:/home/pi/testing#
All 7-segments-4-digit display wires were disconnected too.
“The best way to predict your future is to create it”

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Fri Jan 31, 2014 8:47 pm

I bought few days ago another max7219 from a different batch. Still waiting it to arrive. But, as you said @techpaul, the problem can be another one
“The best way to predict your future is to create it”

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MAX7219 and 7 segments 4-digit Display

Fri Jan 31, 2014 9:44 pm

Sorry not got back to you been a hectic few days and paying work getting in the way.

Hopefully I can run the code in a few hours if I dont get another interruption or my most awkward customer (the girlfriend) wants something ....
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
neurom4nc3r
Posts: 26
Joined: Fri Dec 06, 2013 2:30 am
Location: Brazil

Re: MAX7219 and 7 segments 4-digit Display

Sat Feb 01, 2014 3:48 pm

Hehe ok, don't worry my friend. My "customer" always do that too.

Very well, updating the topic, i've changed the positions on my breadboard for the 3rd time. The display now turn all digits ON when i run the code (the original one, published by @agent57), BUT, this only happens when i take away the 5V of the 19th pin of max7219. (very strange!!) It work as following details:
  • - When i take away the 5V from the raspberry Pi (physical pin #2) and run the code in test mode: All segments lights on. (where's the power are coming from? I discovered that its probably from the 3v3 of GPIOs, but don't know why the display turn all the digits ON without supplying the pin 19 and the 18 - remember they're sharing the same 5V from pi.)

    - Now, when I put the 5V in the right place again and run the code in test mode: Each digit of display only tries/threaten to light up for some miliseconds and turn off, one at time: I run the code, the 1st digit just blinks quickly. I run the code again, the 2nd digit just blinks quickly ... And so on, successively. Maybe its irrelevant to say that the "digit blink" is more precise when have only a 10k resistor in max7219 pin 18. When have 10k + 10k the blink's behavior is very strange, only 4º digit blinks with precision. The other digits appears to be glitched, with low power or something else that i don't know how to explain! So, i let only 10k resistor.
Now, still with all the wires setup corretly again. The important part:
Changing the software code (the original one) to send any number to an any digit of display obviously without the testing mode set to 1, some random segments only blinks (initially with an almost transparent color, but i increased the intensity value to 10 and solved this). As if the segments outputs of the max7219 weren't set correctly, but i viewed the datasheet and the wiring one hundred times. See the attached image in this post.

When i sent the number 2 to digit 1 with the command MAX7219Send(2,2), for the first time all the 4 digits of display turned ON and keeped enlightened only the G segment. Except of digit 1, that lights only the segment F. But after i rebooted the Pi, the effect became different again: Some segments of display only blinks quickly and once at each time i run the software.
if i change the command to MAX7219Send(2,3), the leds of display shuts down. if i run the command again, only blinks.

I made a loop via shell command linux, and noticed that blinking this way, is formed a digit from 0-9 in the first digit of display. (but only works when i send a number to the digit 1). The other digits doesn't work. I tried for example "MAX7219Send(0x02,3)", doesn't work. I saw only random segments, even blinking. In another words, the situation is very strange!

I did the same tests using your improved code, sometimes random digits blinks. sometimes random segments blink. I ran it several times, the errors i said in early post also persists.

I suppose is not too late to say that I'm using the model 5461AS of 7-segment 4-digit display. To knowledge effect, here's datasheet i've found:
http://pdf.datasheetarchive.com/indexer ... X27120.pdf
Attachments
ligacao.jpg
Actual wiring
ligacao.jpg (56.29 KiB) Viewed 15096 times
Last edited by neurom4nc3r on Sat Feb 08, 2014 8:27 pm, edited 1 time in total.
“The best way to predict your future is to create it”

Return to “General discussion”