LabsDigital / Mbed 2 deprecated Lab5_MCP4922

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP4922.h Source File

MCP4922.h

00001 /*
00002  * MCP4922 - DAC 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 MCP4922_H
00028 #define MCP4922_H
00029 
00030 /* Reference: Microchip Technology (2005), MCP4821/MCP4822 DAC Data Sheet. */
00031 
00032 // MCP4922 reference voltage.
00033 //#define MCP4922_VREF     2048    // Reference voltage (mV)
00034 #define MCP4922_VREF     3300    // Reference voltage (mV)
00035 
00036 /* Define possible combinations of 16-bit command register bits */
00037 #define MCP4922_REG_A1   0x3000  // Channel A gain 1
00038 #define MCP4922_REG_B1   0xB000  // Channel B gain 1
00039 #define MCP4922_REG_SHDN 0x0000  // Output power down
00040 
00041 class MCP4922 {
00042 public:
00043 
00044     MCP4922 (PinName mosi, PinName sclk, PinName cs);
00045 
00046     ~MCP4922();
00047 
00048     /*+
00049      * frequency: Set the SPI bus clock frequency in Hz.
00050      *        The SPI bus frequency in Hz. Must be within the range
00051      *        supported by both the SPI interface and the DAC chips
00052      *        (~10 KHz to 20 MHz).
00053      */
00054     void frequency( int freq );
00055     
00056     void writeA(int value );
00057     
00058     void writeB(int value );
00059 
00060     void write( int nchans, int values[], int gain=2, int latch=1 );
00061 
00062     void latch_enable();
00063     
00064     void latch_disable();
00065 
00066 private:
00067     
00068     MCP4922( const MCP4922& rhs );
00069     
00070     void _init();
00071 
00072     int _ndacs;              // The number of DACS in the array
00073     int _latched;            // Is the "not LDAC" pin used (1=yes; 0=no)?
00074     SPI _spi;                // SPI bus object for communicating with DAC.
00075     DigitalOut** _ncs_array; // Array of pointers to DigitalOut objects
00076     // connected to "not CS" pins.
00077     DigitalOut* _nldac;      // Pointer to DigitalOut object connected
00078     // to "not LDAC" pin (if any - NULL if none).
00079 };
00080 
00081 #endif