General usable MCP4728 quad DAC implementation only limited function has to be used together with the DevInterface lib

Dependents:   mbedSerialInterface_talkback2 MCP4728test mbedSerialInterface_sequencer

Committer:
wbeaumont
Date:
Tue Jan 12 16:04:02 2016 +0000
Revision:
4:f45f4ff5a51a
Parent:
3:2a2eafba8a1d
Child:
5:164362cf5836
some corrections in the api doc

Who changed what in which revision?

UserRevisionLine numberNew 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 4:f45f4ff5a51a 91 * @param checkall check if the channel nr and adress is returned correctly in 04x this is not tested /checked so set to false .
wbeaumont 4:f45f4ff5a51a 92 * @returns 0 on success non-0 on failure
wbeaumont 0:7a0ebc527fb9 93 */
wbeaumont 3:2a2eafba8a1d 94 int update(bool checkall);
wbeaumont 4:f45f4ff5a51a 95
wbeaumont 4:f45f4ff5a51a 96 /** Read the contents of the dac registers,( also eeprom settings) , and if an eeprom write is currently active.
wbeaumont 4:f45f4ff5a51a 97 * result of the reading is stored in the channelstatus structure .
wbeaumont 4:f45f4ff5a51a 98 * implementation of the update from the DACInterface
wbeaumont 4:f45f4ff5a51a 99 * @returns 0 on success non-0 on failure
wbeaumont 4:f45f4ff5a51a 100 */
wbeaumont 3:2a2eafba8a1d 101
wbeaumont 1:cd7c70a46739 102 virtual int update() { return update(false);}
wbeaumont 3:2a2eafba8a1d 103
wbeaumont 3:2a2eafba8a1d 104
wbeaumont 1:cd7c70a46739 105 /** format a string with the channel status
wbeaumont 1:cd7c70a46739 106 * @param ch : the channel nr
wbeaumont 3:2a2eafba8a1d 107 * @param updatereq: if true the function update(true) is called else the class status table is used .
wbeaumont 3:2a2eafba8a1d 108 * @param eprom: if true the values in the eprom are used else the values of the DAC channel
wbeaumont 4:f45f4ff5a51a 109 * @param str a pointer to an array of char by example chanstatstr[200] 200 is minuimum.
wbeaumont 1:cd7c70a46739 110 * @param dimension of the str array ( so sizeoff(chanstatstr)
wbeaumont 4:f45f4ff5a51a 111 * @return the number of chars in the string or negative in case of an error
wbeaumont 1:cd7c70a46739 112 */
wbeaumont 1:cd7c70a46739 113 int StatusString(int ch,bool updatereq ,bool eprom, char *str , int length);
wbeaumont 0:7a0ebc527fb9 114
wbeaumont 3:2a2eafba8a1d 115 /**gives the last value of the DAC register obtained by the last update command.
wbeaumont 3:2a2eafba8a1d 116 Implementation of the getDACvalue of the DACInterface
wbeaumont 3:2a2eafba8a1d 117 @param value by reference , the DAC value for the channel
wbeaumont 3:2a2eafba8a1d 118 @param ch the channel nr 0..3
wbeaumont 3:2a2eafba8a1d 119 @return the I2C result of the last update command
wbeaumont 3:2a2eafba8a1d 120 */
wbeaumont 1:cd7c70a46739 121 virtual int getDACvalue(int &value, int ch=0);
wbeaumont 3:2a2eafba8a1d 122 /** gives the last value of the DAC output voltage obtained by the last update command.
wbeaumont 3:2a2eafba8a1d 123 Use the channel status bits and eventual Vdd value to convert the DAC value to a voltage.
wbeaumont 3:2a2eafba8a1d 124 Implementation of the getVoltagevalue of the DACInterface
wbeaumont 3:2a2eafba8a1d 125 @param voltage by reference , the DAC output voltage for the channel
wbeaumont 3:2a2eafba8a1d 126 @param ch the channel nr 0..3
wbeaumont 3:2a2eafba8a1d 127 @return the I2C result of the last update command
wbeaumont 3:2a2eafba8a1d 128 */
wbeaumont 1:cd7c70a46739 129 virtual int getVoltage(float &voltage, int ch=0);
wbeaumont 0:7a0ebc527fb9 130
wbeaumont 3:2a2eafba8a1d 131 /** set the dac value for a certain channel.
wbeaumont 3:2a2eafba8a1d 132 Implementation of the setDACvalue of the DACInterface
wbeaumont 3:2a2eafba8a1d 133 It also sets the DAC configuration bits ( Vref, power mode , gain )
wbeaumont 3:2a2eafba8a1d 134 ( For version 0.4x this is internal, normal power , gain x2 )
wbeaumont 3:2a2eafba8a1d 135 @param value the DAC value to be set
wbeaumont 3:2a2eafba8a1d 136 @param ch the channel nr 0..3
wbeaumont 4:f45f4ff5a51a 137 @return the I2C result when trying to set the DAC value
wbeaumont 3:2a2eafba8a1d 138 */
wbeaumont 0:7a0ebc527fb9 139 virtual int setDACvalue( int value, int ch=0);
wbeaumont 3:2a2eafba8a1d 140
wbeaumont 3:2a2eafba8a1d 141
wbeaumont 3:2a2eafba8a1d 142 /** gives the last value of the DAC output voltage obtained by the last update command.
wbeaumont 3:2a2eafba8a1d 143 Use the channel configuration setings and eventual Vdd value to convert the voltage to a DAC value.
wbeaumont 3:2a2eafba8a1d 144 Then it calls setDACValue so it sets also the channel configuration.
wbeaumont 3:2a2eafba8a1d 145 Implementation of the setVoltagevalue of the DACInterface
wbeaumont 3:2a2eafba8a1d 146 @param voltage by reference , the request DAC output voltage for the channel
wbeaumont 3:2a2eafba8a1d 147 @param ch the channel nr 0..3
wbeaumont 4:f45f4ff5a51a 148 @return the I2C result when trying to set the DAC value
wbeaumont 3:2a2eafba8a1d 149 */
wbeaumont 3:2a2eafba8a1d 150
wbeaumont 1:cd7c70a46739 151 virtual int setVoltage (float voltage, int ch=0);
wbeaumont 2:366e996af95c 152
wbeaumont 2:366e996af95c 153
wbeaumont 2:366e996af95c 154
wbeaumont 0:7a0ebc527fb9 155 protected:
wbeaumont 1:cd7c70a46739 156 /** pointer to the I2C interface driver. */
wbeaumont 0:7a0ebc527fb9 157 I2CInterface* _i2c_interface;
wbeaumont 0:7a0ebc527fb9 158 /** The full i2c device address. */
wbeaumont 0:7a0ebc527fb9 159 int _device_address;
wbeaumont 1:cd7c70a46739 160 char _adrbytes;
wbeaumont 3:2a2eafba8a1d 161 /** calculates the voltage based on the value and the channel configuration bits
wbeaumont 3:2a2eafba8a1d 162 @param value : the value of the DAC register
wbeaumont 3:2a2eafba8a1d 163 @param gain : the gain setting x1 or x2
wbeaumont 3:2a2eafba8a1d 164 @param vref : use internal or external reference
wbeaumont 3:2a2eafba8a1d 165 @return : the voltage based on the parameters
wbeaumont 3:2a2eafba8a1d 166 */
wbeaumont 1:cd7c70a46739 167 float dig2volt( short int value,GainMode gain, VrefMode vref);
wbeaumont 3:2a2eafba8a1d 168
wbeaumont 3:2a2eafba8a1d 169 /** calculates the nearest DAC register setting to get the requested voltage output
wbeaumont 3:2a2eafba8a1d 170 given the DAC configuration. There is no "under/ overflow" check
wbeaumont 3:2a2eafba8a1d 171 @param voltage the requested voltage
wbeaumont 3:2a2eafba8a1d 172 @param gain : the gain setting x1 or x2
wbeaumont 4:f45f4ff5a51a 173 @param vref : use internal or external reference
wbeaumont 3:2a2eafba8a1d 174 @return : the DAC value based the parameters to get the requested output voltage
wbeaumont 3:2a2eafba8a1d 175 */
wbeaumont 1:cd7c70a46739 176 short int volt2dig( float voltage,GainMode gain, VrefMode vref);
wbeaumont 3:2a2eafba8a1d 177
wbeaumont 3:2a2eafba8a1d 178 /** holds the Vdd value , used to calculate the voltage in case of vref = external */
wbeaumont 1:cd7c70a46739 179 float Vdd;
wbeaumont 3:2a2eafba8a1d 180 /** holds the status of the last update request */
wbeaumont 3:2a2eafba8a1d 181 int lastupdateresult;
wbeaumont 0:7a0ebc527fb9 182
wbeaumont 3:2a2eafba8a1d 183 /** array for reading the status will be removed after debugging so don't use for future development */
wbeaumont 3:2a2eafba8a1d 184 char rbdata[24] ;
wbeaumont 2:366e996af95c 185
wbeaumont 3:2a2eafba8a1d 186 /** used setting the configuration bits for each channel
wbeaumont 3:2a2eafba8a1d 187 These values are the "requested" values so not the actual values.
wbeaumont 3:2a2eafba8a1d 188 These values are not updated after a update request !!
wbeaumont 3:2a2eafba8a1d 189 */
wbeaumont 2:366e996af95c 190 MCP4728ChCfg ChCfg[4];
wbeaumont 3:2a2eafba8a1d 191 /** structure to store all channel status / config bits */
wbeaumont 2:366e996af95c 192 typedef struct MCP4728ChStatus{
wbeaumont 2:366e996af95c 193 GainMode gain;
wbeaumont 2:366e996af95c 194 VrefMode vref;
wbeaumont 2:366e996af95c 195 PowerMode pwr;
wbeaumont 3:2a2eafba8a1d 196 bool busy;
wbeaumont 2:366e996af95c 197 bool por;
wbeaumont 2:366e996af95c 198 short int dacvalue;
wbeaumont 2:366e996af95c 199 short int promvalue;
wbeaumont 2:366e996af95c 200 GainMode promgain;
wbeaumont 2:366e996af95c 201 VrefMode promvref;
wbeaumont 2:366e996af95c 202 PowerMode prompwr;
wbeaumont 2:366e996af95c 203
wbeaumont 2:366e996af95c 204 } MCP4728Chstatus;
wbeaumont 2:366e996af95c 205
wbeaumont 3:2a2eafba8a1d 206 /** storage of the channel status and configuration bits */
wbeaumont 3:2a2eafba8a1d 207 MCP4728ChStatus chstat[4];
wbeaumont 2:366e996af95c 208
wbeaumont 2:366e996af95c 209
wbeaumont 0:7a0ebc527fb9 210 };
wbeaumont 0:7a0ebc527fb9 211
wbeaumont 0:7a0ebc527fb9 212 #endif