The Best / MCP4725

Dependents:   dacc

Fork of MCP4725 by Neil Thiessen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP4725.h Source File

MCP4725.h

00001 /* MCP4725 Driver Library
00002  * Copyright (c) 2014 Neil Thiessen
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef MCP4725_H
00018 #define MCP4725_H
00019 
00020 #include "mbed.h"
00021 
00022 /** MCP4725 class.
00023  *  Used for controlling an MCP4725 DAC connected via I2C.
00024  *
00025  * Example:
00026  * @code
00027  * #include "mbed.h"
00028  * #include "MCP4725.h"
00029  *
00030  * //Create an MCP4725 object at the default address (ADDRESS_0)
00031  * MCP4725 dac(p28, p27);
00032  *
00033  * int main()
00034  * {
00035  *     //Try to open the MCP4725
00036  *     if (dac.open()) {
00037  *         printf("Device detected!\n");
00038  *
00039  *         //Wake up the DAC
00040  *         //NOTE: This might wake up other I2C devices as well!
00041  *         dac.wakeup();
00042  *
00043  *         while (1) {
00044  *             //Generate a sine wave on the DAC
00045  *             for (float i = 0.0; i < 360.0; i += 0.1)
00046  *                 dac = 0.5 * (sinf(i * 3.14159265 / 180.0) + 1);
00047  *         }
00048  *     } else {
00049  *         error("Device not detected!\n");
00050  *     }
00051  * }
00052  * @endcode
00053  */
00054 class MCP4725
00055 {
00056 public:
00057     /** Represents the different I2C address possibilities for the MCP4725
00058      */
00059     enum Address {
00060         ADDRESS_0 = (0x60 << 1),    /**< A[2:0] bits = 000 */
00061         ADDRESS_1 = (0x61 << 1),    /**< A[2:0] bits = 001 */
00062         ADDRESS_2 = (0x62 << 1),    /**< A[2:0] bits = 010 */
00063         ADDRESS_3 = (0x63 << 1),    /**< A[2:0] bits = 011 */
00064         ADDRESS_4 = (0x64 << 1),    /**< A[2:0] bits = 100 */
00065         ADDRESS_5 = (0x65 << 1),    /**< A[2:0] bits = 101 */
00066         ADDRESS_6 = (0x67 << 1),    /**< A[2:0] bits = 110 */
00067         ADDRESS_7 = (0x68 << 1)     /**< A[2:0] bits = 111 */
00068     };
00069 
00070     /** Represents the power mode of the MCP4725
00071      */
00072     enum PowerMode {
00073         POWER_NORMAL,           /**< Chip is enabled, and the output is active */
00074         POWER_SHUTDOWN_1K,      /**< Chip is shutdown, and the output is grounded with a 1kΩ resistor */
00075         POWER_SHUTDOWN_100K,    /**< Chip is shutdown, and the output is grounded with a 100kΩ resistor */
00076         POWER_SHUTDOWN_500K     /**< Chip is shutdown, and the output is grounded with a 500kΩ resistor */
00077     };
00078 
00079     /** Create an MCP4725 object connected to the specified I2C pins with the specified I2C slave address
00080      *
00081      * @param sda The I2C data pin.
00082      * @param scl The I2C clock pin.
00083      * @param addr The I2C slave address (defaults to ADDRESS_0).
00084      * @param hz The I2C bus frequency (defaults to 400kHz).
00085      */
00086     MCP4725(PinName sda, PinName scl, Address addr = ADDRESS_0, int hz = 400000);
00087 
00088     /** Probe for the MCP4725 and indicate if it's present on the bus
00089      *
00090      * @returns
00091      *   'true' if the device exists on the bus,
00092      *   'false' if the device doesn't exist on the bus.
00093      */
00094     bool open();
00095 
00096     /** Issue a General Call Reset command to reset all MCP4725 devices on the bus
00097      *
00098      * @warning This might reset other I2C devices as well
00099      */
00100     void reset();
00101 
00102     /** Issue a General Call Wake-up command to power-up all MCP4725 devices on the bus
00103      *
00104      * @warning This might wake up other I2C devices as well
00105      */
00106     void wakeup();
00107 
00108     /** Get the current power mode of the MCP4725
00109      *
00110      * @returns The current power mode as a PowerMode enum.
00111      */
00112     MCP4725::PowerMode powerMode();
00113 
00114     /** Set the power mode of the MCP4725
00115      *
00116      * @param mode The new power mode as a PowerMode enum.
00117      */
00118     void powerMode(PowerMode mode);
00119 
00120     /** Get the current output voltage of the MCP4725 as a percentage
00121      *
00122      * @returns The current output voltage as a percentage (0.0 to 1.0 * VDD).
00123      */
00124     float read();
00125 
00126     /** Set the output voltage of the MCP4725 from a percentage
00127      *
00128      * @param value The new output voltage as a percentage (0.0 to 1.0 * VDD).
00129      */
00130     void write(float value);
00131 
00132     /** Set the output voltage of the MCP4725 from a 16-bit range
00133      *
00134      * @param value The new output voltage as a 16-bit unsigned short (0x0000 to 0xFFFF).
00135      */
00136     void write_u12(unsigned short value);
00137 
00138     /** Get the current DAC settings in EEPROM
00139      *
00140      * @param mode Pointer to a PowerMode enum for the power mode in EEPROM.
00141      * @param value Pointer to an unsigned short for the 12-bit DAC value in EEPROM (0x0000 to 0x0FFF).
00142      */
00143     void readEeprom(PowerMode* mode, unsigned short* value);
00144 
00145     /** Set the DAC settings in EEPROM
00146      *
00147      * @param mode The new EEPROM power mode as a PowerMode enum.
00148      * @param value The new EEPROM DAC value as a 12-bit unsigned short (0x0000 to 0x0FFF).
00149      */
00150     void writeEeprom(PowerMode mode, unsigned short value);
00151 
00152 #ifdef MBED_OPERATORS
00153     /** A shorthand for read()
00154      *
00155      * @returns The current output voltage as a percentage (0.0 to 1.0 * VDD).
00156      */
00157     operator float();
00158 
00159     /** A shorthand for write()
00160      *
00161      * @param value The new output voltage as a percentage (0.0 to 1.0 * VDD).
00162      */
00163     MCP4725& operator=(float value);
00164 #endif
00165 
00166 private:
00167     //Member variables
00168     I2C m_I2C;
00169     const int m_ADDR;
00170     MCP4725::PowerMode m_PowerMode;
00171     unsigned short m_DacValue;
00172 
00173     //Internal functions
00174     void readDac();
00175     void writeDac();
00176 };
00177 
00178 #endif