# 模数转换器 (ADC)

[English]

## 概述

本指南介绍了 ESP32-S2 的模数转换器 (ADC)。首先介绍 ADC 核心概念，如 ADC 转换、原始数据分辨率、参考电压和衰减配置，随后详细讲解两种支持的驱动模式：单次转换模式和连续转换模式，以及用于提升精度的 ADC 校准功能。

ESP32-S2 集成了 2 个 ADC，用于测量多路输入通道的模拟信号。具体测量通道数（支持模拟功能的引脚）、电压范围和 ADC 特性，请参阅 技术规格书。

## ADC 转换

ADC 转换是将输入的模拟电压转换为数字值的过程。ADC 驱动 API 提供的原始转换结果是以数字形式反映模拟输入信号。

默认情况下，ADC 原始转换结果的位宽为 12 位，这意味着输入电压范围被划分为 4096 (212) 个离散电平，由此定义了输入信号的最小可检测变化量。

根据原始值 `data` 计算对应电压值 `Vdata` 的公式如下：

\[V_{data} = \frac{data}{2^{bitwidth} - 1} \times V_{ref}\]

其中：

- `data` 是 ADC 原始数据

- `bitwidth` 是 ADC 原始数据的分辨率（如 12 位）

- `Vref` 是 ADC 参考电压

设计上，`Vref` 出厂设定为 1100 mV，但由于制造差异，实际值可能在 1000 mV 到 1200 mV 之间波动。

请参阅 模数转换器 (ADC) 校准驱动程序 了解如何利用 ADC 校准驱动以得到校准后的准确电压值。

## ADC 衰减

ADC 可以测量 0 V 到 `Vref` 范围内的模拟电压。如需测量更高电压，可以在输入 ADC 之前对输入电压进行衰减。

支持的衰减等级包括：

- 0 dB (k≈100%)

- 2.5 dB (k≈75%)

- 6 dB (k≈50%)

- 12 dB (k≈25%)

衰减等级越高，ADC 可测量的输入电压范围越大。应用衰减后的电压 `Vdata` 计算公式为：

\[V_{data} = \frac{V_{ref}}{k}\times{\frac{data}{2^{bitwidth} - 1}}\]

其中：

- `k`：衰减参数对应的比例值

- 其他变量定义同上

各衰减配置对应的可测量输入电压范围，请参考 技术规格书 > 电气特性 > ADC 特性。

## 驱动使用

- 每个 ADC 单元支持 单次转换模式。该模式适用于单次采样：ADC 一次对一个通道进行采样。

- 每个 ADC 单元支持 连续转换模式。该模式适用于连续采样：ADC 依次对一组通道进行采样，或者连续对单个通道进行采样。

详见以下指南：

- 模数转换器 (ADC) 单次转换模式驱动
  - 简介
  - 功能概述
  - 应用示例
  - API 参考
- 模数转换器 (ADC) 连续转换模式驱动
  - 简介
  - 驱动原理
  - 功能概述
  - 应用示例
  - API 参考

## ADC 校准功能

ADC 校准驱动通过软件方式，修正偏差，获取更准确的输出结果。

详见以下指南：

- 模数转换器 (ADC) 校准驱动程序
  - 简介
  - 功能概述
  - API 参考

## API 参考

### Header File

- components/soc/esp32s2/include/soc/adc_channel.h

- This header file can be included with:

  >     #include "soc/adc_channel.h"

### Macros

ADC1_GPIO1_CHANNEL

ADC1_CHANNEL_0_GPIO_NUM

ADC1_GPIO2_CHANNEL

ADC1_CHANNEL_1_GPIO_NUM

ADC1_GPIO3_CHANNEL

ADC1_CHANNEL_2_GPIO_NUM

ADC1_GPIO4_CHANNEL

ADC1_CHANNEL_3_GPIO_NUM

ADC1_GPIO5_CHANNEL

ADC1_CHANNEL_4_GPIO_NUM

ADC1_GPIO6_CHANNEL

ADC1_CHANNEL_5_GPIO_NUM

ADC1_GPIO7_CHANNEL

ADC1_CHANNEL_6_GPIO_NUM

ADC1_GPIO8_CHANNEL

ADC1_CHANNEL_7_GPIO_NUM

ADC1_GPIO9_CHANNEL

ADC1_CHANNEL_8_GPIO_NUM

ADC1_GPIO10_CHANNEL

ADC1_CHANNEL_9_GPIO_NUM

ADC2_GPIO11_CHANNEL

ADC2_CHANNEL_0_GPIO_NUM

ADC2_GPIO12_CHANNEL

ADC2_CHANNEL_1_GPIO_NUM

ADC2_GPIO13_CHANNEL

ADC2_CHANNEL_2_GPIO_NUM

ADC2_GPIO14_CHANNEL

ADC2_CHANNEL_3_GPIO_NUM

ADC2_GPIO15_CHANNEL

ADC2_CHANNEL_4_GPIO_NUM

ADC2_GPIO16_CHANNEL

ADC2_CHANNEL_5_GPIO_NUM

ADC2_GPIO17_CHANNEL

ADC2_CHANNEL_6_GPIO_NUM

ADC2_GPIO18_CHANNEL

ADC2_CHANNEL_7_GPIO_NUM

ADC2_GPIO19_CHANNEL

ADC2_CHANNEL_8_GPIO_NUM

ADC2_GPIO20_CHANNEL

ADC2_CHANNEL_9_GPIO_NUM

### Header File

- components/esp_hal_ana_conv/include/hal/adc_types.h

- This header file can be included with:

  >     #include "hal/adc_types.h"

- This header file is a part of the API provided by the `esp_hal_ana_conv` component. To declare that your component depends on `esp_hal_ana_conv`, add the following to your CMakeLists.txt:

  >     REQUIRES esp_hal_ana_conv

  > or

  >     PRIV_REQUIRES esp_hal_ana_conv

### Structures

struct adc_digi_pattern_config_t

  ADC digital controller pattern configuration.

  Public Members

  uint8_t atten

    Attenuation of this ADC channel.

  uint8_t channel

    ADC channel.

  uint8_t unit

    ADC unit.

  uint8_t bit_width

    ADC output bit width.

### Type Definitions

typedef soc_periph_adc_rtc_clk_src_t adc_oneshot_clk_src_t

  Clock source type of oneshot mode which uses RTC controller.

typedef soc_periph_adc_digi_clk_src_t adc_continuous_clk_src_t

  Clock source type of continuous mode which uses digital controller.

### Enumerations

enum adc_unit_t

  ADC unit.

  Values:

  enumerator ADC_UNIT_1

    SAR ADC 1.

  enumerator ADC_UNIT_2

    SAR ADC 2.

enum adc_channel_t

  ADC channels.

  Values:

  enumerator ADC_CHANNEL_0

    ADC channel.

  enumerator ADC_CHANNEL_1

    ADC channel.

  enumerator ADC_CHANNEL_2

    ADC channel.

  enumerator ADC_CHANNEL_3

    ADC channel.

  enumerator ADC_CHANNEL_4

    ADC channel.

  enumerator ADC_CHANNEL_5

    ADC channel.

  enumerator ADC_CHANNEL_6

    ADC channel.

  enumerator ADC_CHANNEL_7

    ADC channel.

  enumerator ADC_CHANNEL_8

    ADC channel.

  enumerator ADC_CHANNEL_9

    ADC channel.

  enumerator ADC_CHANNEL_10

    ADC channel.

enum adc_atten_t

  ADC attenuation parameter. Different parameters determine the range of the ADC.

  Values:

  enumerator ADC_ATTEN_DB_0

    No input attenuation, ADC can measure up to approx.

  enumerator ADC_ATTEN_DB_2_5

    The input voltage of ADC will be attenuated extending the range of measurement by about 2.5 dB.

  enumerator ADC_ATTEN_DB_6

    The input voltage of ADC will be attenuated extending the range of measurement by about 6 dB.

  enumerator ADC_ATTEN_DB_12

    The input voltage of ADC will be attenuated extending the range of measurement by about 12 dB.

enum adc_bitwidth_t

  ADC bitwidth.

  Values:

  enumerator ADC_BITWIDTH_DEFAULT

    Default ADC output bits, max supported width will be selected.

  enumerator ADC_BITWIDTH_9

    ADC output width is 9Bit.

  enumerator ADC_BITWIDTH_10

    ADC output width is 10Bit.

  enumerator ADC_BITWIDTH_11

    ADC output width is 11Bit.

  enumerator ADC_BITWIDTH_12

    ADC output width is 12Bit.

  enumerator ADC_BITWIDTH_13

    ADC output width is 13Bit.

enum adc_ulp_mode_t

  ADC ULP working mode.

  This decides the controller that controls ADC when in low power mode. Set `ADC_ULP_MODE_DISABLE` for normal mode.

  Values:

  enumerator ADC_ULP_MODE_DISABLE

    ADC ULP mode is disabled.

  enumerator ADC_ULP_MODE_FSM

    ADC is controlled by ULP FSM.

  enumerator ADC_ULP_MODE_RISCV

    ADC is controlled by ULP RISCV.

enum adc_digi_convert_mode_t

  ADC digital controller (DMA mode) work mode.

  Values:

  enumerator ADC_CONV_SINGLE_UNIT_1

    Only use ADC1 for conversion.

  enumerator ADC_CONV_SINGLE_UNIT_2

    Only use ADC2 for conversion.

  enumerator ADC_CONV_BOTH_UNIT

    Use Both ADC1 and ADC2 for conversion simultaneously.

  enumerator ADC_CONV_ALTER_UNIT

    Use both ADC1 and ADC2 for conversion by turn. e.g. ADC1 -> ADC2 -> ADC1 -> ADC2 .....

enum adc_digi_output_format_t

  ADC digital controller (DMA mode) output data format option.

  Values:

  enumerator ADC_DIGI_OUTPUT_FORMAT_TYPE1

    See `adc_digi_output_data_t.type1`

  enumerator ADC_DIGI_OUTPUT_FORMAT_TYPE2

    See `adc_digi_output_data_t.type2`

enum adc_digi_iir_filter_t

  ADC IIR Filter ID.

  Values:

  enumerator ADC_DIGI_IIR_FILTER_0

    Filter 0.

  enumerator ADC_DIGI_IIR_FILTER_1

    Filter 1.

enum adc_digi_iir_filter_coeff_t

  IIR Filter Coefficient.

  Values:

  enumerator ADC_DIGI_IIR_FILTER_COEFF_2

    The filter coefficient is 2.

  enumerator ADC_DIGI_IIR_FILTER_COEFF_4

    The filter coefficient is 4.

  enumerator ADC_DIGI_IIR_FILTER_COEFF_8

    The filter coefficient is 8.

  enumerator ADC_DIGI_IIR_FILTER_COEFF_16

    The filter coefficient is 16.

  enumerator ADC_DIGI_IIR_FILTER_COEFF_32

    The filter coefficient is 32.

  enumerator ADC_DIGI_IIR_FILTER_COEFF_64

    The filter coefficient is 64.

enum adc_monitor_id_t

  ADC monitor (continuous mode) ID.

  Values:

  enumerator ADC_MONITOR_0

    The monitor index 0.

  enumerator ADC_MONITOR_1

    The monitor index 1.

enum adc_monitor_mode_t

  Monitor config/event mode type.

  Values:

  enumerator ADC_MONITOR_MODE_HIGH

    ADC raw_result > threshold value, monitor interrupt will be generated.

  enumerator ADC_MONITOR_MODE_LOW

    ADC raw_result < threshold value, monitor interrupt will be generated.

此文档对您有帮助吗？

- 反馈已收到，谢谢！
- 如果您有其他意见，欢迎填写乐鑫文档反馈表。

- 我们重视您的反馈。
- 您可以填写乐鑫文档反馈表告诉我们如何改进该文档。