Page 1 of 1

ESP32 Timer Problem

Posted: Fri Apr 01, 2022 7:34 pm
by Jachin
I added four timers to realize the timing of 1, 2, 3, and 4 seconds respectively, but it is obvious that Timer1 does not work. I want to know why.

Code: Untitled.txt Select all


#include "esp_system.h"

const int wdtTimeout = 1000;
hw_timer_t *timer0 = NULL;
hw_timer_t *timer1 = NULL;
hw_timer_t *timer2 = NULL;
hw_timer_t *timer3 = NULL;

void ARDUINO_ISR_ATTR resetModule0() {
Serial.println("1");
timerDetachInterrupt(timer0);
timer0 = NULL;
}
void ARDUINO_ISR_ATTR resetModule1() {
Serial.println("2");
timerDetachInterrupt(timer1);
timer1 = NULL;
}
void ARDUINO_ISR_ATTR resetModule2() {
Serial.println("3");
timerDetachInterrupt(timer2);
timer2 = NULL;
}

void ARDUINO_ISR_ATTR resetModule3() {
Serial.println("4");
timerDetachInterrupt(timer3);
timer3 = NULL;

}

void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("running setup");
timer0 = timerBegin(0, 80, true); //timer 0, div 80
timerAttachInterrupt(timer0, &resetModule0, true); //attach callback
timerAlarmWrite(timer0, wdtTimeout * 1000, false); //set time in us
timer1 = timerBegin(1, 80, true); //timer 0, div 80
timerAttachInterrupt(timer1, &resetModule1, true); //attach callback
timerAlarmWrite(timer1, wdtTimeout * 2000, false); //set time in us
timer2 = timerBegin(2, 80, true); //timer 0, div 80
timerAttachInterrupt(timer2, &resetModule2, true); //attach callback
timerAlarmWrite(timer2, wdtTimeout * 3000, false); //set time in us
timer3 = timerBegin(3, 80, true); //timer 0, div 80
timerAttachInterrupt(timer3, &resetModule3, true); //attach callback
timerAlarmWrite(timer3, wdtTimeout * 4000, false); //set time in us
timerAlarmEnable(timer0);
timerAlarmEnable(timer1);
timerAlarmEnable(timer2);
timerAlarmEnable(timer3);
}

void loop() {
Serial.print("#");
delay(100);
}

Code: Untitled.txt Select all


[2022-04-02 03:32:33.631]
running setup[2022-04-02 03:32:33.631]
##########1[2022-04-02 03:32:34.620]
####################3[2022-04-02 03:32:36.631]
##########4[2022-04-02 03:32:37.621]

Re: ESP32 Timer Problem

Posted: Mon Apr 04, 2022 7:16 am
by rpiloverbd
Hi, you may get some ideas from this video. It's regarding configuration of multiple timers together. https://www.youtube.com/watch?v=LONGI_JcwEQ