ESP32 Timers & Timer Interrupt

ESP32 Timers & Timer Interrupt

In this article we will study how to use ESP32 internal Timers & generate Timer Interrupt events in Arduino IDE.We’ll discuss how ESP32 Timers work, how to configure ESP32’s Timers, and how to generate periodic interrupts to synchronize the execution of logic within our project.

ESP32 Timers:-

The ESP32 SoCs come with 4 hardware timers, each of which is a general-purpose 64-bit up/down counter with a 16-bit prescaler. Except for ESP32-C3 which has only 2 timers each of which is 54 bits instead. The ESP32 timers have the capability of auto-reloading at the end of the counting period as well.

ESP32 Timers Functional Description:-

Each ESP32 timer uses the APB clock (APB_CLK which is normally 80 MHz in frequency) as a base clock. This clock is then scaled down by a 16-bit prescaler which generates the time-base tick time. Therefore, we’ll be changing the value of the prescaler in order to control the timer tick time.

The 16-Bit prescaler can divide the APB_CLK by a factor from 2 to 65536. When you set the prescaler value to be either 1 or 2, the clock divisor is 2; when you set the prescaler to 0, the clock divisor is 65536. Any other value will cause the clock to be divided by exactly that value that you’ve written to the prescaler register.

ESP32 Timers Alarm Generation:-

ESP32 timers can trigger an alarm (Event) which will cause a timer to reload and/or interrupt to occur, depending on your configuration. The alarm event is triggered when the value you’ve stored in the alarm register matches the current timer value. This is very useful to set periodic interrupts to execute some pieces of logic periodically in your project as we’ll be doing hereafter.

ESP32 Timers Equation:-

In order to generate periodic events with ESP32, we’ll be using the alarm event generation as well as the timer’s prescaler in order to achieve the desired interrupt periodicity. There are 3 special cases for the Timer equation down below, which are when the prescaler value is = 0,1, and 2. When Prescaler=1or2, it’s going to be as follows [ TOUT = TimerTicks x (2/APB_CLK) ]. When Prescaler=0, it’s going to be as follows [ TOUT = TimerTicks x (65536/APB_CLK) ]. Otherwise, we can generally use the equation down below.

Tout = TimerTicks * (Prescaler/APB_CLK)

Some Arduino-ESP32 Timer APIs for our application:-

timerBegin

This function is used to configure the timer. After successful setup the timer will automatically start.

hw_timer_t * timerBegin(uint32_t frequency);
        

  • frequency select timer frequency in Hz. Sets how quickly the timer counter is “ticking”.

This function will return timer structure if configuration is successful. If NULL is returned, error occurs and the timer was not configured.

timerAttachInterrupt

This function is used to attach interrupt to timer.

void timerAttachInterrupt(hw_timer_t * timer, void (*userFunc)(void));
        

  • timer timer struct.
  • userFunc funtion to be called when interrupt is triggered.

timerAlarm

This function is used to configure alarm value and autoreload of the timer. Alarm is automaticaly enabled.

void timerAlarm(hw_timer_t * timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count);
        

  • timer timer struct.
  • alarm_value alarm value to generate event.
  • autoreload enabled/disabled autorealod.
  • reload_count number of autoreloads (0 = unlimited). Has no effect if autorealod is disabled.


ESP32 Timer Example in C code:-

#define LED_BUILTIN 2

 

hw_timer_t *Timer0_Cfg = NULL;

 

void IRAM_ATTR Timer0_ISR()

{

    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));

}

void setup()

{

    pinMode(LED_BUILTIN, OUTPUT);
    //// Set timer frequency to 1Mhz
    Timer0_Cfg = timerBegin(1000000);
    // Attach onTimer function to our timer.
    timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR);

    // Set alarm to call Timer0_ISR function every 100 ms (value in microseconds).
  // Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).

    timerAlarm(Timer0_Cfg, 100000, true, 0);

}

void loop()

{

    // Do Nothing!

}
        


That's it we will compile and download the executable to see the result on esp32 development board.

Note:- The board is available in amazon.in & the price is nearly about 500 to 600.So buy for learning purpose or any business purpose your wish.


Development Environment- Arduino IDE

Development kit- ESP32 wroom devkit version 1

Programming language - C


Thanks for reading this article & you are welcome.

要查看或添加评论,请登录

Satyabrata Senapati的更多文章

  • How I got a core embedded job by securing only 44.66% in 10th

    How I got a core embedded job by securing only 44.66% in 10th

    In this article we will discuss about my educational qualifications for getting a core job in embedded. Actually I was…

  • Salary of VLSI Engineer

    Salary of VLSI Engineer

    In this article we will discuss salary of a VLSI Engineer. The landscape of VLSI jobs offers positions that command…

  • OSEK(Open Systems and their Interfaces for the Electronics in Motor Vehicles)

    OSEK(Open Systems and their Interfaces for the Electronics in Motor Vehicles)

    OSEK (Offene Systeme und deren Schnittstellen für die Elektronik in Kraftfahrzeugen; English: "Open Systems and their…

  • AUTOSAR

    AUTOSAR

    This article will focus on AUTOSAR. AUTOSAR (AUTomotive Open System ARchitecture) is a global development partnership…

  • Embedded AI

    Embedded AI

    In this article we will discuss about Embedded AI. Embedded artificial intelligence (AI) seamlessly integrates AI into…

  • Real Time System

    Real Time System

    In this article we will discuss about real time systems. Hard and Soft Real-Time Operating System A real-time operating…

  • Core jobs after btech electronics & telecommunication engineering

    Core jobs after btech electronics & telecommunication engineering

    In this article we will discuss opportunities after completing b.tech in electronics & telecommunication engineering.

  • Instrument cluster

    Instrument cluster

    In this article we will discuss about different types of instrument clusters. Overview Automotive instrument clusters…

  • Embedded Linux

    Embedded Linux

    Computer operating systems based on the Linux kernel are used in embedded systems such as consumer electronics (eg…

  • Solver in SIMULINK

    Solver in SIMULINK

    This article describes various types of solvers used in simulation. The Solver parameter specifies the solver that…