12-Bit DAC with internal Vref and SPI interface

Fork of MCP4822 by TeamElectronics

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP4822.cpp Source File

MCP4822.cpp

00001 /* mbed MCP4822 Library, for driving the 12-Bit DAC with internal Vref and SPI interface
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 "MCP4822.h"
00025  
00026 //=============================================================================
00027 // Public functions
00028 //=============================================================================
00029 
00030     MCP4822::MCP4822(SPI& spi, PinName nCs, PinName nLDAC) : _spi(spi), _nCs(nCs), _nLDAC(nLDAC)
00031     {
00032         _nCs = 1; 
00033     }
00034     
00035     void MCP4822::a(bool gain, int data)
00036     {
00037         _write(0, gain, 1, data);    
00038     }
00039     
00040     void MCP4822::b(bool gain, int data)
00041     {
00042         _write(1, gain, 1, data);     
00043     }
00044    
00045     void MCP4822::ldac(bool act)
00046     {
00047         _nLDAC = ~act;    
00048     }
00049     
00050     bool MCP4822::ldac()
00051     {
00052         return _nLDAC;    
00053     }
00054     
00055     void MCP4822::shdn(bool output, bool act)
00056     {
00057         _write(output, 1, ~act, 0x00);
00058         _SHDN_STATUS = act;    
00059     }
00060     
00061     bool MCP4822::shdn()
00062     {
00063         return _SHDN_STATUS;    
00064     }
00065     
00066 //=============================================================================
00067 // Private functions
00068 //=============================================================================
00069 
00070     char MCP4822::_make_upper_half(bool output, bool gain, bool shdn, int data)
00071     {
00072         char responce = 0;
00073         
00074         if(output == 0)
00075         {
00076             responce = responce | CB_OUTPUT_A;        
00077         }
00078         else
00079         {
00080             responce = responce | CB_OUTPUT_B;      
00081         }
00082         
00083         if(gain == 0)
00084         {
00085             responce = responce | CB_GAIN_2X;        
00086         }
00087         else
00088         {
00089             responce = responce | CB_GAIN_1X;      
00090         }
00091         
00092         if(shdn == 0)
00093         {
00094             responce = responce | CB_NSHDN;        
00095         }
00096         else
00097         {
00098             responce = responce | CB_SHDN;      
00099         }
00100         
00101         if(data > 0xff && data <= 0xfff)
00102         {
00103             responce = responce | (data >> 8);        
00104         }
00105         
00106         return responce;     
00107     }
00108     
00109     char MCP4822::_make_lower_half(int data)
00110     {
00111       return (data & 0xff);    
00112     }
00113     
00114     void MCP4822::_write(bool output, bool gain, bool shdn, int data)  
00115     {
00116         
00117         _upper_half = _make_upper_half(output, gain, shdn, data);
00118         _lower_half = _make_lower_half(data);
00119         
00120         _nCs = 0;
00121         _spi.write(_upper_half);
00122         _spi.write(_lower_half);
00123         _nCs = 1;    
00124     }