Grzegorz Kaczmarek / Mbed 2 deprecated Max7221

Dependencies:   mbed

Fork of Max7221 by Dwayne Dilbeck

Files at this revision

API Documentation at this revision

Comitter:
jakowisp
Date:
Tue Aug 06 08:18:53 2013 +0000
Parent:
0:cb8e1d05a4a7
Child:
2:828c62cc1861
Commit message:
Library mostly complete. Need to clean up code and document.

Changed in this revision

MAX7221/Max7221.cpp Show annotated file Show diff for this revision Revisions of this file
MAX7221/Max7221.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX7221/Max7221.cpp	Tue Aug 06 08:18:53 2013 +0000
@@ -0,0 +1,195 @@
+#include "mbed.h"
+#include "Max7221.h"
+
+int Max7221::maxInUseSPI1 = 0;
+int Max7221::maxInUseSPI2 = 0;
+SPI *Max7221::spi1=NULL;
+SPI *Max7221::spi2=NULL;
+DigitalOut *Max7221::load1=NULL;
+DigitalOut *Max7221::load2=NULL;
+
+Max7221::Max7221(PinName msoi, PinName mclk, PinName load){
+   switch (msoi) {
+     case p5: maxInUseSPI1++; 
+              this->id=maxInUseSPI1;
+              this->maxInUse=&maxInUseSPI1;
+              if (spi1 ==NULL) {
+                 spi1 = new SPI(msoi, NC, mclk);
+                 load1 = new DigitalOut(load);
+              } else {
+                //TODO: Check that load pin is the same for all SP2 
+              }
+              this->max72_spi=spi1;
+              this->load = load2;
+              break;
+     case p11: maxInUseSPI2++;  
+              this->id=maxInUseSPI2;
+              this->maxInUse=&maxInUseSPI2;
+              if (spi2 ==NULL) {
+                 spi2 = new SPI(msoi, NC, mclk);
+                 load2 = new DigitalOut(load);
+              } else {
+                //TODO: Check that load pin is the same for all SP2 
+              }
+              this->max72_spi=spi2;
+              this->load = load2;
+              break; 
+     default: error("Not a SPI port");
+   }
+   
+}
+
+void Max7221::Write( unsigned int reg, unsigned int col) {
+//maxOne is for adressing different MAX7219's,
+//while having a couple of them cascaded
+    int c = 0;
+    *load = LOW;
+
+    for ( c = *maxInUse; c > this->id; c--) {
+        max72_spi->write(0);  // no-op
+        max72_spi->write(0);  // no-op
+    }
+
+    max72_spi->write(reg);  // specify register
+    max72_spi->write(col);  // put data
+
+    for ( c=this->id-1; c >= 1; c--) {
+        max72_spi->write(0);  // no-op
+        max72_spi->write(0);  // no-op
+    }
+    *load = HIGH;
+}
+
+void Max7221::Setup () {
+    // initiation of the max 7219
+    // SPI setup: 8 bits, mode 0
+    max72_spi->format(8, 0);
+    
+    // going by the datasheet, min clk is 100ns so theoretically 10MHz should work...
+    max72_spi->frequency(10*MHZ);
+    
+    Write(max7219_reg_scanLimit, 0x07);
+    Write(max7219_reg_decodeMode, 0xff);  // using an led matrix (not digits)
+    Write(max7219_reg_shutdown, 0x01);    // not in shutdown mode
+    Write(max7219_reg_displayTest, 0x00); // no display test
+    for (int e=1; e<=8; e++) {            // empty registers, turn all LEDs off
+        Write(e,0xf);
+    }
+    Write(max7219_reg_intensity, 0x01 & 0x0f);    // the first 0x0f is the value you can set
+                                                  // range: 0x00 to 0x0f
+}
+
+
+void Max7221::WriteAll (unsigned int reg, unsigned int col) {    // initialize  all  MAX7219's in the system
+    if(load1 !=NULL) {
+        *load1 = LOW;                    // begin
+        for ( int c=1; c<= maxInUseSPI1; c++) {
+            spi1->write(reg);  // specify register
+            spi1->write(col);  // put data
+        }
+        *load1 = HIGH;
+    }
+    if(load2 !=NULL) {
+        *load2 = LOW;
+        for ( int c=1; c<= maxInUseSPI2; c++) {
+            spi2->write(reg);  // specify register
+            spi2->write(col);  // put data
+        }
+        *load2 = HIGH;
+    }
+}
+
+void Max7221::WriteInt( int value ){
+  char buffer[16];
+   
+  //TODO:SET UPPERBOUND AND LOWERBOUND based on NUMDIGITS
+  if (value <= UPPERBOUND && value >= LOWERBOUND) {
+     sprintf(buffer,"%8d",value);
+  } else {
+     sprintf(buffer,"--------");
+  } 
+  
+  Write(max7219_reg_decodeMode, 0xff);
+  for (int i=0;i<NUMDIGITS;i++) {
+       switch(buffer[i]){
+           case 0x2d: buffer[i]=0xa; break;
+           case 0x20: buffer[i]=0xf; break;
+           default: buffer[i]= buffer[i] & 0x0f;
+       }
+       Write(NUMDIGITS-i,buffer[i]);
+  }
+}
+
+void Max7221::WriteFloat( float value ){
+  char buffer[32];
+  int ptr=-1,len;
+  int i;
+ 
+  sprintf(buffer,"%f",value);
+  len=strlen(buffer);
+  i=len-1;
+  while(buffer[i]==0x30) {
+     buffer[i]='\0';
+     i--;
+     len--;
+  }
+  for( i =0; i<=len; i++) {
+      switch(buffer[i]){
+               case 0x2d: buffer[i]=0xa; break;
+               case 0x20: buffer[i]=0xf; break;
+               case 0x2e: buffer[i]=buffer[i-1] | 0x80;
+                           ptr = i-1;
+                           break;
+               default: buffer[i]= buffer[i];
+      }       
+      if (ptr != -1) {
+        buffer[i-1]=buffer[i]; 
+     }
+  }
+     
+  len=strlen(buffer);
+  Write(max7219_reg_decodeMode, 0xff);
+
+  // If too large for display set to '-'
+  if(len > NUMDIGITS && (ptr==-1 || ptr>NUMDIGITS))
+        for (int i=0;i<NUMDIGITS;i++) {
+           buffer[i]=0x0a;
+        }
+  //if number is smaller than display, fill with ' '      
+  if (len<=NUMDIGITS) { 
+      for (int i=1;i<=NUMDIGITS;i++) {
+           if(len-i>=0) {
+             Write(i,buffer[len-i]);
+           } else {
+             Write(i,0xf);   
+           }
+      }
+  } else {
+  //Write out the buffer, truncating the decimal digits if larger than display
+      for (int i=0;i<NUMDIGITS;i++) {
+          Write(NUMDIGITS-i,buffer[i]);
+      }
+  }
+}
+
+void Max7221::SetupAll () {
+    // initiation of the max 7219
+    // SPI setup: 8 bits, mode 0
+    if(spi1!=NULL) {
+       spi1->format(8, 0);
+       spi1->frequency(10*MHZ);    
+    }        
+    if(spi2!=NULL) {
+       spi2->format(8, 0);
+       spi2->frequency(10*MHZ);    
+    }        
+    WriteAll(max7219_reg_scanLimit, 0x07);
+    WriteAll(max7219_reg_decodeMode, 0xff);  // using an led matrix (not digits)
+    WriteAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
+    WriteAll(max7219_reg_displayTest, 0x00); // no display test
+    for (int e=1; e<=8; e++) {            // empty registers, turn all LEDs off
+        WriteAll(e,0xf);
+    }
+    WriteAll(max7219_reg_intensity, 0x01 & 0x0f);    // the first 0x0f is the value you can set
+                                                  // range: 0x00 to 0x0f
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX7221/Max7221.h	Tue Aug 06 08:18:53 2013 +0000
@@ -0,0 +1,68 @@
+#ifndef Max7221_H
+#define Max7221_H
+
+// define max7219/max7221 registers
+#define max7219_reg_noop         0x00
+#define max7219_reg_digit0       0x01
+#define max7219_reg_digit1       0x02
+#define max7219_reg_digit2       0x03
+#define max7219_reg_digit3       0x04
+#define max7219_reg_digit4       0x05
+#define max7219_reg_digit5       0x06
+#define max7219_reg_digit6       0x07
+#define max7219_reg_digit7       0x08
+#define max7219_reg_decodeMode   0x09
+#define max7219_reg_intensity    0x0a
+#define max7219_reg_scanLimit    0x0b
+#define max7219_reg_shutdown     0x0c
+#define max7219_reg_displayTest  0x0f
+
+#define LOW 0
+#define HIGH 1
+#define MHZ 1000000
+#define NUMDIGITS 8
+
+#ifdef NUMDIGITS 
+#define UPPERBOUND 99999999
+#define LOWERBOUND -9999999
+#endif
+
+class Max7221 {
+public:
+   Max7221(PinName msoi=p5, PinName mclk=p7, PinName load=p8);
+   
+   void Write( unsigned int reg, unsigned int col);
+   
+   void WriteInt( int value );
+   void WriteFloat( float value);
+   Max7221& operator= (int value){ 
+        WriteInt(value); 
+        return *this;
+        };
+   Max7221& operator= (float value){ 
+        WriteFloat(value); 
+        return *this; 
+        };
+   
+   void Setup (void);
+   
+   static void WriteAll (unsigned int reg, unsigned int col);
+   static void SetupAll (void);
+   
+private:
+   SPI *max72_spi;
+   DigitalOut *load;
+   int id;
+   int *maxInUse;
+   
+   
+   static SPI *spi1;
+   static SPI *spi2;
+   static DigitalOut *load1;
+   static DigitalOut *load2;
+   static int maxInUseSPI1;
+   static int maxInUseSPI2;
+   
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Mon Aug 05 18:08:30 2013 +0000
+++ b/main.cpp	Tue Aug 06 08:18:53 2013 +0000
@@ -1,170 +1,34 @@
- #include "mbed.h"
-
-// p5: DIN, p7: CLK, p8: LOAD/CS
-SPI max72_spi(p5, NC, p7);
-DigitalOut load(p8);
-
-int maxInUse = 1;    //change this variable to set how many MAX7219's you'll use
-
-// define max7219/max7221 registers
-#define max7219_reg_noop         0x00
-#define max7219_reg_digit0       0x01
-#define max7219_reg_digit1       0x02
-#define max7219_reg_digit2       0x03
-#define max7219_reg_digit3       0x04
-#define max7219_reg_digit4       0x05
-#define max7219_reg_digit5       0x06
-#define max7219_reg_digit6       0x07
-#define max7219_reg_digit7       0x08
-#define max7219_reg_decodeMode   0x09
-#define max7219_reg_intensity    0x0a
-#define max7219_reg_scanLimit    0x0b
-#define max7219_reg_shutdown     0x0c
-#define max7219_reg_displayTest  0x0f
-
-#define LOW 0
-#define HIGH 1
-#define MHZ 1000000
-unsigned int count;
-
-void maxSingle( unsigned int reg, unsigned int col) {
-//maxSingle is the "easy"  function to use for a
-//single max7219
-
-    load = LOW;            // begin
-
-#ifdef SPI16
-    unsigned int temp;
-    temp=(reg<<8)||col;
-    max72_spi.format(16, 0);
-    max72_spi.write(temp); 
-    max72_spi.format(8, 0);
-#else
-    max72_spi.write(reg);
-    max72_spi.write(col);  // put data
-#endif
-    load = HIGH;           // make sure data is loaded (on rising edge of LOAD/CS)
-}
-
-void maxAll (unsigned int reg, unsigned int col) {    // initialize  all  MAX7219's in the system
-    load = LOW;                    // begin
-    for ( int c=1; c<= maxInUse; c++) {
-        max72_spi.write(reg);  // specify register
-        max72_spi.write(col);  // put data
-    }
-    load = HIGH;
-}
-
-void maxOne(unsigned int maxNr, unsigned int reg, unsigned int col) {
-//maxOne is for adressing different MAX7219's,
-//while having a couple of them cascaded
-    int c = 0;
-    load = LOW;
-
-    for ( c = maxInUse; c > maxNr; c--) {
-        max72_spi.write(0);  // no-op
-        max72_spi.write(0);  // no-op
-    }
-
-    max72_spi.write(reg);  // specify register
-    max72_spi.write(col);  // put data
-
-    for ( c=maxNr-1; c >= 1; c--) {
-        max72_spi.write(0);  // no-op
-        max72_spi.write(0);  // no-op
-    }
-    load = HIGH;
-}
-
-
-void setup () {
-    // initiation of the max 7219
-    // SPI setup: 8 bits, mode 0
-    max72_spi.format(8, 0);
-    
-    // going by the datasheet, min clk is 100ns so theoretically 10MHz should work...
-     max72_spi.frequency(10*MHZ);
-    
-    maxAll(max7219_reg_scanLimit, 0x07);
-    maxAll(max7219_reg_decodeMode, 0xff);  // using an led matrix (not digits)
-    maxAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
-    maxAll(max7219_reg_displayTest, 0x00); // no display test
-    for (int e=1; e<=8; e++) {    // empty registers, turn all LEDs off
-        maxAll(e,0xf);
-    }
-    maxAll(max7219_reg_intensity, 0x01 & 0x0f);    // the first 0x0f is the value you can set
-    // range: 0x00 to 0x0f
-}
-
-void loop () {
-  
-  
-    //if you use just one MAX7219 it should look like this
-     
-  for (int i=1;i<=8;i++) {
-     if (i<=count && i !=0) 
-        maxSingle(i,i);
-     else 
-       maxSingle(i,0xf);
-   }
-  
-   if (count <8)
-      count=count++;
-   else 
-     count=0;
-      
-     /*
-     maxSingle(1,255);                       //  + - - - - - - -
-     maxSingle(2,255);                       //  - + - - - - - -
-     maxSingle(3,255);                       //  - - + - - - - -
-     maxSingle(4,255);                       //  - - - + - - - -
-     maxSingle(5,255);                      //  - - - - + - - -
-     maxSingle(6,255);                      //  - - - - - + - -
-     maxSingle(7,0);                      //  - - - - - - + -
-     maxSingle(8,0);                     //  - - - - - - - +
-    */
-    //if you use more than one MAX7219, it should look like this
-
-    /*
-    maxAll(1,1);                       //  + - - - - - - -
-    maxAll(2,3);                       //  + + - - - - - -
-    maxAll(3,7);                       //  + + + - - - - -
-    maxAll(4,15);                      //  + + + + - - - -
-    maxAll(5,31);                      //  + + + + + - - -
-    maxAll(6,63);                      //  + + + + + + - -
-    maxAll(7,127);                     //  + + + + + + + -
-    maxAll(8,255);                     //  + + + + + + + +
-    */
-
-    //
-
-    //if you use more then one max7219 the second one should look like this
-
-
-    /*
-    maxOne(2,1,1);                       //  + - - - - - - -
-    maxOne(2,2,2);                       //  - + - - - - - -
-    maxOne(2,3,4);                       //  - - + - - - - -
-    maxOne(2,4,8);                       //  - - - + - - - -
-    maxOne(2,5,16);                      //  - - - - + - - -
-    maxOne(2,6,32);                      //  - - - - - + - -
-    maxOne(2,7,64);                      //  - - - - - - + -
-    maxOne(2,8,128);                     //  - - - - - - - +
-
-    */
-    
-    //
-    
-
-}
-
-int main() {
-    count=1;
-    setup ();
-    while (1) {
-    loop ();
-    wait_ms(1000);
-    }
-    
-    return 0;
-}
\ No newline at end of file
+ #include "mbed.h"
+ #include "Max7221.h"
+ 
+
+// p5: DIN, p7: CLK, p8: LOAD/CS
+// Max7221 max7221disp1(p5, p7, p8);
+Max7221 max7221disp1(p11, p13, p14);
+
+int count=-9999999;
+
+void loop(void) {
+   max7221disp1.WriteInt(count);
+   if (count < 10)
+      count=count+1;
+   else 
+      count=-9999999; 
+}
+
+int main() {
+    
+    max7221disp1.Setup();
+    Max7221::WriteAll(0x0f,0x01);
+    wait_ms(500);
+    Max7221::WriteAll(0x0f,0x00);
+    max7221disp1.WriteFloat(123.125);
+    wait(1.0);
+    
+
+    while (1) {
+        loop();
+        wait(1.0);
+    }
+}
+