官方文档翻译-esp32-adc

模数转换器

概述

ESP32 integrates two 12-bit SAR (Successive Approximation Register) ADCs (Analog to Digital Converters) and supports measurements on 18 channels (analog enabled pins). Some of these pins can be used to build a programmable gain amplifier which is used for the measurement of small analog signals.

ESP32集成了两个12位SAR(逐次逼近寄存器)ADC(模数转换器),并支持18个通道(模拟使能引脚)上的测量。其中一些引脚可用通过可编程增益放大器实现测量小的模拟信号。

The ADC driver API supports ADC1 (9 channels, attached to GPIOs 32 - 39), and ADC2 (10 channels, attached to GPIOs 0, 2, 4, 12 - 15 and 25 - 27).However, there’re some restrictions for the application to use ADC2:

ADC驱动程序API支持ADC1(9个通道,连接到GPIO 32-39)和ADC2(10个通道,连接到GPIO 0,2,4,12-15和25-27)。但是,应用程序使用ADC2有一些限制:

  1. The application can use ADC2 only when Wi-Fi driver is not started, since the ADC is also used by the Wi-Fi driver, which has higher priority.
  2. Some of the ADC2 pins are used as strapping pins (GPIO 0, 2, 15), so they cannot be used freely. For examples, for official Develop Kits:

  3. 只有当Wi-Fi未启动时,应用程序才可以使用ADC2,因为ADC也被较高优先级的Wi-Fi驱动器使用。

  4. 某些ADC2引脚用作固定用途(GPIO 0,2,15),因此不能自由使用。例如,官方开发工具包:
  • ESP32核心板V2 / ESP32 DevKitC:由于外部自动编程电路,GPIO 0不能使用。
  • ESP-WROVER-KIT V3:由于不同用途的外部连接,GPIO 0,2,4和15不能使用。

配置和读取ADC

The ADC should be configured before reading is taken.

读取前应配置ADC。

  • For ADC1, configure desired precision and attenuation by calling functions adc1_config_width() and adc1_config_channel_atten().
  • 对于ADC1,通过调用功能adc1_config_width()和配置来配置所需的精度和衰减adc1_config_channel_atten()
  • For ADC2, configure the attenuation by adc2_config_channel_atten(). The reading width of ADC2 is configured every time you take the reading.
  • 对于ADC2,通过adc2_config_channel_atten()配置衰减。每次读取,ADC2的读数宽度都会被配置一次。

Attenuation configuration is done per channel, see adc1_channel_t and adc2_channel_t, set as a parameter of above functions.

衰减配置是每个通道单独配置的,请参阅adc1_channel_tadc2_channel_t设置上述功能的参数。

Then it is possible to read ADC conversion result with adc1_get_raw() and adc2_get_raw(). Reading width of ADC2 should be set as a parameter of adc2_get_raw() instead of in the configuration functions.

然后可以用adc1_get_raw()adc2_get_raw()读取ADC转换结果。ADC2的读取宽度应由adc2_get_raw() 的参数设置而不是配置功能中的参数。

注意

Since the ADC2 is shared with the WIFI module, which has higher priority, reading operation of adc2_get_raw() will fail between esp_wifi_start() and esp_wifi_stop(). Use the return code to see whether the reading is successful.

由于ADC2与具有较高优先级的WIFI模块共享,读取操作adc2_get_raw()将在esp_wifi_start()esp_wifi_stop()之间失败。使用返回码查看读数是否成功。

It is also possible to read the internal hall effect sensor via ADC1 by calling dedicated function hall_sensor_read(). Note that even the hall sensor is internal to ESP32, reading from it uses channels 0 and 3 of ADC1 (GPIO 36 and 39). Do not connect anything else to these pins and do not change their configuration. Otherwise it may affect the measurement of low value signal from the sesnor.

也可以通过调用专用功能通过ADC1读取内部霍尔效应传感器hall_sensor_read()。请注意,即使霍尔传感器位于ESP32内部,读取它仍会使用ADC1(GPIO 36和39)的通道0和3。不要将其他任何东西连接到这些引脚,也不要更改其配置。否则可能会影响来自sesnor的低电压信号的测量。

This API provides convenient way to configure ADC1 for reading from ULP. To do so, call function adc1_ulp_enable() and then set precision and attenuation as discussed above.

该API提供了配置ADC1以便读取ULP的简便方法。为此,调用函数adc1_ulp_enable(),然后按照如上所述的方式设置精度(precision )和衰减(attenuation )。

There is another specific function adc2_vref_to_gpio() used to route internal reference voltage to a GPIO pin. It comes handy to calibrate ADC reading and this is discussed in section Minimizing Noise.

还有一个特定的功能adc2_vref_to_gpio()用于将内部参考电压路由至GPIO引脚。它适用于校准ADC读数,这在最小化噪声一节中讨论。

应用示例

Reading voltage on ADC1 channel 0 (GPIO 36):

读取ADC1通道0(GPIO 36)上的电压:

#include 

...

    adc1_config_width(ADC_WIDTH_BIT_12);
    adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_DB_0);
    int val = adc1_get_raw(ADC1_CHANNEL_0);

The input voltage in above example is from 0 to 1.1V (0 dB attenuation). The input range can be extended by setting higher attenuation, see adc_atten_t.An example using the ADC driver including calibration (discussed below) is available in esp-idf: peripherals/adc

上例中的输入电压为0至1.1V(衰减0 dB)。输入范围可以通过设置更高的衰减来扩展,参见adc_atten_t。esp-idf:peripherals / adc中提供了一个使用包括校准功能的ADC驱动器的示例(在下面讨论)

Reading voltage on ADC2 channel 7 (GPIO 27):

读取ADC2通道7(GPIO 27)上的电压:

#include 

...

    int read_raw;
    adc2_config_channel_atten( ADC2_CHANNEL_7, ADC_ATTEN_0db );

    esp_err_t r = adc2_get_raw( ADC2_CHANNEL_7, ADC_WIDTH_12Bit, &read_raw);
    if ( r == ESP_OK ) {
        printf("%d\n", read_raw );
    } else if ( r == ESP_ERR_TIMEOUT ) {
        printf("ADC2 used by Wi-Fi.\n");
    }

The reading may fail due to collision with Wi-Fi, should check it.An example using the ADC2 driver to read the output of DAC is available in esp-idf: peripherals/adc2

由于与Wi-Fi冲突,读取可能会失败,所以我们应检查它。使用ADC2驱动程序读取DAC输出的示例可在esp-idf:peripherals / adc2中找到

Reading the internal hall effect sensor:

读取内部霍尔效应传感器:

#include 

...

    adc1_config_width(ADC_WIDTH_BIT_12);
    int val = hall_sensor_read();

The value read in both these examples is 12 bits wide (range 0-4095).

这两个示例中读取的值都是12位宽(范围0-4095)。

最小噪声

The ESP32 ADC can be sensitive to noise leading to large discrepancies in ADC readings. To minimize noise, users may connect a 0.1uF capacitor to the ADC input pad in use. Multisampling may also be used to further mitigate the effects of noise.

ESP32 ADC可能对噪声敏感,导致ADC读数差异较大。为了使噪声最小化,用户可以在使用中将0.1uF电容连接到ADC输入焊盘。多重采样也可以用来进一步减轻噪音的影响。

官方文档翻译-esp32-adc_第1张图片

图表说明使用电容器和64个采样的多重采样来减少噪音。

ADC校准

The esp_adc_cal/include/esp_adc_cal.h API provides functions to correct for differences in measured voltages caused by variation of ADC reference voltages (Vref) between chips. Per design the ADC reference voltage is 1100mV, however the true reference voltage can range from 1000mV to 1200mV amongst different ESP32s.

esp_adc_cal /包含/ esp_adc_cal.h所述的API提供通过参考电压校准adc的功能。根据设计,ADC参考电压为1100mV,但不同ESP32之间的真实参考电压范围可以在1000mV至1200mV之间。

官方文档翻译-esp32-adc_第2张图片

图表说明不同参考电压对ADC电压曲线的影响。

Correcting ADC readings using this API involves characterizing one of the ADCs at a given attenuation to obtain a characteristics curve (ADC-Voltage curve) that takes into account the difference in ADC reference voltage. The characteristics curve is in the form of y = coeff_a * x + coeff_b and is used to convert ADC readings to voltages in mV. Calculation of the characteristics curve is based on calibration values which can be stored in eFuse or provided by the user.

使用这个API校正ADC读数涉及到一个给定衰减的ADCs,以获得一个特征曲线(ADC-电压曲线),它考虑了ADC参考电压的差异。在特定衰减的情况下对ADC进行描述:特征曲线是y = coeff_a * x + coeff_b的形式,用于将ADC读数转换成mV中的电压。特征曲线的计算是基于可以存储在eFuse或用户提供的校准值。

校准值

Calibration values are used to generate characteristic curves that account for the unique ADC reference voltage of a particular ESP32. There are currently three sources of calibration values. The availability of these calibration values will depend on the type and production date of the ESP32 chip/module.

校准值用于生成特定曲线,该曲线考虑了特定ESP32的独特ADC参考电压。目前有三个校准值来源。这些校准值的可用性将取决于ESP32芯片/模块的类型和生产日期。

Two Point values represent each of the ADCs’ readings at 150mV and 850mV. These values are measured and burned into eFuse BLOCK3 during factory calibration.

两点值代表每个ADC在150mV和850mV的读数。BLOCK3在工厂校准过程中测量这些值并将其烧入eFuse 。

eFuse Vref represents the true ADC reference voltage. This value is measured and burned into eFuse BLOCK0 during factory calibration.

eFuse 参考电压表示真实的ADC参考电压。BLOCK0在工厂校准期间测量该值并将其烧入eFuse 。

Default Vref is an estimate of the ADC reference voltage provided by the user as a parameter during characterization. If Two Point or eFuse Vref values are unavailable, Default Vref will be used.

默认参考电压是在表征过程中用户提供的ADC参考电压估计值作为参数。如果两点或eFuse Vref值不可用,则将使用默认参考电压

应用示例

有关完整示例,请参阅esp-idf:peripherals / adc

在特定衰减的情况下对ADC进行描述:

#include "driver/adc.h"
#include "esp_adc_cal.h"

...

    //Characterize ADC at particular atten
    esp_adc_cal_characteristics_t *adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
    esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
    //Check type of calibration value used to characterize ADC
    if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
        printf("eFuse Vref");
    } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
        printf("Two Point");
    } else {
        printf("Default");
    }

读取ADC然后将读数转换为电压:

#include "driver/adc.h"
#include "esp_adc_cal.h"

...
    uint32_t reading =  adc1_get_raw(ADC1_CHANNEL_5);
    uint32_t voltage = esp_adc_cal_raw_to_voltage(reading, adc_chars);

Routing ADC reference voltage to GPIO, so it can be manually measured (for Default Vref):

将ADC参考电压设置到GPIO,手动测量参考电压(对于默认参考电压):

#include "driver/adc.h"

...

    esp_err_t status = adc2_vref_to_gpio(GPIO_NUM_25);
    if (status == ESP_OK) {
        printf("v_ref routed to GPIO\n");
    } else {
        printf("failed to route v_ref\n");
    }

GPIO查找宏

There are macros available to specify the GPIO number of a ADC channel, or vice versa. e.g.

有宏可用于指定ADC通道的GPIO编号,反之亦然。例如

  1. ADC1_CHANNEL_0_GPIO_NUM is the GPIO number of ADC1 channel 0 (36);
  2. ADC1_GPIO32_CHANNEL is the ADC1 channel number of GPIO 32 (ADC1 channel 4).

  3. ADC1_CHANNEL_0_GPIO_NUM 是ADC1通道0(36)的GPIO编号;

  4. ADC1_GPIO32_CHANNEL 是GPIO 32(ADC1通道4)的ADC1通道号。

API参考

该参考文献涵盖三个部分:

  • ADC驱动程序
  • ADC校准
  • GPIO查找宏

本文翻译自https://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/adc.html

你可能感兴趣的:(单片机,esp32,外围设备)