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 15:03:38 2016 +0000
Revision:
2:366e996af95c
Parent:
1:cd7c70a46739
Child:
3:2a2eafba8a1d
comments added

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 1:cd7c70a46739 12 #define VERSION_MCP4728_HDR "0.44"
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 1:cd7c70a46739 29 int lastreadresult;
wbeaumont 1:cd7c70a46739 30 char rbdata[24] ; //array for reading the status will be removed after debugging
wbeaumont 0:7a0ebc527fb9 31 public:
wbeaumont 0:7a0ebc527fb9 32 /** The device supports two types of power modes: normal and power-down. In normal mode the device
wbeaumont 0:7a0ebc527fb9 33 * operates a normal digital to analog conversion. In power-down mode all digitial to analog
wbeaumont 0:7a0ebc527fb9 34 * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power
wbeaumont 0:7a0ebc527fb9 35 * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */
wbeaumont 0:7a0ebc527fb9 36 enum PowerMode {
wbeaumont 0:7a0ebc527fb9 37 /** In normal mode the device operates a normal D2A conversion. */
wbeaumont 0:7a0ebc527fb9 38 Normal=0,
wbeaumont 0:7a0ebc527fb9 39 /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */
wbeaumont 0:7a0ebc527fb9 40 PowerDown1k=1,
wbeaumont 0:7a0ebc527fb9 41 /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */
wbeaumont 0:7a0ebc527fb9 42 PowerDown100k=2,
wbeaumont 0:7a0ebc527fb9 43 /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */
wbeaumont 0:7a0ebc527fb9 44 PowerDown500k=3
wbeaumont 0:7a0ebc527fb9 45 };
wbeaumont 2:366e996af95c 46 /** Voltage reference can be either External, this means the Vdd or internal , 2.048 V
wbeaumont 2:366e996af95c 47 The output goes from 0 to Vref ( either 2.048 V or Vdd ),
wbeaumont 2:366e996af95c 48 */
wbeaumont 0:7a0ebc527fb9 49 enum VrefMode {
wbeaumont 0:7a0ebc527fb9 50 ExternRef=0, // == VDD
wbeaumont 1:cd7c70a46739 51 InternRef=1
wbeaumont 0:7a0ebc527fb9 52 };
wbeaumont 2:366e996af95c 53 /** In case the internal reference is used the output can be multiplied by a factor 2.
wbeaumont 2:366e996af95c 54 in that case the output goes between 0 and 4.096 V
wbeaumont 2:366e996af95c 55 */
wbeaumont 0:7a0ebc527fb9 56 enum GainMode {
wbeaumont 0:7a0ebc527fb9 57 GainX1=0,
wbeaumont 0:7a0ebc527fb9 58 GainX2=1
wbeaumont 0:7a0ebc527fb9 59 };
wbeaumont 0:7a0ebc527fb9 60
wbeaumont 1:cd7c70a46739 61
wbeaumont 1:cd7c70a46739 62
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 2:366e996af95c 74 /** structure used to set the channel configuration */
wbeaumont 2:366e996af95c 75 typedef struct MCP4728ChCfg{
wbeaumont 2:366e996af95c 76 GainMode gain;
wbeaumont 2:366e996af95c 77 VrefMode vref;
wbeaumont 2:366e996af95c 78 PowerMode pwr;
wbeaumont 2:366e996af95c 79 } MCP4728ChCfgdummy;
wbeaumont 2:366e996af95c 80
wbeaumont 0:7a0ebc527fb9 81
wbeaumont 0:7a0ebc527fb9 82 /** Create an mcp4725 I2C interface
wbeaumont 0:7a0ebc527fb9 83 *
wbeaumont 1:cd7c70a46739 84 * @param i2cinterface pointer to the I2C interface (proto type)
wbeaumont 0:7a0ebc527fb9 85 * @param device_address_bits The 3bit address bits of the device.
wbeaumont 1:cd7c70a46739 86 * @Vdd the voltage on the Vdd pin ( power)
wbeaumont 0:7a0ebc527fb9 87 */
wbeaumont 1:cd7c70a46739 88 MCP4728(I2CInterface* i2cinterface, int device_address_bits, float Vdd );
wbeaumont 0:7a0ebc527fb9 89
wbeaumont 1:cd7c70a46739 90
wbeaumont 1:cd7c70a46739 91 /** Read the contents of the dac registers,( also eeprom settings) , and if an eeprom write is currently active.
wbeaumont 1:cd7c70a46739 92 * result of the reading is stored in the channelstatus structure .
wbeaumont 1:cd7c70a46739 93 * @param checkall check if the channel nr and adress is returned correctly .
wbeaumont 0:7a0ebc527fb9 94 * @returns
wbeaumont 0:7a0ebc527fb9 95 * 0 on success,
wbeaumont 0:7a0ebc527fb9 96 * non-0 on failure
wbeaumont 0:7a0ebc527fb9 97 */
wbeaumont 1:cd7c70a46739 98 int update(bool checkall);
wbeaumont 1:cd7c70a46739 99 virtual int update() { return update(false);}
wbeaumont 1:cd7c70a46739 100 /** format a string with the channel status
wbeaumont 1:cd7c70a46739 101 * will do a call to the update() function
wbeaumont 1:cd7c70a46739 102 * @param ch : the channel nr
wbeaumont 1:cd7c70a46739 103 * @param updatereq , if true the function update(true) is called ,
wbeaumont 1:cd7c70a46739 104 * else the class status table is used .
wbeaumont 1:cd7c70a46739 105 * @param eprom if true the values in the eprom else the values of the DAC channel
wbeaumont 1:cd7c70a46739 106 * @param str a pointer to an array of char by example chanstatstr[100] 100 is minuimum.
wbeaumont 1:cd7c70a46739 107 * @param dimension of the str array ( so sizeoff(chanstatstr)
wbeaumont 1:cd7c70a46739 108 */
wbeaumont 1:cd7c70a46739 109 int StatusString(int ch,bool updatereq ,bool eprom, char *str , int length);
wbeaumont 0:7a0ebc527fb9 110
wbeaumont 1:cd7c70a46739 111 /**returns the last value of the DAC register obtained by the last read command.*/
wbeaumont 1:cd7c70a46739 112 virtual int getDACvalue(int &value, int ch=0);
wbeaumont 1:cd7c70a46739 113 virtual int getVoltage(float &voltage, int ch=0);
wbeaumont 0:7a0ebc527fb9 114
wbeaumont 1:cd7c70a46739 115 /** set the dac value for a certain channel. */
wbeaumont 0:7a0ebc527fb9 116 virtual int setDACvalue( int value, int ch=0);
wbeaumont 1:cd7c70a46739 117 virtual int setVoltage (float voltage, int ch=0);
wbeaumont 2:366e996af95c 118
wbeaumont 2:366e996af95c 119
wbeaumont 2:366e996af95c 120
wbeaumont 0:7a0ebc527fb9 121 protected:
wbeaumont 1:cd7c70a46739 122 /** pointer to the I2C interface driver. */
wbeaumont 0:7a0ebc527fb9 123 I2CInterface* _i2c_interface;
wbeaumont 0:7a0ebc527fb9 124 /** The full i2c device address. */
wbeaumont 0:7a0ebc527fb9 125 int _device_address;
wbeaumont 1:cd7c70a46739 126 char _adrbytes;
wbeaumont 1:cd7c70a46739 127
wbeaumont 1:cd7c70a46739 128 float dig2volt( short int value,GainMode gain, VrefMode vref);
wbeaumont 1:cd7c70a46739 129 short int volt2dig( float voltage,GainMode gain, VrefMode vref);
wbeaumont 1:cd7c70a46739 130 float Vdd;
wbeaumont 0:7a0ebc527fb9 131
wbeaumont 2:366e996af95c 132
wbeaumont 2:366e996af95c 133
wbeaumont 2:366e996af95c 134 /** used for read back the setting */
wbeaumont 2:366e996af95c 135 MCP4728ChCfg ChCfg[4];
wbeaumont 2:366e996af95c 136
wbeaumont 2:366e996af95c 137 typedef struct MCP4728ChStatus{
wbeaumont 2:366e996af95c 138 GainMode gain;
wbeaumont 2:366e996af95c 139 VrefMode vref;
wbeaumont 2:366e996af95c 140 PowerMode pwr;
wbeaumont 2:366e996af95c 141 bool bussy;
wbeaumont 2:366e996af95c 142 bool por;
wbeaumont 2:366e996af95c 143 short int dacvalue;
wbeaumont 2:366e996af95c 144 short int promvalue;
wbeaumont 2:366e996af95c 145 GainMode promgain;
wbeaumont 2:366e996af95c 146 VrefMode promvref;
wbeaumont 2:366e996af95c 147 PowerMode prompwr;
wbeaumont 2:366e996af95c 148
wbeaumont 2:366e996af95c 149 } MCP4728Chstatus;
wbeaumont 2:366e996af95c 150
wbeaumont 2:366e996af95c 151
wbeaumont 2:366e996af95c 152 MCP4728ChStatus chstat[4]; // for readback .
wbeaumont 2:366e996af95c 153
wbeaumont 2:366e996af95c 154
wbeaumont 2:366e996af95c 155
wbeaumont 2:366e996af95c 156
wbeaumont 0:7a0ebc527fb9 157 };
wbeaumont 0:7a0ebc527fb9 158
wbeaumont 0:7a0ebc527fb9 159 #endif