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

Skip line 139 -- haven't seen a need to use the SPI in this code, and besides it should be Serial.begin(9600);

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

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

EX ---- Error reported for this article Nov. 30 2007 ---- is fixed

In the pdf-file of the tutorial the relationship between the columns of a matrix and the segments is wrong. See original bug report for details.

(Note for the tutorial: SegDP is MSB and SegG is LSB, so D7..D0 are SegDP, SegA, ..., SegG.)

>>>> hej, i added some sentence to the tutorial to make the order of the segments more clear to the reader, i hope that there will be no misunderstanding anymore. Thanks to Eberhard and Andrea for pointing this out. If there is still any problems or questions feel free to contact me.


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 useing more then one 7219 in a row,
  3. ______________________________________
  4.  
  5.  Code History:
  6.  --------------
  7.  
  8. The orginal 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.  
  27. -if you are only using one max7219, then use the function maxSingle to control
  28.  the little guy ---maxSingle(register (1-8), collum (0-255))
  29.  
  30. -if you are using more then one max7219, and they all should work the same,
  31. then use the function maxAll ---maxAll(register (1-8), collum (0-255))
  32.  
  33. -if you are using more than one max7219 and just want to change something
  34. at one little guy, then use the function maxOne
  35. ---maxOne(Max you wane controll (1== the first one), register (1-8),
  36. collum (0-255))
  37.  
  38. /* During initiation, be sure to send every part to every max7219 and then
  39.  upload it.
  40. For example, if you have five max7219's, you have to send the scanLimit 5 times
  41. before you load it-- other wise not every max7219 will get the data. the
  42. function maxInUse  keeps track of this, just tell it how many max7219 you are
  43. using.
  44. */
  45.  
  46. int dataIn = 2;
  47. int load = 3;
  48. int clock = 4;
  49.  
  50. int maxInUse = 4;    //change this variable to set how many MAX7219's you'll use
  51.  
  52. int e = 0;           // just a varialble
  53.  
  54.                      // define max7219 registers
  55. byte max7219_reg_noop        = 0x00;
  56. byte max7219_reg_digit0      = 0x01;
  57. byte max7219_reg_digit1      = 0x02;
  58. byte max7219_reg_digit2      = 0x03;
  59. byte max7219_reg_digit3      = 0x04;
  60. byte max7219_reg_digit4      = 0x05;
  61. byte max7219_reg_digit5      = 0x06;
  62. byte max7219_reg_digit6      = 0x07;
  63. byte max7219_reg_digit7      = 0x08;
  64. byte max7219_reg_decodeMode  = 0x09;
  65. byte max7219_reg_intensity   = 0x0a;
  66. byte max7219_reg_scanLimit   = 0x0b;
  67. byte max7219_reg_shutdown    = 0x0c;
  68. byte max7219_reg_displayTest = 0x0f;
  69.  
  70. void putByte(byte data) {
  71.   byte i = 8;
  72.   byte mask;
  73.   while(i > 0) {
  74.     mask = 0x01 << (i - 1);      // get bitmask
  75.     digitalWrite( clock, LOW);   // tick
  76.     if (data & mask){            // choose bit
  77.       digitalWrite(dataIn, HIGH);// send 1
  78.     }else{
  79.       digitalWrite(dataIn, LOW); // send 0
  80.     }
  81.     digitalWrite(clock, HIGH);   // tock
  82.     --i;                         // move to lesser bit
  83.   }
  84. }
  85.  
  86. void maxSingle( byte reg, byte col) {    
  87. //maxSingle is the "easy"  function to use for a     //single max7219
  88.  
  89.   digitalWrite(load, LOW);       // begin    
  90.   putByte(reg);                  // specify register
  91.   putByte(col);//((data & 0x01) * 256) + data >> 1); // put data  
  92.   digitalWrite(load, LOW);       // and load da shit
  93.   digitalWrite(load,HIGH);
  94. }
  95.  
  96. void maxAll (byte reg, byte col) {    // initialize  all  MAX7219's in the system
  97.   int c = 0;
  98.   digitalWrite(load, LOW);  // begin    
  99.   for ( c =1; c<= maxInUse; c++) {
  100.   putByte(reg);  // specify register
  101.   putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  102.     }
  103.   digitalWrite(load, LOW);
  104.   digitalWrite(load,HIGH);
  105. }
  106.  
  107. void maxOne(byte maxNr, byte reg, byte col) {    
  108. //maxOne is for adressing different MAX7219's,
  109. //whilele having a couple of them cascaded
  110.  
  111.   int c = 0;
  112.   digitalWrite(load, LOW);  // begin    
  113.  
  114.   for ( c = maxInUse; c > maxNr; c--) {
  115.     putByte(0);    // means no operation
  116.     putByte(0);    // means no operation
  117.   }
  118.  
  119.   putByte(reg);  // specify register
  120.   putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  121.  
  122.   for ( c =maxNr-1; c >= 1; c--) {
  123.     putByte(0);    // means no operation
  124.     putByte(0);    // means no operation
  125.   }
  126.  
  127.   digitalWrite(load, LOW); // and load da shit
  128.   digitalWrite(load,HIGH);
  129. }
  130.  
  131.  
  132. void setup () {
  133.  
  134.  
  135.   pinMode(dataIn, OUTPUT);
  136.   pinMode(clock,  OUTPUT);
  137.   pinMode(load,   OUTPUT);
  138.  
  139.   //beginSerial(9600);
  140.   digitalWrite(13, HIGH);  
  141.  
  142. //initiation of the max 7219
  143.   maxAll(max7219_reg_scanLimit, 0x07);      
  144.   maxAll(max7219_reg_decodeMode, 0x00);  // using an led matrix (not digits)
  145.   maxAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
  146.   maxAll(max7219_reg_displayTest, 0x00); // no display test
  147.    for (e=1; e<=8; e++) {    // empty registers, turn all LEDs off
  148.     maxAll(e,0);
  149.   }
  150.   maxAll(max7219_reg_intensity, 0x0f & 0x0f);    // the first 0x0f is the value you can set
  151.                                                   // range: 0x00 to 0x0f
  152. }  
  153.  
  154. void loop () {
  155.  
  156.  
  157.   //if you use just one MAX7219 it should look like this
  158.   /*
  159.    maxSingle(1,1);                       //  + - - - - - - -
  160.    maxSingle(2,2);                       //  - + - - - - - -
  161.    maxSingle(3,4);                       //  - - + - - - - -
  162.    maxSingle(4,8);                       //  - - - + - - - -
  163.    maxSingle(5,16);                      //  - - - - + - - -
  164.    maxSingle(6,32);                      //  - - - - - + - -
  165.    maxSingle(7,64);                      //  - - - - - - + -
  166.    maxSingle(8,128);                     //  - - - - - - - +
  167.  
  168.   */
  169.   //if you use more than one MAX7219, it should look like this
  170.  
  171.   /*
  172.   maxAll(1,1);                       //  + - - - - - - -
  173.   maxAll(2,3);                       //  + + - - - - - -
  174.   maxAll(3,7);                       //  + + + - - - - -
  175.   maxAll(4,15);                      //  + + + + - - - -
  176.   maxAll(5,31);                      //  + + + + + - - -
  177.   maxAll(6,63);                      //  + + + + + + - -
  178.   maxAll(7,127);                     //  + + + + + + + -
  179.   maxAll(8,255);                     //  + + + + + + + +
  180.   */
  181.  
  182.   //
  183.  
  184.   //if you use more then one max7219 the second one should look like this
  185.  
  186.  
  187.   maxOne(2,1,1);                       //  + - - - - - - -
  188.   maxOne(2,2,2);                       //  - + - - - - - -
  189.   maxOne(2,3,4);                       //  - - + - - - - -
  190.   maxOne(2,4,8);                       //  - - - + - - - -
  191.   maxOne(2,5,16);                      //  - - - - + - - -
  192.   maxOne(2,6,32);                      //  - - - - - + - -
  193.   maxOne(2,7,64);                      //  - - - - - - + -
  194.   maxOne(2,8,128);                     //  - - - - - - - +
  195.  
  196.  
  197.   //
  198.   delay(2000);
  199.  
  200. }

Share