MAX7219 LED Matrix

j

Hi, first of all please edit your post above and put code tags around your sketch. It should look like...

...this

Second, ask a question. What is wrong and what help do you need?

Third, please answer this question from me: why are you using a sketch written for 74hc595 shift resisters with a max7219 chip?

Paul

RTFM.
That is the data sheet and the tutorials:-
http://playground.arduino.cc/Main/LEDMatrix

Then learn that it does not need 8 bits but 16 bits to talk to it.
Next learn how to use arrays:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Finally learn how to ask a question.

Ten months later any you still haven't got that LED matrix working properly.

And you still haven't read the "How To" for posting code. Presumably, the two problems are intimately related.

OK, I had a quick look at that nonsense code. I couldn't get past the use of "TimerOne.h" and interrupts - just a total disaster.

OK, just try my test code here:

/* Loop scanner demonstration
Code for max 7219 from maxim, reduced & optimised for using multiple 7219 cascaded.
______________________________________

General notes: 

- if using only one max7219, then use maxSingle function to control
the module --- maxSingle(register (1-8), column (0-255))

- if using more then one max7219, and all should work the same, use maxAll
function --- maxAll(register (1-8), collum (0-255))

- if using more than one max7219 and want to change something on one module only,
then use maxOne function 
--- maxOne(module you want to control [1=first], register [1-8], column [0-255])

 During initiation, be sure to send every part to every max7219 and then upload it.
For example, if you have five max7219's, you have to send the scanLimit 5 times
before you load it, otherwise not every max7219 will get the data. the (fixed)
variable maxInUse keeps track of this, just tell it how many max7219 you are using.
*/

int dataIn = 11;            // "DIN" on module
int load = 8;              // "CS" on module
int clock = 12;             // "CLK" on module
const int ledPin =  13;    // LED pin number

int maxInUse = 1;          // set how many MAX7219's used
int ledState = LOW;        // initialise the LED

int e = 0;                 // just a varialble

// define max7219 registers
byte max7219_reg_noop        = 0x00;
byte max7219_reg_digit0      = 0x01;
byte max7219_reg_digit1      = 0x02;
byte max7219_reg_digit2      = 0x03;
byte max7219_reg_digit3      = 0x04;
byte max7219_reg_digit4      = 0x05;
byte max7219_reg_digit5      = 0x06;
byte max7219_reg_digit6      = 0x07;
byte max7219_reg_digit7      = 0x08;
byte max7219_reg_decodeMode  = 0x09;
byte max7219_reg_intensity   = 0x0a;
byte max7219_reg_scanLimit   = 0x0b;
byte max7219_reg_shutdown    = 0x0c;
byte max7219_reg_displayTest = 0x0f;


void putByte(byte data) {
  byte i = 8;
  byte mask;
  while(i > 0) {
    mask = 0x01 << (i - 1);      // get bitmask
    digitalWrite( clock, LOW);   // tick
    if (data & mask) {           // choose bit
      digitalWrite(dataIn, HIGH);// send 1
    } else {
      digitalWrite(dataIn, LOW); // send 0
    }
    digitalWrite(clock, HIGH);   // tock
    --i;                         // move to lesser bit
  }
}

// maxSingle is the "easy" function to use for a single max7219
void maxSingle( byte reg, byte col) {    
  digitalWrite(load, LOW);  // begin     
  putByte(reg);             // specify register
  putByte(col);             //((data & 0x01) * 256) + data >> 1); // put data   
  digitalWrite(load,HIGH); 
}

// initialize all MAX7219's
void maxAll( byte reg, byte col) {
  int c = 0;
  digitalWrite(load, LOW);
  for ( c =1; c<= maxInUse; c++) {
  putByte(reg);             // specify register
  putByte(col);             //((data & 0x01) * 256) + data >> 1); // put data
    }
  digitalWrite(load,HIGH);
}

// for adressing different MAX7219's while cascaded
void maxOne(byte maxNr, byte reg, byte col) {    
  int c = 0;
  digitalWrite(load, LOW);  // begin     
  for ( c = maxInUse; c > maxNr; c--) {
    putByte(0);             // no operation
    putByte(0);             // no operation
  }

  putByte(reg);             // specify register
  putByte(col);             //((data & 0x01) * 256) + data >> 1); // put data 

  for ( c = maxNr-1; c >= 1; c--) {
    putByte(0);             // no operation
    putByte(0);             // no operation
  }
  digitalWrite(load,HIGH); 
}

void putCol( byte colno, byte coldat) {
// Interprets colno as (zero ref) index in combined array
    byte t;
    t = colno >> 3;
    byte u;
    u = colno & 0x07;
    maxOne(t+1, u+1, coldat);
}


void dispon() {
 maxAll(max7219_reg_shutdown, 0x01);               // Display on
}  

void dispoff() {
 maxAll(max7219_reg_shutdown, 0x00);              // Display off
}  

byte irow = 0;          // Row index
byte icol = 0;          // Column index
byte pattern;           // bit mask
byte lcol;              // left border
byte rcol;              // right border
byte trow;              // top row marker
byte brow;              // bottom row marker

int s_vert;             // Vertical switch
int s_horz;             // Horizontal switch

void worker () {

  if (pattern == 0) pattern = trow;            // pattern must be set
  if (s_vert != 0) {
    if (s_vert == -1) {                     // moving upward
      pattern = pattern >> 1;
      if (pattern == trow) {                   // hit the top
        s_vert = 0; s_horz = 1;
      }
    } else {
      pattern = pattern << 1;               // moving downward
      if (pattern == brow) {                // hit the bottom
        s_vert = 0; s_horz = -1;
      }
    }
    putCol(icol,pattern);              // Show the column.
    return;
  }

  if (s_horz != 0) {
    putCol(icol,0);                    // blank previous column.
    if (s_horz == -1) {                     // moving left
      icol--;
      if (icol == lcol) {                      // hit the side
        s_horz = 0; s_vert = -1;
      }
    } else {
      icol++;                               // moving right
      if (icol == rcol) {                      // hit the side
        s_horz = 0; s_vert = 1;
      }
    }
    putCol(icol,pattern);              // Show the column.
  }
 }
  
// the follow variable is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 60;           // interval at which to step (milliseconds)
long previousMillis = 0;      // will store last time LED was updated


void setup () {

  pinMode(dataIn, OUTPUT);
  pinMode(clock,  OUTPUT);
  pinMode(load,   OUTPUT);
  pinMode(ledPin, OUTPUT);      

  //Serial begin(9600);
  digitalWrite(13, HIGH);  

//initiation of the max 7219
  maxAll(max7219_reg_displayTest, 0x00); // no display test
  maxAll(max7219_reg_scanLimit, 0x07);   // all columns in use   
  maxAll(max7219_reg_decodeMode, 0x00);  // using a LED matrix (not digits)
  maxAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
  for (e=1; e<=8; e++) {                 // empty registers, turn all LEDs off 
    maxAll(e,0);
  }
  maxAll(max7219_reg_intensity, 0x08 & 0x0f);  // middle argument is intensity value
                                               // range: 0x00 to 0x0f
                                                 
  pattern = 0;
  s_vert = 0;
  s_horz = 1;

// define edges of loop
  lcol = 1;              // left border
  rcol = 6;              // right border
  trow = 0x02;           // top row marker
  brow = 0x40;           // bottom row marker      
      
}  

void loop () {

  unsigned long currentMillis = millis();
 
// Active waiting for next event
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) { ledState = HIGH;  } else { ledState = LOW; }
    // Timed process:
    
    worker();

    // set the LED according to ledState:
    digitalWrite(ledPin, ledState);
    }

}

Mind you, I just noticed - this code only works for a MAX7219 module - your code for what it is worth says it is for a 74HC595, the two are entirely unrelated.

So now - which is it?

This code worked fine with the MAX7219 I have, so thanks. I struggle with C code quite a lot.

And, just a wee note to say the 'How To' for posting questions, while it is helpful, isn't exactly short. Maybe that's why people are avoiding it.

I did note however, that it does encourage forum users to be polite. Is it therefore necessary for some users (eg. Grumpy_Mike) to begin their posts with stuff like 'RTFM'?

Just saying...

They want you to format your code by clicking the code button (see attached image) like so -

  #include <TimerOne.h>
 
int latchPin = 8; //Pin connected to Pin 12 of 74HC595 (Latch)    voilet wire
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)   blue wire
int dataPin = 11; //Pin connected to Pin 14 of 74HC595 (Data)     grey wire
 
byte led[8];  // 8 element unsigned integer array to store the sprite

void setup() {
        pinMode(latchPin, OUTPUT);  // set the 3 digital pins to outputs
        pinMode(clockPin, OUTPUT);
        pinMode(dataPin, OUTPUT);
        led[0] = B11111;  // enter the binary representation of the image
        led[1] = B10101;  // into the array
        led[2] = B10101;
        led[3] = B10101;
        led[4] = B10101;
        led[5] = B10101;
        led[6] = B10101;
        led[7] = B11111;
        // set a timer of length 10000 microseconds (1/100th of a second)
        Timer1.initialize(10000); 
        // attach the screenUpdate function to the interrupt timer
        Timer1.attachInterrupt(screenUpdate); 
}
 
void loop() {
        for (int i=0; i<7; i++) {
                led= ~led; // invert each row of the binary image
        }
        delay(500);
}
 
void screenUpdate() { // function to display image
        byte row = B10000000; // row 1
        for (byte k = 0; k < 8; k++) {
                digitalWrite(latchPin, LOW); // open latch ready to receive data
        shiftIt(~led[k] ); // shift out the LED array (inverted)
        shiftIt(row ); // shift out row binary number 
 
        // Close the latch, sending the data in the registers out to the matrix
        digitalWrite(latchPin, HIGH);     
        row = row << 1; // bitshift left
        }
}
 
void shiftIt(byte dataOut) {    // Shift out 8 bits LSB first, on rising edge of clock
 
        boolean pinState; 
        digitalWrite(dataPin, LOW); //clear shift register read for sending data
 
        for (int i=0; i<8; i++)  {    // for each bit in dataOut send out a bit
                digitalWrite(clockPin, LOW); //set clockPin to LOW prior to sending bit
 
                // if the value of DataOut and (logical AND) a bitmask
                // are true, set pinState to 1 (HIGH)
               
      if ( dataOut & (1<<i) )
                      {
                        pinState = HIGH;
                      }

                else           
            {
                        pinState = LOW;
                      }
                //sets dataPin to HIGH or LOW depending on pinState
                digitalWrite(dataPin, pinState); 
                digitalWrite(clockPin, HIGH); //send bit out on rising edge of clock 
                digitalWrite(dataPin, LOW);
        }
digitalWrite(clockPin, LOW); //stop shifting
}

Rubicon71:
This code worked fine with the MAX7219 I have, so thanks. I struggle with C code quite a lot.

Are you referring to the code I posted? I do believe it to be bulletproof - no use of libraries.

Rubicon71:
Is it therefore necessary for some users (eg. Grumpy_Mike) to begin their posts with stuff like 'RTFM'?

You don't suppose there is a hint in his choice of pen name?

Paul__B:
You don't suppose there is a hint in his choice of pen name?

Whatever you do, don't forget the resistors :sunglasses: