MCP4725 DAC with RPi

There are some examples out there on the internet on how to use the MCP4725 with WiringPi. I found them to work, however writing to the EEPROM didn’t.
The solution is to use WiringPi’s wiringPiI2CWriteReg16 function:
Pass the first byte (which specifies whether to write to the DAC register only or to both DAC and EEPROM)
as the register parameter and the 12 bit voltage value as a 16 bit int (hence shifted by four bits) as the second
parameter.

#define DEVICEID 0x61

void setDACVoltage(int voltage, int writeToEEPROM) {
    int fd = wiringPiI2CSetup(DEVICEID);

    voltage = (voltage > 4095) ? 4095 : voltage;
    voltage = (voltage >> 4);

    if (writeToEEPROM)
        wiringPiI2CWriteReg16(fd, 0b01100000, voltage);
    else
        wiringPiI2CWriteReg16(fd, 0b01000000, voltage);

    close(fd);
}

Leave a Reply

Your Data

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

What do you want to say?