From b4224641db6285d61d89f77e8412e7b0697fd44b Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Wed, 10 Dec 2025 09:47:51 +0300 Subject: [PATCH] feat!: changed support for AC frequency to 400 Hz only --- README.md | 10 +++------- version.txt | 2 +- zh_ac_dimmer.c | 24 ++++++------------------ 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 17435ec..08456a3 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,13 @@ -# ESP32 ESP-IDF component for AC dimmer +# ESP32 ESP-IDF component for AC dimmer 115V/400Hz ## Tested on 1. [ESP32 ESP-IDF v5.5.1](https://docs.espressif.com/projects/esp-idf/en/v5.5.1/esp32/index.html) -## Features - -1. Supports frequency up to 400 Hz. -2. Automatic frequency detection. - ## Attention -For correct operation, please enable the following settings in the menuconfig: +1. Supports frequency 400Hz only. +2. For correct operation, please enable the following settings in the menuconfig: ```text GPIO_CTRL_FUNC_IN_IRAM diff --git a/version.txt b/version.txt index 867e524..359a5b9 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.2.0 \ No newline at end of file +2.0.0 \ No newline at end of file diff --git a/zh_ac_dimmer.c b/zh_ac_dimmer.c index c4ec561..3a6385d 100755 --- a/zh_ac_dimmer.c +++ b/zh_ac_dimmer.c @@ -18,9 +18,6 @@ static gptimer_alarm_config_t _alarm_config = {0}; static zh_ac_dimmer_init_config_t _init_config = {0}; static volatile uint64_t _prev_us = 0; -static volatile uint32_t _current_period_us = 0; -static volatile uint32_t _prev_period_us = 0; -static volatile uint16_t _zero_cross_us = 0; static volatile uint8_t _dimmer_value = 0; static volatile bool _is_dimmer_work = false; static bool _is_initialized = false; @@ -73,9 +70,6 @@ esp_err_t zh_ac_dimmer_deinit(void) _is_initialized = false; _dimmer_timer = NULL; _dimmer_value = 0; - _current_period_us = 0; - _prev_period_us = 0; - _zero_cross_us = 0; _prev_us = 0; ZH_LOGI("AC dimmer deinitialization completed successfully."); return ESP_OK; @@ -144,7 +138,7 @@ static esp_err_t _zh_ac_dimmer_gpio_init(const zh_ac_dimmer_init_config_t *confi .intr_type = GPIO_INTR_POSEDGE, .mode = GPIO_MODE_INPUT, .pin_bit_mask = (1ULL << config->zero_cross_gpio), - .pull_down_en = GPIO_PULLDOWN_DISABLE, + .pull_down_en = GPIO_PULLDOWN_ENABLE, .pull_up_en = GPIO_PULLUP_DISABLE, }; err = gpio_config(&zero_cross_gpio_config); @@ -188,23 +182,17 @@ static esp_err_t _zh_ac_dimmer_timer_init(void) static void IRAM_ATTR _zh_ac_dimmer_isr_handler(void *arg) { - if (_is_dimmer_work == false) + uint64_t _current_us = esp_timer_get_time(); + if (_current_us - _prev_us <= (1250 * 0.9)) // 90% of zero crossing period (1250 µs) at 400 Hz. { return; } gpio_set_level(_init_config.triac_gpio, 0); - uint64_t _current_us = esp_timer_get_time(); - _current_period_us = (uint32_t)(_current_us - _prev_us); _prev_us = _current_us; - if (_current_period_us < 1000) + if (_is_dimmer_work == false) { - if (_current_period_us > 50) - { - _zero_cross_us = (uint16_t)_current_period_us; - } - _current_period_us = _prev_period_us; + return; } - _prev_period_us = _current_period_us; if (_dimmer_value != 0) { if (_dimmer_value == 100) @@ -212,7 +200,7 @@ static void IRAM_ATTR _zh_ac_dimmer_isr_handler(void *arg) gpio_set_level(_init_config.triac_gpio, 1); return; } - _alarm_config.alarm_count = (uint64_t)(((_current_period_us / 110) * (100 - _dimmer_value)) + _zero_cross_us); + _alarm_config.alarm_count = (uint64_t)((((1250 - 330) / 100) * (100 - _dimmer_value)) + 330); // 330 is 50% of zero crossing time (by logic analyser). _alarm_config.flags.auto_reload_on_alarm = false; gptimer_set_alarm_action(_dimmer_timer, &_alarm_config); gptimer_start(_dimmer_timer);