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:
Fri Jan 08 11:58:33 2016 +0000
Revision:
1:cd7c70a46739
Parent:
0:7a0ebc527fb9
Child:
2:366e996af95c
Read, write to the DAC,  setVoltage, readVoltage  to different channels checked.  No detail checks done for config settings is done,  only one write method implemented, no eprom write checks.

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 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 1:cd7c70a46739 11 #define VERSION_MCP4728_HDR "0.44"
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 1:cd7c70a46739 15 * For version 0.4X mode can not be set ( Intern ref, gain x2 power mode normal)
wbeaumont 1:cd7c70a46739 16 * ( DACInterface inherits getVersion )
wbeaumont 0:7a0ebc527fb9 17 * (C) Wim Beaumont Universiteit Antwerpen 2015
wbeaumont 0:7a0ebc527fb9 18 *
wbeaumont 0:7a0ebc527fb9 19 */
wbeaumont 1:cd7c70a46739 20 class MCP4728 : public DACInterface {
wbeaumont 1:cd7c70a46739 21
wbeaumont 1:cd7c70a46739 22 int lastreadresult;
wbeaumont 1:cd7c70a46739 23 char rbdata[24] ; //array for reading the status will be removed after debugging
wbeaumont 0:7a0ebc527fb9 24 public:
wbeaumont 0:7a0ebc527fb9 25 /** The device supports two types of power modes: normal and power-down. In normal mode the device
wbeaumont 0:7a0ebc527fb9 26 * operates a normal digital to analog conversion. In power-down mode all digitial to analog
wbeaumont 0:7a0ebc527fb9 27 * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power
wbeaumont 0:7a0ebc527fb9 28 * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */
wbeaumont 0:7a0ebc527fb9 29 enum PowerMode {
wbeaumont 0:7a0ebc527fb9 30 /** In normal mode the device operates a normal D2A conversion. */
wbeaumont 0:7a0ebc527fb9 31 Normal=0,
wbeaumont 0:7a0ebc527fb9 32 /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */
wbeaumont 0:7a0ebc527fb9 33 PowerDown1k=1,
wbeaumont 0:7a0ebc527fb9 34 /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */
wbeaumont 0:7a0ebc527fb9 35 PowerDown100k=2,
wbeaumont 0:7a0ebc527fb9 36 /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */
wbeaumont 0:7a0ebc527fb9 37 PowerDown500k=3
wbeaumont 0:7a0ebc527fb9 38 };
wbeaumont 0:7a0ebc527fb9 39
wbeaumont 0:7a0ebc527fb9 40 enum VrefMode {
wbeaumont 0:7a0ebc527fb9 41 ExternRef=0, // == VDD
wbeaumont 1:cd7c70a46739 42 InternRef=1
wbeaumont 0:7a0ebc527fb9 43 };
wbeaumont 1:cd7c70a46739 44
wbeaumont 0:7a0ebc527fb9 45 enum GainMode {
wbeaumont 0:7a0ebc527fb9 46 GainX1=0,
wbeaumont 0:7a0ebc527fb9 47 GainX2=1
wbeaumont 0:7a0ebc527fb9 48 };
wbeaumont 0:7a0ebc527fb9 49
wbeaumont 0:7a0ebc527fb9 50 typedef struct MCP4728ChCfg{
wbeaumont 0:7a0ebc527fb9 51 GainMode gain;
wbeaumont 0:7a0ebc527fb9 52 VrefMode vref;
wbeaumont 0:7a0ebc527fb9 53 PowerMode pwr;
wbeaumont 0:7a0ebc527fb9 54 } MCP4728ChCfgdummy;
wbeaumont 0:7a0ebc527fb9 55
wbeaumont 1:cd7c70a46739 56 MCP4728ChCfg ChCfg[4]; //used for setting
wbeaumont 1:cd7c70a46739 57
wbeaumont 1:cd7c70a46739 58 typedef struct MCP4728ChStatus{
wbeaumont 1:cd7c70a46739 59 GainMode gain;
wbeaumont 1:cd7c70a46739 60 VrefMode vref;
wbeaumont 1:cd7c70a46739 61 PowerMode pwr;
wbeaumont 1:cd7c70a46739 62 bool bussy;
wbeaumont 1:cd7c70a46739 63 bool por;
wbeaumont 1:cd7c70a46739 64 short int dacvalue;
wbeaumont 1:cd7c70a46739 65 short int promvalue;
wbeaumont 1:cd7c70a46739 66 GainMode promgain;
wbeaumont 1:cd7c70a46739 67 VrefMode promvref;
wbeaumont 1:cd7c70a46739 68 PowerMode prompwr;
wbeaumont 1:cd7c70a46739 69
wbeaumont 1:cd7c70a46739 70 } MCP4728Chstatus;
wbeaumont 1:cd7c70a46739 71
wbeaumont 1:cd7c70a46739 72
wbeaumont 1:cd7c70a46739 73 MCP4728ChStatus chstat[4]; // for readback .
wbeaumont 1:cd7c70a46739 74
wbeaumont 1:cd7c70a46739 75
wbeaumont 1:cd7c70a46739 76
wbeaumont 0:7a0ebc527fb9 77
wbeaumont 0:7a0ebc527fb9 78
wbeaumont 0:7a0ebc527fb9 79 /** The device supports 3 different I2C bus frequencies.*/
wbeaumont 0:7a0ebc527fb9 80 enum BusFrequency {
wbeaumont 0:7a0ebc527fb9 81 /** Standard 100kHz bus. */
wbeaumont 0:7a0ebc527fb9 82 Standard100kHz,
wbeaumont 0:7a0ebc527fb9 83 /** Fast 400kHz bus. */
wbeaumont 0:7a0ebc527fb9 84 Fast400kHz,
wbeaumont 0:7a0ebc527fb9 85 /** 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 86 HighSpeed3_4Mhz
wbeaumont 0:7a0ebc527fb9 87 };
wbeaumont 0:7a0ebc527fb9 88
wbeaumont 0:7a0ebc527fb9 89 /** Create an mcp4725 I2C interface
wbeaumont 0:7a0ebc527fb9 90 *
wbeaumont 1:cd7c70a46739 91 * @param i2cinterface pointer to the I2C interface (proto type)
wbeaumont 0:7a0ebc527fb9 92 * @param device_address_bits The 3bit address bits of the device.
wbeaumont 1:cd7c70a46739 93 * @Vdd the voltage on the Vdd pin ( power)
wbeaumont 0:7a0ebc527fb9 94 */
wbeaumont 1:cd7c70a46739 95 MCP4728(I2CInterface* i2cinterface, int device_address_bits, float Vdd );
wbeaumont 0:7a0ebc527fb9 96
wbeaumont 1:cd7c70a46739 97
wbeaumont 1:cd7c70a46739 98 /** Read the contents of the dac registers,( also eeprom settings) , and if an eeprom write is currently active.
wbeaumont 1:cd7c70a46739 99 * result of the reading is stored in the channelstatus structure .
wbeaumont 1:cd7c70a46739 100 * @param checkall check if the channel nr and adress is returned correctly .
wbeaumont 0:7a0ebc527fb9 101 * @returns
wbeaumont 0:7a0ebc527fb9 102 * 0 on success,
wbeaumont 0:7a0ebc527fb9 103 * non-0 on failure
wbeaumont 0:7a0ebc527fb9 104 */
wbeaumont 1:cd7c70a46739 105 int update(bool checkall);
wbeaumont 1:cd7c70a46739 106 virtual int update() { return update(false);}
wbeaumont 1:cd7c70a46739 107 /** format a string with the channel status
wbeaumont 1:cd7c70a46739 108 * will do a call to the update() function
wbeaumont 1:cd7c70a46739 109 * @param ch : the channel nr
wbeaumont 1:cd7c70a46739 110 * @param updatereq , if true the function update(true) is called ,
wbeaumont 1:cd7c70a46739 111 * else the class status table is used .
wbeaumont 1:cd7c70a46739 112 * @param eprom if true the values in the eprom else the values of the DAC channel
wbeaumont 1:cd7c70a46739 113 * @param str a pointer to an array of char by example chanstatstr[100] 100 is minuimum.
wbeaumont 1:cd7c70a46739 114 * @param dimension of the str array ( so sizeoff(chanstatstr)
wbeaumont 1:cd7c70a46739 115 */
wbeaumont 1:cd7c70a46739 116 int StatusString(int ch,bool updatereq ,bool eprom, char *str , int length);
wbeaumont 0:7a0ebc527fb9 117
wbeaumont 1:cd7c70a46739 118 /**returns the last value of the DAC register obtained by the last read command.*/
wbeaumont 1:cd7c70a46739 119 virtual int getDACvalue(int &value, int ch=0);
wbeaumont 1:cd7c70a46739 120 virtual int getVoltage(float &voltage, int ch=0);
wbeaumont 0:7a0ebc527fb9 121
wbeaumont 1:cd7c70a46739 122 /** set the dac value for a certain channel. */
wbeaumont 0:7a0ebc527fb9 123 virtual int setDACvalue( int value, int ch=0);
wbeaumont 1:cd7c70a46739 124 virtual int setVoltage (float voltage, int ch=0);
wbeaumont 0:7a0ebc527fb9 125 protected:
wbeaumont 1:cd7c70a46739 126 /** pointer to the I2C interface driver. */
wbeaumont 0:7a0ebc527fb9 127 I2CInterface* _i2c_interface;
wbeaumont 0:7a0ebc527fb9 128 /** The full i2c device address. */
wbeaumont 0:7a0ebc527fb9 129 int _device_address;
wbeaumont 1:cd7c70a46739 130 char _adrbytes;
wbeaumont 1:cd7c70a46739 131
wbeaumont 1:cd7c70a46739 132 float dig2volt( short int value,GainMode gain, VrefMode vref);
wbeaumont 1:cd7c70a46739 133 short int volt2dig( float voltage,GainMode gain, VrefMode vref);
wbeaumont 1:cd7c70a46739 134 float Vdd;
wbeaumont 0:7a0ebc527fb9 135
wbeaumont 0:7a0ebc527fb9 136 };
wbeaumont 0:7a0ebc527fb9 137
wbeaumont 0:7a0ebc527fb9 138 #endif