Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of test01 by
mcp4725.h@0:77006df50c28, 2016-11-02 (annotated)
- Committer:
- xWhitfordx
- Date:
- Wed Nov 02 15:41:23 2016 +0000
- Revision:
- 0:77006df50c28
test01
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xWhitfordx | 0:77006df50c28 | 1 | #ifndef MCP_4725_H |
xWhitfordx | 0:77006df50c28 | 2 | #define MCP_4725_H |
xWhitfordx | 0:77006df50c28 | 3 | |
xWhitfordx | 0:77006df50c28 | 4 | #include "stdbool.h" |
xWhitfordx | 0:77006df50c28 | 5 | #include "mbed.h" |
xWhitfordx | 0:77006df50c28 | 6 | #include "I2C.h" |
xWhitfordx | 0:77006df50c28 | 7 | |
xWhitfordx | 0:77006df50c28 | 8 | |
xWhitfordx | 0:77006df50c28 | 9 | /** MCP4725 class. |
xWhitfordx | 0:77006df50c28 | 10 | * Used for interfacing with a mcp4725 12-Bit Digital-to-Analog Converter. |
xWhitfordx | 0:77006df50c28 | 11 | * To convert between the 12bit dac_value and Vout, use the following formula: dac_value = (Vout*4096/Vref), where Vout is |
xWhitfordx | 0:77006df50c28 | 12 | * the desired output analog voltage, Vref is the voltage connected to the Vdd pin of the device. Typically Vdd will be 3.3volts. |
xWhitfordx | 0:77006df50c28 | 13 | * |
xWhitfordx | 0:77006df50c28 | 14 | * Note: There is an accompanying test suite program "MCP_4725_Library_Test" that can be used to test this library. |
xWhitfordx | 0:77006df50c28 | 15 | * |
xWhitfordx | 0:77006df50c28 | 16 | */ |
xWhitfordx | 0:77006df50c28 | 17 | class MCP4725 |
xWhitfordx | 0:77006df50c28 | 18 | { |
xWhitfordx | 0:77006df50c28 | 19 | public: |
xWhitfordx | 0:77006df50c28 | 20 | /** The device supports two types of power modes: normal and power-down. In normal mode the device |
xWhitfordx | 0:77006df50c28 | 21 | * operates a normal digital to analog conversion. In power-down mode all digitial to analog |
xWhitfordx | 0:77006df50c28 | 22 | * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power |
xWhitfordx | 0:77006df50c28 | 23 | * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */ |
xWhitfordx | 0:77006df50c28 | 24 | enum PowerMode { |
xWhitfordx | 0:77006df50c28 | 25 | /** In normal mode the device operates a normal D2A conversion. */ |
xWhitfordx | 0:77006df50c28 | 26 | Normal=0, |
xWhitfordx | 0:77006df50c28 | 27 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */ |
xWhitfordx | 0:77006df50c28 | 28 | PowerDown1k=1, |
xWhitfordx | 0:77006df50c28 | 29 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */ |
xWhitfordx | 0:77006df50c28 | 30 | PowerDown100k=2, |
xWhitfordx | 0:77006df50c28 | 31 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */ |
xWhitfordx | 0:77006df50c28 | 32 | PowerDown500k=3 |
xWhitfordx | 0:77006df50c28 | 33 | }; |
xWhitfordx | 0:77006df50c28 | 34 | |
xWhitfordx | 0:77006df50c28 | 35 | /** The device supports 3 different I2C bus frequencies.*/ |
xWhitfordx | 0:77006df50c28 | 36 | enum BusFrequency { |
xWhitfordx | 0:77006df50c28 | 37 | /** Standard 100kHz bus. */ |
xWhitfordx | 0:77006df50c28 | 38 | Standard100kHz, |
xWhitfordx | 0:77006df50c28 | 39 | /** Fast 400kHz bus. */ |
xWhitfordx | 0:77006df50c28 | 40 | Fast400kHz, |
xWhitfordx | 0:77006df50c28 | 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.*/ |
xWhitfordx | 0:77006df50c28 | 42 | HighSpeed3_4Mhz |
xWhitfordx | 0:77006df50c28 | 43 | }; |
xWhitfordx | 0:77006df50c28 | 44 | |
xWhitfordx | 0:77006df50c28 | 45 | /** Create an mcp4725 I2C interface |
xWhitfordx | 0:77006df50c28 | 46 | * |
xWhitfordx | 0:77006df50c28 | 47 | * @param sda I2C data line pin |
xWhitfordx | 0:77006df50c28 | 48 | * @param scl I2C clock line pin |
xWhitfordx | 0:77006df50c28 | 49 | * @param bus_frequency the frequency at which the I2C bus is running. |
xWhitfordx | 0:77006df50c28 | 50 | * @param device_address_bits The 3bit address bits of the device. |
xWhitfordx | 0:77006df50c28 | 51 | */ |
xWhitfordx | 0:77006df50c28 | 52 | MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits); |
xWhitfordx | 0:77006df50c28 | 53 | |
xWhitfordx | 0:77006df50c28 | 54 | |
xWhitfordx | 0:77006df50c28 | 55 | /* Update the power mode and DAC value. |
xWhitfordx | 0:77006df50c28 | 56 | * |
xWhitfordx | 0:77006df50c28 | 57 | * @param mode The Power mode to set the device into. |
xWhitfordx | 0:77006df50c28 | 58 | * @param dac_value The 12bit dac register data value. |
xWhitfordx | 0:77006df50c28 | 59 | * @param writeToEeprom True if the config is to be stored |
xWhitfordx | 0:77006df50c28 | 60 | * in eeprom, otherwise false |
xWhitfordx | 0:77006df50c28 | 61 | * |
xWhitfordx | 0:77006df50c28 | 62 | * @returns |
xWhitfordx | 0:77006df50c28 | 63 | * 0 on success, |
xWhitfordx | 0:77006df50c28 | 64 | * non-0 on failure |
xWhitfordx | 0:77006df50c28 | 65 | */ |
xWhitfordx | 0:77006df50c28 | 66 | int write(enum PowerMode mode, int dac_value, bool writeToEeprom); |
xWhitfordx | 0:77006df50c28 | 67 | |
xWhitfordx | 0:77006df50c28 | 68 | |
xWhitfordx | 0:77006df50c28 | 69 | /** Read the contents of the dac register, the contents of eeprom, and if an eeprom write is currently active. |
xWhitfordx | 0:77006df50c28 | 70 | * |
xWhitfordx | 0:77006df50c28 | 71 | * @param mode Pointer to variable to store the current power mode. |
xWhitfordx | 0:77006df50c28 | 72 | * @param mode_eeprom Pointer to variable to store the power mode as is stored in eeprom. |
xWhitfordx | 0:77006df50c28 | 73 | * @param dac_value Pointer to variable to store the current dac value. |
xWhitfordx | 0:77006df50c28 | 74 | * @param dac_value_eeprom Pointer to variable to store the dac value as is stored in eeprom. |
xWhitfordx | 0:77006df50c28 | 75 | * @param eeprom_write_in_progress Pointer to variable to store the current eeprom write status. |
xWhitfordx | 0:77006df50c28 | 76 | * |
xWhitfordx | 0:77006df50c28 | 77 | * @returns |
xWhitfordx | 0:77006df50c28 | 78 | * 0 on success, |
xWhitfordx | 0:77006df50c28 | 79 | * non-0 on failure |
xWhitfordx | 0:77006df50c28 | 80 | */ |
xWhitfordx | 0:77006df50c28 | 81 | int read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress); |
xWhitfordx | 0:77006df50c28 | 82 | |
xWhitfordx | 0:77006df50c28 | 83 | protected: |
xWhitfordx | 0:77006df50c28 | 84 | /** mbed I2C interface driver. */ |
xWhitfordx | 0:77006df50c28 | 85 | I2C _i2c_interface; |
xWhitfordx | 0:77006df50c28 | 86 | /** The full i2c device address. */ |
xWhitfordx | 0:77006df50c28 | 87 | int _device_address; |
xWhitfordx | 0:77006df50c28 | 88 | |
xWhitfordx | 0:77006df50c28 | 89 | }; |
xWhitfordx | 0:77006df50c28 | 90 | |
xWhitfordx | 0:77006df50c28 | 91 | #endif |