DAC Lib
Dependents: Cube_Mini_Solution Cube_Mini_Solution
mcp4725.h@6:0e58d3068eb8, 2020-01-31 (annotated)
- Committer:
- BoulusAJ
- Date:
- Fri Jan 31 17:59:17 2020 +0000
- Revision:
- 6:0e58d3068eb8
- Parent:
- 5:3e6ffce1eea2
Latest changes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donalm | 0:3214e3bbf25c | 1 | #ifndef MCP_4725_H |
donalm | 0:3214e3bbf25c | 2 | #define MCP_4725_H |
donalm | 0:3214e3bbf25c | 3 | |
donalm | 0:3214e3bbf25c | 4 | #include "stdbool.h" |
donalm | 0:3214e3bbf25c | 5 | #include "mbed.h" |
donalm | 0:3214e3bbf25c | 6 | #include "I2C.h" |
donalm | 0:3214e3bbf25c | 7 | |
donalm | 0:3214e3bbf25c | 8 | |
donalm | 0:3214e3bbf25c | 9 | /** MCP4725 class. |
donalm | 0:3214e3bbf25c | 10 | * Used for interfacing with a mcp4725 12-Bit Digital-to-Analog Converter. |
donalm | 4:e0d667c9b23b | 11 | * To convert between the 12bit dac_value and Vout, use the following formula: dac_value = (Vout*4096/Vref), where Vout is |
donalm | 4:e0d667c9b23b | 12 | * the desired output analog voltage, Vref is the voltage connected to the Vdd pin of the device. Typically Vdd will be 3.3volts. |
donalm | 3:4488787a216b | 13 | * |
donalm | 3:4488787a216b | 14 | * Note: There is an accompanying test suite program "MCP_4725_Library_Test" that can be used to test this library. |
donalm | 3:4488787a216b | 15 | * |
donalm | 0:3214e3bbf25c | 16 | */ |
donalm | 0:3214e3bbf25c | 17 | class MCP4725 |
donalm | 0:3214e3bbf25c | 18 | { |
donalm | 0:3214e3bbf25c | 19 | public: |
donalm | 0:3214e3bbf25c | 20 | /** The device supports two types of power modes: normal and power-down. In normal mode the device |
donalm | 0:3214e3bbf25c | 21 | * operates a normal digital to analog conversion. In power-down mode all digitial to analog |
donalm | 0:3214e3bbf25c | 22 | * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power |
donalm | 0:3214e3bbf25c | 23 | * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */ |
donalm | 0:3214e3bbf25c | 24 | enum PowerMode { |
donalm | 0:3214e3bbf25c | 25 | /** In normal mode the device operates a normal D2A conversion. */ |
donalm | 0:3214e3bbf25c | 26 | Normal=0, |
donalm | 0:3214e3bbf25c | 27 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */ |
donalm | 0:3214e3bbf25c | 28 | PowerDown1k=1, |
donalm | 0:3214e3bbf25c | 29 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */ |
donalm | 0:3214e3bbf25c | 30 | PowerDown100k=2, |
donalm | 0:3214e3bbf25c | 31 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */ |
donalm | 0:3214e3bbf25c | 32 | PowerDown500k=3 |
donalm | 0:3214e3bbf25c | 33 | }; |
donalm | 0:3214e3bbf25c | 34 | |
donalm | 0:3214e3bbf25c | 35 | /** The device supports 3 different I2C bus frequencies.*/ |
donalm | 0:3214e3bbf25c | 36 | enum BusFrequency { |
donalm | 0:3214e3bbf25c | 37 | /** Standard 100kHz bus. */ |
donalm | 0:3214e3bbf25c | 38 | Standard100kHz, |
donalm | 0:3214e3bbf25c | 39 | /** Fast 400kHz bus. */ |
donalm | 0:3214e3bbf25c | 40 | Fast400kHz, |
donalm | 1:e78abc423fb5 | 41 | /** High Speed 3.4Mhz bus. WARNING: the test suite fails for the mbed LPC1768 when this frequency is selected - not tested on other platforms.*/ |
donalm | 0:3214e3bbf25c | 42 | HighSpeed3_4Mhz |
donalm | 0:3214e3bbf25c | 43 | }; |
donalm | 0:3214e3bbf25c | 44 | |
donalm | 0:3214e3bbf25c | 45 | /** Create an mcp4725 I2C interface |
donalm | 0:3214e3bbf25c | 46 | * |
donalm | 0:3214e3bbf25c | 47 | * @param sda I2C data line pin |
donalm | 0:3214e3bbf25c | 48 | * @param scl I2C clock line pin |
donalm | 0:3214e3bbf25c | 49 | * @param bus_frequency the frequency at which the I2C bus is running. |
donalm | 0:3214e3bbf25c | 50 | * @param device_address_bits The 3bit address bits of the device. |
donalm | 0:3214e3bbf25c | 51 | */ |
donalm | 0:3214e3bbf25c | 52 | MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits); |
donalm | 0:3214e3bbf25c | 53 | |
donalm | 0:3214e3bbf25c | 54 | |
donalm | 0:3214e3bbf25c | 55 | /* Update the power mode and DAC value. |
donalm | 0:3214e3bbf25c | 56 | * |
donalm | 0:3214e3bbf25c | 57 | * @param mode The Power mode to set the device into. |
donalm | 0:3214e3bbf25c | 58 | * @param dac_value The 12bit dac register data value. |
donalm | 0:3214e3bbf25c | 59 | * @param writeToEeprom True if the config is to be stored |
donalm | 0:3214e3bbf25c | 60 | * in eeprom, otherwise false |
donalm | 0:3214e3bbf25c | 61 | * |
donalm | 0:3214e3bbf25c | 62 | * @returns |
donalm | 0:3214e3bbf25c | 63 | * 0 on success, |
donalm | 0:3214e3bbf25c | 64 | * non-0 on failure |
donalm | 0:3214e3bbf25c | 65 | */ |
BoulusAJ | 6:0e58d3068eb8 | 66 | int write(float dac_value, enum PowerMode mode = MCP4725::Normal, bool writeToEeprom = false); |
donalm | 0:3214e3bbf25c | 67 | |
donalm | 0:3214e3bbf25c | 68 | |
donalm | 0:3214e3bbf25c | 69 | /** Read the contents of the dac register, the contents of eeprom, and if an eeprom write is currently active. |
donalm | 0:3214e3bbf25c | 70 | * |
donalm | 0:3214e3bbf25c | 71 | * @param mode Pointer to variable to store the current power mode. |
donalm | 0:3214e3bbf25c | 72 | * @param mode_eeprom Pointer to variable to store the power mode as is stored in eeprom. |
donalm | 0:3214e3bbf25c | 73 | * @param dac_value Pointer to variable to store the current dac value. |
donalm | 0:3214e3bbf25c | 74 | * @param dac_value_eeprom Pointer to variable to store the dac value as is stored in eeprom. |
donalm | 0:3214e3bbf25c | 75 | * @param eeprom_write_in_progress Pointer to variable to store the current eeprom write status. |
donalm | 0:3214e3bbf25c | 76 | * |
donalm | 0:3214e3bbf25c | 77 | * @returns |
donalm | 0:3214e3bbf25c | 78 | * 0 on success, |
donalm | 0:3214e3bbf25c | 79 | * non-0 on failure |
donalm | 0:3214e3bbf25c | 80 | */ |
donalm | 0:3214e3bbf25c | 81 | int read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress); |
donalm | 0:3214e3bbf25c | 82 | |
donalm | 0:3214e3bbf25c | 83 | protected: |
donalm | 0:3214e3bbf25c | 84 | /** mbed I2C interface driver. */ |
donalm | 5:3e6ffce1eea2 | 85 | I2C _i2c_interface; |
donalm | 0:3214e3bbf25c | 86 | /** The full i2c device address. */ |
donalm | 0:3214e3bbf25c | 87 | int _device_address; |
donalm | 0:3214e3bbf25c | 88 | |
donalm | 0:3214e3bbf25c | 89 | }; |
donalm | 0:3214e3bbf25c | 90 | |
donalm | 0:3214e3bbf25c | 91 | #endif |