Steen Jørgensen / MCP4261

Fork of MCP4261 by TeamElectronics

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP4261.cpp Source File

MCP4261.cpp

00001 /* mbed MCP4261 Library, for driving the 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  #include "MCP4261.h"
00025  
00026 //=============================================================================
00027 // Public functions
00028 //=============================================================================
00029 
00030     MCP4261::MCP4261(SPI& spi, PinName nCs, PinName nWP, PinName nSHDN): _spi(spi), _nCs(nCs), _nWP(nWP), _nSHDN(nSHDN) 
00031     {
00032         _nCs = 1;            
00033     }    
00034     
00035     MCP4261::MCP4261(SPI& spi, PinName nCs, PinName nWP): _spi(spi), _nCs(nCs), _nWP(nWP), _nSHDN(NC) 
00036     {
00037         _nCs = 1;           
00038     }    
00039     
00040     int MCP4261::read(char address)
00041     {
00042         return _read(address);    
00043     }
00044 
00045    
00046     void MCP4261::write(char address, int data)
00047     {
00048         _write(address, data);
00049     }
00050     
00051     void MCP4261::inc(bool number)
00052     {
00053         if(number == 0)
00054         {
00055             _make_command_byte(CB_INCR, VW0_ADDR, 0); 
00056             _nCs = 0;
00057             _spi.write(_command_byte);
00058             _nCs = 1;   
00059         }
00060         else
00061         {
00062             _make_command_byte(CB_INCR, VW1_ADDR, 0);
00063             _nCs = 0;
00064             _spi.write(_command_byte);
00065             _nCs = 1;    
00066         }    
00067     }
00068    
00069     void MCP4261::dec(bool number)
00070     {
00071         if(number == 0)
00072         {
00073             _make_command_byte(CB_DECR, VW0_ADDR, 0); 
00074             _nCs = 0;
00075             _spi.write(_command_byte);
00076             _nCs = 1;   
00077         }
00078         else
00079         {
00080             _make_command_byte(CB_DECR, VW1_ADDR, 0);
00081             _nCs = 0;
00082             _spi.write(_command_byte);
00083             _nCs = 1;    
00084         }     
00085     }
00086     
00087     
00088     int MCP4261::status()
00089     {
00090         return _read(STATUS_ADDR);    
00091     }
00092     
00093     
00094     int MCP4261::tcon()
00095     {
00096         return _read(TCON_ADDR);    
00097     }
00098     
00099    
00100     void MCP4261::tcon(int data)
00101     {
00102         _write(TCON_ADDR, data);    
00103     }
00104     
00105     
00106     int MCP4261::wiper(bool number)
00107     {
00108         if(number == 0)
00109         {
00110             return _read(VW0_ADDR);
00111         }
00112         else
00113         {
00114             return _read(VW1_ADDR);
00115         }
00116              
00117     }
00118     
00119     
00120     void MCP4261::wiper(bool number, int data)
00121     {
00122         if(number == 0)
00123         {
00124             _write(VW0_ADDR, data);
00125         }
00126         else
00127         {
00128             _write(VW1_ADDR, data);
00129         }       
00130     }
00131     
00132     
00133     int MCP4261::nvwiper(bool number)
00134     {
00135         if(number == 0)
00136         {
00137             return _read(NVW0_ADDR);
00138         }
00139         else
00140         {
00141             return _read(NVW1_ADDR);
00142         }     
00143     }
00144     
00145     
00146     void MCP4261::nvwiper(bool number, int data)
00147     {
00148         if(number == 0)
00149         {
00150             _write(NVW0_ADDR, data);
00151         }
00152         else
00153         {
00154             _write(NVW1_ADDR, data);
00155         }    
00156     }
00157     
00158     
00159     void MCP4261::shdn(bool act)
00160     {
00161         _nSHDN = ~act;    
00162     }
00163     
00164     
00165     void MCP4261::wp(bool act)
00166     {
00167         _nWP = ~act;
00168     }
00169     
00170 //=============================================================================
00171 // Private functions
00172 //=============================================================================
00173 
00174     char MCP4261::_make_command_byte(int com, char address, int data)
00175     {
00176         if(data > 0xff && data <= 0x3FF)
00177         {
00178             _command_byte = address << 4;                   // add address to _command_byte  
00179             _command_byte = _command_byte | (data >> 8);    // add data to _command_byte
00180             _command_byte = _command_byte | (com << 2);     // add com to _command_byte         
00181         }
00182         else
00183         {
00184             _command_byte = address << 4;                   // add address to _command_byte  
00185             _command_byte = _command_byte | (com << 2);     // add com to _command_byte      
00186         }
00187         
00188         return _command_byte;   
00189     }
00190     
00191     int MCP4261::_read(char address)                         
00192     {
00193         int _response_msb;
00194         int _response_lsb;
00195         int _response;
00196          
00197         _response = 0;                                      // clear _response for old data
00198         _response_msb = 0;                                  // clear _response_msb for old data
00199         _response_lsb = 0;                                  // clear _response_lsb for old data
00200         
00201         _make_command_byte(CB_READ, address, 0);                       
00202         
00203         _nCs = 0;
00204         _response_msb = _spi.write(_command_byte);
00205         _response_lsb = _spi.write(0xff);                   // not important bit of the 16 bits
00206         _nCs = 1;
00207         
00208         _response = _response_msb << 8;
00209         _response = _response | _response_lsb;
00210         
00211         return _response;
00212     }
00213                                     
00214     void MCP4261::_write(char address, int data)            
00215     {
00216         _make_command_byte(CB_WRITE, address, data);
00217         int _send_data = data & 0xff;
00218         
00219         _nCs = 0;
00220         _spi.write(_command_byte);
00221         _spi.write(_send_data);
00222         _nCs = 1;
00223     }
00224 
00225   
00226 
00227 
00228