A feature complete driver for the MCP4725 DAC from Microchip with an AnalogOut-compatible API.
Dependents: MCP4725_HelloWorld DAC02_RFID_and_TFT
MCP4725.h@0:203cdd3b30fc, 2014-05-06 (annotated)
- Committer:
- neilt6
- Date:
- Tue May 06 17:11:35 2014 +0000
- Revision:
- 0:203cdd3b30fc
- Child:
- 1:3b687a9acdf3
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 0:203cdd3b30fc | 1 | /* MCP4725 Driver Library |
neilt6 | 0:203cdd3b30fc | 2 | * Copyright (c) 2014 Neil Thiessen |
neilt6 | 0:203cdd3b30fc | 3 | * |
neilt6 | 0:203cdd3b30fc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
neilt6 | 0:203cdd3b30fc | 5 | * you may not use this file except in compliance with the License. |
neilt6 | 0:203cdd3b30fc | 6 | * You may obtain a copy of the License at |
neilt6 | 0:203cdd3b30fc | 7 | * |
neilt6 | 0:203cdd3b30fc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
neilt6 | 0:203cdd3b30fc | 9 | * |
neilt6 | 0:203cdd3b30fc | 10 | * Unless required by applicable law or agreed to in writing, software |
neilt6 | 0:203cdd3b30fc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
neilt6 | 0:203cdd3b30fc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
neilt6 | 0:203cdd3b30fc | 13 | * See the License for the specific language governing permissions and |
neilt6 | 0:203cdd3b30fc | 14 | * limitations under the License. |
neilt6 | 0:203cdd3b30fc | 15 | */ |
neilt6 | 0:203cdd3b30fc | 16 | |
neilt6 | 0:203cdd3b30fc | 17 | #ifndef MCP4725_H |
neilt6 | 0:203cdd3b30fc | 18 | #define MCP4725_H |
neilt6 | 0:203cdd3b30fc | 19 | |
neilt6 | 0:203cdd3b30fc | 20 | #include "mbed.h" |
neilt6 | 0:203cdd3b30fc | 21 | |
neilt6 | 0:203cdd3b30fc | 22 | /** MCP4725 class. |
neilt6 | 0:203cdd3b30fc | 23 | * Used for controlling an MCP4725 DAC connected via I2C. |
neilt6 | 0:203cdd3b30fc | 24 | * |
neilt6 | 0:203cdd3b30fc | 25 | * Example: |
neilt6 | 0:203cdd3b30fc | 26 | * @code |
neilt6 | 0:203cdd3b30fc | 27 | * #include "mbed.h" |
neilt6 | 0:203cdd3b30fc | 28 | * #include "MCP4725.h" |
neilt6 | 0:203cdd3b30fc | 29 | * |
neilt6 | 0:203cdd3b30fc | 30 | * //Create an MCP4725 object at the default address (ADDRESS_0) |
neilt6 | 0:203cdd3b30fc | 31 | * MCP4725 dac(p28, p27); |
neilt6 | 0:203cdd3b30fc | 32 | * |
neilt6 | 0:203cdd3b30fc | 33 | * int main() |
neilt6 | 0:203cdd3b30fc | 34 | * { |
neilt6 | 0:203cdd3b30fc | 35 | * //Try to open the MCP4725 |
neilt6 | 0:203cdd3b30fc | 36 | * if (dac.open()) { |
neilt6 | 0:203cdd3b30fc | 37 | * printf("Device detected!\n"); |
neilt6 | 0:203cdd3b30fc | 38 | * |
neilt6 | 0:203cdd3b30fc | 39 | * //Wake up the DAC |
neilt6 | 0:203cdd3b30fc | 40 | * //NOTE: This might wake up other I2C devices as well! |
neilt6 | 0:203cdd3b30fc | 41 | * dac.wakeup(); |
neilt6 | 0:203cdd3b30fc | 42 | * |
neilt6 | 0:203cdd3b30fc | 43 | * while (1) { |
neilt6 | 0:203cdd3b30fc | 44 | * //Generate a sine wave on the DAC |
neilt6 | 0:203cdd3b30fc | 45 | * for (float i = 0.0; i < 360.0; i += 0.1) |
neilt6 | 0:203cdd3b30fc | 46 | * dac = 0.5 * (sinf(i * 3.14159265 / 180.0) + 1); |
neilt6 | 0:203cdd3b30fc | 47 | * } |
neilt6 | 0:203cdd3b30fc | 48 | * } else { |
neilt6 | 0:203cdd3b30fc | 49 | * error("Device not detected!\n"); |
neilt6 | 0:203cdd3b30fc | 50 | * } |
neilt6 | 0:203cdd3b30fc | 51 | * } |
neilt6 | 0:203cdd3b30fc | 52 | * @endcode |
neilt6 | 0:203cdd3b30fc | 53 | */ |
neilt6 | 0:203cdd3b30fc | 54 | class MCP4725 |
neilt6 | 0:203cdd3b30fc | 55 | { |
neilt6 | 0:203cdd3b30fc | 56 | public: |
neilt6 | 0:203cdd3b30fc | 57 | /** Represents the different I2C address possibilities for the MCP4725 |
neilt6 | 0:203cdd3b30fc | 58 | */ |
neilt6 | 0:203cdd3b30fc | 59 | enum Address { |
neilt6 | 0:203cdd3b30fc | 60 | ADDRESS_0 = (0x60 << 1), /**< A[2:0] bits = 000 */ |
neilt6 | 0:203cdd3b30fc | 61 | ADDRESS_1 = (0x61 << 1), /**< A[2:0] bits = 001 */ |
neilt6 | 0:203cdd3b30fc | 62 | ADDRESS_2 = (0x62 << 1), /**< A[2:0] bits = 010 */ |
neilt6 | 0:203cdd3b30fc | 63 | ADDRESS_3 = (0x63 << 1), /**< A[2:0] bits = 011 */ |
neilt6 | 0:203cdd3b30fc | 64 | ADDRESS_4 = (0x64 << 1), /**< A[2:0] bits = 100 */ |
neilt6 | 0:203cdd3b30fc | 65 | ADDRESS_5 = (0x65 << 1), /**< A[2:0] bits = 101 */ |
neilt6 | 0:203cdd3b30fc | 66 | ADDRESS_6 = (0x67 << 1), /**< A[2:0] bits = 110 */ |
neilt6 | 0:203cdd3b30fc | 67 | ADDRESS_7 = (0x68 << 1) /**< A[2:0] bits = 111 */ |
neilt6 | 0:203cdd3b30fc | 68 | }; |
neilt6 | 0:203cdd3b30fc | 69 | |
neilt6 | 0:203cdd3b30fc | 70 | /** Represents the power mode of the MCP4725 |
neilt6 | 0:203cdd3b30fc | 71 | */ |
neilt6 | 0:203cdd3b30fc | 72 | enum PowerMode { |
neilt6 | 0:203cdd3b30fc | 73 | POWER_NORMAL, /**< Chip is enabled, and the output is active */ |
neilt6 | 0:203cdd3b30fc | 74 | POWER_SHUTDOWN_1K, /**< Chip is shutdown, and the output is grounded with a 1kΩ resistor */ |
neilt6 | 0:203cdd3b30fc | 75 | POWER_SHUTDOWN_100K, /**< Chip is shutdown, and the output is grounded with a 100kΩ resistor */ |
neilt6 | 0:203cdd3b30fc | 76 | POWER_SHUTDOWN_500K /**< Chip is shutdown, and the output is grounded with a 500kΩ resistor */ |
neilt6 | 0:203cdd3b30fc | 77 | }; |
neilt6 | 0:203cdd3b30fc | 78 | |
neilt6 | 0:203cdd3b30fc | 79 | /** Create an MCP4725 object connected to the specified I2C pins with the specified I2C slave address |
neilt6 | 0:203cdd3b30fc | 80 | * |
neilt6 | 0:203cdd3b30fc | 81 | * @param sda The I2C data pin. |
neilt6 | 0:203cdd3b30fc | 82 | * @param scl The I2C clock pin. |
neilt6 | 0:203cdd3b30fc | 83 | * @param addr The I2C slave address (defaults to ADDRESS_0). |
neilt6 | 0:203cdd3b30fc | 84 | * @param hz The I2C bus frequency (defaults to 400kHz). |
neilt6 | 0:203cdd3b30fc | 85 | */ |
neilt6 | 0:203cdd3b30fc | 86 | MCP4725(PinName sda, PinName scl, Address addr = ADDRESS_0, int hz = 400000); |
neilt6 | 0:203cdd3b30fc | 87 | |
neilt6 | 0:203cdd3b30fc | 88 | /** Probe for the MCP4725 and indicate if it's present on the bus |
neilt6 | 0:203cdd3b30fc | 89 | * |
neilt6 | 0:203cdd3b30fc | 90 | * @returns |
neilt6 | 0:203cdd3b30fc | 91 | * 'true' if the device exists on the bus, |
neilt6 | 0:203cdd3b30fc | 92 | * 'false' if the device doesn't exist on the bus. |
neilt6 | 0:203cdd3b30fc | 93 | */ |
neilt6 | 0:203cdd3b30fc | 94 | bool open(); |
neilt6 | 0:203cdd3b30fc | 95 | |
neilt6 | 0:203cdd3b30fc | 96 | /** Issue a General Call Reset command to reset all MCP4725 devices on the bus |
neilt6 | 0:203cdd3b30fc | 97 | * |
neilt6 | 0:203cdd3b30fc | 98 | * @warning This might reset other I2C devices as well |
neilt6 | 0:203cdd3b30fc | 99 | */ |
neilt6 | 0:203cdd3b30fc | 100 | void reset(); |
neilt6 | 0:203cdd3b30fc | 101 | |
neilt6 | 0:203cdd3b30fc | 102 | /** Issue a General Call Wake-up command to power-up all MCP4725 devices on the bus |
neilt6 | 0:203cdd3b30fc | 103 | * |
neilt6 | 0:203cdd3b30fc | 104 | * @warning This might wake up other I2C devices as well |
neilt6 | 0:203cdd3b30fc | 105 | */ |
neilt6 | 0:203cdd3b30fc | 106 | void wakeup(); |
neilt6 | 0:203cdd3b30fc | 107 | |
neilt6 | 0:203cdd3b30fc | 108 | /** Get the current power mode of the MCP4725 |
neilt6 | 0:203cdd3b30fc | 109 | * |
neilt6 | 0:203cdd3b30fc | 110 | * @returns The current power mode as a PowerMode enum. |
neilt6 | 0:203cdd3b30fc | 111 | */ |
neilt6 | 0:203cdd3b30fc | 112 | MCP4725::PowerMode powerMode(); |
neilt6 | 0:203cdd3b30fc | 113 | |
neilt6 | 0:203cdd3b30fc | 114 | /** Set the power mode of the MCP4725 |
neilt6 | 0:203cdd3b30fc | 115 | * |
neilt6 | 0:203cdd3b30fc | 116 | * @param mode The new power mode as a PowerMode enum. |
neilt6 | 0:203cdd3b30fc | 117 | */ |
neilt6 | 0:203cdd3b30fc | 118 | void powerMode(PowerMode mode); |
neilt6 | 0:203cdd3b30fc | 119 | |
neilt6 | 0:203cdd3b30fc | 120 | /** Get the current output voltage of the MCP4725 as a percentage |
neilt6 | 0:203cdd3b30fc | 121 | * |
neilt6 | 0:203cdd3b30fc | 122 | * @returns The current output voltage as a percentage (0.0 to 1.0 * VDD). |
neilt6 | 0:203cdd3b30fc | 123 | */ |
neilt6 | 0:203cdd3b30fc | 124 | float read(); |
neilt6 | 0:203cdd3b30fc | 125 | |
neilt6 | 0:203cdd3b30fc | 126 | /** Set the output voltage of the MCP4725 from a percentage |
neilt6 | 0:203cdd3b30fc | 127 | * |
neilt6 | 0:203cdd3b30fc | 128 | * @param value The new output voltage as a percentage (0.0 to 1.0 * VDD). |
neilt6 | 0:203cdd3b30fc | 129 | */ |
neilt6 | 0:203cdd3b30fc | 130 | void write(float value); |
neilt6 | 0:203cdd3b30fc | 131 | |
neilt6 | 0:203cdd3b30fc | 132 | /** Set the output voltage of the MCP4725 from a 12-bit range |
neilt6 | 0:203cdd3b30fc | 133 | * |
neilt6 | 0:203cdd3b30fc | 134 | * @param value The new output voltage as a 12-bit unsigned short (0x0000 to 0x0FFF). |
neilt6 | 0:203cdd3b30fc | 135 | */ |
neilt6 | 0:203cdd3b30fc | 136 | void write_u12(unsigned short value); |
neilt6 | 0:203cdd3b30fc | 137 | |
neilt6 | 0:203cdd3b30fc | 138 | /** Set the output voltage of the MCP4725 from a 16-bit range |
neilt6 | 0:203cdd3b30fc | 139 | * |
neilt6 | 0:203cdd3b30fc | 140 | * @param value The new output voltage as as a 16-bit unsigned short (0x0000 to 0xFFFF). |
neilt6 | 0:203cdd3b30fc | 141 | */ |
neilt6 | 0:203cdd3b30fc | 142 | void write_u16(unsigned short value); |
neilt6 | 0:203cdd3b30fc | 143 | |
neilt6 | 0:203cdd3b30fc | 144 | /** Get the current DAC settings in EEPROM |
neilt6 | 0:203cdd3b30fc | 145 | * |
neilt6 | 0:203cdd3b30fc | 146 | * @param mode Pointer to a PowerMode enum for the power mode in EEPROM. |
neilt6 | 0:203cdd3b30fc | 147 | * @param value Pointer to an unsigned short for the 12-bit DAC value in EEPROM (0x0000 to 0x0FFF). |
neilt6 | 0:203cdd3b30fc | 148 | */ |
neilt6 | 0:203cdd3b30fc | 149 | void readEeprom(PowerMode* mode, unsigned short* value); |
neilt6 | 0:203cdd3b30fc | 150 | |
neilt6 | 0:203cdd3b30fc | 151 | /** Set the DAC settings in EEPROM |
neilt6 | 0:203cdd3b30fc | 152 | * |
neilt6 | 0:203cdd3b30fc | 153 | * @param mode The new EEPROM power mode as a PowerMode enum. |
neilt6 | 0:203cdd3b30fc | 154 | * @param value The new EEPROM DAC value as a 12-bit unsigned short (0x0000 to 0x0FFF). |
neilt6 | 0:203cdd3b30fc | 155 | */ |
neilt6 | 0:203cdd3b30fc | 156 | void writeEeprom(PowerMode mode, unsigned short value); |
neilt6 | 0:203cdd3b30fc | 157 | |
neilt6 | 0:203cdd3b30fc | 158 | #ifdef MBED_OPERATORS |
neilt6 | 0:203cdd3b30fc | 159 | /** A shorthand for read() |
neilt6 | 0:203cdd3b30fc | 160 | * |
neilt6 | 0:203cdd3b30fc | 161 | * @returns The current output voltage as a percentage (0.0 to 1.0 * VDD). |
neilt6 | 0:203cdd3b30fc | 162 | */ |
neilt6 | 0:203cdd3b30fc | 163 | operator float(); |
neilt6 | 0:203cdd3b30fc | 164 | |
neilt6 | 0:203cdd3b30fc | 165 | /** A shorthand for write() |
neilt6 | 0:203cdd3b30fc | 166 | * |
neilt6 | 0:203cdd3b30fc | 167 | * @param value The new output voltage as a percentage (0.0 to 1.0 * VDD). |
neilt6 | 0:203cdd3b30fc | 168 | */ |
neilt6 | 0:203cdd3b30fc | 169 | MCP4725& operator=(float value); |
neilt6 | 0:203cdd3b30fc | 170 | #endif |
neilt6 | 0:203cdd3b30fc | 171 | |
neilt6 | 0:203cdd3b30fc | 172 | private: |
neilt6 | 0:203cdd3b30fc | 173 | //Member variables |
neilt6 | 0:203cdd3b30fc | 174 | I2C m_I2C; |
neilt6 | 0:203cdd3b30fc | 175 | const int m_ADDR; |
neilt6 | 0:203cdd3b30fc | 176 | MCP4725::PowerMode m_PowerMode; |
neilt6 | 0:203cdd3b30fc | 177 | unsigned short m_DacValue; |
neilt6 | 0:203cdd3b30fc | 178 | |
neilt6 | 0:203cdd3b30fc | 179 | //Internal functions |
neilt6 | 0:203cdd3b30fc | 180 | void readDac(); |
neilt6 | 0:203cdd3b30fc | 181 | void writeDac(); |
neilt6 | 0:203cdd3b30fc | 182 | }; |
neilt6 | 0:203cdd3b30fc | 183 | |
neilt6 | 0:203cdd3b30fc | 184 | #endif |