Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b4224641db | |||
| c0ca733872 | |||
| fe8b53eafe | |||
| 3ed2a77ea5 |
15
README.md
15
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
|
||||
@@ -49,14 +45,19 @@ void app_main(void)
|
||||
config.triac_gpio = GPIO_NUM_17;
|
||||
zh_ac_dimmer_init(&config);
|
||||
zh_ac_dimmer_start();
|
||||
uint8_t value = 0;
|
||||
for (uint8_t i = 0; i <= 100; ++i)
|
||||
{
|
||||
zh_ac_dimmer_set(i);
|
||||
zh_ac_dimmer_get(&value);
|
||||
printf("Dimmer value: %d.\n", value);
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
for (uint8_t i = 100; i > 0; --i)
|
||||
{
|
||||
zh_ac_dimmer_set(i);
|
||||
zh_ac_dimmer_get(&value);
|
||||
printf("Dimmer value: %d.\n", value);
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
zh_ac_dimmer_stop();
|
||||
|
||||
@@ -76,6 +76,15 @@ extern "C"
|
||||
*/
|
||||
esp_err_t zh_ac_dimmer_set(uint8_t value);
|
||||
|
||||
/**
|
||||
* @brief Get AC dimmer dimming value.
|
||||
*
|
||||
* @param[out] value Pointer to dimming value.
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
*/
|
||||
esp_err_t zh_ac_dimmer_get(uint8_t *value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1 +1 @@
|
||||
1.1.1
|
||||
2.0.0
|
||||
@@ -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;
|
||||
@@ -109,6 +103,16 @@ esp_err_t zh_ac_dimmer_set(uint8_t value)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_ac_dimmer_get(uint8_t *value)
|
||||
{
|
||||
ZH_LOGI("AC dimmer getting status begin.");
|
||||
ZH_ERROR_CHECK(value != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer getting status failed. Value is NULL.");
|
||||
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "AC dimmer getting status failed. AC dimmer is not initialized.");
|
||||
*value = _dimmer_value;
|
||||
ZH_LOGI("AC dimmer getting status completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t _zh_ac_dimmer_validate_config(const zh_ac_dimmer_init_config_t *config)
|
||||
{
|
||||
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Initial config is NULL.");
|
||||
@@ -134,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);
|
||||
@@ -178,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;
|
||||
return;
|
||||
}
|
||||
_current_period_us = _prev_period_us;
|
||||
}
|
||||
_prev_period_us = _current_period_us;
|
||||
if (_dimmer_value != 0)
|
||||
{
|
||||
if (_dimmer_value == 100)
|
||||
@@ -202,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);
|
||||
|
||||
Reference in New Issue
Block a user