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

Dependents:   mbedSerialInterface_talkback2 MCP4728test mbedSerialInterface_sequencer

Revision:
1:cd7c70a46739
Parent:
0:7a0ebc527fb9
Child:
2:366e996af95c
--- a/mcp4728.h	Fri Oct 23 19:35:27 2015 +0000
+++ b/mcp4728.h	Fri Jan 08 11:58:33 2016 +0000
@@ -1,6 +1,6 @@
 #ifndef MCP_4728_H
 #define MCP_4728_H
-
+#include "getVersion.h"
 
 #include "stdbool.h"
 
@@ -8,21 +8,19 @@
 #include "I2CInterface.h" 
 #include "DACInterface.h" 
 
-#define VERSION_MCP4728_HDR "0.01"
+#define VERSION_MCP4728_HDR "0.44"
 
 /** MCP4728 class.
  *  Used for interfacing with a mcp4728 12-Bit QUAD  Digital-to-Analog Converter.
- *  To convert between the 12bit dac_value and Vout, use the following formula: dac_value = (Vout*4096/Vref), where Vout is  
- *  the desired output analog voltage, Vref is the voltage connected to the Vdd pin of the device. Typically Vdd will be 3.3volts.
- *
+ *  For version 0.4X mode can not be set ( Intern ref,  gain x2  power mode normal) 
+ *  ( DACInterface inherits getVersion  )
  * (C) Wim Beaumont Universiteit Antwerpen 2015        
  *  
  */
-class MCP4728 : public DACInterface
-{
-    float vref ;
-    
-    
+class MCP4728 : public DACInterface {
+   
+    int lastreadresult;
+    char rbdata[24] ; //array for reading the status will be removed after debugging 
     public:
     /** The device supports two types of power modes: normal and power-down. In normal mode the device
      * operates a normal digital to analog conversion. In power-down mode all digitial to analog  
@@ -39,15 +37,11 @@
         PowerDown500k=3
     };
     
-    
-    
     enum VrefMode {
         ExternRef=0,  // == VDD 
-        InternRef=1
-        
+        InternRef=1    
     };
-    
-    
+
     enum GainMode {
         GainX1=0,
         GainX2=1
@@ -59,7 +53,27 @@
          PowerMode pwr;
     } MCP4728ChCfgdummy;
     
-    MCP4728ChCfg ChCfg[4];
+    MCP4728ChCfg ChCfg[4]; //used for setting 
+    
+    typedef struct MCP4728ChStatus{
+         GainMode gain;
+         VrefMode  vref;
+         PowerMode pwr;
+         bool bussy;
+         bool por;
+         short int dacvalue;
+         short int promvalue;
+         GainMode promgain;
+         VrefMode  promvref;
+         PowerMode prompwr;
+         
+    } MCP4728Chstatus;
+    
+    
+    MCP4728ChStatus chstat[4]; // for readback .
+    
+    
+    
         
     
     /** The device supports 3 different I2C bus frequencies.*/
@@ -74,49 +88,50 @@
     
     /** Create an mcp4725 I2C interface
      * 
-     * @param i2cinterface pointer to the  MBED I2C interface 
-     * @param scl I2C clock line pin
-     * @param bus_frequency the frequency at which the I2C bus is running.
+     * @param i2cinterface pointer to the   I2C interface (proto type) 
      * @param device_address_bits The 3bit address bits of the device.
+     * @Vdd the voltage on the Vdd pin ( power) 
      */
-    MCP4728(I2CInterface* i2cinterface,  int device_address_bits);
-    
+    MCP4728(I2CInterface* i2cinterface,  int device_address_bits, float Vdd );
     
-    /* Update the power mode and DAC value.
-     *
-     * @param   mode The Power mode to set the device into.
-     * @param   dac_value The 12bit dac register data value.
-     * @param   writeToEeprom True if the config is to be stored 
-     *          in eeprom, otherwise false
-     * 
+      
+    /** Read the contents of the dac registers,( also eeprom settings)  , and if an eeprom write is currently active.
+     *  result of the reading is stored in the channelstatus  structure .
+     * @param checkall  check if the channel nr and adress is returned correctly .     
      * @returns 
      *       0 on success,
      *   non-0 on failure
      */
-    int write(enum PowerMode mode, int dac_value, bool writeToEeprom);
+     int update(bool checkall);    
+     virtual int update() { return update(false);}
+    /** format a string with the channel status 
+    * will do a call to the update() function 
+    * @param ch :  the channel nr
+    * @param updatereq  , if true the function update(true) is called , 
+    *    else the class status table is used . 
+    * @param eprom if true the values in the eprom else the values of the DAC channel 
+    * @param str a pointer to an array of char  by example chanstatstr[100] 100 is minuimum.
+    * @param dimension of the str array  ( so sizeoff(chanstatstr)  
+    */
+    int StatusString(int ch,bool updatereq ,bool eprom,  char *str , int length);
     
-        
-    /** Read the contents of the dac register, the contents of eeprom, and if an eeprom write is currently active.
-     * 
-     * @param   mode Pointer to variable to store the current power mode.
-     * @param   mode_eeprom Pointer to variable to store the power mode as is stored in eeprom.
-     * @param   dac_value Pointer to variable to store the current dac value.
-     * @param   dac_value_eeprom Pointer to variable to store the dac value as is stored in eeprom.
-     * @param   eeprom_write_in_progress Pointer to variable to store the current eeprom write status.
-     *
-     * @returns 
-     *       0 on success,
-     *   non-0 on failure
-     */
-    int read(enum PowerMode& mode, enum PowerMode& mode_eeprom, int& dac_value, int& dac_value_eeprom, bool& eeprom_write_in_progress);
+    /**returns the last value of the DAC register obtained by the last read command.*/
+    virtual int getDACvalue(int &value, int ch=0);
+    virtual int getVoltage(float &voltage, int ch=0);
     
-    virtual int getDACvalue(int &value, int ch=0);
+    /** set the dac value for a certain channel. */
     virtual int setDACvalue( int value, int ch=0);
+    virtual int setVoltage (float voltage, int ch=0);
     protected:
-    /** mbed I2C interface driver. */
+    /** pointer to the I2C interface driver. */
     I2CInterface* _i2c_interface;
     /** The full i2c device address. */
     int _device_address;
+    char _adrbytes;
+    
+    float dig2volt( short int value,GainMode gain,  VrefMode  vref); 
+    short int volt2dig( float voltage,GainMode gain,  VrefMode  vref);
+    float Vdd;
     
 };