Fix to have load pin working with SPI1.

Dependencies:   mbed

Fork of Max7221 by Dwayne Dilbeck

Files at this revision

API Documentation at this revision

Comitter:
ky3orr
Date:
Tue Nov 24 00:38:36 2015 +0000
Parent:
8:3f5901f2f813
Commit message:
All instances now share one SPI and /CS signal and operate in display chain.

Changed in this revision

Max7221.cpp Show annotated file Show diff for this revision Revisions of this file
Max7221.h Show annotated file Show diff for this revision Revisions of this file
diff -r 3f5901f2f813 -r 75bddbfbb692 Max7221.cpp
--- a/Max7221.cpp	Mon Nov 23 11:38:17 2015 +0000
+++ b/Max7221.cpp	Tue Nov 24 00:38:36 2015 +0000
@@ -10,13 +10,32 @@
 #include "mbed.h"
 #include "Max7221.h"
 
-Max7221::Max7221(SPI *spi, DigitalOut *cs, unsigned int position) :
-    mp_spi(spi),
-    mp_cs(cs),
-    m_position(position)
-{
+// Static members initialization
+SPI *Max7221::mp_spi = NULL;
+DigitalOut *Max7221::mp_cs = NULL;
+unsigned int Max7221::m_counter = 0;
+
+Max7221::Max7221() {
+    m_position = m_counter;
+    m_counter++;
 }
 
+Max7221::Max7221(SPI *spi, DigitalOut *cs) {
+    mp_spi = spi;
+    mp_cs = cs;
+    m_position = m_counter;
+    m_counter++;
+}
+
+void Max7221::SetSpi(SPI *spi) {
+    mp_spi = spi;
+}
+
+void Max7221::SetCs(DigitalOut *cs) {
+    mp_cs = cs;
+}
+    
+    
 void Max7221::CsLow()
 {
     if(mp_cs != NULL) {
@@ -42,11 +61,17 @@
     unsigned int i;
     
     CsLow();
+    if((m_counter > 1) && ((m_counter - 1) > m_position)) {
+        i = m_counter - 1 - m_position;
+        while(i--) {
+            WriteRaw(max7221_reg_noop, 0x00);
+        }        
+    }
     WriteRaw(reg, data);
     if(m_position > 0) {
         i = m_position;
         while(i--) {
-            WriteRaw(max7219_reg_noop, 0x00);
+            WriteRaw(max7221_reg_noop, 0x00);
         }
     }
     CsHigh();
@@ -54,40 +79,71 @@
 
 void Max7221::TestMode(bool mode) {
     if(mode) {
-        Write(max7219_reg_displayTest, 0x01);
+        Write(max7221_reg_displayTest, 0x01);
     } else {
-        Write(max7219_reg_displayTest, 0x00);
+        Write(max7221_reg_displayTest, 0x00);
     }
 }
 
 void Max7221::UseDigitsNo(unsigned int digits_no) {
     if((digits_no > 0) && (digits_no < 9)) {
-        Write(max7219_reg_scanLimit, (digits_no - 1));
+        Write(max7221_reg_scanLimit, (digits_no - 1));
     }
 }
 
 void Max7221::DecodeMode(unsigned int mode) {
-    Write(max7219_reg_decodeMode, mode);
+    Write(max7221_reg_decodeMode, mode);
 }
 
 void Max7221::WriteDigit(unsigned int digit_no, unsigned int value) {
+    unsigned int digit_reg;
+
     if(digit_no < 8) {
-        Write(digit_no, value);
-    }
+        switch(digit_no) {
+            case 0:
+                digit_reg = max7221_reg_digit0;
+                break;
+            case 1:
+                digit_reg = max7221_reg_digit1;
+                break;
+            case 2:
+                digit_reg = max7221_reg_digit2;
+                break;
+            case 3:
+                digit_reg = max7221_reg_digit3;
+                break;
+            case 4:
+                digit_reg = max7221_reg_digit4;
+                break;
+            case 5:
+                digit_reg = max7221_reg_digit5;
+                break;
+            case 6:
+                digit_reg = max7221_reg_digit6;
+                break;
+            case 7:
+                digit_reg = max7221_reg_digit7;
+                break;
+            default:
+                digit_reg = max7221_reg_noop;
+                break;
+        }
+        Write(digit_reg, value);
+    }    
 }
 
 void Max7221::Intensity(unsigned int intensity) {
     if(intensity > 0x0F) {
         intensity = 0x0F;
     }
-    Write(max7219_reg_intensity, intensity);
+    Write(max7221_reg_intensity, intensity);
 }
 
 void Max7221::OperationMode(bool mode) {
     if(mode) {
-        Write(max7219_reg_shutdown, 0x01);
+        Write(max7221_reg_shutdown, 0x01);
     } else {
-        Write(max7219_reg_shutdown, 0x00);
+        Write(max7221_reg_shutdown, 0x00);
     }
 }
 
@@ -97,7 +153,7 @@
     TestMode(false);            // Disable display test
     UseDigitsNo(8);             // Use all 8 digits
     DecodeMode(0xFF);           // Turn on Code B font decode for all digits
-    for(i=1;i<9;i++) {          // Clean all digits
+    for(i=0;i<8;i++) {          // Clean all digits
         WriteDigit(i, 0x0F);
     }
     Intensity(0x01);            // Set lowest display intensity(0x00-0xFF)
diff -r 3f5901f2f813 -r 75bddbfbb692 Max7221.h
--- a/Max7221.h	Mon Nov 23 11:38:17 2015 +0000
+++ b/Max7221.h	Tue Nov 24 00:38:36 2015 +0000
@@ -11,12 +11,20 @@
 #define Max7221_H
 
 // MAX7219/7221 register map
-#define max7219_reg_noop         0x00
-#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 max7221_reg_noop         0x00
+#define max7221_reg_digit0       0x01
+#define max7221_reg_digit1       0x02
+#define max7221_reg_digit2       0x03
+#define max7221_reg_digit3       0x04
+#define max7221_reg_digit4       0x05
+#define max7221_reg_digit5       0x06
+#define max7221_reg_digit6       0x07
+#define max7221_reg_digit7       0x08
+#define max7221_reg_decodeMode   0x09
+#define max7221_reg_intensity    0x0A
+#define max7221_reg_scanLimit    0x0B
+#define max7221_reg_shutdown     0x0C
+#define max7221_reg_displayTest  0x0F
 
 // Logical values
 #define LOG_0 0
@@ -24,18 +32,35 @@
 
 class Max7221 {
 private:
-   SPI *mp_spi;             // SPI used for this display (no data transmission when NULL)
-   DigitalOut *mp_cs;       // /CS signal (it should be handled manually when NULL)
-   unsigned int m_position; // Display position on SPI chain (0 = 1'st)
-
+    static SPI *mp_spi;             // SPI used for this display (no data transmission when NULL)
+    static DigitalOut *mp_cs;       // /CS signal (it should be handled manually when NULL)
+    static unsigned int m_counter;  // Total number of displays on SPI chain
+    unsigned int m_position;        // Display position on SPI chain (0 = 1'st)
 public:
     /**
     *  Default constructor.
-    *  @param spi       Initialized SPI port used for communication with MAX7219/7221.
-    *  @param cs        Initialized output pin used as SW CS signal or NULL when this signal is handled manually.
-    *  @param position  Position on SPI port (for displays connected in daisy-chain: DOUT-DIN). 0=1st.
+    *  At least one instance should set SPI and /CS pointers for display chain to work.
+    *  Use SetSpi() and SetCs() or parametrized constructor.
+    */
+    Max7221();
+
+    /**
+    *  Constructor.
+    *  Sets SPI and /CS pointers. Display chain is operational.
     */
-    Max7221(SPI *spi, DigitalOut *cs, unsigned int position=0);
+    Max7221(SPI *spi, DigitalOut *cs);
+
+    /**
+    *  Sets pointer to configured SPI.
+    *  SPI is used by all display instances.
+    */
+    void SetSpi(SPI *spi);
+
+    /**
+    *  Sets pointer to configured /CS signal.
+    *  /CS signal is used by all display instances.
+    */    
+    void SetCs(DigitalOut *cs);
 
     /**
     *  Method for setting /CS signal LOW (enable data reception).