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