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

Files at this revision

API Documentation at this revision

Comitter:
stjo2809
Date:
Mon Mar 23 07:52:40 2015 +0000
Commit message:
revision 0.1

Changed in this revision

MCP4261.cpp Show annotated file Show diff for this revision Revisions of this file
MCP4261.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4261.cpp	Mon Mar 23 07:52:40 2015 +0000
@@ -0,0 +1,226 @@
+/* mbed MCP4261 Library, for driving the 8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory
+ * Copyright (c) 2015, Created by Steen Joergensen (stjo2809)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+ 
+ #include "mbed.h"
+ #include "MCP4261.h"
+ 
+//=============================================================================
+// Public functions
+//=============================================================================
+
+    MCP4261::MCP4261(SPI& spi, PinName nWP, PinName nSHDN, PinName nCs): _spi(spi), _nWP(nWP), _nSHDN(nSHDN), _nCs(nCs)
+    {
+                
+    }    
+    
+    MCP4261::MCP4261(PinName nWP, PinName nSHDN, PinName mosi, PinName miso, PinName sck, PinName nCs) : _nWP(nWP), _nSHDN(nSHDN), _mosi(mosi), _miso(miso), _sck(sck), _nCs(nCs)
+    {
+        SPI _spi(_mosi,_miso,_sck); 
+              
+    }
+        
+
+    int MCP4261::read(char address)
+    {
+        _read(char address);    
+    }
+
+   
+    void MCP4261::write(char address, int data)
+    {
+        _write(char address, int data);
+    }
+    
+    void MCP4261::inc(bool number)
+    {
+        if(number == '0')
+        {
+            _make_command_byte(CB_INCR, VW0_ADDR, 0); 
+            _nCs = 0;
+            _spi.write(_command_byte);
+            _nCs = 1;   
+        }
+        else
+        {
+            _make_command_byte(CB_INCR, VW1_ADDR, 0);
+            _nCs = 0;
+            _spi.write(_command_byte);
+            _nCs = 1;    
+        }    
+    }
+   
+    void MCP4261::dec(bool number)
+    {
+        if(number == '0')
+        {
+            _make_command_byte(CB_DECR, VW0_ADDR, 0); 
+            _nCs = 0;
+            _spi.write(_command_byte);
+            _nCs = 1;   
+        }
+        else
+        {
+            _make_command_byte(CB_DECR, VW1_ADDR, 0);
+            _nCs = 0;
+            _spi.write(_command_byte);
+            _nCs = 1;    
+        }     
+    }
+    
+    
+    int MCP4261::status()
+    {
+        _read(STATUS_ADDR);    
+    }
+    
+    
+    int MCP4261::tcon()
+    {
+        _read(TCON_ADDR);    
+    }
+    
+   
+    void MCP4261::tcon(int data)
+    {
+        _write(TCON_ADDR, int data);    
+    }
+    
+    
+    int MCP4261::wiper(bool number)
+    {
+        if(number == '0')
+        {
+            _read(VW0_ADDR);
+        }
+        else
+        {
+            _read(VW1_ADDR);
+        }     
+    }
+    
+    
+    void MCP4261::wiper(bool number, int data)
+    {
+        if(number == '0')
+        {
+            _write(VW0_ADDR, int data);
+        }
+        else
+        {
+            _write(VW1_ADDR, int data);
+        }       
+    }
+    
+    
+    int MCP4261::nvwiper(bool number)
+    {
+        if(number == '0')
+        {
+            _read(NVW0_ADDR);
+        }
+        else
+        {
+            _read(NVW1_ADDR);
+        }     
+    }
+    
+    
+    void MCP4261::nvwiper(bool number, int data)
+    {
+        if(number == '0')
+        {
+            _write(NVW0_ADDR, int data);
+        }
+        else
+        {
+            _write(NVW1_ADDR, int data);
+        }    
+    }
+    
+    
+    void MCP4261::shdn(bool act)
+    {
+        _nSHDN = ~act;    
+    }
+    
+    
+    void MCP4261::wp(bool act)
+    {
+        _nWP = ~act;
+    }
+    
+//=============================================================================
+// Private functions
+//=============================================================================
+
+    char MCP4261::_make_command_byte(int com, char address, int data)
+    {
+        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         
+        }
+        else
+        {
+            _command_byte = address << 4;                   // add address to _commad_byte  
+            _command_byte = _command_byte | (com << 2);     // add com to _commad_byte      
+        }   
+    }
+    
+    int MCP4261::_read(char address)                         
+    {
+        int _response_msb;
+        int _response_lsb;
+        int _response;
+         
+        _response = 0;                                      // clear _response for old data
+        _response_msb = 0;                                  // clear _response_msb for old data
+        _response_lsb = 0;                                  // clear _response_lsb for old data
+        
+        _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
+        _nCs = 1;
+        
+        _response = _response_msb << 8;
+        _response = _response | _response_lsb;
+        
+        return _response;
+    }
+                                    
+    void MCP4261::_write(char address, int data)            
+    {
+        _make_command_byte(CB_WRITE, address, data);
+        int _send_data = data & 0xff;
+        
+        _nCs = 0;
+        _spi.write(_command_byte);
+        _spi.write(_send_data);
+        _nCs = 1;
+    }
+
+  
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4261.h	Mon Mar 23 07:52:40 2015 +0000
@@ -0,0 +1,186 @@
+/* mbed MCP4261 Library, for driving the 7/8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory
+ * Copyright (c) 2015, Created by Steen Joergensen (stjo2809)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+ 
+#include "mbed.h"
+
+#ifndef MBED_MCP4261_H
+#define MBED_MCP4261_H
+
+//=============================================================================
+// All The Addresses
+//=============================================================================
+
+#define TCON_ADDR       0x04       // Controls the state of each resistor network terminal connection.   
+#define STATUS_ADDR     0x05       // Status (STATUS) Register, This register contains 5 status bits. WiperLock bits, Shutdown bit, Write Protect bit, EEPROM write cycle.
+#define VW0_ADDR        0x02       // Volatile Wiper 0
+#define VW1_ADDR        0x03       // Volatile Wiper 1
+#define NVW0_ADDR       0x04       // Non Volatile Wiper 0
+#define NVW1_ADDR       0x05       // Non Volatile Wiper 1
+
+// DATA EEPROM locations has the address from 0x06 to 0x0F 
+
+//=============================================================================
+// Declaration of variables & custom #defines
+//=============================================================================
+
+#define CB_WRITE        0x00       // Device commad bit for WRITE
+#define CB_INCR         0x01       // Device commad bit for INCREMENT
+#define CB_DECR         0x02       // Device commad bit for DECREMENT
+#define CB_READ         0x03       // Device commad bit for READ
+
+//=============================================================================
+// Functions Declaration
+//=============================================================================
+
+/** Interface to the 7/8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory
+ *
+  *  Using the driver:
+ *   - remenber to setup SPI in main routine or use pins instance.
+ *
+ *  Defaults in this driver on start up:
+ *   - as default is HARDWARE WRITE PROTECT PIN "Off".
+ *   - as default is HARDWARE SHUTDOWN PIN  "Off".
+ *
+ */
+class MCP4261 {
+public:
+    /** Create an instance of the MCP4261 connected via specfied SPI instance.
+     *
+     * @param spi The mbed SPI instance (make in main routine)
+     * @param nWP The Hardware Write Protect Control pin.
+     * @param nSHDN The Shutdown pin.
+     * @param nCs The SPI chip select pin.
+     */
+    MCP4261(SPI& spi, PinName nWP, PinName nSHDN, PinName nCs);
+    
+    /** Create an instance of the MCP4261 connected with SPI pins.
+     *
+     * @param nWP The Hardware Write Protect Control pin.
+     * @param nSHDN The Shutdown pin.
+     * @param mosi The SPI Master Output, Slave Input pin.
+     * @param miso The SPI Master Input, Slave Output pin. 
+     * @param sck The SPI Serial Clock pin.
+     * @param nCs The SPI chip select pin.
+     */
+    MCP4261(PinName nWP, PinName nSHDN, PinName mosi, PinName miso,PinName sck, PinName nCs);
+    
+
+    /** Read an Address.
+     *
+     * @param address The selected register to read from.
+     * @return The 16 bits read.
+     */
+    int read(char address);
+
+    /** Write to Address.
+     *
+     * @param address The selected register to write to.
+     * @param data The 16 bits to write to the register
+     */
+    void write(char address, int data);
+    
+    /** Increment wiper.
+     *
+     * @param number The selected wiper to increment.
+     */
+    void inc(bool number);
+    
+    /** Decrement wiper.
+     *
+     * @param number The selected wiper to decrement.
+     */
+    void dec(bool number);
+    
+    /** Read the Status register.
+     *
+     * @return The 16 bits read.
+     */
+    int status();
+    
+    /** Read the tcon register.
+     *
+     * @return The 16 bits read.
+     */
+    int tcon();
+    
+    /** write to tcon register.
+     *
+     * @param data The 16 bits to write to the register
+     */
+    void tcon(int data);
+    
+    /** Read the Volatile Wiper.
+     *
+     * @param number The wiper number = '0' or '1'
+     * @return The 16 bits read.
+     */
+    int wiper(bool number);
+    
+    /** write to Volatile Wiper.
+     *
+     * @param number The wiper number = '0' or '1'
+     * @param data The 16 bits to write to the register
+     */
+    void wiper(bool number, int data);
+    
+    /** Read the non-volatile wiper (Power On Reset start value).
+     *
+     * @param number The wiper number = '0' or '1'
+     * @return The 16 bits read.
+     */
+    int nvwiper(bool number);
+    
+    /** write to non-volatile wiper (Power On Reset start value).
+     *
+     * @param number The wiper number = '0' or '1'
+     * @param data The 16 bits to write to the register
+     */
+    void nvwiper(bool number, int data);
+    
+    /** HARDWARE SHUTDOWN PIN (SHDN)
+     *
+     * @param act SHDN is Active = true and Inactive = false
+     */
+    void shdn(bool act);
+    
+    /** HARDWARE WRITE PROTECT PIN (WP)
+     *
+     * @param act WP is Active = true and Inactive = false
+     */
+    void wp(bool act);
+  
+
+private:
+    SPI& _spi;
+    DigitalOut _nWP;
+    DigitalOut _nSHDN;
+    DigitalOut _nCs;
+        
+    char _command_byte;
+    
+    char _make_command_byte(int com, char address, int data);
+    int _read(char address);                          
+    void _write(char address, int data);             
+
+};
+
+#endif
\ No newline at end of file