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