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.h Source File

MCP4822.h

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 
00025 #ifndef MBED_MCP4822_H
00026 #define MBED_MCP4822_H
00027 
00028 //=============================================================================
00029 // Declaration of variables & custom #defines
00030 //=============================================================================
00031 
00032 #define CB_OUTPUT_A     0x00       // command bit for output A
00033 #define CB_OUTPUT_B     0x80       // command bit for output A
00034 #define CB_GAIN_1X      0x20       // command bit for gain 1x
00035 #define CB_GAIN_2X      0x00       // command bit for gain 2x
00036 #define CB_SHDN         0x10       // command bit for Output enabled
00037 #define CB_NSHDN        0x00       // command bit for Output buffer disabled, output is high-impedance
00038 
00039 //=============================================================================
00040 // Functions Declaration
00041 //=============================================================================
00042 
00043 /** Interface to the 12-Bit DAC with internal Vref and SPI interface
00044  *
00045   *  Using the driver:
00046  *   - remenber to setup SPI in main routine or use pins instance.
00047  *
00048  *  Defaults in this driver on start up:
00049  *   - Datasheet start up
00050  */
00051 class MCP4822 {
00052 public:
00053     /** Create an instance of the MCP4822 connected via specfied SPI instance.
00054      *
00055      * @param spi The mbed SPI instance (make in main routine)
00056      * @param nLDAC The Latch DAC Synchronization pin 
00057      * @param nCs The SPI chip select pin.
00058      */
00059     MCP4822(SPI& spi, PinName nCs, PinName nLDAC);
00060     
00061     /** Write to output A.
00062      *
00063      * @param gain the gain is 2x = '0' and 1x = '1'.
00064      * @param data The 12 bits value to write to the output.
00065      */
00066     void a(bool gain, int data);
00067     
00068     /** Write to output B.
00069      *
00070      * @param gain the gain is 2x = '0' and 1x = '1'.
00071      * @param data The 12 bits value to write to the output.
00072      */
00073     void b(bool gain, int data);
00074     
00075     /** Output Synchronization.
00076      *
00077      * @param act The LDAC is Active = true and Inactive = false.
00078      */
00079     void ldac(bool act);
00080     
00081     /** Output Synchronization.
00082      *
00083      * @return The status the LDAC.
00084      */
00085     bool ldac();
00086     
00087     /** Output Shutdown.
00088      *
00089      * @param output A = '0' and B = '1'.
00090      * @param act The SHDN is Active = true and Inactive = false.
00091      */
00092     void shdn(bool output, bool act);
00093     
00094     /** Output Shutdown.
00095      *
00096      * @return The status the SHDN. 
00097      */
00098     bool shdn();
00099     
00100   
00101 
00102 private:
00103     SPI& _spi;
00104     DigitalOut _nCs;
00105     DigitalOut _nLDAC;
00106     bool _SHDN_STATUS;
00107     
00108     char _upper_half;
00109     char _lower_half;
00110     
00111     char _make_upper_half(bool output, bool gain, bool shdn, int data);
00112     char _make_lower_half(int data);
00113     void _write(bool output, bool gain, bool shdn, int data);             
00114 
00115 };
00116 
00117 #endif