---> Fixes to the code listed below May 2011
The following are fixes I entered to make the sketch work correctly for me:

Skip lines 92, 103, 127 -- the load is already LOW, so just set it to HIGH to load the crap

Edit line 137 -- it should read digitalWrite(clock, HIGH);

Great concise code for running one or multiple max7219s! Thanks!!


Start of original article

(for now I am posting just the code and an initial tutorial, but I will return...)

- this is now my c version of the pre tutorial, as soon as I get this thing with the ISet straight and figure out the limitation of cascading the max7219 i will return with the real tutorial.


  1. /* code for max 7219 from maxim,
  2. reduced and optimised for using more than one 7219 in a row,
  3. ______________________________________
  4.  
  5.  Code History:
  6.  --------------
  7.  
  8. The original code was written for the Wiring board by:
  9.  * Nicholas Zambetti and Dave Mellis /Interaction Design Institute Ivrea /Dec 2004
  10.  * http://www.potemkin.org/uploads/Wiring/MAX7219.txt
  11.  
  12. First modification by:
  13.  * Marcus Hannerstig/  K3, malmö högskola /2006
  14.  * http://www.xlab.se | http://arduino.berlios.de
  15.  
  16. This version is by:
  17.  * tomek ness /FH-Potsdam / Feb 2007
  18.  * http://design.fh-potsdam.de/
  19.  
  20.  * @acknowledgements: eric f.
  21.  
  22. -----------------------------------
  23.  
  24. General notes:
  25.  
  26. -if you are only using one max7219, then use the function maxSingle to control
  27.  the little guy ---maxSingle(register (1-8), collum (0-255))
  28.  
  29. -if you are using more than one max7219, and they all should work the same,
  30. then use the function maxAll ---maxAll(register (1-8), collum (0-255))
  31.  
  32. -if you are using more than one max7219 and just want to change something
  33. at one little guy, then use the function maxOne
  34. ---maxOne(Max you want to control (1== the first one), register (1-8),
  35. column (0-255))
  36.  
  37. During initiation, be sure to send every part to every max7219 and then
  38. upload it.
  39. For example, if you have five max7219's, you have to send the scanLimit 5 times
  40. before you load it-- otherwise not every max7219 will get the data. the
  41. function maxInUse keeps track of this, just tell it how many max7219 you are
  42. using.
  43. */
  44.  
  45. int dataIn = 2;
  46. int load = 3;
  47. int clock = 4;
  48.  
  49. int maxInUse = 4;    //change this variable to set how many MAX7219's you'll use
  50.  
  51. int e = 0;           // just a variable
  52.  
  53. // define max7219 registers
  54. byte max7219_reg_noop        = 0x00;
  55. byte max7219_reg_digit0      = 0x01;
  56. byte max7219_reg_digit1      = 0x02;
  57. byte max7219_reg_digit2      = 0x03;
  58. byte max7219_reg_digit3      = 0x04;
  59. byte max7219_reg_digit4      = 0x05;
  60. byte max7219_reg_digit5      = 0x06;
  61. byte max7219_reg_digit6      = 0x07;
  62. byte max7219_reg_digit7      = 0x08;
  63. byte max7219_reg_decodeMode  = 0x09;
  64. byte max7219_reg_intensity   = 0x0a;
  65. byte max7219_reg_scanLimit   = 0x0b;
  66. byte max7219_reg_shutdown    = 0x0c;
  67. byte max7219_reg_displayTest = 0x0f;
  68.  
  69. void putByte(byte data) {
  70.   byte i = 8;
  71.   byte mask;
  72.   while(i > 0) {
  73.     mask = 0x01 << (i - 1);      // get bitmask
  74.     digitalWrite( clock, LOW);   // tick
  75.     if (data & mask){            // choose bit
  76.       digitalWrite(dataIn, HIGH);// send 1
  77.     }else{
  78.       digitalWrite(dataIn, LOW); // send 0
  79.     }
  80.     digitalWrite(clock, HIGH);   // tock
  81.     --i;                         // move to lesser bit
  82.   }
  83. }
  84.  
  85. void maxSingle( byte reg, byte col) {    
  86. //maxSingle is the "easy"  function to use for a single max7219
  87.  
  88.   digitalWrite(load, LOW);       // begin    
  89.   putByte(reg);                  // specify register
  90.   putByte(col);//((data & 0x01) * 256) + data >> 1); // put data  
  91.   digitalWrite(load, LOW);       // and load da stuff
  92.   digitalWrite(load,HIGH);
  93. }
  94.  
  95. void maxAll (byte reg, byte col) {    // initialize  all  MAX7219's in the system
  96.   int c = 0;
  97.   digitalWrite(load, LOW);  // begin    
  98.   for ( c =1; c<= maxInUse; c++) {
  99.   putByte(reg);  // specify register
  100.   putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  101.     }
  102.   digitalWrite(load, LOW);
  103.   digitalWrite(load,HIGH);
  104. }
  105.  
  106. void maxOne(byte maxNr, byte reg, byte col) {    
  107. //maxOne is for addressing different MAX7219's,
  108. //while having a couple of them cascaded
  109.  
  110.   int c = 0;
  111.   digitalWrite(load, LOW);  // begin    
  112.  
  113.   for ( c = maxInUse; c > maxNr; c--) {
  114.     putByte(0);    // means no operation
  115.     putByte(0);    // means no operation
  116.   }
  117.  
  118.   putByte(reg);  // specify register
  119.   putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  120.  
  121.   for ( c =maxNr-1; c >= 1; c--) {
  122.     putByte(0);    // means no operation
  123.     putByte(0);    // means no operation
  124.   }
  125.  
  126.   digitalWrite(load, LOW); // and load da stuff
  127.   digitalWrite(load,HIGH);
  128. }
  129.  
  130.  
  131. void setup () {
  132.  
  133.   pinMode(dataIn, OUTPUT);
  134.   pinMode(clock,  OUTPUT);
  135.   pinMode(load,   OUTPUT);
  136.  
  137.   digitalWrite(13, HIGH);  
  138.  
  139. //initiation of the max 7219
  140.   maxAll(max7219_reg_scanLimit, 0x07);      
  141.   maxAll(max7219_reg_decodeMode, 0x00);  // using an led matrix (not digits)
  142.   maxAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
  143.   maxAll(max7219_reg_displayTest, 0x00); // no display test
  144.    for (e=1; e<=8; e++) {    // empty registers, turn all LEDs off
  145.     maxAll(e,0);
  146.   }
  147.   maxAll(max7219_reg_intensity, 0x0f & 0x0f);    // the first 0x0f is the value you can set
  148.                                                   // range: 0x00 to 0x0f
  149. }  
  150.  
  151. void loop () {
  152.  
  153.   //if you use just one MAX7219 it should look like this
  154.    maxSingle(1,1);                       //  + - - - - - - -
  155.    maxSingle(2,2);                       //  - + - - - - - -
  156.    maxSingle(3,4);                       //  - - + - - - - -
  157.    maxSingle(4,8);                       //  - - - + - - - -
  158.    maxSingle(5,16);                      //  - - - - + - - -
  159.    maxSingle(6,32);                      //  - - - - - + - -
  160.    maxSingle(7,64);                      //  - - - - - - + -
  161.    maxSingle(8,128);                     //  - - - - - - - +
  162.  
  163.  
  164.  //if you use more than one MAX7219, it should look like this
  165.   /*
  166.   maxAll(1,1);                       //  + - - - - - - -
  167.   maxAll(2,3);                       //  + + - - - - - -
  168.   maxAll(3,7);                       //  + + + - - - - -
  169.   maxAll(4,15);                      //  + + + + - - - -
  170.   maxAll(5,31);                      //  + + + + + - - -
  171.   maxAll(6,63);                      //  + + + + + + - -
  172.   maxAll(7,127);                     //  + + + + + + + -
  173.   maxAll(8,255);                     //  + + + + + + + +
  174.   */
  175.  
  176.  
  177.   //if you use more than one max7219 the second one should look like this
  178.   /*
  179.   maxOne(2,1,1);                       //  + - - - - - - -
  180.   maxOne(2,2,2);                       //  - + - - - - - -
  181.   maxOne(2,3,4);                       //  - - + - - - - -
  182.   maxOne(2,4,8);                       //  - - - + - - - -
  183.   maxOne(2,5,16);                      //  - - - - + - - -
  184.   maxOne(2,6,32);                      //  - - - - - + - -
  185.   maxOne(2,7,64);                      //  - - - - - - + -
  186.   maxOne(2,8,128);                     //  - - - - - - - +
  187.   */
  188.  
  189.   delay(2000);
  190.  
  191. }

Share