๐Ÿ•ฐ Simple Timer Interrupt ESP32-S3 Arduino Work Example

Introduction

Let's see how to use hardware timer interrupt (RTOS not used) from the timer with real tests on the devboard and measurements using logic analyzer to confirm correctness of the code.

Frequency

If timer's frequency TIM_FREQ=1000000 Hz (1MHz), so one tick = 1 us

If we set timer alarm (interrupt) for TIM_PER=200, that means period is: 200 * 1us=200 us

So, the frequency of the timer will be 1/200 us = 5000 Hz = 5 kHz. The final equation is here (obviously ESP32 has limitations, so you can't use any numbers):

fTIM =ย TIM_PER/(TIM_FREQ) = 1000000/200 = 5000 Hz = 5 kHz

*The period of the generated signal will be twice bigger (2*200 us=400 us) than actual timer interrupt frequency because we make pin toggle each time when we enter the handler.

Code

You can choose any GPIO pin that you need, in my case it's GPIO48.

You can find it on GitHub: https://github.com/Egoruch/ESP32-TIM-INT-ARDUINO

Or here:

  1. /* Hardware Timer Interrupt ESP32 (No RTOS) */
  2.  
  3. bool timer_flag = false;
  4.  
  5. hw_timer_t *timer = NULL;
  6.  
  7. #define GPIO_PIN 48
  8.  
  9. void setup() {
  10.  
  11. Serial.begin(115200);
  12.  
  13. pinMode(GPIO_PIN, OUTPUT);
  14.  
  15. Timer_Init();
  16.  
  17. Serial.println("Start");
  18. }
  19.  
  20. void loop() {
  21.  
  22. if(timer_flag == true)
  23. {
  24. static uint8_t toggle = 0;
  25.  
  26. if(toggle)
  27. {
  28. digitalWrite(GPIO_PIN, HIGH);
  29.  
  30. }else
  31. {
  32. digitalWrite(GPIO_PIN, LOW);
  33.  
  34. }
  35.  
  36. toggle = ~toggle;
  37.  
  38. timer_flag = false;
  39. }
  40.  
  41. /*---- loop end ----*/
  42. }
  43.  
  44. /* Timer Interrupt Handler */
  45. void ARDUINO_ISR_ATTR onTimer() {
  46.  
  47. if(timer_flag == false)
  48. {
  49. timer_flag = true;
  50. }
  51.  
  52. }
  53.  
  54. void Timer_Init(void)
  55. {
  56. // Set timer frequency
  57. timer = timerBegin(1000000);
  58.  
  59. // Attach onTimer function to our timer.
  60. timerAttachInterrupt(timer, &onTimer);
  61.  
  62. // Set alarm to call onTimer function every second (value in microseconds).
  63. // Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
  64. timerAlarm(timer, 200, true, 0);
  65.  
  66. }

Arduino Configurations

Double check your flash size, for me it's 16MB, but your chip can have another flash volume!

Enable USB CDC on Boot if you're use USB for flashing!

ImageImage

Test

Connection

Since I use GPIO48 of the ESP32 I connected probes to the ground (GND) and pin 48:

Image

Frequency Meter

Multimeter with frequency counter could be very useful tool, because it can easily read rectangular (PWM) signals and correctly show not only frequency, but also duty cycle:

ImageImage

Logic Analyzer

Even most cheap logic analyzer is super powerful instrument, because of excellent software, which give a chance to make long and detail recordings of digital signals with several channels:

Image

Android Oscilloscope

This is the ESP32-STM32 oscilloscope, that works with Android devices uses proprietary application, which makes possible to export high-resolution pics, which super cool for technical articles.

Image
Image

Portable Oscilloscope

This is portable oscilloscope multimeter, which very handy for fast measurements, but resolution of the screen is not good for today (230x320 px):

ImageImage

Conclusions

  • It's pretty easy to make hardware timer interrupt and use it for pin toggle
  • In example the frequency of the output signal is twice lower because in the interrupt handler state of this pin toggling
214
No comments yet. Be the first to add a comment!
Cookies?