Easy Matrix MAX7219
 
Notifications
Clear all

Easy Matrix MAX7219

2 Posts
1 Users
0 Reactions
3,425 Views
Posts: 137
Topic starter
(@cpwtech)
Estimable Member
Joined: 12 years ago
[#37383]

I am working on a easy to build Matrix by cascading MAX7219's and an Arduino. I have a 32X8 Matrix running right now with Generic Serial interface of Vixen 2.1.1.

Here's a little Video of it running some Snowflakes.

And here it is doing some Text all coming from Vixen.


1 Reply
Posts: 137
Topic starter
(@cpwtech)
Estimable Member
Joined: 12 years ago

Ok folks I know it has been awhile since I started this thread, but I have made some progress. I have finally got the matrix expanded to 8 MAX719's, for a total of 512 LED's(32X16).

I would like to ask all of you Programming Guru's to please help me find ways to optimize this sketch so I can expand the Matrix even Larger. I want to at least double the size of it, 1024 LED's (32X32).

Thanks in advance,

/* Sketch to control a 32X16 matrix using 8 MAX7219 chips
 *  I took the LEDControl Library and stripped it down to just what I needed.
 *  Please feel free to use/change/delete whatever you see needs to be improved, because Lord knows I am no programmer.
 *  
 *    Permission is hereby granted, free of charge, to any person
 *    obtaining a copy of this software and associated documentation
 *    files (the "Software"), to deal in the Software without
 *    restriction, including without limitation the rights to use,
 *    copy, modify, merge, publish, distribute, sublicense, and/or sell
 *    copies of the Software, and to permit persons to whom the
 *    Software is furnished to do so, subject to the following
 *    conditions:
 * 
 *    This permission notice shall be included in all copies or 
 *    substantial portions of the Software.
 * 
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *    OTHER DEALINGS IN THE SOFTWARE.
 */


#include <SPI.h>
// Change the following to how many MAX72XX chips you are using.
#define maxDevices 8
//Array to hold the data to be sent out to the matrix
byte spidata[32];
//Array to keep track of the status of the matrix
byte status[64];
//Array of incoming data from Vixen
int data[64];
//This keeps track of chip we are talking to.  the one closest to the Arduino is chip 0
int times =0;
//This keeps track of which row we are setting
int ad = 0;


//the opcodes for the MAX7221 and MAX7219
#define OP_NOOP   0
#define OP_DIGIT0 1
#define OP_DIGIT1 2
#define OP_DIGIT2 3
#define OP_DIGIT3 4
#define OP_DIGIT4 5
#define OP_DIGIT5 6
#define OP_DIGIT6 7
#define OP_DIGIT7 8
#define OP_DECODEMODE  9
#define OP_INTENSITY   10
#define OP_SCANLIMIT   11
#define OP_SHUTDOWN    12
#define OP_DISPLAYTEST 15

void setup(){
  //Clearing all data and setting up the array matrix
  int maxbytes=maxDevices<<1;
    for(int i=0;i<maxbytes;i++)
        spidata[i]=(byte)0;
//Setting up the SPI connection
  SPISettings(2000000,MSBFIRST,SPI_MODE0);
  SPI.begin();
  setupdisplays(maxDevices);
  Serial.begin(57600);
}

void setupdisplays(byte numDevices){
  for(int i=0;i<(numDevices<<3);i++) 
        status[i]=0x00;
    for(int i=0;i<maxDevices;i++) {
        spiTransfer(i,OP_DISPLAYTEST,0);
        //scanlimit is set to max on startup
        setScanLimit(i,7);
        //decode is done in source
        spiTransfer(i,OP_DECODEMODE,0);
        clearDisplay(i);
        //we turn on the displays
        shutdown(i,false);
        setIntensity(i,3);
}}

void shutdown(int addr, bool b) {
    if(addr<0 || addr>=maxDevices)
        return;
    if(b)
        spiTransfer(addr, OP_SHUTDOWN,0);
    else
        spiTransfer(addr, OP_SHUTDOWN,1);
}

void setScanLimit(int addr, int limit) {
    if(addr<0 || addr>=maxDevices)
        return;
    if(limit>=0 && limit<8)
        spiTransfer(addr, OP_SCANLIMIT,limit);
}

void setIntensity(int addr, int intensity) {
    if(addr<0 || addr>=maxDevices)
        return;
    if(intensity>=0 && intensity<16)  
        spiTransfer(addr, OP_INTENSITY,intensity);
}

void clearDisplay(int addr) {
    int offset;

    if(addr<0 || addr>=maxDevices)
        return;
    offset=addr<<3;
    for(int i=0;i<8;i++) {
        status[offset+i]=0;
        spiTransfer(addr, i+1,status[offset+i]);
    }
}


void spiTransfer(int addr, volatile byte opcode, volatile byte data) {
    //Create an array with the data to shift out
    int offset=addr<<1;
    int maxbytes=maxDevices<<1;

   //put our device data into the array
    spidata[offset+1]=opcode;
    spidata[offset]=data;
    //enable the line 
    digitalWrite(SS,LOW);
    //Now shift out the data 
    for(int i=maxbytes;i>0;i--)
        SPI.transfer(spidata[i-1]);
    //latch the data onto the display
    digitalWrite(SS,HIGH);
}    

void setRow(int addr, int row, byte value) {
    int offset;
    if(addr<0 || addr>=maxDevices)
        return;
    if(row<0 || row>7)
        return;
    offset=addr<<3;
    status[offset+row]=value;
    spiTransfer(addr, row+1,status[offset+row]);
}


void loop() {
  if (Serial.available() > 62) {
    for (int y = 0; y < 64;y++){
     byte e = Serial.read();
//     byte e= data[i];
     if (y >=0 && y <=7){
        ad = 7-y;
        times = times + 1;
     }
    
      if (y>7 && y<=15){
         times = times + 3;
         ad = 7- (y-8);
     }
     if (y >15 && y <=23){
       times = times + 5;
       ad = 7- (y-16);
     }
     if (y>23 && y<=31){
       times = times + 7;
       ad = 7-(y - 24);
     }
     if (y >31 && y <=39){
      times = times;
      ad = 7-(y -32);
     }
     if (y>39 && y<=47){
        times = times + 2;
        ad = 7-(y -40);
        }
     if (y >47 && y <=55){
        times = times + 4;
        ad = 7-(y -48);
     }
    if (y>55 && y<=64){
        times = times + 6;
        ad = 7-(y -56);
        }
    if (times>7){
        times = 0;}
    
 
     setRow(times,ad,e);
     times = 0;
      }
      byte wastechannel = Serial.read();
      }
     }

Reply
Share: