12-Bit DAC with internal Vref and SPI interface
Fork of MCP4822 by
MCP4822.h@1:d97fd6c6e2e4, 2016-01-20 (annotated)
- Committer:
- stjo2809
- Date:
- Wed Jan 20 10:55:01 2016 +0000
- Revision:
- 1:d97fd6c6e2e4
- Parent:
- 0:929babff65b1
tested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stjo2809 | 0:929babff65b1 | 1 | /* mbed MCP4822 Library, for driving the 12-Bit DAC with internal Vref and SPI interface |
stjo2809 | 0:929babff65b1 | 2 | * Copyright (c) 2015, Created by Steen Joergensen (stjo2809) |
stjo2809 | 0:929babff65b1 | 3 | * |
stjo2809 | 0:929babff65b1 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
stjo2809 | 0:929babff65b1 | 5 | * of this software and associated documentation files (the "Software"), to deal |
stjo2809 | 0:929babff65b1 | 6 | * in the Software without restriction, including without limitation the rights |
stjo2809 | 0:929babff65b1 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
stjo2809 | 0:929babff65b1 | 8 | * copies of the Software, and to permit persons to whom the Software is |
stjo2809 | 0:929babff65b1 | 9 | * furnished to do so, subject to the following conditions: |
stjo2809 | 0:929babff65b1 | 10 | * |
stjo2809 | 0:929babff65b1 | 11 | * The above copyright notice and this permission notice shall be included in |
stjo2809 | 0:929babff65b1 | 12 | * all copies or substantial portions of the Software. |
stjo2809 | 0:929babff65b1 | 13 | * |
stjo2809 | 0:929babff65b1 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
stjo2809 | 0:929babff65b1 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
stjo2809 | 0:929babff65b1 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
stjo2809 | 0:929babff65b1 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
stjo2809 | 0:929babff65b1 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
stjo2809 | 0:929babff65b1 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
stjo2809 | 0:929babff65b1 | 20 | * THE SOFTWARE. |
stjo2809 | 0:929babff65b1 | 21 | */ |
stjo2809 | 0:929babff65b1 | 22 | |
stjo2809 | 0:929babff65b1 | 23 | #include "mbed.h" |
stjo2809 | 0:929babff65b1 | 24 | |
stjo2809 | 0:929babff65b1 | 25 | #ifndef MBED_MCP4822_H |
stjo2809 | 0:929babff65b1 | 26 | #define MBED_MCP4822_H |
stjo2809 | 0:929babff65b1 | 27 | |
stjo2809 | 0:929babff65b1 | 28 | //============================================================================= |
stjo2809 | 0:929babff65b1 | 29 | // Declaration of variables & custom #defines |
stjo2809 | 0:929babff65b1 | 30 | //============================================================================= |
stjo2809 | 0:929babff65b1 | 31 | |
stjo2809 | 1:d97fd6c6e2e4 | 32 | #define CB_OUTPUT_A 0x00 // command bit for output A |
stjo2809 | 1:d97fd6c6e2e4 | 33 | #define CB_OUTPUT_B 0x80 // command bit for output A |
stjo2809 | 1:d97fd6c6e2e4 | 34 | #define CB_GAIN_1X 0x20 // command bit for gain 1x |
stjo2809 | 1:d97fd6c6e2e4 | 35 | #define CB_GAIN_2X 0x00 // command bit for gain 2x |
stjo2809 | 1:d97fd6c6e2e4 | 36 | #define CB_SHDN 0x10 // command bit for Output enabled |
stjo2809 | 1:d97fd6c6e2e4 | 37 | #define CB_NSHDN 0x00 // command bit for Output buffer disabled, output is high-impedance |
stjo2809 | 0:929babff65b1 | 38 | |
stjo2809 | 0:929babff65b1 | 39 | //============================================================================= |
stjo2809 | 0:929babff65b1 | 40 | // Functions Declaration |
stjo2809 | 0:929babff65b1 | 41 | //============================================================================= |
stjo2809 | 0:929babff65b1 | 42 | |
stjo2809 | 0:929babff65b1 | 43 | /** Interface to the 12-Bit DAC with internal Vref and SPI interface |
stjo2809 | 0:929babff65b1 | 44 | * |
stjo2809 | 0:929babff65b1 | 45 | * Using the driver: |
stjo2809 | 0:929babff65b1 | 46 | * - remenber to setup SPI in main routine or use pins instance. |
stjo2809 | 0:929babff65b1 | 47 | * |
stjo2809 | 0:929babff65b1 | 48 | * Defaults in this driver on start up: |
stjo2809 | 0:929babff65b1 | 49 | * - Datasheet start up |
stjo2809 | 0:929babff65b1 | 50 | */ |
stjo2809 | 0:929babff65b1 | 51 | class MCP4822 { |
stjo2809 | 0:929babff65b1 | 52 | public: |
stjo2809 | 0:929babff65b1 | 53 | /** Create an instance of the MCP4822 connected via specfied SPI instance. |
stjo2809 | 0:929babff65b1 | 54 | * |
stjo2809 | 0:929babff65b1 | 55 | * @param spi The mbed SPI instance (make in main routine) |
stjo2809 | 0:929babff65b1 | 56 | * @param nLDAC The Latch DAC Synchronization pin |
stjo2809 | 0:929babff65b1 | 57 | * @param nCs The SPI chip select pin. |
stjo2809 | 0:929babff65b1 | 58 | */ |
stjo2809 | 1:d97fd6c6e2e4 | 59 | MCP4822(SPI& spi, PinName nCs, PinName nLDAC); |
stjo2809 | 0:929babff65b1 | 60 | |
stjo2809 | 0:929babff65b1 | 61 | /** Write to output A. |
stjo2809 | 0:929babff65b1 | 62 | * |
stjo2809 | 0:929babff65b1 | 63 | * @param gain the gain is 2x = '0' and 1x = '1'. |
stjo2809 | 0:929babff65b1 | 64 | * @param data The 12 bits value to write to the output. |
stjo2809 | 0:929babff65b1 | 65 | */ |
stjo2809 | 0:929babff65b1 | 66 | void a(bool gain, int data); |
stjo2809 | 0:929babff65b1 | 67 | |
stjo2809 | 0:929babff65b1 | 68 | /** Write to output B. |
stjo2809 | 0:929babff65b1 | 69 | * |
stjo2809 | 0:929babff65b1 | 70 | * @param gain the gain is 2x = '0' and 1x = '1'. |
stjo2809 | 0:929babff65b1 | 71 | * @param data The 12 bits value to write to the output. |
stjo2809 | 0:929babff65b1 | 72 | */ |
stjo2809 | 0:929babff65b1 | 73 | void b(bool gain, int data); |
stjo2809 | 0:929babff65b1 | 74 | |
stjo2809 | 0:929babff65b1 | 75 | /** Output Synchronization. |
stjo2809 | 0:929babff65b1 | 76 | * |
stjo2809 | 0:929babff65b1 | 77 | * @param act The LDAC is Active = true and Inactive = false. |
stjo2809 | 0:929babff65b1 | 78 | */ |
stjo2809 | 0:929babff65b1 | 79 | void ldac(bool act); |
stjo2809 | 0:929babff65b1 | 80 | |
stjo2809 | 1:d97fd6c6e2e4 | 81 | /** Output Synchronization. |
stjo2809 | 1:d97fd6c6e2e4 | 82 | * |
stjo2809 | 1:d97fd6c6e2e4 | 83 | * @return The status the LDAC. |
stjo2809 | 1:d97fd6c6e2e4 | 84 | */ |
stjo2809 | 1:d97fd6c6e2e4 | 85 | bool ldac(); |
stjo2809 | 1:d97fd6c6e2e4 | 86 | |
stjo2809 | 0:929babff65b1 | 87 | /** Output Shutdown. |
stjo2809 | 0:929babff65b1 | 88 | * |
stjo2809 | 0:929babff65b1 | 89 | * @param output A = '0' and B = '1'. |
stjo2809 | 0:929babff65b1 | 90 | * @param act The SHDN is Active = true and Inactive = false. |
stjo2809 | 0:929babff65b1 | 91 | */ |
stjo2809 | 0:929babff65b1 | 92 | void shdn(bool output, bool act); |
stjo2809 | 0:929babff65b1 | 93 | |
stjo2809 | 1:d97fd6c6e2e4 | 94 | /** Output Shutdown. |
stjo2809 | 1:d97fd6c6e2e4 | 95 | * |
stjo2809 | 1:d97fd6c6e2e4 | 96 | * @return The status the SHDN. |
stjo2809 | 1:d97fd6c6e2e4 | 97 | */ |
stjo2809 | 1:d97fd6c6e2e4 | 98 | bool shdn(); |
stjo2809 | 1:d97fd6c6e2e4 | 99 | |
stjo2809 | 0:929babff65b1 | 100 | |
stjo2809 | 0:929babff65b1 | 101 | |
stjo2809 | 0:929babff65b1 | 102 | private: |
stjo2809 | 0:929babff65b1 | 103 | SPI& _spi; |
stjo2809 | 0:929babff65b1 | 104 | DigitalOut _nCs; |
stjo2809 | 0:929babff65b1 | 105 | DigitalOut _nLDAC; |
stjo2809 | 1:d97fd6c6e2e4 | 106 | bool _SHDN_STATUS; |
stjo2809 | 0:929babff65b1 | 107 | |
stjo2809 | 0:929babff65b1 | 108 | char _upper_half; |
stjo2809 | 0:929babff65b1 | 109 | char _lower_half; |
stjo2809 | 0:929babff65b1 | 110 | |
stjo2809 | 0:929babff65b1 | 111 | char _make_upper_half(bool output, bool gain, bool shdn, int data); |
stjo2809 | 0:929babff65b1 | 112 | char _make_lower_half(int data); |
stjo2809 | 0:929babff65b1 | 113 | void _write(bool output, bool gain, bool shdn, int data); |
stjo2809 | 0:929babff65b1 | 114 | |
stjo2809 | 0:929babff65b1 | 115 | }; |
stjo2809 | 0:929babff65b1 | 116 | |
stjo2809 | 0:929babff65b1 | 117 | #endif |