Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices

Dependents:   MCP41xxxApp MCP320xApp MCP41xxxApp

Files at this revision

API Documentation at this revision

Comitter:
Yann
Date:
Thu Sep 05 07:29:00 2013 +0000
Parent:
6:ded0d8a6729c
Commit message:
Minor bug fiixed

Changed in this revision

MCP4xxxx_SPI.cpp Show annotated file Show diff for this revision Revisions of this file
MCP4xxxx_SPI.h Show annotated file Show diff for this revision Revisions of this file
--- a/MCP4xxxx_SPI.cpp	Thu Jun 06 07:59:16 2013 +0000
+++ b/MCP4xxxx_SPI.cpp	Thu Sep 05 07:29:00 2013 +0000
@@ -23,7 +23,7 @@
 
     unsigned char CMCP4xxxx_SPI::SPIModuleRefCounter = 0;
 
-    CMCP4xxxx_SPI::CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs, const PinName p_reset, const PinName p_shdn, const Mcp4xxxFamilly p_familly, const unsigned int p_frequency) : _internalId("") {
+    CMCP4xxxx_SPI::CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs, const PinName p_reset, const PinName p_shdn, const Mcp4xxxFamily p_family, const unsigned int p_frequency) : _internalId("") {
         DEBUG_ENTER("CMCP4xxxx_SPI")
 
         if (CMCP4xxxx_SPI::SPIModuleRefCounter != 0) {
@@ -63,8 +63,8 @@
             _shdn = NULL; // Not used
         }
         
-        _familly = p_familly;
-        SetFamilly(p_familly);
+        _family = p_family;
+        SetFamilly(p_family);
         
         _incStep = 0x80; // Set to middle step
         
@@ -101,7 +101,7 @@
     }
     
     unsigned short CMCP4xxxx_SPI::WriteRegister(const Addresses p_address, const unsigned char p_value) {
-        DEBUG_ENTER("CMCP4xxxx_SPI::WriteRegister: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_address, p_value, (unsigned char)_familly)
+        DEBUG_ENTER("CMCP4xxxx_SPI::WriteRegister: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_address, p_value, (unsigned char)_family)
         
         // Sanity check
         if (p_address == CMCP4xxxx_SPI::Status) {
@@ -117,8 +117,7 @@
             _cs->write(0);
         }
         unsigned short result = _spiInstance->write(command);
-        DEBUG("CMCP4xxxx_SPI::WriteRegister: returned value=%d", result)
-        if (_cs != NULL) {
+         if (_cs != NULL) {
             _cs->write(1);
         }
         
@@ -127,7 +126,7 @@
     }
     
     unsigned short CMCP4xxxx_SPI::ReadRegister(const Addresses p_address) {
-        DEBUG_ENTER("CMCP4xxxx_SPI::ReadRegister: 0x%02x - 0x%02x", (unsigned char)p_address, (unsigned char)_familly)
+        DEBUG_ENTER("CMCP4xxxx_SPI::ReadRegister: 0x%02x", (unsigned char)p_address)
         
         unsigned short command = ((p_address & 0x0f) << 4 | (0x03/*TODO Use 'define' for Read command*/ << 2)) << 8;
         
@@ -145,7 +144,7 @@
         return result & 0x00ff;
     }
     
-    unsigned short CMCP4xxxx_SPI::Incrememt(const Commands p_command) { 
+    unsigned short CMCP4xxxx_SPI::Increment(const Commands p_command) { 
         // Sanity check
         if (_incStep != 0xff) { 
             _incStep += 1; // TODO Change increment 1 by a parametrized increment step
@@ -163,7 +162,7 @@
 
     
     unsigned short CMCP4xxxx_SPI::Write(const Commands p_command, const unsigned char p_value) {
-        DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_command, p_value, (unsigned char)_familly)
+        DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_command, p_value, (unsigned char)_family)
         
         // Sanity check
         if ((p_command != WriteToDigiPot1) && (p_command != WriteToDigiPot2) && (p_command != WriteToBoth)) {
@@ -172,7 +171,7 @@
         }
         
         unsigned short command = 0;
-        if ((_familly == CMCP4xxxx_SPI::_41xxx) || (_familly == CMCP4xxxx_SPI::_42xxx)) {  // See DS11195C
+        if ((_family == CMCP4xxxx_SPI::_41xxx) || (_family == CMCP4xxxx_SPI::_42xxx)) {  // See DS11195C
             switch (p_command) {
                 case WriteToDigiPot1: 
                     command = (0x11 << 8 | p_value);
@@ -193,7 +192,7 @@
             } // End of 'switch' statement
         }
         
-        DEBUG("CMCP4xxxx_SPI::Write: Send command: 0x%04x", (unsigned char)command, (unsigned char)_familly)
+        DEBUG("CMCP4xxxx_SPI::Write: Send command: 0x%04x", (unsigned char)command)
         if (_cs != NULL) {
             _cs->write(0);
         }
@@ -207,7 +206,7 @@
     }
 
     unsigned short CMCP4xxxx_SPI::Shutdown(const Commands p_command, const bool p_set) {
-        DEBUG_ENTER("CMCP4xxxx_SPI::Shutdown: 0x%02x - 0x%02x", (unsigned char)p_command, (unsigned char)_familly)
+        DEBUG_ENTER("CMCP4xxxx_SPI::Shutdown: 0x%02x - 0x%02x", (unsigned char)p_command, (unsigned char)_family)
         
         // Sanity check
         if ((p_command != ShutdownDigiPot1) && (p_command != ShutdownDigiPot2) && (p_command != ShutdownBoth)) {
@@ -216,7 +215,7 @@
         }
         
        unsigned short command = 0;
-       if ((_familly == CMCP4xxxx_SPI::_41xxx) || (_familly == CMCP4xxxx_SPI::_42xxx)) { // See DS11195C
+       if ((_family == CMCP4xxxx_SPI::_41xxx) || (_family == CMCP4xxxx_SPI::_42xxx)) { // See DS11195C
            switch (p_command) {
                 case ShutdownDigiPot1:
                     command = (0x21 << 8);
@@ -266,19 +265,20 @@
         DEBUG_LEAVE("CMCP4xxxx_SPI::Shutdown: %d", result)
         return result;
     }
-    unsigned short CMCP4xxxx_SPI::Write() {
-        return _spiInstance->write(0);
-    }
+    
+    //unsigned short CMCP4xxxx_SPI::Write() {
+    //    return _spiInstance->write(0);
+    //}
     
-    CMCP4xxxx_SPI::Mcp4xxxFamilly CMCP4xxxx_SPI::SetFamilly(const CMCP4xxxx_SPI::Mcp4xxxFamilly p_familly) { 
-        DEBUG_ENTER("CMCP4xxxx_SPI::SetFamilly: 0x%02x", (unsigned char)p_familly)
+    CMCP4xxxx_SPI::Mcp4xxxFamily CMCP4xxxx_SPI::SetFamilly(const CMCP4xxxx_SPI::Mcp4xxxFamily p_family) { 
+        DEBUG_ENTER("CMCP4xxxx_SPI::SetFamilly: 0x%02x", (unsigned char)p_family)
         
-        Mcp4xxxFamilly old = _familly;
-        _familly = p_familly;
-        if ((_familly != CMCP4xxxx_SPI::_41xxx) && (_familly != CMCP4xxxx_SPI::_42xxx)) {
+        Mcp4xxxFamily old = _family;
+        _family = p_family;
+        if ((_family != CMCP4xxxx_SPI::_41xxx) && (_family != CMCP4xxxx_SPI::_42xxx)) {
             // Setup TCON register 
             DEBUG("CMCP4xxxx_SPI::SetFamilly: Setup TCON register")
-            WriteRegister(CMCP4xxxx_SPI::TCon, 0x7f); // See DS22060B-page 36 REGISTER 4-2: TCON BITS
+            WriteRegister(CMCP4xxxx_SPI::TCon, 0xff); // See DS22060B-page 36 REGISTER 4-2: TCON BITS
                                                       // TODO Use 'define' for command
         }
         
--- a/MCP4xxxx_SPI.h	Thu Jun 06 07:59:16 2013 +0000
+++ b/MCP4xxxx_SPI.h	Thu Sep 05 07:29:00 2013 +0000
@@ -65,15 +65,15 @@
          */
         unsigned char _incStep;
      public:
-        /** MCP32xx familly
+        /** MCP32xx family
          */
-        enum Mcp4xxxFamilly {
-            _41xxx = 0x00,  //<! 41xxx familly such as MCP41010 - See DS11195C
-            _42xxx = 0x01,  //<! 42xxx familly such as MCP42010 - See DS11195C
-            _41xx = 0x02,   //<! 41xx familly such as MCP4251 - Single Potentiometer/Rheostat
-            _42xx = 0x03    //<! 42x2 familly such as MCP42x2 - Dual Potentiometer/Rheostat
+        enum Mcp4xxxFamily {
+            _41xxx = 0x00,  //<! 41xxx family such as MCP41010 - See DS11195C
+            _42xxx = 0x01,  //<! 42xxx family such as MCP42010 - See DS11195C
+            _41xx = 0x02,   //<! 41xx family such as MCP4251 - Single Potentiometer/Rheostat
+            _42xx = 0x03    //<! 42x2 family such as MCP42x2 - Dual Potentiometer/Rheostat
        };
-        Mcp4xxxFamilly _familly;     
+        Mcp4xxxFamily _family;     
      public:
         /** Authorized commands
          * See DS11195C-page 18
@@ -104,7 +104,7 @@
          * @param p_shdn: MBed pin to manage /SHDN input. If NC, /SHDN is not managed, default value is NC, not connected
          * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz
          */
-        CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs = NC, const PinName p_reset = NC, const PinName p_shdn = NC, const Mcp4xxxFamilly familly = _41xxx, const unsigned int p_frequency = 1000000);
+        CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs = NC, const PinName p_reset = NC, const PinName p_shdn = NC, const Mcp4xxxFamily family = _41xxx, const unsigned int p_frequency = 1000000);
     
         /** Destructor
          */
@@ -131,7 +131,7 @@
          * @param p_command The digital potentiometer to increment. Default: WriteToDigiPot1
          * @return 0x0000 on success, any value otherwise
          */
-        unsigned short Incrememt(const Commands p_command = WriteToDigiPot1);
+        unsigned short Increment(const Commands p_command = WriteToDigiPot1);
         
         /** Decrement the specified digital potentiometer
          * @param p_command The digital potentiometer to decrement. Default: WriteToDigiPot1
@@ -174,13 +174,13 @@
     
          /** Write a NOP command
           */
-        unsigned short Write();
+        //unsigned short Write();
     
-         /** Change the current familly 
-          * @param familly The new type of ICs 
+         /** Change the current family 
+          * @param family The new type of ICs 
           * @return the previous type of ICs 
           */
-        Mcp4xxxFamilly SetFamilly(const Mcp4xxxFamilly familly);
+        Mcp4xxxFamily SetFamilly(const Mcp4xxxFamily family);
     
        /** Reset the device
         * @code