Timer interrupt using ESP32 IDF

507 Views Asked by At

I'm a beginner learning ESP32 using IDF and having a task to use interrupt timer. The task is to generate a signal using GPIO pin. The signal should be on for 20ms and off for 6ms. Here is the code that I've tried. I don't know where I'm going wrong and please guide me if my understanding is wrong.

#include <stdio.h>
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/gpio.h"

void timer_callback(void *param)
{
  static bool on;
  on = !on;
  
  gpio_set_level(GPIO_NUM_2, on);
}

void app_main(void)
{
  gpio_pad_select_gpio(GPIO_NUM_2);
  gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);

  const esp_timer_create_args_t my_timer_args = {
      .callback = &timer_callback,
      .name = "a timer"};
  esp_timer_handle_t timer_handler;
  ESP_ERROR_CHECK(esp_timer_create(&my_timer_args, &timer_handler));
  ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handler, 20000));
  ESP_ERROR_CHECK(esp_timer_stop_periodic(timer_handler, 6000));

  while (true)
  {
    //other stuff here
    esp_timer_dump(stdout);
    vTaskDelay(pdMS_TO_TICKS(1000));
  }
}


 
0

There are 0 best solutions below