DAC Lib
Dependents: Cube_Mini_Solution Cube_Mini_Solution
mcp4725.h@1:e78abc423fb5, 2013-10-27 (annotated)
- Committer:
- donalm
- Date:
- Sun Oct 27 14:03:15 2013 +0000
- Revision:
- 1:e78abc423fb5
- Parent:
- 0:3214e3bbf25c
- Child:
- 2:2288d9dfab02
Tidy up before publishing.
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 | 0:3214e3bbf25c | 11 | */ |
donalm | 0:3214e3bbf25c | 12 | class MCP4725 |
donalm | 0:3214e3bbf25c | 13 | { |
donalm | 0:3214e3bbf25c | 14 | public: |
donalm | 0:3214e3bbf25c | 15 | /** The device supports two types of power modes: normal and power-down. In normal mode the device |
donalm | 0:3214e3bbf25c | 16 | * operates a normal digital to analog conversion. In power-down mode all digitial to analog |
donalm | 0:3214e3bbf25c | 17 | * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power |
donalm | 0:3214e3bbf25c | 18 | * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */ |
donalm | 0:3214e3bbf25c | 19 | enum PowerMode { |
donalm | 0:3214e3bbf25c | 20 | /** In normal mode the device operates a normal D2A conversion. */ |
donalm | 0:3214e3bbf25c | 21 | Normal=0, |
donalm | 0:3214e3bbf25c | 22 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */ |
donalm | 0:3214e3bbf25c | 23 | PowerDown1k=1, |
donalm | 0:3214e3bbf25c | 24 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */ |
donalm | 0:3214e3bbf25c | 25 | PowerDown100k=2, |
donalm | 0:3214e3bbf25c | 26 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */ |
donalm | 0:3214e3bbf25c | 27 | PowerDown500k=3 |
donalm | 0:3214e3bbf25c | 28 | }; |
donalm | 0:3214e3bbf25c | 29 | |
donalm | 0:3214e3bbf25c | 30 | /** The device supports 3 different I2C bus frequencies.*/ |
donalm | 0:3214e3bbf25c | 31 | enum BusFrequency { |
donalm | 0:3214e3bbf25c | 32 | /** Standard 100kHz bus. */ |
donalm | 0:3214e3bbf25c | 33 | Standard100kHz, |
donalm | 0:3214e3bbf25c | 34 | /** Fast 400kHz bus. */ |
donalm | 0:3214e3bbf25c | 35 | Fast400kHz, |
donalm | 1:e78abc423fb5 | 36 | /** 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 | 37 | HighSpeed3_4Mhz |
donalm | 0:3214e3bbf25c | 38 | }; |
donalm | 0:3214e3bbf25c | 39 | |
donalm | 0:3214e3bbf25c | 40 | /** Create an mcp4725 I2C interface |
donalm | 0:3214e3bbf25c | 41 | * |
donalm | 0:3214e3bbf25c | 42 | * @param sda I2C data line pin |
donalm | 0:3214e3bbf25c | 43 | * @param scl I2C clock line pin |
donalm | 0:3214e3bbf25c | 44 | * @param bus_frequency the frequency at which the I2C bus is running. |
donalm | 0:3214e3bbf25c | 45 | * @param device_address_bits The 3bit address bits of the device. |
donalm | 0:3214e3bbf25c | 46 | */ |
donalm | 0:3214e3bbf25c | 47 | MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits); |
donalm | 0:3214e3bbf25c | 48 | |
donalm | 0:3214e3bbf25c | 49 | |
donalm | 0:3214e3bbf25c | 50 | /* Update the power mode and DAC value. |
donalm | 0:3214e3bbf25c | 51 | * |
donalm | 0:3214e3bbf25c | 52 | * @param mode The Power mode to set the device into. |
donalm | 0:3214e3bbf25c | 53 | * @param dac_value The 12bit dac register data value. |
donalm | 0:3214e3bbf25c | 54 | * @param writeToEeprom True if the config is to be stored |
donalm | 0:3214e3bbf25c | 55 | * in eeprom, otherwise false |
donalm | 0:3214e3bbf25c | 56 | * |
donalm | 0:3214e3bbf25c | 57 | * @returns |
donalm | 0:3214e3bbf25c | 58 | * 0 on success, |
donalm | 0:3214e3bbf25c | 59 | * non-0 on failure |
donalm | 0:3214e3bbf25c | 60 | */ |
donalm | 0:3214e3bbf25c | 61 | int write(enum PowerMode mode, int dac_value, bool writeToEeprom); |
donalm | 0:3214e3bbf25c | 62 | |
donalm | 0:3214e3bbf25c | 63 | |
donalm | 0:3214e3bbf25c | 64 | /** Read the contents of the dac register, the contents of eeprom, and if an eeprom write is currently active. |
donalm | 0:3214e3bbf25c | 65 | * |
donalm | 0:3214e3bbf25c | 66 | * @param mode Pointer to variable to store the current power mode. |
donalm | 0:3214e3bbf25c | 67 | * @param mode_eeprom Pointer to variable to store the power mode as is stored in eeprom. |
donalm | 0:3214e3bbf25c | 68 | * @param dac_value Pointer to variable to store the current dac value. |
donalm | 0:3214e3bbf25c | 69 | * @param dac_value_eeprom Pointer to variable to store the dac value as is stored in eeprom. |
donalm | 0:3214e3bbf25c | 70 | * @param eeprom_write_in_progress Pointer to variable to store the current eeprom write status. |
donalm | 0:3214e3bbf25c | 71 | * |
donalm | 0:3214e3bbf25c | 72 | * @returns |
donalm | 0:3214e3bbf25c | 73 | * 0 on success, |
donalm | 0:3214e3bbf25c | 74 | * non-0 on failure |
donalm | 0:3214e3bbf25c | 75 | */ |
donalm | 0:3214e3bbf25c | 76 | int read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress); |
donalm | 0:3214e3bbf25c | 77 | |
donalm | 0:3214e3bbf25c | 78 | protected: |
donalm | 0:3214e3bbf25c | 79 | /** mbed I2C interface driver. */ |
donalm | 0:3214e3bbf25c | 80 | I2C * _i2c_interface; |
donalm | 0:3214e3bbf25c | 81 | /** The full i2c device address. */ |
donalm | 0:3214e3bbf25c | 82 | int _device_address; |
donalm | 0:3214e3bbf25c | 83 | |
donalm | 0:3214e3bbf25c | 84 | }; |
donalm | 0:3214e3bbf25c | 85 | |
donalm | 0:3214e3bbf25c | 86 | #endif |