8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory

Fork of MCP4261 by TeamElectronics

Revision:
2:1e4469bdbed9
Parent:
1:935321af7311
diff -r 935321af7311 -r 1e4469bdbed9 MCP4261.cpp
--- a/MCP4261.cpp	Sun Jun 28 10:24:49 2015 +0000
+++ b/MCP4261.cpp	Wed Jan 20 10:54:05 2016 +0000
@@ -29,33 +29,28 @@
 
     MCP4261::MCP4261(SPI& spi, PinName nCs, PinName nWP, PinName nSHDN): _spi(spi), _nCs(nCs), _nWP(nWP), _nSHDN(nSHDN) 
     {
-                
+        _nCs = 1;            
     }    
     
-    MCP4261::MCP4261(PinName mosi, PinName miso, PinName sck, PinName nCs, PinName nWP, PinName nSHDN) : _spi(mosi, miso, sck), _nCs(nCs), _nWP(nWP), _nSHDN(nSHDN) 
+    MCP4261::MCP4261(SPI& spi, PinName nCs, PinName nWP): _spi(spi), _nCs(nCs), _nWP(nWP), _nSHDN(NC) 
     {
-              
-    }
-    
-    MCP4261::MCP4261(SPI& spi, PinName nCs, PinName nWP): _spi(spi), _nCs(nCs), _nWP(nWP)
-    {
-                
+        _nCs = 1;           
     }    
     
     int MCP4261::read(char address)
     {
-        _read(char address);    
+        return _read(address);    
     }
 
    
     void MCP4261::write(char address, int data)
     {
-        _write(char address, int data);
+        _write(address, data);
     }
     
     void MCP4261::inc(bool number)
     {
-        if(number == '0')
+        if(number == 0)
         {
             _make_command_byte(CB_INCR, VW0_ADDR, 0); 
             _nCs = 0;
@@ -73,7 +68,7 @@
    
     void MCP4261::dec(bool number)
     {
-        if(number == '0')
+        if(number == 0)
         {
             _make_command_byte(CB_DECR, VW0_ADDR, 0); 
             _nCs = 0;
@@ -92,70 +87,71 @@
     
     int MCP4261::status()
     {
-        _read(STATUS_ADDR);    
+        return _read(STATUS_ADDR);    
     }
     
     
     int MCP4261::tcon()
     {
-        _read(TCON_ADDR);    
+        return _read(TCON_ADDR);    
     }
     
    
     void MCP4261::tcon(int data)
     {
-        _write(TCON_ADDR, int data);    
+        _write(TCON_ADDR, data);    
     }
     
     
     int MCP4261::wiper(bool number)
     {
-        if(number == '0')
+        if(number == 0)
         {
-            _read(VW0_ADDR);
+            return _read(VW0_ADDR);
         }
         else
         {
-            _read(VW1_ADDR);
-        }     
+            return _read(VW1_ADDR);
+        }
+             
     }
     
     
     void MCP4261::wiper(bool number, int data)
     {
-        if(number == '0')
+        if(number == 0)
         {
-            _write(VW0_ADDR, int data);
+            _write(VW0_ADDR, data);
         }
         else
         {
-            _write(VW1_ADDR, int data);
+            _write(VW1_ADDR, data);
         }       
     }
     
     
     int MCP4261::nvwiper(bool number)
     {
-        if(number == '0')
+        if(number == 0)
         {
-            _read(NVW0_ADDR);
+            return _read(NVW0_ADDR);
         }
         else
         {
-            _read(NVW1_ADDR);
+            return _read(NVW1_ADDR);
         }     
     }
     
     
     void MCP4261::nvwiper(bool number, int data)
     {
-        if(number == '0')
+        if(number == 0)
         {
-            _write(NVW0_ADDR, int data);
+            _write(NVW0_ADDR, data);
         }
         else
         {
-            _write(NVW1_ADDR, int data);
+            _write(NVW1_ADDR, data);
         }    
     }
     
@@ -177,17 +173,19 @@
 
     char MCP4261::_make_command_byte(int com, char address, int data)
     {
-        if(data > 0xff && data < 0x3FF)
+        if(data > 0xff && data <= 0x3FF)
         {
-            _command_byte = address << 4;                   // add address to _commad_byte  
-            _command_byte = _command_byte | (data >> 8);    // add data to _commad_byte
-            _command_byte = _command_byte | (com << 2);     // add com to _commad_byte         
+            _command_byte = address << 4;                   // add address to _command_byte  
+            _command_byte = _command_byte | (data >> 8);    // add data to _command_byte
+            _command_byte = _command_byte | (com << 2);     // add com to _command_byte         
         }
         else
         {
-            _command_byte = address << 4;                   // add address to _commad_byte  
-            _command_byte = _command_byte | (com << 2);     // add com to _commad_byte      
-        }   
+            _command_byte = address << 4;                   // add address to _command_byte  
+            _command_byte = _command_byte | (com << 2);     // add com to _command_byte      
+        }
+        
+        return _command_byte;   
     }
     
     int MCP4261::_read(char address)                         
@@ -203,10 +201,8 @@
         _make_command_byte(CB_READ, address, 0);                       
         
         _nCs = 0;
-        _spi.write(_command_byte);
-        _spi.write(0xff);                                    // not important bit of the 16 bits
-        _response_msb = _spi.write();                        // get response
-        _response_lsb = _spi.write();                        // get response
+        _response_msb = _spi.write(_command_byte);
+        _response_lsb = _spi.write(0xff);                   // not important bit of the 16 bits
         _nCs = 1;
         
         _response = _response_msb << 8;
@@ -229,3 +225,4 @@
   
 
 
+