This is a program to output a sinwave in 12 bits Digital to Analog converter MCP4922. Sampling frequency is 166,667Hz and produce an interrupt of Ticker every 6uS. The Program calculates the data of the sinewave beforehand.

Dependencies:   TextLCD mbed

Committer:
jf1vrr
Date:
Thu May 12 11:12:47 2011 +0000
Revision:
1:09b69f38fc73
Parent:
0:5737b1972549
Rev, 0.01A New 2011/05/11

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jf1vrr 0:5737b1972549 1 /*
jf1vrr 0:5737b1972549 2 * MCP4922 - DAC library.
jf1vrr 0:5737b1972549 3 *
jf1vrr 0:5737b1972549 4 * Copyright (c) 2011 Steven Beard, UK Astronomy Technology Centre.
jf1vrr 0:5737b1972549 5 *
jf1vrr 0:5737b1972549 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
jf1vrr 0:5737b1972549 7 * of this software and associated documentation files (the "Software"), to deal
jf1vrr 0:5737b1972549 8 * in the Software without restriction, including without limitation the rights
jf1vrr 0:5737b1972549 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jf1vrr 0:5737b1972549 10 * copies of the Software, and to permit persons to whom the Software is
jf1vrr 0:5737b1972549 11 * furnished to do so, subject to the following conditions:
jf1vrr 0:5737b1972549 12 *
jf1vrr 0:5737b1972549 13 * The above copyright notice and this permission notice shall be included in
jf1vrr 0:5737b1972549 14 * all copies or substantial portions of the Software.
jf1vrr 0:5737b1972549 15 *
jf1vrr 0:5737b1972549 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jf1vrr 0:5737b1972549 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jf1vrr 0:5737b1972549 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jf1vrr 0:5737b1972549 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jf1vrr 0:5737b1972549 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jf1vrr 0:5737b1972549 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jf1vrr 0:5737b1972549 22 * THE SOFTWARE.
jf1vrr 0:5737b1972549 23 */
jf1vrr 0:5737b1972549 24
jf1vrr 0:5737b1972549 25 #include "mbed.h"
jf1vrr 0:5737b1972549 26
jf1vrr 0:5737b1972549 27 #ifndef MCP4922_H
jf1vrr 0:5737b1972549 28 #define MCP4922_H
jf1vrr 0:5737b1972549 29
jf1vrr 0:5737b1972549 30 /* Reference: Microchip Technology (2005), MCP4821/MCP4822 DAC Data Sheet. */
jf1vrr 0:5737b1972549 31
jf1vrr 0:5737b1972549 32 // MCP4922 reference voltage.
jf1vrr 0:5737b1972549 33 #define MCP4922_VREF 2048 // Reference voltage (mV)
jf1vrr 0:5737b1972549 34 //#define MCP4922_VREF 3300 // Reference voltage (mV)
jf1vrr 0:5737b1972549 35
jf1vrr 0:5737b1972549 36 /* Define possible combinations of 16-bit command register bits */
jf1vrr 0:5737b1972549 37 #define MCP4922_REG_A1 0x3000 // Channel A gain 1
jf1vrr 0:5737b1972549 38 #define MCP4922_REG_B1 0xB000 // Channel B gain 1
jf1vrr 0:5737b1972549 39 #define MCP4922_REG_SHDN 0x0000 // Output power down
jf1vrr 0:5737b1972549 40
jf1vrr 0:5737b1972549 41 class MCP4922 {
jf1vrr 0:5737b1972549 42 public:
jf1vrr 0:5737b1972549 43
jf1vrr 0:5737b1972549 44 MCP4922 (PinName mosi, PinName sclk, PinName cs);
jf1vrr 0:5737b1972549 45
jf1vrr 0:5737b1972549 46 ~MCP4922();
jf1vrr 0:5737b1972549 47
jf1vrr 0:5737b1972549 48 /*+
jf1vrr 0:5737b1972549 49 * frequency: Set the SPI bus clock frequency in Hz.
jf1vrr 0:5737b1972549 50 * The SPI bus frequency in Hz. Must be within the range
jf1vrr 0:5737b1972549 51 * supported by both the SPI interface and the DAC chips
jf1vrr 0:5737b1972549 52 * (~10 KHz to 20 MHz).
jf1vrr 0:5737b1972549 53 */
jf1vrr 0:5737b1972549 54 void frequency( int freq );
jf1vrr 0:5737b1972549 55
jf1vrr 0:5737b1972549 56 void writeA(int value );
jf1vrr 0:5737b1972549 57
jf1vrr 0:5737b1972549 58 void writeB(int value );
jf1vrr 0:5737b1972549 59
jf1vrr 0:5737b1972549 60 void write( int nchans, int values[], int gain=2, int latch=1 );
jf1vrr 0:5737b1972549 61
jf1vrr 0:5737b1972549 62 void latch_enable();
jf1vrr 0:5737b1972549 63
jf1vrr 0:5737b1972549 64 void latch_disable();
jf1vrr 0:5737b1972549 65
jf1vrr 0:5737b1972549 66 private:
jf1vrr 0:5737b1972549 67
jf1vrr 0:5737b1972549 68 MCP4922( const MCP4922& rhs );
jf1vrr 0:5737b1972549 69
jf1vrr 0:5737b1972549 70 void _init();
jf1vrr 0:5737b1972549 71
jf1vrr 0:5737b1972549 72 int _ndacs; // The number of DACS in the array
jf1vrr 0:5737b1972549 73 int _latched; // Is the "not LDAC" pin used (1=yes; 0=no)?
jf1vrr 0:5737b1972549 74 SPI _spi; // SPI bus object for communicating with DAC.
jf1vrr 0:5737b1972549 75 DigitalOut** _ncs_array; // Array of pointers to DigitalOut objects
jf1vrr 0:5737b1972549 76 // connected to "not CS" pins.
jf1vrr 0:5737b1972549 77 DigitalOut* _nldac; // Pointer to DigitalOut object connected
jf1vrr 0:5737b1972549 78 // to "not LDAC" pin (if any - NULL if none).
jf1vrr 0:5737b1972549 79 };
jf1vrr 0:5737b1972549 80
jf1vrr 0:5737b1972549 81 #endif