Page 1 of 1

Timer/Counter with ZeroCross for dimmer

Posted: Sat Feb 19, 2022 2:01 pm
by mm_1993
Hello All
I use ESP32-C3 idf 4.3
I want to know how can reset timer in this example :
https://github.com/espressif/esp-idf/bl ... ple_main.c

I wanna build a dimmer with zeroCross pulse
I read ZC Pulse in GPIO interrupt and then set out put LED GPIO
here I need a timer for dimming LED (timer for -> 0 to 10 millisecond)
so How can Reset Timer Value to zero? is there any Idea?

Do not be embarrassed any idea may be useful
thank you

Re: Timer/Counter with external pulse

Posted: Wed Feb 23, 2022 8:10 am
by mm_1993
this is my code :
Edited !!

  1.  
  2.  
  3. #include <stdio.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "freertos/queue.h"
  7. #include "driver/timer.h"
  8. #include "esp_log.h"
  9.  
  10.  
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include "esp_timer.h"
  14.  
  15. #include "esp_sleep.h"
  16. #include "sdkconfig.h"
  17. #include "driver/gpio.h"
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. static const char* TAG = "example";
  25.  
  26.  
  27.  
  28. #define TIMER_RESOLUTION_HZ         10000000        // resolution
  29. #define TIMER_ALARM_PERIOD_S        1               // Alarm period sec
  30.  
  31. #define ZeroCross                   GPIO_NUM_4      // zerocross
  32. #define ZeroCrossPIN_Select         (1ULL << ZeroCross)
  33.  
  34. #define Indicator_LED1              GPIO_NUM_8
  35.  
  36.  
  37.  
  38. static xQueueHandle gpio_evt_queue;
  39.  
  40.  
  41. esp_timer_handle_t oneshot_timer;
  42. uint32_t counter1 = 0;
  43.  
  44.  
  45. // static void periodic_timer_callback(void* arg);
  46. static void oneshot_timer_callback(void* arg);
  47.  
  48.  
  49.  
  50. bool LED_Toggle(gpio_num_t gpioNum)
  51. {
  52.     //
  53.     bool state = false;
  54.     gpio_num_t pin = (gpio_num_t)(gpioNum & 0x1F);
  55.  
  56.     state = GPIO_REG_READ(GPIO_ENABLE_REG);
  57.  
  58.     if(GPIO_REG_READ(GPIO_ENABLE_REG) & BIT(pin))
  59.     {
  60.         //pin is output - read the GPIO_OUT_REG register
  61.         state ^= (GPIO_REG_READ(GPIO_OUT_REG) >> pin) & 1U;
  62.         gpio_set_level(gpioNum, state);
  63.     }
  64.     else
  65.     {
  66.         //pin is input - read the GPIO_IN_REG register
  67.         state = (GPIO_REG_READ(GPIO_IN_REG) >> pin) & 1U;
  68.     }
  69.  
  70.     return state;
  71. }
  72.  
  73.  
  74. //********************************************************
  75. static void IRAM_ATTR GPIO_ISR_Handler(void* arg)
  76. {
  77.     uint32_t gpio_num = (uint32_t) arg;
  78.    
  79.     xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
  80. }
  81.  
  82. //**********************************************************************
  83. static void Interrupt_Task_Function(void* arg)
  84. {
  85.     uint32_t ioNum;
  86.  
  87.     printf("GPIO Interrupt Task Start... \r\n");
  88.  
  89.     while(1)
  90.     {
  91.         if(xQueueReceive(gpio_evt_queue, &ioNum, portMAX_DELAY))
  92.         {
  93.             if(ioNum == ZeroCross)
  94.             {
  95.                 printf("GPIO ZeroCross detected ... \r\n");
  96.                 // reset timer value   ????????????????????????
  97.  
  98.                 counter1 = counter1 + 100;
  99.                
  100.             }
  101.  
  102.         }
  103.     }
  104. }
  105.  
  106.  
  107. void app_main(void)
  108. {
  109.     //
  110.     const esp_timer_create_args_t oneshot_timer_args = {
  111.             .callback = &oneshot_timer_callback,
  112.             /* argument specified here will be passed to timer callback function */
  113.             // .arg = (void*) periodic_timer,
  114.             .name = "one-shot"
  115.     };
  116.  
  117.     ESP_ERROR_CHECK(esp_timer_create(&oneshot_timer_args, &oneshot_timer));
  118.  
  119.     /* Start the timers */
  120.     ESP_ERROR_CHECK(esp_timer_start_once(oneshot_timer, 1000000));
  121.     ESP_LOGI(TAG, "Started timers, time since boot: %lld us", esp_timer_get_time());
  122.  
  123.  
  124.     ESP_LOGI(TAG, "Entering light sleep for 0.5s, time since boot: %lld us",
  125.             esp_timer_get_time());
  126.  
  127.    
  128.     gpio_pad_select_gpio(Indicator_LED1);
  129.     gpio_set_direction(Indicator_LED1, GPIO_MODE_OUTPUT);
  130.     gpio_set_level(Indicator_LED1, 0);
  131.  
  132.     gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  133.  
  134.     if(xTaskCreate(Interrupt_Task_Function, "Interrupt_Task_Function", 2048, NULL, 3, NULL) == pdPASS)
  135.     {
  136.         printf("Interrupt_Task_Function Created Success! \r\n");
  137.     }
  138.  
  139.     // zero cross
  140.     gpio_set_direction(ZeroCross, GPIO_MODE_INPUT);
  141.     gpio_set_intr_type(ZeroCross, GPIO_INTR_POSEDGE);
  142.     gpio_set_pull_mode(ZeroCross, GPIO_PULLDOWN_ONLY);
  143.  
  144.     gpio_install_isr_service(0);
  145.     gpio_isr_handler_add(ZeroCross, GPIO_ISR_Handler, (void*) ZeroCross);
  146.  
  147.  
  148.     ESP_LOGI(TAG, "while run");
  149.     while(1)
  150.     {
  151.         //
  152.         printf("timer value : %d \r\n", counter1);
  153.         vTaskDelay(pdMS_TO_TICKS(500));
  154.         // LED_Toggle(Indicator_LED1);
  155.     }
  156.  
  157.     /* Clean up and finish the example */
  158.     ESP_ERROR_CHECK(esp_timer_stop(oneshot_timer));
  159.     // ESP_ERROR_CHECK(esp_timer_delete(periodic_timer));
  160.     ESP_ERROR_CHECK(esp_timer_delete(oneshot_timer));
  161.     ESP_LOGI(TAG, "Stopped and deleted timers");
  162.  
  163. }
  164.  
  165.  
  166. // static void periodic_timer_callback(void* arg)
  167. // {
  168. //     int64_t time_since_boot = esp_timer_get_time();
  169. //     ESP_LOGI(TAG, "Periodic timer called, time since boot: %lld us", time_since_boot);
  170. // }
  171.  
  172. static void oneshot_timer_callback(void* arg)
  173. {
  174.     int64_t time_since_boot = esp_timer_get_time();
  175.     ESP_LOGI(TAG, "One-shot timer called, time since boot: %lld us", time_since_boot);
  176.  
  177. }
  178.  
  179.  
  180.  

Re: Timer/Counter with ZeroCross for dimmer

Posted: Wed Feb 23, 2022 8:55 am
by ESP_Dazz
You don't need create your own timers to dim an LED. The ESP chips have dedicated hardware to dim LEDs. See the LED Control driver for more details.

Re: Timer/Counter with ZeroCross for dimmer

Posted: Wed Feb 23, 2022 11:42 am
by mm_1993
ESP_Dazz wrote:
Wed Feb 23, 2022 8:55 am
You don't need create your own timers to dim an LED. The ESP chips have dedicated hardware to dim LEDs. See the LED Control driver for more details.
thank you I will check it but I want to use Triac for dimm
and triac need a sync pulse signal with zerocross

so this is my idea that when any zerocross pulse come and gprio interrupt occur reset my timer and set it to zero
then for example it count up for 5 millisecond and timer interrupt occur and turn on led or lamp or ...