Digital To Analog Converter (DAC)

Overview

ESP32-S2 has two 8-bit DAC (digital to analog converter) channels, connected to GPIO17 (Channel 1) and GPIO18 (Channel 2).

The DAC driver allows these channels to be set to arbitrary voltages.

The DAC channels can also be driven with DMA-style written sample data by the digital controller, however the driver does not supported this yet.

For other analog output options, see the Sigma-delta Modulation module and the LED Control module. Both these modules produce high frequency PWM output, which can be hardware low-pass filtered in order to generate a lower frequency analog output.

Application Example

Setting DAC channel 1 (GPIO17) voltage to approx 0.78 of VDD_A voltage (VDD * 200 / 255). For VDD_A 3.3V, this is 2.59V.

#include <driver/dac.h>

...

dac_output_enable(DAC_CHANNEL_1);
dac_output_voltage(DAC_CHANNEL_1, 200);

API Reference

Functions

esp_err_t dac_digi_init(void)

DAC digital controller initialization.

返回

  • ESP_OK success

esp_err_t dac_digi_deinit(void)

DAC digital controller deinitialization.

返回

  • ESP_OK success

esp_err_t dac_digi_controller_config(const dac_digi_config_t *cfg)

Setting the DAC digital controller.

参数

cfg – Pointer to digital controller paramter. See dac_digi_config_t.

返回

  • ESP_OK success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t dac_digi_start(void)

DAC digital controller start output voltage.

返回

  • ESP_OK success

esp_err_t dac_digi_stop(void)

DAC digital controller stop output voltage.

返回

  • ESP_OK success

esp_err_t dac_digi_fifo_reset(void)

Reset DAC digital controller FIFO.

返回

  • ESP_OK success

esp_err_t dac_digi_reset(void)

Reset DAC digital controller.

返回

  • ESP_OK success

Functions

esp_err_t dac_pad_get_io_num(dac_channel_t channel, gpio_num_t *gpio_num)

Get the GPIO number of a specific DAC channel.

参数
  • channel – Channel to get the gpio number

  • gpio_num – output buffer to hold the gpio number

返回

  • ESP_OK if success

esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)

Set DAC output voltage. DAC output is 8-bit. Maximum (255) corresponds to VDD3P3_RTC.

备注

Need to configure DAC pad before calling this function. DAC channel 1 is attached to GPIO25, DAC channel 2 is attached to GPIO26

参数
  • channel – DAC channel

  • dac_value – DAC output value

返回

  • ESP_OK success

esp_err_t dac_output_enable(dac_channel_t channel)

DAC pad output enable.

备注

DAC channel 1 is attached to GPIO25, DAC channel 2 is attached to GPIO26 I2S left channel will be mapped to DAC channel 2 I2S right channel will be mapped to DAC channel 1

参数

channel – DAC channel

esp_err_t dac_output_disable(dac_channel_t channel)

DAC pad output disable.

备注

DAC channel 1 is attached to GPIO25, DAC channel 2 is attached to GPIO26

参数

channel – DAC channel

返回

  • ESP_OK success

esp_err_t dac_cw_generator_enable(void)

Enable cosine wave generator output.

返回

  • ESP_OK success

esp_err_t dac_cw_generator_disable(void)

Disable cosine wave generator output.

返回

  • ESP_OK success

esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)

Config the cosine wave generator function in DAC module.

参数

cw – Configuration.

返回

  • ESP_OK success

  • ESP_ERR_INVALID_ARG The parameter is NULL.

GPIO Lookup Macros

Some useful macros can be used to specified the GPIO number of a DAC channel, or vice versa. e.g.

  1. DAC_CHANNEL_1_GPIO_NUM is the GPIO number of channel 1 (GPIO17);

  2. DAC_GPIO18_CHANNEL is the channel number of GPIO 26 (channel 2).

Macros

DAC_GPIO17_CHANNEL
DAC_CHANNEL_1_GPIO_NUM
DAC_GPIO18_CHANNEL
DAC_CHANNEL_2_GPIO_NUM

Structures

struct dac_cw_config_t

Config the cosine wave generator function in DAC module.

Public Members

dac_channel_t en_ch

Enable the cosine wave generator of DAC channel.

dac_cw_scale_t scale

Set the amplitude of the cosine wave generator output.

dac_cw_phase_t phase

Set the phase of the cosine wave generator output.

uint32_t freq

Set frequency of cosine wave generator output. Range: 130(130Hz) ~ 55000(100KHz).

int8_t offset

Set the voltage value of the DC component of the cosine wave generator output. Note: Unreasonable settings can cause waveform to be oversaturated. Range: -128 ~ 127.

struct dac_digi_config_t

DAC digital controller (DMA mode) configuration parameters.

Public Members

dac_digi_convert_mode_t mode

DAC digital controller (DMA mode) work mode. See dac_digi_convert_mode_t.

uint32_t interval

The number of interval clock cycles for the DAC digital controller to output voltage. The unit is the divided clock. Range: 1 ~ 4095. Expression: dac_output_freq = controller_clk / interval. Refer to adc_digi_clk_t. Note: The sampling rate of each channel is also related to the conversion mode (See dac_digi_convert_mode_t) and pattern table settings.

adc_digi_clk_t dig_clk

DAC digital controller clock divider settings. Refer to adc_digi_clk_t. Note: The clocks of the DAC digital controller use the ADC digital controller clock divider.

Enumerations

enum dac_channel_t

Values:

enumerator DAC_CHANNEL_1

DAC channel 1 is GPIO25(ESP32) / GPIO17(ESP32S2)

enumerator DAC_CHANNEL_2

DAC channel 2 is GPIO26(ESP32) / GPIO18(ESP32S2)

enumerator DAC_CHANNEL_MAX
enum dac_cw_scale_t

The multiple of the amplitude of the cosine wave generator. The max amplitude is VDD3P3_RTC.

Values:

enumerator DAC_CW_SCALE_1

1/1. Default.

enumerator DAC_CW_SCALE_2

1/2.

enumerator DAC_CW_SCALE_4

1/4.

enumerator DAC_CW_SCALE_8

1/8.

enum dac_cw_phase_t

Set the phase of the cosine wave generator output.

Values:

enumerator DAC_CW_PHASE_0

Phase shift +0°

enumerator DAC_CW_PHASE_180

Phase shift +180°

enum dac_digi_convert_mode_t

DAC digital controller (DMA mode) work mode.

Values:

enumerator DAC_CONV_NORMAL

The data in the DMA buffer is simultaneously output to the enable channel of the DAC.

enumerator DAC_CONV_ALTER

The data in the DMA buffer is alternately output to the enable channel of the DAC.

enumerator DAC_CONV_MAX