Steven Beard / MCP4822A

Dependents:   MCP4822_demo MCP4822_SinewaveV2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP4822A.h Source File

MCP4822A.h

00001 /*
00002  * MCP4822A - DAC array library.
00003  *
00004  * Copyright (c) 2011 Steven Beard, UK Astronomy Technology Centre.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  */
00024 
00025 #include "mbed.h"
00026 
00027 #ifndef MCP4822_H
00028 #define MCP4822_H
00029 
00030 /* Reference: Microchip Technology (2005), MCP4821/MCP4822 DAC Data Sheet. */
00031 
00032 // MCP4822 reference voltage.
00033 #define MCP4822_VREF     2048    // Reference voltage (mV)
00034 
00035 /* Define possible combinations of 16-bit command register bits */
00036 #define MCP4822_REG_A1   0x3000  // Channel A gain 1
00037 #define MCP4822_REG_A2   0x1000  // Channel A gain 2
00038 #define MCP4822_REG_B1   0xB000  // Channel B gain 1
00039 #define MCP4822_REG_B2   0x9000  // Channel B gain 2
00040 #define MCP4822_REG_SHDN 0x0000  // Output power down
00041 
00042 /*+
00043  * Interface to an array of MCP4822 12-bit dual-output DACs daisy chained
00044  * on an SPI bus, with each DAC selected using an array of DigitalOut pins.
00045  * All the DACs may be latched together using a common nLDAC pin.
00046  *
00047  *       +-----------------------------------+
00048  *       |+------------------------+         |
00049  *       ||+-------------+         |         |
00050  *       |||            nCS1      nCS2      nCS3
00051  *    +--+++-+           |         |         |
00052  *    |      |         +-+----+  +-+----+  +-+----+
00053  *    | mbed +---SPI---+ DAC1 +--+ DAC2 +--+ DAC3 +--etc...
00054  *    |      |         +-+----+  +-+----+  +-+----+
00055  *    +---+--+           |         |         |
00056  *        |            nLDAC     nLDAC     nLDAC
00057  *        +--------------+---------+---------+
00058  *-
00059  */
00060 class MCP4822A {
00061 public:
00062     /*+
00063      * Constructor: MCP4822A
00064      *
00065      * Description:
00066      *    A class which describes an array of MCP4822 DACs connected in
00067      *    a daisy chain with an SPI interface. Each DAC is selected using
00068      *    its own separate "not CS" pin and all the DACs may be latched
00069      *    together using a single "not LDAC" pin.
00070      *
00071      * Parameters:
00072      *    ndacs, int
00073      *        The number of DAC chips included in the array. Must be at
00074      *        least 1. (Limited by the number of "not CS" pins available.)
00075      *    mosi, PinName
00076      *        The SPI data out pin.
00077      *    sclk, PinName
00078      *        The SPI clock pin.
00079      *    ncslist, PinName[]
00080      *        An array of "not chip select" ("not CS") pins - one per DAC
00081      *        chip. Each pin is held low to make the DAC respond to SPI
00082      *        input. The array must contain at least ndacs elements (only
00083      *        the first ndacs elements will be used).
00084      *    nldac, PinName (optional)
00085      *        The "not latch DAC" ("not LDAC") pin. Setting this pin low
00086      *        causes each DAC to apply the preset A and B voltages to its
00087      *        outputs. The pin is optional. The DAC will apply new voltages
00088      *        immediately if the "not LDAC" pin is held permanently to
00089      *        ground. This parameter is optional, and if not given will be
00090      *        assumed NC (not connected).
00091      *-
00092      */
00093     MCP4822A ( int ndacs, PinName mosi, PinName sclk, PinName ncslist[], PinName nldac=NC );
00094 
00095     /*+
00096      * Destructor: MCP4822A
00097      *-
00098      */
00099     ~MCP4822A();
00100 
00101     /*+
00102      * frequency: Set the SPI bus clock frequency in Hz.
00103      *
00104      * Parameters:
00105      *    freq, int
00106      *        The SPI bus frequency in Hz. Must be within the range
00107      *        supported by both the SPI interface and the DAC chips
00108      *        (~10 KHz to 20 MHz).
00109      *-
00110      */
00111     void frequency( int freq );
00112 
00113     /*+
00114      * writeA1: Write 12-bit data to channel A with gain set to 1.
00115      * writeA2: Write 12-bit data to channel A with gain set to 2.
00116      * writeB1: Write 12-bit data to channel B with gain set to 1.
00117      * writeB2: Write 12-bit data to channel B with gain set to 2.
00118      *
00119      * These functions will automatically select the chip with the "not CS"
00120      * signal. If the "not LDAC" pin is used, it is up to the caller to
00121      * enable or disable that signal. For example:
00122      *
00123      *     latch_disable();
00124      *     writeA2( 1500 );
00125      *     latch_enable();
00126      *
00127      * Choose these functions when high performance is most important.
00128      *
00129      * Parameters:
00130      *    dac, int
00131      *        The DAC chip to be addressed, corresponding to the elements
00132      *        of the ncslist[] array used to create the MCP4822A object.
00133      *        Must be in the range 0 to ndacs-1.
00134      *    value, int
00135      *        The number to be written to the 12 data bits of the A or B
00136      *        register of the DAC chip. At a gain setting of 2, this value
00137      *        corresponds to a voltage demand in millivolts. Only the first
00138      *        12 bits are used, so the value will wrap around after 4095.
00139      *        (Note: Strictly this should be an unsigned int, but the SPI
00140      *        interface library expects an int.)
00141      *-
00142      */
00143     void writeA1( int dac, int value );
00144     void writeA2( int dac, int value );
00145     void writeB1( int dac, int value );
00146     void writeB2( int dac, int value );
00147 
00148     /*+
00149      * write: Write an array of 12-bit register values to multiple DAC channels.
00150      *
00151      * Choose this function when flexibility and convenience are most important.
00152      *
00153      * Parameters:
00154      *    nchans, int
00155      *        The number of channels to be written. There are two channels
00156      *        per DAC, so this value must lie between 2 and ndacs x 2.
00157      *    values, int[]
00158      *        An array of values to be written to the 12 data bits of the DAC
00159      *        registers, in the order dac/chan = 0/A, 0/B, 1/A, 1/B, etc...
00160      *        The array must contain at least nchans elements (but only the
00161      *        first nchans elements will be used for longer arrays).
00162      *        At a gain setting of 2, these values corresponds to a voltage
00163      *        demand in millivolts. They will wrap back to 0 above 4095.
00164      *        (Note: Strictly this should be an unsigned int array, but the SPI
00165      *        interface library expects an int.)
00166      *    gain, int (optional)
00167      *        The required gain setting (1 or 2). If not specified, a default
00168      *        of 2 will be assumed.
00169      *    latch, int (optional)
00170      *        A flag set to 1 if the "not LDAC" signal should be latched after
00171      *        setting up the array of voltages. If not specified, a default
00172      *        of 1 will be assumed.
00173      *-
00174      */
00175     void write( int nchans, int values[], int gain=2, int latch=1 );
00176 
00177     /*+
00178      * voltage2value - convert a voltage into a 12-bit data value.
00179      *
00180      * Parameters:
00181      *    voltage, float
00182      *        The voltage to be converted (in volts). The sign is ignored.
00183      *    gain, int (optional)
00184      *        The gain setting required (1 or 2). If not specified, a default
00185      *        of 2 will be assumed.
00186      *
00187      * Returns:
00188      *    value, int.
00189      *        The 12-bit data value corresponding to the given
00190      *        voltage. 4095 will be returned for out of range
00191      *        voltages.
00192      *-
00193      */
00194     int voltage2value( float voltage, int gain=2 );
00195 
00196     /*+
00197      * value2voltage - convert a 12-bit data value into a voltage.
00198      *
00199      * Parameters:
00200      *    value, int
00201      *        The 12-bit data value to be converted to a voltage.
00202      *    gain, int (optional)
00203      *        The gain setting required (1 or 2). If not specified, a default
00204      *        of 2 will be assumed.
00205      *
00206      * Returns:
00207      *    voltage, float.
00208      *        The voltage (in volts) corresponding the the given 12-bit
00209      *        data value at the given gain setting.
00210      *-
00211      */
00212     float value2voltage( int value, int gain=2 );
00213 
00214     /*+
00215      * latch_enable: enable the latch signal - the DAC(s) will apply
00216      *     voltage demands.
00217      *
00218      * latch_disable: disable the latch signal - the DAC(s) will not
00219      *     apply voltage demands.
00220      *
00221      * These functions only apply for objects constructed with the
00222      * optional nldac parameter assigned to a pin.
00223      *-
00224      */
00225     void latch_enable();
00226     void latch_disable();
00227 
00228     /*+
00229      * shdn: Shut down a DAC. It will sleep until woken by a new command
00230      * over the SPI bus.
00231      *-
00232      */
00233     void shdn(int dac);
00234     void shdn_all();
00235 
00236 private:
00237     // Make copy constructor private to prevent the object being copied.
00238     // The pins are available to one and only one object.
00239     MCP4822A( const MCP4822A& rhs );
00240 
00241     /* Initialise the DAC interface. */
00242     void _init();
00243 
00244     int _ndacs;              // The number of DACS in the array
00245     int _latched;            // Is the "not LDAC" pin used (1=yes; 0=no)?
00246     SPI _spi;                // SPI bus object for communicating with DAC.
00247     DigitalOut** _ncs_array; // Array of pointers to DigitalOut objects
00248     // connected to "not CS" pins.
00249     DigitalOut* _nldac;      // Pointer to DigitalOut object connected
00250     // to "not LDAC" pin (if any - NULL if none).
00251 };
00252 
00253 #endif