过零检测
过零信号检测驱动程序是一个设计用于分析过零信号的组件。通过检查过零信号的周期和触发边缘,它可以确定信号的有效性、无效性、是否超出预期的频率范围以及是否存在信号丢失等情况。
过零信号检测组件支持检测两种类型的过零信号。
方波形:在信号经过零时翻转当前的电平。
脉冲型:在信号经过零时生成的脉冲。
由于响应延迟等因素,该组件支持两种驱动模式,包括 GPIO 中断 和 MCPWM 捕获。
用户可以灵活配置组件的驱动模式,并调整诸如有效频率范围和有效信号判断次数等参数。这提供了很高的灵活性。
该程序以事件的形式返回结果和数据,满足用户对信号进行及时处理的需求。
过零检测事件
每个零检测事件的触发条件如下表所示:
事件 |
触发条件 |
---|---|
SIGNAL_RISING_EDGE |
上升沿 |
SIGNAL_FALLING_EDGE |
下降沿 |
SIGNAL_VALID |
频率在有效范围内次数大于设置阈值 |
SIGNAL_INVALID |
频率在无效范围内次数大于设置阈值 |
SIGNAL_LOST |
在 100ms 内信号没有上升沿、下降沿 |
SIGNAL_FREQ_OUT_OF_RANGE |
计算的频率在设置的频率范围之外 |
注意
回调函数中不能有 TaskDelay 等阻塞的操作
配置项
USE_GPTIMER : 决定是否使用 GPTimer 驱动 默认使用 ESPTimer
应用示例
创建过零检测
void IRAM_ATTR zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param, void *usr_data) //User's callback API
{
switch (zero_detect_event) {
case SIGNAL_FREQ_OUT_OF_RANGE:
ESP_LOGE(TAG, "SIGNAL_FREQ_OUT_OF_RANGE");
break;
case SIGNAL_VALID:
ESP_LOGE(TAG, "SIGNAL_VALID");
break;
case SIGNAL_LOST:
ESP_LOGE(TAG, "SIGNAL_LOST");
break;
default:
break;
}
}
// Create a zero detection and register call-back
zero_detect_config_t config = {
.capture_pin = 2,
.freq_range_max_hz = 65,
.freq_range_min_hz = 45, //Hz
.valid_times = 6,
.invalid_times = 5,
.zero_signal_type = SQUARE_WAVE,
.zero_driver_type = MCPWM_TYPE,
};
zero_detect_handle_t *g_zcds = zero_detect_create(&config);
if(NULL == g_zcds) {
ESP_LOGE(TAG, "Zero Detection create failed");
}
zero_detect_register_cb(g_zcds, zero_detection_event_cb, NULL);
API Reference
Header File
Functions
-
zero_detect_handle_t zero_detect_create(zero_detect_config_t *config)
Create a zero detect target.
- 参数
config – A zero detect object to config
- 返回
zero_detect_handle_t A zero cross detection object
-
void zero_show_data(zero_detect_handle_t zcd_handle)
Show zero detect test data.
- 参数
zcd_handle – A zero detect handle
-
esp_err_t zero_detect_delete(zero_detect_handle_t zcd_handle)
Delete a zero detect device.
- 参数
zcd_handle – A zero detect handle
- 返回
ESP_OK Success
ESP_FAIL Failure
-
bool zero_detect_get_power_status(zero_detect_handle_t zcd_handle)
Get relay power status.
- 参数
zcd_handle – A zero detect handle
- 返回
true Power on
false Power off
-
bool zero_detect_singal_invaild_status(zero_detect_handle_t zcd_handle)
Get signal invalid status.
- 参数
zcd_handle – A zero detect handle
- 返回
true Signal is invalid
false Signal is valid
-
zero_signal_type_t zero_detect_get_signal_type(zero_detect_handle_t zcd_handle)
Get signal type.
- 参数
zcd_handle – A zero detect handle
- 返回
SQUARE_WAVE Signal type is square
PULSE_WAVE Signal type is pulse
-
esp_err_t zero_detect_register_cb(zero_detect_handle_t zcd_handle, zero_cross_cb_t cb, void *usr_data)
Register zero cross detection callback.
- 参数
zcd_handle – A zero detect handle
cb – A callback function
usr_data – User data
- 返回
ESP_OK Success
ESP_FAIL Failure
Unions
-
union zero_detect_cb_param_t
- #include <zero_detection.h>
Event callback parameters union.
Public Members
-
struct zero_detect_cb_param_t::signal_freq_event_data_t signal_freq_event_data
Signal freq event data struct
-
struct zero_detect_cb_param_t::signal_valid_event_data_t signal_valid_event_data
Signal valid event data struct
-
struct zero_detect_cb_param_t::signal_invalid_event_data_t signal_invalid_event_data
Signal invalid event data struct
-
struct zero_detect_cb_param_t::signal_rising_edge_event_data_t signal_rising_edge_event_data
Signal rising edge event data struct
-
struct zero_detect_cb_param_t::signal_falling_edge_event_data_t signal_falling_edge_event_data
Signal falling edge event_data
-
struct signal_falling_edge_event_data_t
- #include <zero_detection.h>
Signal falling edge data return type.
-
struct signal_freq_event_data_t
- #include <zero_detection.h>
Signal exceeds frequency range data return type.
Public Members
-
zero_signal_edge_t cap_edge
Trigger edge of zero cross signal
-
uint32_t full_cycle_us
Current signal cycle
-
zero_signal_edge_t cap_edge
-
struct signal_invalid_event_data_t
- #include <zero_detection.h>
Signal invalid data return type.
Public Members
-
zero_signal_edge_t cap_edge
Trigger edge of zero cross signal
-
uint32_t full_cycle_us
Current signal cycle
-
uint16_t invalid_count
Counting when the signal is invalid
-
zero_signal_edge_t cap_edge
-
struct signal_rising_edge_event_data_t
- #include <zero_detection.h>
Signal rising edge data return type.
-
struct signal_valid_event_data_t
- #include <zero_detection.h>
Signal valid data return type.
Public Members
-
zero_signal_edge_t cap_edge
Trigger edge of zero cross signal
-
uint32_t full_cycle_us
Current signal cycle
-
uint16_t valid_count
Counting when the signal is valid
-
zero_signal_edge_t cap_edge
-
struct zero_detect_cb_param_t::signal_freq_event_data_t signal_freq_event_data
Structures
-
struct zero_detect_config_t
User config data type.
Public Members
-
int32_t capture_pin
GPIO number for zero cross detect capture
-
uint16_t valid_times
Minimum required number of times for detecting signal validity
-
uint16_t invalid_times
Minimum required number of times for detecting signal invalidity
-
uint64_t signal_lost_time_us
Minimum required duration for detecting signal loss
-
zero_signal_type_t zero_signal_type
Zero Crossing Signal type
-
zero_driver_type_t zero_driver_type
Zero crossing driver type
-
double freq_range_max_hz
Maximum value of the frequency range when determining a valid signal
-
double freq_range_min_hz
Minimum value of the frequency range when determining a valid signal
-
int32_t capture_pin
Macros
-
ZERO_DETECTION_INIT_CONFIG_DEFAULT()
Type Definitions
-
typedef void (*zero_cross_cb_t)(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param, void *usr_data)
Callback format.
-
typedef void *zero_detect_handle_t
Enumerations
-
enum zero_detect_event_t
Zero detection events.
Values:
-
enumerator SIGNAL_FREQ_OUT_OF_RANGE
-
enumerator SIGNAL_VALID
-
enumerator SIGNAL_INVALID
-
enumerator SIGNAL_LOST
-
enumerator SIGNAL_RISING_EDGE
-
enumerator SIGNAL_FALLING_EDGE
-
enumerator SIGNAL_FREQ_OUT_OF_RANGE
-
enum zero_signal_type_t
Zero detection wave type.
Values:
-
enumerator SQUARE_WAVE
-
enumerator PULSE_WAVE
-
enumerator SQUARE_WAVE