General usable MCP4728 quad DAC implementation only limited function has to be used together with the DevInterface lib
Dependents: mbedSerialInterface_talkback2 MCP4728test mbedSerialInterface_sequencer
mcp4728.h@0:7a0ebc527fb9, 2015-10-23 (annotated)
- Committer:
- wbeaumont
- Date:
- Fri Oct 23 19:35:27 2015 +0000
- Revision:
- 0:7a0ebc527fb9
- Child:
- 1:cd7c70a46739
set ch1 hard coding is working a lot of copied code not checked
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 0:7a0ebc527fb9 | 1 | #ifndef MCP_4728_H |
wbeaumont | 0:7a0ebc527fb9 | 2 | #define MCP_4728_H |
wbeaumont | 0:7a0ebc527fb9 | 3 | |
wbeaumont | 0:7a0ebc527fb9 | 4 | |
wbeaumont | 0:7a0ebc527fb9 | 5 | #include "stdbool.h" |
wbeaumont | 0:7a0ebc527fb9 | 6 | |
wbeaumont | 0:7a0ebc527fb9 | 7 | #include "dev_interface_def.h" |
wbeaumont | 0:7a0ebc527fb9 | 8 | #include "I2CInterface.h" |
wbeaumont | 0:7a0ebc527fb9 | 9 | #include "DACInterface.h" |
wbeaumont | 0:7a0ebc527fb9 | 10 | |
wbeaumont | 0:7a0ebc527fb9 | 11 | #define VERSION_MCP4728_HDR "0.01" |
wbeaumont | 0:7a0ebc527fb9 | 12 | |
wbeaumont | 0:7a0ebc527fb9 | 13 | /** MCP4728 class. |
wbeaumont | 0:7a0ebc527fb9 | 14 | * Used for interfacing with a mcp4728 12-Bit QUAD Digital-to-Analog Converter. |
wbeaumont | 0:7a0ebc527fb9 | 15 | * To convert between the 12bit dac_value and Vout, use the following formula: dac_value = (Vout*4096/Vref), where Vout is |
wbeaumont | 0:7a0ebc527fb9 | 16 | * the desired output analog voltage, Vref is the voltage connected to the Vdd pin of the device. Typically Vdd will be 3.3volts. |
wbeaumont | 0:7a0ebc527fb9 | 17 | * |
wbeaumont | 0:7a0ebc527fb9 | 18 | * (C) Wim Beaumont Universiteit Antwerpen 2015 |
wbeaumont | 0:7a0ebc527fb9 | 19 | * |
wbeaumont | 0:7a0ebc527fb9 | 20 | */ |
wbeaumont | 0:7a0ebc527fb9 | 21 | class MCP4728 : public DACInterface |
wbeaumont | 0:7a0ebc527fb9 | 22 | { |
wbeaumont | 0:7a0ebc527fb9 | 23 | float vref ; |
wbeaumont | 0:7a0ebc527fb9 | 24 | |
wbeaumont | 0:7a0ebc527fb9 | 25 | |
wbeaumont | 0:7a0ebc527fb9 | 26 | public: |
wbeaumont | 0:7a0ebc527fb9 | 27 | /** The device supports two types of power modes: normal and power-down. In normal mode the device |
wbeaumont | 0:7a0ebc527fb9 | 28 | * operates a normal digital to analog conversion. In power-down mode all digitial to analog |
wbeaumont | 0:7a0ebc527fb9 | 29 | * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power |
wbeaumont | 0:7a0ebc527fb9 | 30 | * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */ |
wbeaumont | 0:7a0ebc527fb9 | 31 | enum PowerMode { |
wbeaumont | 0:7a0ebc527fb9 | 32 | /** In normal mode the device operates a normal D2A conversion. */ |
wbeaumont | 0:7a0ebc527fb9 | 33 | Normal=0, |
wbeaumont | 0:7a0ebc527fb9 | 34 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */ |
wbeaumont | 0:7a0ebc527fb9 | 35 | PowerDown1k=1, |
wbeaumont | 0:7a0ebc527fb9 | 36 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */ |
wbeaumont | 0:7a0ebc527fb9 | 37 | PowerDown100k=2, |
wbeaumont | 0:7a0ebc527fb9 | 38 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */ |
wbeaumont | 0:7a0ebc527fb9 | 39 | PowerDown500k=3 |
wbeaumont | 0:7a0ebc527fb9 | 40 | }; |
wbeaumont | 0:7a0ebc527fb9 | 41 | |
wbeaumont | 0:7a0ebc527fb9 | 42 | |
wbeaumont | 0:7a0ebc527fb9 | 43 | |
wbeaumont | 0:7a0ebc527fb9 | 44 | enum VrefMode { |
wbeaumont | 0:7a0ebc527fb9 | 45 | ExternRef=0, // == VDD |
wbeaumont | 0:7a0ebc527fb9 | 46 | InternRef=1 |
wbeaumont | 0:7a0ebc527fb9 | 47 | |
wbeaumont | 0:7a0ebc527fb9 | 48 | }; |
wbeaumont | 0:7a0ebc527fb9 | 49 | |
wbeaumont | 0:7a0ebc527fb9 | 50 | |
wbeaumont | 0:7a0ebc527fb9 | 51 | enum GainMode { |
wbeaumont | 0:7a0ebc527fb9 | 52 | GainX1=0, |
wbeaumont | 0:7a0ebc527fb9 | 53 | GainX2=1 |
wbeaumont | 0:7a0ebc527fb9 | 54 | }; |
wbeaumont | 0:7a0ebc527fb9 | 55 | |
wbeaumont | 0:7a0ebc527fb9 | 56 | typedef struct MCP4728ChCfg{ |
wbeaumont | 0:7a0ebc527fb9 | 57 | GainMode gain; |
wbeaumont | 0:7a0ebc527fb9 | 58 | VrefMode vref; |
wbeaumont | 0:7a0ebc527fb9 | 59 | PowerMode pwr; |
wbeaumont | 0:7a0ebc527fb9 | 60 | } MCP4728ChCfgdummy; |
wbeaumont | 0:7a0ebc527fb9 | 61 | |
wbeaumont | 0:7a0ebc527fb9 | 62 | MCP4728ChCfg ChCfg[4]; |
wbeaumont | 0:7a0ebc527fb9 | 63 | |
wbeaumont | 0:7a0ebc527fb9 | 64 | |
wbeaumont | 0:7a0ebc527fb9 | 65 | /** The device supports 3 different I2C bus frequencies.*/ |
wbeaumont | 0:7a0ebc527fb9 | 66 | enum BusFrequency { |
wbeaumont | 0:7a0ebc527fb9 | 67 | /** Standard 100kHz bus. */ |
wbeaumont | 0:7a0ebc527fb9 | 68 | Standard100kHz, |
wbeaumont | 0:7a0ebc527fb9 | 69 | /** Fast 400kHz bus. */ |
wbeaumont | 0:7a0ebc527fb9 | 70 | Fast400kHz, |
wbeaumont | 0:7a0ebc527fb9 | 71 | /** High Speed 3.4Mhz bus. WARNING: the test suite fails for the mbed LPC1768 when this frequency is selected - not tested on other platforms.*/ |
wbeaumont | 0:7a0ebc527fb9 | 72 | HighSpeed3_4Mhz |
wbeaumont | 0:7a0ebc527fb9 | 73 | }; |
wbeaumont | 0:7a0ebc527fb9 | 74 | |
wbeaumont | 0:7a0ebc527fb9 | 75 | /** Create an mcp4725 I2C interface |
wbeaumont | 0:7a0ebc527fb9 | 76 | * |
wbeaumont | 0:7a0ebc527fb9 | 77 | * @param i2cinterface pointer to the MBED I2C interface |
wbeaumont | 0:7a0ebc527fb9 | 78 | * @param scl I2C clock line pin |
wbeaumont | 0:7a0ebc527fb9 | 79 | * @param bus_frequency the frequency at which the I2C bus is running. |
wbeaumont | 0:7a0ebc527fb9 | 80 | * @param device_address_bits The 3bit address bits of the device. |
wbeaumont | 0:7a0ebc527fb9 | 81 | */ |
wbeaumont | 0:7a0ebc527fb9 | 82 | MCP4728(I2CInterface* i2cinterface, int device_address_bits); |
wbeaumont | 0:7a0ebc527fb9 | 83 | |
wbeaumont | 0:7a0ebc527fb9 | 84 | |
wbeaumont | 0:7a0ebc527fb9 | 85 | /* Update the power mode and DAC value. |
wbeaumont | 0:7a0ebc527fb9 | 86 | * |
wbeaumont | 0:7a0ebc527fb9 | 87 | * @param mode The Power mode to set the device into. |
wbeaumont | 0:7a0ebc527fb9 | 88 | * @param dac_value The 12bit dac register data value. |
wbeaumont | 0:7a0ebc527fb9 | 89 | * @param writeToEeprom True if the config is to be stored |
wbeaumont | 0:7a0ebc527fb9 | 90 | * in eeprom, otherwise false |
wbeaumont | 0:7a0ebc527fb9 | 91 | * |
wbeaumont | 0:7a0ebc527fb9 | 92 | * @returns |
wbeaumont | 0:7a0ebc527fb9 | 93 | * 0 on success, |
wbeaumont | 0:7a0ebc527fb9 | 94 | * non-0 on failure |
wbeaumont | 0:7a0ebc527fb9 | 95 | */ |
wbeaumont | 0:7a0ebc527fb9 | 96 | int write(enum PowerMode mode, int dac_value, bool writeToEeprom); |
wbeaumont | 0:7a0ebc527fb9 | 97 | |
wbeaumont | 0:7a0ebc527fb9 | 98 | |
wbeaumont | 0:7a0ebc527fb9 | 99 | /** Read the contents of the dac register, the contents of eeprom, and if an eeprom write is currently active. |
wbeaumont | 0:7a0ebc527fb9 | 100 | * |
wbeaumont | 0:7a0ebc527fb9 | 101 | * @param mode Pointer to variable to store the current power mode. |
wbeaumont | 0:7a0ebc527fb9 | 102 | * @param mode_eeprom Pointer to variable to store the power mode as is stored in eeprom. |
wbeaumont | 0:7a0ebc527fb9 | 103 | * @param dac_value Pointer to variable to store the current dac value. |
wbeaumont | 0:7a0ebc527fb9 | 104 | * @param dac_value_eeprom Pointer to variable to store the dac value as is stored in eeprom. |
wbeaumont | 0:7a0ebc527fb9 | 105 | * @param eeprom_write_in_progress Pointer to variable to store the current eeprom write status. |
wbeaumont | 0:7a0ebc527fb9 | 106 | * |
wbeaumont | 0:7a0ebc527fb9 | 107 | * @returns |
wbeaumont | 0:7a0ebc527fb9 | 108 | * 0 on success, |
wbeaumont | 0:7a0ebc527fb9 | 109 | * non-0 on failure |
wbeaumont | 0:7a0ebc527fb9 | 110 | */ |
wbeaumont | 0:7a0ebc527fb9 | 111 | int read(enum PowerMode& mode, enum PowerMode& mode_eeprom, int& dac_value, int& dac_value_eeprom, bool& eeprom_write_in_progress); |
wbeaumont | 0:7a0ebc527fb9 | 112 | |
wbeaumont | 0:7a0ebc527fb9 | 113 | virtual int getDACvalue(int &value, int ch=0); |
wbeaumont | 0:7a0ebc527fb9 | 114 | virtual int setDACvalue( int value, int ch=0); |
wbeaumont | 0:7a0ebc527fb9 | 115 | protected: |
wbeaumont | 0:7a0ebc527fb9 | 116 | /** mbed I2C interface driver. */ |
wbeaumont | 0:7a0ebc527fb9 | 117 | I2CInterface* _i2c_interface; |
wbeaumont | 0:7a0ebc527fb9 | 118 | /** The full i2c device address. */ |
wbeaumont | 0:7a0ebc527fb9 | 119 | int _device_address; |
wbeaumont | 0:7a0ebc527fb9 | 120 | |
wbeaumont | 0:7a0ebc527fb9 | 121 | }; |
wbeaumont | 0:7a0ebc527fb9 | 122 | |
wbeaumont | 0:7a0ebc527fb9 | 123 | #endif |