Disable Timer within Timer ISR
Posted: Mon Feb 27, 2023 4:57 pm
Is it possible to disable a timer within the timer ISR itself? The example code below works for Arduino Uno and Mega, but not for ESP32. The timer interrupt continues to occur, even after disabling it. The disable command doesn't seem to register within the ISR. To get this to work for ESP32, I set a flag in the ISR, and then check that flag in the main code, and disable the timer when I see that the flag is set. I would like to disable the timer, without having to continuously checking the status of a flag.
This code works...
- 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;
- }
- }
- hw_timer_t * timer = NULL;
- int counter = 10;
- boolean timer_off_flag = false;
- 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);
- if (timer_off_flag == true) {
- timerAlarmDisable(timer);
- timer_off_flag = false;
- }
- delay(100);
- }
- void onTimer() {
- if (counter == 0) {
- timer_off_flag = true;
- } else {
- counter = counter - 1;
- }
- }