mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-04 13:14:32 +02:00
docs: Add description and code example for ADC zero-crossing detection
This commit is contained in:
@@ -117,7 +117,7 @@ If the ADC continuous mode driver is no longer used, you should deinitialize the
|
||||
|
||||
- :cpp:func:`adc_continuous_monitor_enable`: Enable a monitor.
|
||||
- :cpp:func:`adc_continuous_monitor_disable`: Disable a monitor.
|
||||
- :cpp:func:`adc_monitor_register_callbacks`: register user callbacks to take action when the ADC value exceeds of the threshold.
|
||||
- :cpp:func:`adc_continuous_monitor_register_event_callbacks`: register user callbacks to take action when the ADC value exceeds of the thresholds.
|
||||
- :cpp:func:`adc_del_continuous_monitor`: Delete a created monitor and free resources.
|
||||
|
||||
.. only:: esp32s2
|
||||
@@ -129,6 +129,41 @@ If the ADC continuous mode driver is no longer used, you should deinitialize the
|
||||
2. Only one monitor is supported for one ADC unit.
|
||||
3. All enabled channel(s) of a certain ADC unit in ADC continuous mode driver will be monitored. The :cpp:member:`adc_monitor_config_t::channel` parameter will not be used.
|
||||
|
||||
Specifically, the monitor function can be used to implement zero-crossing detection. As ADC cannot directly process negative input signals, an extra **DC bias** should be applied to the original signal before measurement.
|
||||
|
||||
First, add a DC bias to the input signal through a circuit to "shift" the negative signal into the ADC's measurement range. For the measurement range, please refer to the On-Chip Sensor and Analog Signal Processing chapter in `TRM <{IDF_TARGET_TRM_EN_URL}>`__. For example, adding a 1 V bias would transform a signal from -1 V to +1 V into 0 V to 2 V range. Then by setting the appropriate high and low thresholds, the ADC can detect if the input signal approaches zero, allowing for the identification of phase changes in the signal. Refer to the example code below for details.
|
||||
|
||||
.. code:: c
|
||||
|
||||
// Initialize the ADC monitor handle
|
||||
adc_monitor_handle_t adc_monitor_handle = NULL;
|
||||
|
||||
// Configure the ADC monitor
|
||||
adc_monitor_config_t zero_crossing_config = {
|
||||
.adc_unit = EXAMPLE_ADC_UNIT_1, // Specify the ADC unit to monitor
|
||||
.channel = EXAMPLE_ADC_CHANNEL_0, // Specify the ADC channel to monitor
|
||||
.h_threshold = 1100, // Set the high threshold close to the DC bias and adjust it as needed
|
||||
.l_threshold = 900, // Set the low threshold close to the DC bias and adjust it as needed
|
||||
};
|
||||
|
||||
// Create the ADC monitor
|
||||
ESP_ERROR_CHECK(adc_new_continuous_monitor(&zero_crossing_config, &adc_monitor_handle));
|
||||
|
||||
// Register the callback function
|
||||
adc_monitor_evt_cbs_t zero_crossing_cbs = {
|
||||
.on_over_high_thresh = example_on_exceed_high_thresh,
|
||||
.on_below_low_thresh = example_on_below_low_thresh,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_monitor_register_event_callbacks(adc_monitor_handle, &zero_crossing_cbs, NULL));
|
||||
|
||||
// Enable the ADC monitor
|
||||
ESP_ERROR_CHECK(adc_continuous_monitor_enable(adc_monitor_handle));
|
||||
|
||||
// Disable and delete the ADC monitor
|
||||
ESP_ERROR_CHECK(adc_continuous_monitor_disable(adc_monitor_handle));
|
||||
ESP_ERROR_CHECK(adc_del_continuous_monitor(adc_monitor_handle));
|
||||
|
||||
Initialize the ADC Continuous Mode Driver
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -277,7 +312,7 @@ where:
|
||||
* - Dout
|
||||
- ADC raw digital reading result.
|
||||
* - Vmax
|
||||
- Maximum measurable input analog voltage, this is related to the ADC attenuation, please refer to the On-Chip Sensor and Analog Signal Processing chapter in `Datasheet <{IDF_TARGET_DATASHEET_EN_URL}>`__.
|
||||
- Maximum measurable input analog voltage, this is related to the ADC attenuation, please refer to the On-Chip Sensor and Analog Signal Processing chapter in `TRM <{IDF_TARGET_TRM_EN_URL}#sensor>`__.
|
||||
* - Dmax
|
||||
- Maximum of the output ADC raw digital reading result, which is 2^bitwidth, where the bitwidth is the :cpp:member:`adc_digi_pattern_config_t::bit_width` configured before.
|
||||
|
||||
|
@@ -117,7 +117,7 @@ ADC 连续转换模式驱动基于 {IDF_TARGET_NAME} SAR ADC 模块实现,不
|
||||
|
||||
- :cpp:func:`adc_continuous_monitor_enable`:启用监视器。
|
||||
- :cpp:func:`adc_continuous_monitor_disable`:禁用监视器.
|
||||
- :cpp:func:`adc_monitor_register_callbacks`:注册用户回调函数,在 ADC 转换结果超出阈值时,执行相应操作。
|
||||
- :cpp:func:`adc_continuous_monitor_register_event_callbacks`:注册用户回调函数,在 ADC 转换结果超出阈值时,执行相应操作。
|
||||
- :cpp:func:`adc_del_continuous_monitor`:删除监视器,释放资源。
|
||||
|
||||
.. only:: esp32s2
|
||||
@@ -129,6 +129,41 @@ ADC 连续转换模式驱动基于 {IDF_TARGET_NAME} SAR ADC 模块实现,不
|
||||
2. 每个 ADC 单元仅支持一个监视器。
|
||||
3. ADC 连续转换模式驱动中,如果启用了监视器,无需使用参数 :cpp:member:`adc_monitor_config_t::channel` 指定,某个 ADC 单元中所有已启用的通道都会受监视。
|
||||
|
||||
特别地,监视器功能可用于实现过零检测。由于 ADC 无法直接处理负输入信号,可以通过 **直流偏置(DC bias)** 来实现过零检测。
|
||||
|
||||
首先,通过电路将直流偏置添加到输入信号中,以将负信号“移位”到 ADC 的测量范围内。关于 ADC 的测量范围,请参考 `技术参考手册 <{IDF_TARGET_TRM_CN_URL}#sensor>`__ 中的片上传感器与模拟信号处理章节。例如,添加一个 1 V 的偏置可以将 -1 V 至 +1 V 的信号变换到 0 V 至 2 V 的范围。然后,通过设置合适的高阈值与低阈值,ADC 可以检测输入信号是否接近零,从而识别信号的相位变化。详情请参考下面的示例代码。
|
||||
|
||||
.. code:: c
|
||||
|
||||
// 初始化 ADC 监视器句柄
|
||||
adc_monitor_handle_t adc_monitor_handle = NULL;
|
||||
|
||||
// 配置 ADC 监视器
|
||||
adc_monitor_config_t zero_crossing_config = {
|
||||
.adc_unit = EXAMPLE_ADC_UNIT_1, // 指定要监视的 ADC 单元
|
||||
.channel = EXAMPLE_ADC_CHANNEL_0, // 指定要监视的 ADC 通道
|
||||
.h_threshold = 1100, // 设置监视的高阈值为接近偏置值,请根据实际情况进行调整
|
||||
.l_threshold = 900, // 设置监视的低阈值为接近偏置值,请根据实际情况进行调整
|
||||
};
|
||||
|
||||
// 创建 ADC 监视器
|
||||
ESP_ERROR_CHECK(adc_new_continuous_monitor(&zero_crossing_config, &adc_monitor_handle));
|
||||
|
||||
// 注册回调函数
|
||||
adc_monitor_evt_cbs_t zero_crossing_cbs = {
|
||||
.on_over_high_thresh = example_on_exceed_high_thresh,
|
||||
.on_below_low_thresh = example_on_below_low_thresh,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_monitor_register_event_callbacks(adc_monitor_handle, &zero_crossing_cbs, NULL));
|
||||
|
||||
// 启用 ADC 监视器
|
||||
ESP_ERROR_CHECK(adc_continuous_monitor_enable(adc_monitor_handle));
|
||||
|
||||
// 禁用并删除 ADC 监视器
|
||||
ESP_ERROR_CHECK(adc_continuous_monitor_disable(adc_monitor_handle));
|
||||
ESP_ERROR_CHECK(adc_del_continuous_monitor(adc_monitor_handle));
|
||||
|
||||
初始化 ADC 连续转换模式驱动
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -165,7 +200,7 @@ ADC 连续转换模式驱动基于 {IDF_TARGET_NAME} SAR ADC 模块实现,不
|
||||
|
||||
按照以下步骤设置 :cpp:type:`adc_digi_pattern_config_t`:
|
||||
|
||||
- :cpp:member:`adc_digi_pattern_config_t::atten`:ADC 衰减。请参阅 `技术规格书 <{IDF_TARGET_DATASHEET_CN_URL}#sensor>`__ 中的 ``ADC 特性`` 章节。
|
||||
- :cpp:member:`adc_digi_pattern_config_t::atten`:ADC 衰减。请参阅 `技术参考手册 <{IDF_TARGET_TRM_CN_URL}#sensor>`__ 中的 ``ADC 特性`` 章节。
|
||||
- :cpp:member:`adc_digi_pattern_config_t::channel`:IO 对应的 ADC 通道号,请参阅下文注意事项。
|
||||
- :cpp:member:`adc_digi_pattern_config_t::unit`:IO 所属的 ADC 单元。
|
||||
- :cpp:member:`adc_digi_pattern_config_t::bit_width`:原始转换结果的位宽。
|
||||
|
Reference in New Issue
Block a user