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