Skip to content
esp-idf-libPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

esp-idf-lib/mcp4725

Build examples Build docs Validation

Driver for the MCP4725 12-bit digital-to-analog converter.

The MCP4725 is a single-channel buffered voltage output DAC with 12 bits of resolution: the output is VDD * code / 4096, so one code is 0.73 mV at 3.0 V and 1.22 mV at 5.0 V. The chip runs from 2.7 to 5.5 V, the reference is the supply itself, and the output amplifier swings from rail to rail. The last value and the power-down mode can be stored in the internal EEPROM, and the chip loads them at every power-up, so the output is valid before the firmware says anything.

Installation

idf.py add-dependency esp-idf-lib/mcp4725

Configuration

The pins live in the example: CONFIG_EXAMPLE_I2C_MASTER_SDA, CONFIG_EXAMPLE_I2C_MASTER_SCL and CONFIG_EXAMPLE_I2C_ADDR are set in menuconfig under the example's own options.

Hardware

The chip answers at 0x60…0x65, the address is selected by the factory option (the A0, A1, A2 suffixes) and by the A0 pin:

Part Address with A0 low Address with A0 high
MCP4725A0 0x60 0x61
MCP4725A1 0x62 0x63
MCP4725A2 0x64 0x65

mcp4725_init_desc() rejects anything outside 0x60…0x65. The bus is created at 1 MHz, which is the maximum the ESP-IDF I²C master supports; the chip itself can do 3.4 MHz. SDA and SCL are open drain and need pull-ups to the supply of the bus.

VDD is the analog reference as well as the supply, so any noise on it lands on the output directly. The VOUT pin drives a buffered output; the load should stay above a few kiloohms to keep the output in specification.

Writing the output

mcp4725_set_raw_output() takes a code from 0 to MCP4725_MAX_VALUE (4095) and writes it to the DAC register:

i2c_dev_t dev = { 0 };
ESP_ERROR_CHECK(mcp4725_init_desc(&dev, MCP4725A0_I2C_ADDR0, 0,
        GPIO_NUM_22, GPIO_NUM_23));

ESP_ERROR_CHECK(mcp4725_set_raw_output(&dev, 2048, false)); // VDD / 2

The call writes three bytes: the command with the power-down bits and the 12-bit value. The value is checked, so a code above 4095 returns ESP_ERR_INVALID_ARG instead of silently wrapping.

mcp4725_set_voltage() takes volts and converts them with the supplied VDD:

ESP_ERROR_CHECK(mcp4725_set_voltage(&dev, 3.3f, 1.65f, false));

The voltage must be finite and lie between 0 and VDD, otherwise ESP_ERR_INVALID_ARG. Pass the real supply voltage of your board: the driver cannot measure it, and a wrong value scales the whole range.

mcp4725_get_raw_output() and mcp4725_get_voltage() read the register back. A read answer starts with a status byte, then the DAC value, then the EEPROM value; the driver skips the status byte and returns 12 bits.

Power-down modes

The output amplifier can be switched off and the output connected to ground through a resistor:

Constant Resistor
MCP4725_PM_NORMAL output enabled
MCP4725_PM_PD_1K 1 kΩ
MCP4725_PM_PD_100K 100 kΩ
MCP4725_PM_PD_500K 500 kΩ

mcp4725_set_power_mode() reads the current value, changes the two power-down bits and writes the register back, so the output value is preserved. mcp4725_get_power_mode() reads the bits from the DAC register or from the EEPROM, depending on its eeprom argument. In power-down mode the output is not high impedance: the selected resistor pulls it to ground, which is what makes the mode useful for a defined level instead of a floating pin.

The EEPROM

All the writing calls take a bool eeprom argument. With false only the DAC register changes and the new value is lost at power-off. With true the value and the power-down bits are also written to the internal EEPROM, and the chip loads them by itself after power-on — the firmware does not have to write anything before the output is valid.

Writing the EEPROM takes 25 ms typically and 50 ms at most, and the chip ignores the bus while it is busy (the status byte reports it). mcp4725_eeprom_busy() reads the RDY/BSY bit: busy is true while the write is in progress. Write the EEPROM sparingly — it has a limited number of erase cycles, so a value that changes continuously belongs in the DAC register only.

The factory default of the EEPROM is a midscale output (0x800, VDD / 2) in normal mode, so a fresh chip starts at half the supply.

Limitations

  • there is no way to read the POR flag of the status byte, so the driver cannot tell whether the last power-up was clean;
  • the fast-mode write command is not implemented: every write is the three-byte form that also carries the power-down bits;
  • the general call reset command is not implemented;
  • one descriptor per device; the driver serialises transactions of that descriptor with a mutex, two descriptors to the same chip are not coordinated;
  • the EEPROM write time is not enforced by the driver: poll mcp4725_eeprom_busy() after writing to the EEPROM before the next access.

Example

examples/default wakes the chip if it powers up in a power-down mode, stores the maximum output in the EEPROM, then ramps the output from 0 to VDD in 0.1 V steps every 100 ms:

Set default DAC output value to MAX...
Now let's generate the sawtooth wave in slow manner
Vout: 0.10
Vout: 0.20

Support

For questions and discussions about the component, please use Discussions at esp-idf-lib/core.

Contributing

Please read CONTRIBUTING.md at esp-idf-lib/core.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages