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