ESP32 Timer Problem

Jachin
Posts: 11
Joined: Fri Mar 18, 2022 9:06 pm

ESP32 Timer Problem

Postby Jachin » Fri Apr 01, 2022 7:34 pm

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.
  1. #include "esp_system.h"
  2.  
  3. const int wdtTimeout = 1000;
  4. hw_timer_t *timer0 = NULL;
  5. hw_timer_t *timer1 = NULL;
  6. hw_timer_t *timer2 = NULL;
  7. hw_timer_t *timer3 = NULL;
  8.  
  9. void ARDUINO_ISR_ATTR resetModule0() {
  10.     Serial.println("1");
  11.     timerDetachInterrupt(timer0);
  12.     timer0 = NULL;    
  13. }
  14. void ARDUINO_ISR_ATTR resetModule1() {
  15.     Serial.println("2");
  16.     timerDetachInterrupt(timer1);
  17.     timer1 = NULL;    
  18. }
  19. void ARDUINO_ISR_ATTR resetModule2() {
  20.     Serial.println("3");
  21.     timerDetachInterrupt(timer2);
  22.     timer2 = NULL;
  23. }
  24.  
  25. void ARDUINO_ISR_ATTR resetModule3() {
  26.     Serial.println("4");
  27.     timerDetachInterrupt(timer3);
  28.     timer3 = NULL;
  29.      
  30. }
  31.  
  32. void setup() {
  33.   Serial.begin(115200);
  34.   Serial.println();
  35.   Serial.println("running setup");
  36.   timer0 = timerBegin(0, 80, true);                  //timer 0, div 80
  37.   timerAttachInterrupt(timer0, &resetModule0, true);  //attach callback
  38.   timerAlarmWrite(timer0, wdtTimeout * 1000, false); //set time in us
  39.   timer1 = timerBegin(1, 80, true);                  //timer 0, div 80
  40.   timerAttachInterrupt(timer1, &resetModule1, true);  //attach callback
  41.   timerAlarmWrite(timer1, wdtTimeout * 2000, false); //set time in us
  42.   timer2 = timerBegin(2, 80, true);                  //timer 0, div 80
  43.   timerAttachInterrupt(timer2, &resetModule2, true);  //attach callback
  44.   timerAlarmWrite(timer2, wdtTimeout * 3000, false); //set time in us
  45.   timer3 = timerBegin(3, 80, true);                  //timer 0, div 80
  46.   timerAttachInterrupt(timer3, &resetModule3, true);  //attach callback
  47.   timerAlarmWrite(timer3, wdtTimeout * 4000, false); //set time in us
  48.   timerAlarmEnable(timer0);
  49.   timerAlarmEnable(timer1);    
  50.   timerAlarmEnable(timer2);
  51.   timerAlarmEnable(timer3);
  52. }
  53.  
  54. void loop() {
  55.   Serial.print("#");  
  56.   delay(100);
  57. }
  1. [2022-04-02 03:32:33.631]
  2. running setup[2022-04-02 03:32:33.631]
  3. ##########1[2022-04-02 03:32:34.620]
  4. ####################3[2022-04-02 03:32:36.631]
  5. ##########4[2022-04-02 03:32:37.621]

rpiloverbd
Posts: 101
Joined: Tue Mar 22, 2022 5:23 am

Re: ESP32 Timer Problem

Postby rpiloverbd » Mon Apr 04, 2022 7:16 am

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

Who is online

Users browsing this forum: No registered users and 59 guests