Disable Timer within Timer ISR

blengerich
Posts: 2
Joined: Sat Feb 25, 2023 9:12 pm

Disable Timer within Timer ISR

Postby blengerich » Sat Feb 25, 2023 10:06 pm

Is it possible to disable a timer within the timer ISR itself? I want to disable the timer interrupt when a certain condition is met, which can be determined within the timer ISR itself. I wrote an example program which works for Arduino Uno and Mega, but not for ESP32. In the code below, the timer interrupt continues to occur, even though I disable it. To get it to work for the ESP32, I set a flag in the interrupt routine when I want to disable The main code checks this flag, and disables the timer if the flag is set. Is there a more efficient way of doing this?

Code: Untitled.cpp Select all

hw_timer_t * timer = NULL;
int counter = 10;

void setup() {
Serial.begin(115200);

timer = timerBegin(2, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 500000, true); // Interrupt occurs every 500ms
timerAlarmEnable(timer);
}

void loop() {
Serial.println(counter);
delay(100);
}

void onTimer() {
if (counter == 0) {
timerAlarmDisable(timer);
} else {
counter = counter - 1;
}
}

Who is online

Users browsing this forum: No registered users and 3 guests