Using timers and interrupts - AC dimmer application
Posted: Sun Jun 07, 2020 8:05 pm
Hello to everyone,
I want to build an AC dimmer with rotary enconder using the esp32, and add WiFi or bluetooth capability later. So far, I'm able to use timers and interrupts to read my rotary encoder, as well, pin interrupts to switch on/off the lamp.
My code goes like this:
1) I have a dimmer board which has zero-cross detection (from 50Hz mains), which is connected to an input pin (DIMMER_SYNC_PIN). I also use BTN1 as an on/off switch for the dimmer board (and hence the lamp). For both of pins I add ISR handlers.
2) I create 2 timers:
The first timer, "EncoderTimer", is set with a period of encoder_timer_period, and its always running. For each period, calls the function encoder_timer_callback which reads the rotary encoder. This works perfectly fine.
The second timer, "DimmerTimer", is set with autoreload to FALSE, thus it will be a one-shot timer and enters the dormant state after it expires. The idea is to, every time there is a zero-cross detection at the DIMMER_SYNC_PIN, the ISR dimmer_sync_handler() is called:
in this part of the code, the period of the timer timerDimmer is set to 5ms (however this value corresponds to the proper delay in order to dim the lamp. It ranges from 0ms to 10ms [50Hz mains]. I'm using 5ms just for testing 50% dimming light). The timer should start from this ISR and after it expires (delay for dimmer TRIAC) I call the function dimmer_timer_callback which manages to send the pulse to the gate of the TRIAC.
My problem is that timerDimmer is not running, is not able start or run from the ISR function dimmer_sync_handler(). This is my second try but first I tried to use espressif sdk for timers rather than timers in FreeRTOS, yielding the same results, timer not able to start from ISR. What could be the problem? Thanks a lot!
If you are interested in more information, I'm sharing my two codes:
This is my first code, using timers from espressif timer driver: https://drive.google.com/file/d/11WvCDv ... sp=sharing
The second code, which I shared in this post, using FreeRTOS timers:
https://drive.google.com/file/d/11SREzd ... sp=sharing
I want to build an AC dimmer with rotary enconder using the esp32, and add WiFi or bluetooth capability later. So far, I'm able to use timers and interrupts to read my rotary encoder, as well, pin interrupts to switch on/off the lamp.
My code goes like this:
1) I have a dimmer board which has zero-cross detection (from 50Hz mains), which is connected to an input pin (DIMMER_SYNC_PIN). I also use BTN1 as an on/off switch for the dimmer board (and hence the lamp). For both of pins I add ISR handlers.
Code: Select all
//install gpio isr service
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(BTN1_PIN, BTN1_ISR_handler,NULL);
gpio_isr_handler_add(DIMMER_SYNC_PIN, dimmer_sync_handler,NULL);
Code: Select all
TimerHandle_t timerEncoder;
TimerHandle_t timerDimmer;
timerEncoder = xTimerCreate("EncoderTimer",encoder_timer_period,pdTRUE,(void*)0,encoder_timer_callback);
timerDimmer = xTimerCreate("DimmerTimer",dimmer_timer_period,pdFALSE,(void*)0,dimmer_timer_callback);
The second timer, "DimmerTimer", is set with autoreload to FALSE, thus it will be a one-shot timer and enters the dormant state after it expires. The idea is to, every time there is a zero-cross detection at the DIMMER_SYNC_PIN, the ISR dimmer_sync_handler() is called:
Code: Select all
void IRAM_ATTR dimmer_sync_handler(void* arg){
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
TickType_t newPeriod = pdMS_TO_TICKS(5);
xTimerChangePeriodFromISR(timerDimmer,newPeriod,&xHigherPriorityTaskWoken);
xTimerStartFromISR(timerDimmer,&xHigherPriorityTaskWoken);
}
My problem is that timerDimmer is not running, is not able start or run from the ISR function dimmer_sync_handler(). This is my second try but first I tried to use espressif sdk for timers rather than timers in FreeRTOS, yielding the same results, timer not able to start from ISR. What could be the problem? Thanks a lot!
If you are interested in more information, I'm sharing my two codes:
This is my first code, using timers from espressif timer driver: https://drive.google.com/file/d/11WvCDv ... sp=sharing
The second code, which I shared in this post, using FreeRTOS timers:
https://drive.google.com/file/d/11SREzd ... sp=sharing