Steen Jørgensen / MCP4261

Fork of MCP4261 by TeamElectronics

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP4261.h Source File

MCP4261.h

00001 /* mbed MCP4261 Library, for driving the 7/8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory
00002  * Copyright (c) 2015, Created by Steen Joergensen (stjo2809)
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022  
00023 #include "mbed.h"
00024 
00025 #ifndef MBED_MCP4261_H
00026 #define MBED_MCP4261_H
00027 
00028 //=============================================================================
00029 // All The Addresses
00030 //=============================================================================
00031 
00032 #define TCON_ADDR       0x04       // Controls the state of each resistor network terminal connection.   
00033 #define STATUS_ADDR     0x05       // Status (STATUS) Register, This register contains 5 status bits. WiperLock bits, Shutdown bit, Write Protect bit, EEPROM write cycle.
00034 #define VW0_ADDR        0x00       // Volatile Wiper 0
00035 #define VW1_ADDR        0x01       // Volatile Wiper 1
00036 #define NVW0_ADDR       0x02       // Non Volatile Wiper 0
00037 #define NVW1_ADDR       0x03       // Non Volatile Wiper 1
00038 
00039 // DATA EEPROM locations has the address from 0x06 to 0x0F 
00040 
00041 //=============================================================================
00042 // Declaration of variables & custom #defines
00043 //=============================================================================
00044 
00045 #define CB_WRITE        0x00       // Device commad bit for WRITE
00046 #define CB_INCR         0x01       // Device commad bit for INCREMENT
00047 #define CB_DECR         0x02       // Device commad bit for DECREMENT
00048 #define CB_READ         0x03       // Device commad bit for READ
00049 
00050 //=============================================================================
00051 // Functions Declaration
00052 //=============================================================================
00053 
00054 /** Interface to the 7/8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory
00055  *
00056   *  Using the driver:
00057  *   - remenber to setup SPI in main routine or use pins instance.
00058  *
00059  *  Defaults in this driver on start up:
00060  *   - as default is HARDWARE WRITE PROTECT PIN "Off".
00061  *   - as default is HARDWARE SHUTDOWN PIN  "Off".
00062  *
00063  */
00064 class MCP4261 {
00065 public:
00066     /** Create an instance of the MCP4261 connected via specfied SPI instance.
00067      *
00068      * @param spi The mbed SPI instance (make in main routine)
00069      * @param nWP The Hardware Write Protect Control pin.
00070      * @param nSHDN The Shutdown pin.
00071      * @param nCs The SPI chip select pin.
00072      */
00073     MCP4261(SPI& spi, PinName nCs, PinName nWP, PinName nSHDN);
00074     
00075     /** Create an instance of the MCP4261 connected via specfied SPI instance (use wthen SHDN is linked to Global reset).
00076      *
00077      * @param spi The mbed SPI instance (make in main routine)
00078      * @param nWP The Hardware Write Protect Control pin.
00079      * @param nCs The SPI chip select pin.
00080      */
00081     MCP4261(SPI& spi, PinName nCs, PinName nWP);
00082     
00083 
00084     /** Read an Address.
00085      *
00086      * @param address The selected register to read from.
00087      * @return The 16 bits read.
00088      */
00089     int read(char address);
00090 
00091     /** Write to Address.
00092      *
00093      * @param address The selected register to write to.
00094      * @param data The 16 bits to write to the register
00095      */
00096     void write(char address, int data);
00097     
00098     /** Increment wiper.
00099      *
00100      * @param number The selected wiper to increment.
00101      */
00102     void inc(bool number);
00103     
00104     /** Decrement wiper.
00105      *
00106      * @param number The selected wiper to decrement.
00107      */
00108     void dec(bool number);
00109     
00110     /** Read the Status register.
00111      *
00112      * @return The 16 bits read.
00113      */
00114     int status();
00115     
00116     /** Read the tcon register.
00117      *
00118      * @return The 16 bits read.
00119      */
00120     int tcon();
00121     
00122     /** write to tcon register.
00123      *
00124      * @param data The 16 bits to write to the register
00125      */
00126     void tcon(int data);
00127     
00128     /** Read the Volatile Wiper.
00129      *
00130      * @param number The wiper number = '0' or '1'
00131      * @return The 16 bits read.
00132      */
00133     int wiper(bool number);
00134     
00135     /** write to Volatile Wiper.
00136      *
00137      * @param number The wiper number = '0' or '1'
00138      * @param data The 16 bits to write to the register
00139      */
00140     void wiper(bool number, int data);
00141     
00142     /** Read the non-volatile wiper (Power On Reset start value).
00143      *
00144      * @param number The wiper number = '0' or '1'
00145      * @return The 16 bits read.
00146      */
00147     int nvwiper(bool number);
00148     
00149     /** write to non-volatile wiper (Power On Reset start value).
00150      *
00151      * @param number The wiper number = '0' or '1'
00152      * @param data The 16 bits to write to the register
00153      */
00154     void nvwiper(bool number, int data);
00155     
00156     /** HARDWARE SHUTDOWN PIN (SHDN)
00157      *
00158      * @param act SHDN is Active = true and Inactive = false
00159      */
00160     void shdn(bool act);
00161     
00162     /** HARDWARE WRITE PROTECT PIN (WP)
00163      *
00164      * @param act WP is Active = true and Inactive = false
00165      */
00166     void wp(bool act);
00167   
00168 
00169 private:
00170     SPI& _spi;
00171     DigitalOut _nCs;
00172     DigitalOut _nWP;
00173     DigitalOut _nSHDN;
00174     
00175         
00176     char _command_byte;
00177     
00178     char _make_command_byte(int com, char address, int data);
00179     int _read(char address);                          
00180     void _write(char address, int data);             
00181 
00182 };
00183 
00184 #endif