Advertisement
mikroavr

watchdog_timer_esp32

Jun 19th, 2023
1,448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define WATCHDOG_TIMEOUT_S 3
  2.  
  3. hw_timer_t * watchDogTimer = NULL;
  4.  
  5. void IRAM_ATTR watchDogInterrupt() {
  6.   Serial.println("reboot");
  7.   ESP.restart();
  8. }
  9.  
  10.  
  11. void watchDogRefresh()
  12. {
  13.   timerWrite(watchDogTimer, 0);                    //reset timer (feed watchdog)
  14. }
  15.  
  16. void setup() {
  17.   // put your setup code here, to run once:
  18.   Serial.begin(115200);
  19.   Serial.println("Watchdog sketch start");
  20.  
  21.   watchDogTimer = timerBegin(2, 80, true);
  22.   timerAttachInterrupt(watchDogTimer, &watchDogInterrupt, true);
  23.   timerAlarmWrite(watchDogTimer, WATCHDOG_TIMEOUT_S * 1000000, false);
  24.   timerAlarmEnable(watchDogTimer);
  25.  
  26. }
  27.  
  28. void loop() {
  29.   // put your main code here, to run repeatedly:
  30.   for (int i = 0; i < 10; i++) {
  31.     Serial.print("test: ");
  32.     Serial.println(i);
  33.     watchDogRefresh();
  34.     if (i == 8) {
  35.       delay(4000); // terdelay karena fungsi watchDogRefresh() tidak di peroleh selama 3 detik
  36.     }
  37.     delay(1000);
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement