๐ฐ 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:
/* Hardware Timer Interrupt ESP32 (No RTOS) */ bool timer_flag = false; hw_timer_t *timer = NULL; #define GPIO_PIN 48 void setup() { Serial.begin(115200); pinMode(GPIO_PIN, OUTPUT); Timer_Init(); Serial.println("Start"); } void loop() { if(timer_flag == true) { static uint8_t toggle = 0; if(toggle) { digitalWrite(GPIO_PIN, HIGH); }else { digitalWrite(GPIO_PIN, LOW); } toggle = ~toggle; timer_flag = false; } /*---- loop end ----*/ } /* Timer Interrupt Handler */ void ARDUINO_ISR_ATTR onTimer() { if(timer_flag == false) { timer_flag = true; } } void Timer_Init(void) { // Set timer frequency timer = timerBegin(1000000); // Attach onTimer function to our timer. timerAttachInterrupt(timer, &onTimer); // Set alarm to call onTimer function every second (value in microseconds). // Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter). timerAlarm(timer, 200, true, 0); }
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!
Test
Connection
Since I use GPIO48 of the ESP32 I connected probes to the ground (GND) and pin 48:
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:
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:
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.
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):
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
- Comments