A feature complete driver for the MCP4922 DAC from Microchip.

Dependents:   MCP4922_HelloWorld NuMidi401 NuFM401 NuFM402

Committer:
neilt6
Date:
Thu May 15 23:51:38 2014 +0000
Revision:
1:747ec9c5d80e
Parent:
0:e994c6963c66
Constructor now initializes the internal variables, and performs an initial write to make sure they're in sync

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:e994c6963c66 1 /* MCP4922 Driver Library
neilt6 0:e994c6963c66 2 * Copyright (c) 2014 Neil Thiessen
neilt6 0:e994c6963c66 3 *
neilt6 0:e994c6963c66 4 * Licensed under the Apache License, Version 2.0 (the "License");
neilt6 0:e994c6963c66 5 * you may not use this file except in compliance with the License.
neilt6 0:e994c6963c66 6 * You may obtain a copy of the License at
neilt6 0:e994c6963c66 7 *
neilt6 0:e994c6963c66 8 * http://www.apache.org/licenses/LICENSE-2.0
neilt6 0:e994c6963c66 9 *
neilt6 0:e994c6963c66 10 * Unless required by applicable law or agreed to in writing, software
neilt6 0:e994c6963c66 11 * distributed under the License is distributed on an "AS IS" BASIS,
neilt6 0:e994c6963c66 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
neilt6 0:e994c6963c66 13 * See the License for the specific language governing permissions and
neilt6 0:e994c6963c66 14 * limitations under the License.
neilt6 0:e994c6963c66 15 */
neilt6 0:e994c6963c66 16
neilt6 0:e994c6963c66 17 #ifndef MCP4922_H
neilt6 0:e994c6963c66 18 #define MCP4922_H
neilt6 0:e994c6963c66 19
neilt6 0:e994c6963c66 20 #include "mbed.h"
neilt6 0:e994c6963c66 21
neilt6 0:e994c6963c66 22 /** MCP4922 class.
neilt6 0:e994c6963c66 23 * Used for controlling an MCP4922 DAC connected via SPI.
neilt6 0:e994c6963c66 24 *
neilt6 0:e994c6963c66 25 * Example:
neilt6 0:e994c6963c66 26 * @code
neilt6 0:e994c6963c66 27 * #include "mbed.h"
neilt6 0:e994c6963c66 28 * #include "MCP4922.h"
neilt6 0:e994c6963c66 29 *
neilt6 0:e994c6963c66 30 * //Create an MCP4922 object
neilt6 0:e994c6963c66 31 * MCP4922 dac(p11, p13, p14);
neilt6 0:e994c6963c66 32 *
neilt6 0:e994c6963c66 33 * int main()
neilt6 0:e994c6963c66 34 * {
neilt6 0:e994c6963c66 35 * //Configure DAC A
neilt6 0:e994c6963c66 36 * dac.referenceMode(MCP4922::DAC_A, MCP4922::REF_UNBUFFERED);
neilt6 0:e994c6963c66 37 * dac.gainMode(MCP4922::DAC_A, MCP4922::GAIN_1X);
neilt6 0:e994c6963c66 38 * dac.powerMode(MCP4922::DAC_A, MCP4922::POWER_NORMAL);
neilt6 0:e994c6963c66 39 *
neilt6 0:e994c6963c66 40 * while (1) {
neilt6 0:e994c6963c66 41 * //Generate a sine wave on DAC A
neilt6 0:e994c6963c66 42 * for (float i = 0.0; i < 360.0; i += 0.1)
neilt6 0:e994c6963c66 43 * dac.write(MCP4922::DAC_A, 0.5 * (sinf(i * 3.14159265 / 180.0) + 1));
neilt6 0:e994c6963c66 44 * }
neilt6 0:e994c6963c66 45 * }
neilt6 0:e994c6963c66 46 * @endcode
neilt6 0:e994c6963c66 47 */
neilt6 0:e994c6963c66 48 class MCP4922
neilt6 0:e994c6963c66 49 {
neilt6 0:e994c6963c66 50 public:
neilt6 0:e994c6963c66 51 /** Represents the different DACs in the MCP4922
neilt6 0:e994c6963c66 52 */
neilt6 0:e994c6963c66 53 enum DAC {
neilt6 0:e994c6963c66 54 DAC_A, /**< DAC A */
neilt6 0:e994c6963c66 55 DAC_B /**< DAC B */
neilt6 0:e994c6963c66 56 };
neilt6 0:e994c6963c66 57
neilt6 0:e994c6963c66 58 /** Represents the reference buffer mode of DAC A or B in the MCP4922
neilt6 0:e994c6963c66 59 */
neilt6 0:e994c6963c66 60 enum ReferenceMode {
neilt6 0:e994c6963c66 61 REF_UNBUFFERED, /**< DAC VREF is unbuffered */
neilt6 0:e994c6963c66 62 REF_BUFFERED /**< DAC VREF is buffered */
neilt6 0:e994c6963c66 63 };
neilt6 0:e994c6963c66 64
neilt6 0:e994c6963c66 65 /** Represents the gain mode of DAC A or B in the MCP4922
neilt6 0:e994c6963c66 66 */
neilt6 0:e994c6963c66 67 enum GainMode {
neilt6 0:e994c6963c66 68 GAIN_2X, /**< DAC output = 2 * VREF * D/4096 */
neilt6 0:e994c6963c66 69 GAIN_1X /**< DAC output = VREF * D/4096 */
neilt6 0:e994c6963c66 70 };
neilt6 0:e994c6963c66 71
neilt6 0:e994c6963c66 72 /** Represents the power mode of DAC A or B in the MCP4922
neilt6 0:e994c6963c66 73 */
neilt6 0:e994c6963c66 74 enum PowerMode {
neilt6 0:e994c6963c66 75 POWER_SHUTDOWN, /**< DAC output is high impedance */
neilt6 0:e994c6963c66 76 POWER_NORMAL /**< DAC output is enabled */
neilt6 0:e994c6963c66 77 };
neilt6 0:e994c6963c66 78
neilt6 0:e994c6963c66 79 /** Create an MCP4922 object connected to the specified SPI pins
neilt6 0:e994c6963c66 80 *
neilt6 0:e994c6963c66 81 * @param mosi The SPI data out pin.
neilt6 0:e994c6963c66 82 * @param sclk The SPI clock pin.
neilt6 0:e994c6963c66 83 * @param cs The SPI chip select pin.
neilt6 0:e994c6963c66 84 * @param hz The SPI bus frequency (defaults to 20MHz).
neilt6 0:e994c6963c66 85 */
neilt6 0:e994c6963c66 86 MCP4922(PinName mosi, PinName sclk, PinName cs, int hz = 20000000);
neilt6 0:e994c6963c66 87
neilt6 0:e994c6963c66 88 /** Get the current reference mode of the specified DAC in the MCP4922
neilt6 0:e994c6963c66 89 *
neilt6 0:e994c6963c66 90 * @param dac The DAC to read from.
neilt6 0:e994c6963c66 91 *
neilt6 0:e994c6963c66 92 * @returns The current reference mode of the specified DAC as a ReferenceMode enum.
neilt6 0:e994c6963c66 93 */
neilt6 0:e994c6963c66 94 MCP4922::ReferenceMode referenceMode(DAC dac);
neilt6 0:e994c6963c66 95
neilt6 0:e994c6963c66 96 /** Set the reference mode of the specified DAC in the MCP4922
neilt6 0:e994c6963c66 97 *
neilt6 0:e994c6963c66 98 * @param dac The DAC to write to.
neilt6 0:e994c6963c66 99 * @param mode The new reference mode for the specified DAC as a ReferenceMode enum.
neilt6 0:e994c6963c66 100 */
neilt6 0:e994c6963c66 101 void referenceMode(DAC dac, ReferenceMode mode);
neilt6 0:e994c6963c66 102
neilt6 0:e994c6963c66 103 /** Get the current gain mode of the specified DAC in the MCP4922
neilt6 0:e994c6963c66 104 *
neilt6 0:e994c6963c66 105 * @param dac The DAC to read from.
neilt6 0:e994c6963c66 106 *
neilt6 0:e994c6963c66 107 * @returns The current gain mode of the specified DAC as a GainMode enum.
neilt6 0:e994c6963c66 108 */
neilt6 0:e994c6963c66 109 MCP4922::GainMode gainMode(DAC dac);
neilt6 0:e994c6963c66 110
neilt6 0:e994c6963c66 111 /** Set the gain mode of the specified DAC in the MCP4922
neilt6 0:e994c6963c66 112 *
neilt6 0:e994c6963c66 113 * @param dac The DAC to write to.
neilt6 0:e994c6963c66 114 * @param mode The new gain mode for the specified DAC as a GainMode enum.
neilt6 0:e994c6963c66 115 */
neilt6 0:e994c6963c66 116 void gainMode(DAC dac, GainMode mode);
neilt6 0:e994c6963c66 117
neilt6 0:e994c6963c66 118 /** Get the current power mode of the specified DAC in the MCP4922
neilt6 0:e994c6963c66 119 *
neilt6 0:e994c6963c66 120 * @param dac The DAC to read from.
neilt6 0:e994c6963c66 121 *
neilt6 0:e994c6963c66 122 * @returns The current power mode of the specified DAC as a PowerMode enum.
neilt6 0:e994c6963c66 123 */
neilt6 0:e994c6963c66 124 MCP4922::PowerMode powerMode(DAC dac);
neilt6 0:e994c6963c66 125
neilt6 0:e994c6963c66 126 /** Set the power mode of the specified DAC in the MCP4922
neilt6 0:e994c6963c66 127 *
neilt6 0:e994c6963c66 128 * @param dac The DAC to write to.
neilt6 0:e994c6963c66 129 * @param mode The new power mode for the specified DAC as a PowerMode enum.
neilt6 0:e994c6963c66 130 */
neilt6 0:e994c6963c66 131 void powerMode(DAC dac, PowerMode mode);
neilt6 0:e994c6963c66 132
neilt6 0:e994c6963c66 133 /** Get the current output voltage of the specified DAC in the MCP4922 as a percentage
neilt6 0:e994c6963c66 134 *
neilt6 0:e994c6963c66 135 * @param dac The DAC to read from.
neilt6 0:e994c6963c66 136 *
neilt6 0:e994c6963c66 137 * @returns The current output voltage of the specified DAC as a percentage (0.0 to 1.0 * VDD).
neilt6 0:e994c6963c66 138 */
neilt6 0:e994c6963c66 139 float read(DAC dac);
neilt6 0:e994c6963c66 140
neilt6 0:e994c6963c66 141 /** Set the output voltage of the specified DAC in the MCP4922 from a percentage
neilt6 0:e994c6963c66 142 *
neilt6 0:e994c6963c66 143 * @param dac The DAC to write to.
neilt6 0:e994c6963c66 144 * @param value The new output voltage for the specified DAC as a percentage (0.0 to 1.0 * VDD).
neilt6 0:e994c6963c66 145 */
neilt6 0:e994c6963c66 146 void write(DAC dac, float value);
neilt6 0:e994c6963c66 147
neilt6 0:e994c6963c66 148 /** Set the output voltage of the specified DAC in the MCP4922 from a 16-bit range
neilt6 0:e994c6963c66 149 *
neilt6 0:e994c6963c66 150 * @param dac The DAC to write to.
neilt6 0:e994c6963c66 151 * @param value The new output voltage for the specified DAC as a 16-bit unsigned short (0x0000 to 0xFFFF).
neilt6 0:e994c6963c66 152 */
neilt6 0:e994c6963c66 153 void write_u16(DAC dac, unsigned short value);
neilt6 0:e994c6963c66 154
neilt6 0:e994c6963c66 155 private:
neilt6 0:e994c6963c66 156 //SPI member variables
neilt6 0:e994c6963c66 157 SPI m_SPI;
neilt6 0:e994c6963c66 158 DigitalOut m_CS;
neilt6 0:e994c6963c66 159
neilt6 0:e994c6963c66 160 //DAC settings member variables
neilt6 0:e994c6963c66 161 unsigned short m_DacValueA;
neilt6 0:e994c6963c66 162 unsigned short m_DacValueB;
neilt6 0:e994c6963c66 163
neilt6 0:e994c6963c66 164 //Internal functions
neilt6 0:e994c6963c66 165 void writeDac(unsigned short value);
neilt6 0:e994c6963c66 166 };
neilt6 0:e994c6963c66 167
neilt6 0:e994c6963c66 168 #endif