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@3:2a2eafba8a1d, 2016-01-12 (annotated)
- Committer:
- wbeaumont
- Date:
- Tue Jan 12 15:45:23 2016 +0000
- Revision:
- 3:2a2eafba8a1d
- Parent:
- 2:366e996af95c
- Child:
- 4:f45f4ff5a51a
added more API comments, change to requested config bit for the setVoltage member
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 | 1:cd7c70a46739 | 3 | #include "getVersion.h" |
wbeaumont | 0:7a0ebc527fb9 | 4 | |
wbeaumont | 0:7a0ebc527fb9 | 5 | #include "stdbool.h" |
wbeaumont | 0:7a0ebc527fb9 | 6 | |
wbeaumont | 2:366e996af95c | 7 | |
wbeaumont | 0:7a0ebc527fb9 | 8 | #include "dev_interface_def.h" |
wbeaumont | 0:7a0ebc527fb9 | 9 | #include "I2CInterface.h" |
wbeaumont | 0:7a0ebc527fb9 | 10 | #include "DACInterface.h" |
wbeaumont | 0:7a0ebc527fb9 | 11 | |
wbeaumont | 3:2a2eafba8a1d | 12 | #define VERSION_MCP4728_HDR "0.45" |
wbeaumont | 0:7a0ebc527fb9 | 13 | |
wbeaumont | 0:7a0ebc527fb9 | 14 | /** MCP4728 class. |
wbeaumont | 0:7a0ebc527fb9 | 15 | * Used for interfacing with a mcp4728 12-Bit QUAD Digital-to-Analog Converter. |
wbeaumont | 1:cd7c70a46739 | 16 | * For version 0.4X mode can not be set ( Intern ref, gain x2 power mode normal) |
wbeaumont | 2:366e996af95c | 17 | * It has to be used with the https://developer.mbed.org/users/wbeaumont/code/DevInterfaces/ package |
wbeaumont | 2:366e996af95c | 18 | * This includes the "virtual" I2CInterface class that is the interface to the I2C device |
wbeaumont | 2:366e996af95c | 19 | * An implementation of the I2Cinterface class for the MBED can be found at |
wbeaumont | 2:366e996af95c | 20 | * |
wbeaumont | 1:cd7c70a46739 | 21 | * ( DACInterface inherits getVersion ) |
wbeaumont | 2:366e996af95c | 22 | * ee-prom set / read not tested / implemented. |
wbeaumont | 2:366e996af95c | 23 | * |
wbeaumont | 0:7a0ebc527fb9 | 24 | * (C) Wim Beaumont Universiteit Antwerpen 2015 |
wbeaumont | 0:7a0ebc527fb9 | 25 | * |
wbeaumont | 0:7a0ebc527fb9 | 26 | */ |
wbeaumont | 1:cd7c70a46739 | 27 | class MCP4728 : public DACInterface { |
wbeaumont | 1:cd7c70a46739 | 28 | |
wbeaumont | 0:7a0ebc527fb9 | 29 | public: |
wbeaumont | 0:7a0ebc527fb9 | 30 | /** The device supports two types of power modes: normal and power-down. In normal mode the device |
wbeaumont | 0:7a0ebc527fb9 | 31 | * operates a normal digital to analog conversion. In power-down mode all digitial to analog |
wbeaumont | 0:7a0ebc527fb9 | 32 | * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power |
wbeaumont | 0:7a0ebc527fb9 | 33 | * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */ |
wbeaumont | 0:7a0ebc527fb9 | 34 | enum PowerMode { |
wbeaumont | 0:7a0ebc527fb9 | 35 | /** In normal mode the device operates a normal D2A conversion. */ |
wbeaumont | 0:7a0ebc527fb9 | 36 | Normal=0, |
wbeaumont | 0:7a0ebc527fb9 | 37 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */ |
wbeaumont | 0:7a0ebc527fb9 | 38 | PowerDown1k=1, |
wbeaumont | 0:7a0ebc527fb9 | 39 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */ |
wbeaumont | 0:7a0ebc527fb9 | 40 | PowerDown100k=2, |
wbeaumont | 0:7a0ebc527fb9 | 41 | /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */ |
wbeaumont | 0:7a0ebc527fb9 | 42 | PowerDown500k=3 |
wbeaumont | 0:7a0ebc527fb9 | 43 | }; |
wbeaumont | 2:366e996af95c | 44 | /** Voltage reference can be either External, this means the Vdd or internal , 2.048 V |
wbeaumont | 2:366e996af95c | 45 | The output goes from 0 to Vref ( either 2.048 V or Vdd ), |
wbeaumont | 2:366e996af95c | 46 | */ |
wbeaumont | 0:7a0ebc527fb9 | 47 | enum VrefMode { |
wbeaumont | 0:7a0ebc527fb9 | 48 | ExternRef=0, // == VDD |
wbeaumont | 1:cd7c70a46739 | 49 | InternRef=1 |
wbeaumont | 0:7a0ebc527fb9 | 50 | }; |
wbeaumont | 2:366e996af95c | 51 | /** In case the internal reference is used the output can be multiplied by a factor 2. |
wbeaumont | 2:366e996af95c | 52 | in that case the output goes between 0 and 4.096 V |
wbeaumont | 2:366e996af95c | 53 | */ |
wbeaumont | 0:7a0ebc527fb9 | 54 | enum GainMode { |
wbeaumont | 0:7a0ebc527fb9 | 55 | GainX1=0, |
wbeaumont | 0:7a0ebc527fb9 | 56 | GainX2=1 |
wbeaumont | 0:7a0ebc527fb9 | 57 | }; |
wbeaumont | 0:7a0ebc527fb9 | 58 | |
wbeaumont | 1:cd7c70a46739 | 59 | |
wbeaumont | 1:cd7c70a46739 | 60 | |
wbeaumont | 0:7a0ebc527fb9 | 61 | |
wbeaumont | 0:7a0ebc527fb9 | 62 | |
wbeaumont | 0:7a0ebc527fb9 | 63 | /** The device supports 3 different I2C bus frequencies.*/ |
wbeaumont | 0:7a0ebc527fb9 | 64 | enum BusFrequency { |
wbeaumont | 0:7a0ebc527fb9 | 65 | /** Standard 100kHz bus. */ |
wbeaumont | 0:7a0ebc527fb9 | 66 | Standard100kHz, |
wbeaumont | 0:7a0ebc527fb9 | 67 | /** Fast 400kHz bus. */ |
wbeaumont | 0:7a0ebc527fb9 | 68 | Fast400kHz, |
wbeaumont | 0:7a0ebc527fb9 | 69 | /** 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 | 70 | HighSpeed3_4Mhz |
wbeaumont | 0:7a0ebc527fb9 | 71 | }; |
wbeaumont | 2:366e996af95c | 72 | /** structure used to set the channel configuration */ |
wbeaumont | 2:366e996af95c | 73 | typedef struct MCP4728ChCfg{ |
wbeaumont | 2:366e996af95c | 74 | GainMode gain; |
wbeaumont | 2:366e996af95c | 75 | VrefMode vref; |
wbeaumont | 2:366e996af95c | 76 | PowerMode pwr; |
wbeaumont | 2:366e996af95c | 77 | } MCP4728ChCfgdummy; |
wbeaumont | 2:366e996af95c | 78 | |
wbeaumont | 0:7a0ebc527fb9 | 79 | |
wbeaumont | 0:7a0ebc527fb9 | 80 | /** Create an mcp4725 I2C interface |
wbeaumont | 0:7a0ebc527fb9 | 81 | * |
wbeaumont | 1:cd7c70a46739 | 82 | * @param i2cinterface pointer to the I2C interface (proto type) |
wbeaumont | 0:7a0ebc527fb9 | 83 | * @param device_address_bits The 3bit address bits of the device. |
wbeaumont | 3:2a2eafba8a1d | 84 | * @param Vdd the voltage on the Vdd pin ( power) is used to calculate voltage is Vref is set to external |
wbeaumont | 0:7a0ebc527fb9 | 85 | */ |
wbeaumont | 1:cd7c70a46739 | 86 | MCP4728(I2CInterface* i2cinterface, int device_address_bits, float Vdd ); |
wbeaumont | 0:7a0ebc527fb9 | 87 | |
wbeaumont | 1:cd7c70a46739 | 88 | |
wbeaumont | 1:cd7c70a46739 | 89 | /** Read the contents of the dac registers,( also eeprom settings) , and if an eeprom write is currently active. |
wbeaumont | 1:cd7c70a46739 | 90 | * result of the reading is stored in the channelstatus structure . |
wbeaumont | 1:cd7c70a46739 | 91 | * @param checkall check if the channel nr and adress is returned correctly . |
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 | 3:2a2eafba8a1d | 96 | int update(bool checkall); |
wbeaumont | 3:2a2eafba8a1d | 97 | |
wbeaumont | 1:cd7c70a46739 | 98 | virtual int update() { return update(false);} |
wbeaumont | 3:2a2eafba8a1d | 99 | |
wbeaumont | 3:2a2eafba8a1d | 100 | |
wbeaumont | 1:cd7c70a46739 | 101 | /** format a string with the channel status |
wbeaumont | 1:cd7c70a46739 | 102 | * @param ch : the channel nr |
wbeaumont | 3:2a2eafba8a1d | 103 | * @param updatereq: if true the function update(true) is called else the class status table is used . |
wbeaumont | 3:2a2eafba8a1d | 104 | * @param eprom: if true the values in the eprom are used else the values of the DAC channel |
wbeaumont | 1:cd7c70a46739 | 105 | * @param str a pointer to an array of char by example chanstatstr[100] 100 is minuimum. |
wbeaumont | 1:cd7c70a46739 | 106 | * @param dimension of the str array ( so sizeoff(chanstatstr) |
wbeaumont | 1:cd7c70a46739 | 107 | */ |
wbeaumont | 1:cd7c70a46739 | 108 | int StatusString(int ch,bool updatereq ,bool eprom, char *str , int length); |
wbeaumont | 0:7a0ebc527fb9 | 109 | |
wbeaumont | 3:2a2eafba8a1d | 110 | /**gives the last value of the DAC register obtained by the last update command. |
wbeaumont | 3:2a2eafba8a1d | 111 | Implementation of the getDACvalue of the DACInterface |
wbeaumont | 3:2a2eafba8a1d | 112 | @param value by reference , the DAC value for the channel |
wbeaumont | 3:2a2eafba8a1d | 113 | @param ch the channel nr 0..3 |
wbeaumont | 3:2a2eafba8a1d | 114 | @return the I2C result of the last update command |
wbeaumont | 3:2a2eafba8a1d | 115 | */ |
wbeaumont | 1:cd7c70a46739 | 116 | virtual int getDACvalue(int &value, int ch=0); |
wbeaumont | 3:2a2eafba8a1d | 117 | /** gives the last value of the DAC output voltage obtained by the last update command. |
wbeaumont | 3:2a2eafba8a1d | 118 | Use the channel status bits and eventual Vdd value to convert the DAC value to a voltage. |
wbeaumont | 3:2a2eafba8a1d | 119 | Implementation of the getVoltagevalue of the DACInterface |
wbeaumont | 3:2a2eafba8a1d | 120 | @param voltage by reference , the DAC output voltage for the channel |
wbeaumont | 3:2a2eafba8a1d | 121 | @param ch the channel nr 0..3 |
wbeaumont | 3:2a2eafba8a1d | 122 | @return the I2C result of the last update command |
wbeaumont | 3:2a2eafba8a1d | 123 | */ |
wbeaumont | 1:cd7c70a46739 | 124 | virtual int getVoltage(float &voltage, int ch=0); |
wbeaumont | 0:7a0ebc527fb9 | 125 | |
wbeaumont | 3:2a2eafba8a1d | 126 | /** set the dac value for a certain channel. |
wbeaumont | 3:2a2eafba8a1d | 127 | Implementation of the setDACvalue of the DACInterface |
wbeaumont | 3:2a2eafba8a1d | 128 | It also sets the DAC configuration bits ( Vref, power mode , gain ) |
wbeaumont | 3:2a2eafba8a1d | 129 | ( For version 0.4x this is internal, normal power , gain x2 ) |
wbeaumont | 3:2a2eafba8a1d | 130 | @param value the DAC value to be set |
wbeaumont | 3:2a2eafba8a1d | 131 | @param ch the channel nr 0..3 |
wbeaumont | 3:2a2eafba8a1d | 132 | @return the I2C result of the last update command |
wbeaumont | 3:2a2eafba8a1d | 133 | */ |
wbeaumont | 0:7a0ebc527fb9 | 134 | virtual int setDACvalue( int value, int ch=0); |
wbeaumont | 3:2a2eafba8a1d | 135 | |
wbeaumont | 3:2a2eafba8a1d | 136 | |
wbeaumont | 3:2a2eafba8a1d | 137 | /** gives the last value of the DAC output voltage obtained by the last update command. |
wbeaumont | 3:2a2eafba8a1d | 138 | Use the channel configuration setings and eventual Vdd value to convert the voltage to a DAC value. |
wbeaumont | 3:2a2eafba8a1d | 139 | Then it calls setDACValue so it sets also the channel configuration. |
wbeaumont | 3:2a2eafba8a1d | 140 | Implementation of the setVoltagevalue of the DACInterface |
wbeaumont | 3:2a2eafba8a1d | 141 | @param voltage by reference , the request DAC output voltage for the channel |
wbeaumont | 3:2a2eafba8a1d | 142 | @param ch the channel nr 0..3 |
wbeaumont | 3:2a2eafba8a1d | 143 | @return the I2C result of the last update command |
wbeaumont | 3:2a2eafba8a1d | 144 | */ |
wbeaumont | 3:2a2eafba8a1d | 145 | |
wbeaumont | 1:cd7c70a46739 | 146 | virtual int setVoltage (float voltage, int ch=0); |
wbeaumont | 2:366e996af95c | 147 | |
wbeaumont | 2:366e996af95c | 148 | |
wbeaumont | 2:366e996af95c | 149 | |
wbeaumont | 0:7a0ebc527fb9 | 150 | protected: |
wbeaumont | 1:cd7c70a46739 | 151 | /** pointer to the I2C interface driver. */ |
wbeaumont | 0:7a0ebc527fb9 | 152 | I2CInterface* _i2c_interface; |
wbeaumont | 0:7a0ebc527fb9 | 153 | /** The full i2c device address. */ |
wbeaumont | 0:7a0ebc527fb9 | 154 | int _device_address; |
wbeaumont | 1:cd7c70a46739 | 155 | char _adrbytes; |
wbeaumont | 3:2a2eafba8a1d | 156 | /** calculates the voltage based on the value and the channel configuration bits |
wbeaumont | 3:2a2eafba8a1d | 157 | @param value : the value of the DAC register |
wbeaumont | 3:2a2eafba8a1d | 158 | @param gain : the gain setting x1 or x2 |
wbeaumont | 3:2a2eafba8a1d | 159 | @param vref : use internal or external reference |
wbeaumont | 3:2a2eafba8a1d | 160 | @return : the voltage based on the parameters |
wbeaumont | 3:2a2eafba8a1d | 161 | */ |
wbeaumont | 1:cd7c70a46739 | 162 | float dig2volt( short int value,GainMode gain, VrefMode vref); |
wbeaumont | 3:2a2eafba8a1d | 163 | |
wbeaumont | 3:2a2eafba8a1d | 164 | /** calculates the nearest DAC register setting to get the requested voltage output |
wbeaumont | 3:2a2eafba8a1d | 165 | given the DAC configuration. There is no "under/ overflow" check |
wbeaumont | 3:2a2eafba8a1d | 166 | @param voltage the requested voltage |
wbeaumont | 3:2a2eafba8a1d | 167 | @param gain : the gain setting x1 or x2 |
wbeaumont | 3:2a2eafba8a1d | 168 | @return : the DAC value based the parameters to get the requested output voltage |
wbeaumont | 3:2a2eafba8a1d | 169 | */ |
wbeaumont | 1:cd7c70a46739 | 170 | short int volt2dig( float voltage,GainMode gain, VrefMode vref); |
wbeaumont | 3:2a2eafba8a1d | 171 | |
wbeaumont | 3:2a2eafba8a1d | 172 | /** holds the Vdd value , used to calculate the voltage in case of vref = external */ |
wbeaumont | 1:cd7c70a46739 | 173 | float Vdd; |
wbeaumont | 3:2a2eafba8a1d | 174 | /** holds the status of the last update request */ |
wbeaumont | 3:2a2eafba8a1d | 175 | int lastupdateresult; |
wbeaumont | 0:7a0ebc527fb9 | 176 | |
wbeaumont | 3:2a2eafba8a1d | 177 | /** array for reading the status will be removed after debugging so don't use for future development */ |
wbeaumont | 3:2a2eafba8a1d | 178 | char rbdata[24] ; |
wbeaumont | 2:366e996af95c | 179 | |
wbeaumont | 3:2a2eafba8a1d | 180 | /** used setting the configuration bits for each channel |
wbeaumont | 3:2a2eafba8a1d | 181 | These values are the "requested" values so not the actual values. |
wbeaumont | 3:2a2eafba8a1d | 182 | These values are not updated after a update request !! |
wbeaumont | 3:2a2eafba8a1d | 183 | */ |
wbeaumont | 2:366e996af95c | 184 | MCP4728ChCfg ChCfg[4]; |
wbeaumont | 3:2a2eafba8a1d | 185 | /** structure to store all channel status / config bits */ |
wbeaumont | 2:366e996af95c | 186 | typedef struct MCP4728ChStatus{ |
wbeaumont | 2:366e996af95c | 187 | GainMode gain; |
wbeaumont | 2:366e996af95c | 188 | VrefMode vref; |
wbeaumont | 2:366e996af95c | 189 | PowerMode pwr; |
wbeaumont | 3:2a2eafba8a1d | 190 | bool busy; |
wbeaumont | 2:366e996af95c | 191 | bool por; |
wbeaumont | 2:366e996af95c | 192 | short int dacvalue; |
wbeaumont | 2:366e996af95c | 193 | short int promvalue; |
wbeaumont | 2:366e996af95c | 194 | GainMode promgain; |
wbeaumont | 2:366e996af95c | 195 | VrefMode promvref; |
wbeaumont | 2:366e996af95c | 196 | PowerMode prompwr; |
wbeaumont | 2:366e996af95c | 197 | |
wbeaumont | 2:366e996af95c | 198 | } MCP4728Chstatus; |
wbeaumont | 2:366e996af95c | 199 | |
wbeaumont | 3:2a2eafba8a1d | 200 | /** storage of the channel status and configuration bits */ |
wbeaumont | 3:2a2eafba8a1d | 201 | MCP4728ChStatus chstat[4]; |
wbeaumont | 2:366e996af95c | 202 | |
wbeaumont | 2:366e996af95c | 203 | |
wbeaumont | 2:366e996af95c | 204 | |
wbeaumont | 2:366e996af95c | 205 | |
wbeaumont | 0:7a0ebc527fb9 | 206 | }; |
wbeaumont | 0:7a0ebc527fb9 | 207 | |
wbeaumont | 0:7a0ebc527fb9 | 208 | #endif |