Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Timer interrupt using ESP32 IDF

Status
Not open for further replies.

balajibaski

Newbie
Joined
Jan 23, 2023
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
8
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.

C:
#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)
{
esp_timer_dump(stdout);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
 

Hi,

Please give an error description.
What is happening? Compiler error? GPIO HIGH/LOW/HIGH-Z? Explodes?

How do you test it?
Is the code properly uploaded?

*******
To generate a 20ms/6ms signal I'd use the PWM module. --> most accurate, lowest jitter, consumes no processing power, no interrupt, no ISR being executed,
(Depends on microcontroller)

Klaus
 

Hi,

Please give an error description.
What is happening? Compiler error? GPIO HIGH/LOW/HIGH-Z? Explodes?

How do you test it?
Is the code properly uploaded?

*******
To generate a 20ms/6ms signal I'd use the PWM module. --> most accurate, lowest jitter, consumes no processing power, no interrupt, no ISR being executed,
(Depends on microcontroller)

Klaus
Hello,

I'm not getting any error; I think there's a logical mistake, not able to get the proper output. Can you please share your code with me (both the codes with PWM and with interrupt). Thanks

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 where to add the 6ms off timing and don't know where I'm going wrong and please guide me if my understanding is wrong.

C:
#include <stdio.h>
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include <stdbool.h>
#include "esp_err.h"
#include "sdkconfig.h"
void timer_callback(void *param)
{
  gpio_set_level(GPIO_NUM_2, 1);
}
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(timer_handler));
  //ESP_ERROR_CHECK(esp_timer_start_once(timer_handler, 94));
  while (true)
  {
    esp_timer_dump(stdout);
    vTaskDelay(pdMS_TO_TICKS(1000));
  }
}
 

You are still not being clear about what you are actually getting that you consider an error.
Are you getting ANYTHING out of the GPIO pin?
Have you tried a simpler program that toggles the GPIO pin at any speed (if it is very fast then you will need a scope to show it changing)?
I'm also a bit suspicious of your call to 'esp_timer_stop_periodic()' - does this function exist in the official API. Looking at the Espressif web site I can see the 'esp_timer_start_periodic()' function which calls the callback at regular intervals (being however many microseconds you pass as the argument). Also the only other response to a Google search for that function shows a 'Stack Overflow' entry with the same code in it - presumably an entry you have made.
I think you have the wrong idea of how to go about solving your problem. I get the feeling that this might be an exercise from a class you are taking - is that right? If so what else has the teacher mentioned in class that might be relevant. (Hint: the different mark to space times means you need to think more carefully about using a timer - have you created code that uses a timer to toggle the LED on and off with equal on and off times as that would probably be the first step.)
If you can't use the PWM as @KlausST suggests and must use a timer, then think how to use a single timer and a couple of counters and a flag variable, to achieve the outcome - another hint is that the timer must be called at least at the 'highest common factor' of both the on and off periods.
(I'm definitely NOT going to give you the code as you will not learn from that as much as sorting this out for yourself.)
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top