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 Feb 05, 2014 2:37 pm

neurom4nc3r wrote: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:
Sorry if you have to disconnect 5V from the device for it to work then there is a wiring mistake, which means when disconnected the device is getting power from other GPIO pins. You are counting the pin numbers on MAX7219 or Pi GPIO wrong when wiring or upside down or something.

Do initial tests with a SINGLE LED not the display, check that when in test mode you can get a single LED to light in each segment (anode of LED) for each digit that is active (cathode of LED).

I ran the following code

Code: Select all

/*
 max7219test.c
 
 Raspberry Pi driving the Max7219

 to compile : gcc max7219test.c -o max7219test -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


// 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
//
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
  digitalWrite( CLOCK, 0 );  	// set clock to 0 (and leave in that state on exit)
  }
while( mask != 0 );		// After shifts mask == 0
}

// Take a reg number 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
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
test = digitalRead( CLOCK ); 	// Check LOAD pin actually HIGH as test and delay
}

// 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

// 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
}


// 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;
}
and got the following result on a logic analyser, with NOTHING connected, note due to way LOAD pulse is generated it can be treated as an SPI transaction which is decoded by the scope as bottom trace.
TEK00000.PNG
Scope trace of bus software in action
TEK00000.PNG (4.96 KiB) Viewed 1918 times
Decoding the signals as SPI shows the following values written to a device as a 16 bit value top byte register bottom byte value as

Code: Select all

"Tektronix MSO2024, version v1.25, serial number C000321"
"Bus Definition: SPI"
 Time, MOSI, MISO
-2.135200e-05, 0000 ,  
-3.509000e-06, 0C01 ,  
6.023000e-06, 0F01 ,  
1.567000e-05, 0B03 ,  
2.541800e-05, 0901 ,  
3.492500e-05, 0A01 ,  
4.465300e-05, 0F00 ,  
5.414000e-05, 0106 ,  
Which shows a correct sequence being sent.

Do not rush it do it in simple steps your are trying to test too much at once and not finding the real faults.
Last edited by techpaul on Wed Feb 05, 2014 2:40 pm, edited 2 times in total.
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 Feb 05, 2014 2:38 pm

What I mean by wiring mistake is your circuit diagram looks right, but the actual wiring does NOT match the diagram as you are counting pin numbers in a wrong way.
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 Feb 05, 2014 2:46 pm

Also digit numbering starts from 0 so a 4 digit display is using digit drives 0 to 3 on MAX7219 or MAX7221.

This is common engineering and computing method.

Your circuit diagram needs to change to match.
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 08, 2014 8:14 pm

techpaul wrote:Also digit numbering starts from 0 so a 4 digit display is using digit drives 0 to 3 on MAX7219 or MAX7221.

This is common engineering and computing method.

Your circuit diagram needs to change to match.
Well, let's go:
* Yes, i've changed the wiring again to start counting from dig0 - dig3 (max7219) to dig1 - dig4 (display). Reviewed the both datasheets again, I just can say that's all correctly done. Isn't upside down, or wired in wrong side of display (or max7219).

* I'm tested all with another RPi board, with another power supply (still with same problems described in my last post).

* I've tested all with another breadboard, a better one. Also made a connection with two max7219 using the pin DOUT from one to DIN of another exactly as described in datasheet. (still with same problems described in my last post).

* I measured with a voltmeter the first four DIGITS pins of max7219. They're pushing 5V all the time, even when the software IS RUNNING (or not running). I guess it's abnormal behaviour of Integrated Circuit, because the datasheet says: "Dig0-7: Eight-Digit Drive Lines that sink current from the display common cathode. The MAX7219 pulls the digit outputs to V+ when TURNED OFF."

* I measured with a voltmeter all the SEGMENTS pins of max7219. They're pushing 0V all the time. even when the software IS RUNNING (or not running)! I guess it's another abnormal behaviour of IC, because the datasheet says: "Seven Segment Drives and Decimal Point Drive that source current to the display. On the MAX7219, when a segment driver is TURNED OFF it is pulled to GND."
Do initial tests with a SINGLE LED not the display, check that when in test mode you can get a single LED to light in each segment (anode of LED) for each digit that is active (cathode of LED).
I put a single led in positions that you adviced-me. The led lights on always! Even when i try to send a number 6 to the dig0 (display dig1) through software, the led lighted up in all segments positions, even the belonging to B segment (that should be turned off to draw a readable "6" number.).

When i use the display, the segments just blinks quickly as already said in my last post. The segments never keep turned on! If i put the software in a loop, i can see the 6 drawed in the first digit, but in others, just random segments. Sending another number to the first digit, sometimes don't work. The number six or number 4 for example, "work" always. And in another digits, none of them work! Just random blinking segments!

Maybe it's a clock problem? Or software? Or power? Or none of them?

Dude, i'm almost giving up. :(
“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

Sun Feb 09, 2014 12:37 am

neurom4nc3r wrote:
techpaul wrote:Also digit numbering starts from 0 so a 4 digit display is using digit drives 0 to 3 on MAX7219 or MAX7221.

This is common engineering and computing method.

Your circuit diagram needs to change to match.
Well, let's go:
* Yes, i've changed the wiring again to start counting from dig0 - dig3 (max7219) to dig1 - dig4 (display). Reviewed the both datasheets again, I just can say that's all correctly done. Isn't upside down, or wired in wrong side of display (or max7219).
Check again the MAX7219 datasheet Page 3 look at Logic High Input Voltage which is minimum of 3.5V
* I'm tested all with another RPi board, with another power supply (still with same problems described in my last post).
Do initial tests with a SINGLE LED not the display, check that when in test mode you can get a single LED to light in each segment (anode of LED) for each digit that is active (cathode of LED).
Or you have the LED back to front for that test if it always lights and supposedly turned off
Maybe it's a clock problem? Or software? Or power? Or none of them?

Dude, i'm almost giving up. :(
Maybe it is input levels not being high enough and you need a level translater to boost the signals to 5V levels, it sounds more like it is getting garbage commands as it does not always see at least 3.5V at its inputs.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Return to “General discussion”