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.cpp

Committer:
wbeaumont
Date:
2015-10-23
Revision:
0:7a0ebc527fb9
Child:
1:cd7c70a46739

File content as of revision 0:7a0ebc527fb9:

#include "mcp4728.h"
#include "mbed.h"


#define VERSION_MCP4728_SRC  "0.02"  
// verions 0.02 has a fixed setDAC but now ch1 reacts on the heart beat  some bit maniputation error in the setDac

#define CMDFAST  0
#define CMDWRITEDAC 2  // writes to the DAC and / or eeprom depending on the write funcion
//#define CMDWRITEI2CADDR  3 // handled in a different class 
#define CMDWRITEVREF  4
#define CMDWRITEGAIN  6
#define CMDWRITEPWDDOWN  5

#define WRITEFCTMULTI  0  // write to multiple  DAC ch  given by ch selectio bits 
#define WRITEFCTSEQ  2       // write to DAC AND eeprom,  from ch given in ch selection bits to D
#define WRITEFCTSINGLE 3   // write to a singe DAC AND eeprom  ch given in ch selection bits

MCP4728::MCP4728(I2CInterface* i2cinterface, int device_address_bits  ): _i2c_interface(i2cinterface) ,
    getVersion( VERSION_MCP4728_HDR,VERSION_MCP4728_SRC, __TIME__, __DATE__)
{
    
    // Assemble the full I2C device address.
    _device_address = 0xC0; // Prime the full device address with the device code.
    _device_address |= (device_address_bits<<1);
    // next has to be read back from the device but
    
    for ( int ch=0;ch<4;ch++) {
             ChCfg[ch].pwr=Normal;
             ChCfg[ch].vref=InternRef;
             ChCfg[ch].gain=GainX2;
      }      
}

int MCP4728::getDACvalue(int& value, int ch){
    // has to limit to reading only the dac value not the other items. 
    enum PowerMode mode; enum PowerMode mode_eeprom; int dac_value; int dac_value_eeprom; bool eeprom_write_in_progress;
    int status = read( mode, mode_eeprom,  dac_value, dac_value_eeprom,  eeprom_write_in_progress);
    value=dac_value;
    return status;
}

int MCP4728::read(enum PowerMode& mode, enum PowerMode& mode_eeprom, int& dac_value, int& dac_value_eeprom, bool& eeprom_write_in_progress)
{
    char data[5];
    int result;
    
    // Read the raw data from the device.
    result = _i2c_interface->read(_device_address, data, sizeof(data)/sizeof(*data), false);
    
    // Parse the raw data, extracting our fields. Refer to MCP4728 ref manual, section 6.2
    if (result == 0)
    {
        eeprom_write_in_progress = (data[0] & 0x80)? false:true;
        
        mode = (enum PowerMode) ((data[0] & 0x06)>>1);
        
        dac_value  = (data[1]<<4) + (data[2]>>4);
        
        mode_eeprom = (enum PowerMode)((data[3] & 0x60)>>5);
        
        dac_value_eeprom = ((data[3] & 0x0F) <<8) + data[4];
    }
     
    return result;
}


int MCP4728::setDACvalue( int value, int ch){
    char data[3], tmp ; 
 /*   data[0] = CMDWRITEDAC << 6; // select write DAC function
    data[0] = data[0] | WRITEFCTMULTI << 4 ; // select write multi ch no eeprom 
    ch =ch & 3;
    tmp =(char)  ch; // select the channel
    data[0] = data[0] | tmp ;
    data[0] = data[0] | 0 ; // update now , assuming !LDAC =0
*/
    data[2] = 0xFF &  (char)value;
/*    value=value >>8;
    data[1] = 0x0F &  (char)value;
    data[1] = data[1] | (char)( ChCfg[ch].gain <<4); //set gain
    data[1] = data[1] | (char)( ChCfg[ch].pwr <<5); // set pwr mode 
    data[1] = data[1] | (char)( ChCfg[ch].vref  <<7); // set pwr mode 
*/
    data[0]= 0x52; // 0b0101 0010  ch 1  !UDAC=0 
    data[1]= 0x17; // 0b0000 0111   ref=Vdd, pwd =0  normal  ; gain =1  d11=0 ,111,       
    return _i2c_interface->write(_device_address, data, sizeof(data)/sizeof(*data), false);
 
    
}




int MCP4728::write(enum PowerMode mode, int dac_value, bool writeToEeprom)
{
    char data[3]={0};
    int write_command;
    
    //Which write command are we to use?
    if (writeToEeprom == true)
    {
        //Write DAC Register and EEPROM
        write_command = 3;
    }
    else
    {
        //Write DAC Register
        write_command = 2;
    }
    
    //Assemble our three bytes of data - Refer to MCP4728 ref manual, section 6.
    data[0] = (write_command <<5) | ((int)mode<<1);
    data[1] = (dac_value>>4);
    data[2] = (dac_value<<4);
    
    // Now write them to the device.
    return _i2c_interface->write(_device_address, data, sizeof(data)/sizeof(*data), false);
}