Watchdog time accuracy

Hi

I’ve discovered that watchdog timer is not 1,2,4,8 sec as it should be.

For 1sec configuration the real time between WDR interrupts is 1.156
For 2sec is 2.312
For 8sec is 9.251

Is it a normal behaviour (9.2sec instead of 8sec) or something’s wrong in my scetch?

#include "Arduino.h"
#include <avr/wdt.h>

volatile uint8_t wdt_h = false; //WDT interrupt flag

void setup() {

 noInterrupts();
 uint8_t MCUSR_c=MCUSR;//MCUSR mirroring
 MCUSR=0; //reset wdt status register
 WDTCSR |= (1<<WDE) | (1<<WDCE); //0b00011000; configure mode
 WDTCSR = (1<<WDIE) | (1<<WDP3) | (1<<WDP0);//8 sec(WDP0, WDP3), interrupts, disable WDE (reset on timeout)
 //WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);//2 sec(WDP2, WDP1, WDP0), interrupts, disable WDE (reset on timeout)
 //WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP1);//1 sec(WDP2, WDP1), interrupts, disable WDE (reset on timeout)

 interrupts();
 Serial.begin(115200);
 Serial.println("start");
 Serial.println(MCUSR_c);
 Serial.println(millis());

}

ISR(WDT_vect) {
 wdt_h = true; //wdt interrupt happens
}


void loop() {
 if(wdt_h) { //if wdt interrupt happens 
 wdt_h=false;
 unsigned long m = millis();
 Serial.println("wdt");
 Serial.println(m);
 } else {
 }
}

Output in serial monitor:

start
0
0
wdt
9251
wdt
18503
wdt
27755
...

I’m using Arduino UNO (328p)

the watchdog timer is explicitly “low accuracy” and also varies significantly with Vcc and Temperature.
Section 13.7 if the datasheet says that it is nominally 128kHz at 3V and 25C, and section 33.1.11 shows it goes down with increasing Vcc (about 114.5kHz at 5V and 25C) - those values are more-or-less consistent with the times you are seeing.

westfw

Thanks a lot for the quick and very informative reply. Now it's clear for me.