12-Bit DAC with internal Vref and SPI interface

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       // commad bit for output A
00033 #define CB_OUTPUT_B     0x80       // commad bit for output A
00034 #define CB_GAIN_1X      0x20       // commad bit for gain 1x
00035 #define CB_GAIN_2X      0x00       // commad bit for gain 2x
00036 #define CB_SHDN         0x10       // commad bit for Output enabled
00037 #define CB_NSHDN        0x00       // commad 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 nLDAC, PinName nCs);
00060     
00061     /** Create an instance of the MCP4261 connected with SPI pins.
00062      *
00063      * @param nLDAC The Latch DAC Synchronization pin 
00064      * @param mosi The SPI Master Output, Slave Input pin.
00065      * @param miso The SPI Master Input, Slave Output pin. 
00066      * @param sck The SPI Serial Clock pin.
00067      * @param nCs The SPI chip select pin.
00068      */
00069     MCP4822(PinName nLDAC, PinName mosi, PinName miso,PinName sck, PinName nCs);
00070     
00071 
00072     /** Write to output A.
00073      *
00074      * @param gain the gain is 2x = '0' and 1x = '1'.
00075      * @param data The 12 bits value to write to the output.
00076      */
00077     void a(bool gain, int data);
00078     
00079     /** Write to output B.
00080      *
00081      * @param gain the gain is 2x = '0' and 1x = '1'.
00082      * @param data The 12 bits value to write to the output.
00083      */
00084     void b(bool gain, int data);
00085     
00086     /** Output Synchronization.
00087      *
00088      * @param act The LDAC is Active = true and Inactive = false.
00089      */
00090     void ldac(bool act);
00091     
00092     /** Output Shutdown.
00093      *
00094      * @param output A = '0' and B = '1'.
00095      * @param act The SHDN is Active = true and Inactive = false.
00096      */
00097     void shdn(bool output, bool act);
00098     
00099   
00100 
00101 private:
00102     SPI& _spi;
00103     DigitalOut _nCs;
00104     DigitalOut _nLDAC;
00105     
00106     char _upper_half;
00107     char _lower_half;
00108     
00109     char _make_upper_half(bool output, bool gain, bool shdn, int data);
00110     char _make_lower_half(int data);
00111     void _write(bool output, bool gain, bool shdn, int data);             
00112 
00113 };
00114 
00115 #endif