GRT_FS2019_VoiceCoil / MCP4725

Dependents:   Cube_Mini_Template

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mcp4725.h Source File

mcp4725.h

00001 #ifndef MCP_4725_H
00002 #define MCP_4725_H
00003 
00004 #include "stdbool.h"
00005 #include "mbed.h"
00006 #include "I2C.h"
00007 
00008 
00009 /** MCP4725 class.
00010  *  Used for interfacing with a mcp4725 12-Bit Digital-to-Analog Converter.
00011  *  To convert between the 12bit dac_value and Vout, use the following formula: dac_value = (Vout*4096/Vref), where Vout is  
00012  *  the desired output analog voltage, Vref is the voltage connected to the Vdd pin of the device. Typically Vdd will be 3.3volts.
00013  *
00014  *  Note: There is an accompanying test suite program "MCP_4725_Library_Test" that can be used to test this library.
00015  *  
00016  */
00017 class MCP4725
00018 {
00019     public:
00020     /** The device supports two types of power modes: normal and power-down. In normal mode the device
00021      * operates a normal digital to analog conversion. In power-down mode all digitial to analog  
00022      * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power 
00023      * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */
00024     enum PowerMode {
00025         /** In normal mode the device operates a normal D2A conversion. */
00026         Normal=0,
00027         /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */
00028         PowerDown1k=1,
00029         /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */
00030         PowerDown100k=2,
00031         /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */
00032         PowerDown500k=3
00033     };
00034     
00035     /** The device supports 3 different I2C bus frequencies.*/
00036     enum BusFrequency {
00037         /** Standard 100kHz bus. */
00038         Standard100kHz,
00039         /** Fast 400kHz bus. */
00040         Fast400kHz,
00041         /** High Speed 3.4Mhz bus. WARNING: the test suite fails for the mbed LPC1768 when this frequency is selected - not tested on other platforms.*/
00042         HighSpeed3_4Mhz
00043     };
00044     
00045     /** Create an mcp4725 I2C interface
00046      * 
00047      * @param sda I2C data line pin
00048      * @param scl I2C clock line pin
00049      * @param bus_frequency the frequency at which the I2C bus is running.
00050      * @param device_address_bits The 3bit address bits of the device.
00051      */
00052     MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits);
00053     
00054     
00055     /* Update the power mode and DAC value.
00056      *
00057      * @param   mode The Power mode to set the device into.
00058      * @param   dac_value The 12bit dac register data value.
00059      * @param   writeToEeprom True if the config is to be stored 
00060      *          in eeprom, otherwise false
00061      * 
00062      * @returns 
00063      *       0 on success,
00064      *   non-0 on failure
00065      */
00066     int write(float dac_value, enum PowerMode mode = MCP4725::Normal, bool writeToEeprom = false);
00067     
00068         
00069     /** Read the contents of the dac register, the contents of eeprom, and if an eeprom write is currently active.
00070      * 
00071      * @param   mode Pointer to variable to store the current power mode.
00072      * @param   mode_eeprom Pointer to variable to store the power mode as is stored in eeprom.
00073      * @param   dac_value Pointer to variable to store the current dac value.
00074      * @param   dac_value_eeprom Pointer to variable to store the dac value as is stored in eeprom.
00075      * @param   eeprom_write_in_progress Pointer to variable to store the current eeprom write status.
00076      *
00077      * @returns 
00078      *       0 on success,
00079      *   non-0 on failure
00080      */
00081     int read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress);
00082     
00083     protected:
00084     /** mbed I2C interface driver. */
00085     I2C _i2c_interface;
00086     /** The full i2c device address. */
00087     int _device_address;
00088     
00089 };
00090 
00091 #endif