navigationGo.pngQuick Navigation
allprojects32.pngAll projects
hardware32.pngHardware
links32.pngLinks

favoriteStar32.pngTop projects
Alan numitron clock
Clapclap 2313/1386
SNES Pi Webserver
USB Volume/USB toys
Smokey amp
Laser cutter
WordClock
ardReveil v3
SNES Arcade cabinet
Game boy projects
cameleon
Home Presence Detector

github32.pngGitHub
AlanFromJapan

navigationMail.pngContact me

alanfjmail.png
3flags.pngWho's Alan?


Akizukidenshi
Elec-lab
Rand Nerd Tut
EEVblog
SpritesMods
AvrFreaks
Gameboy Dev
FLOZz' blog
Switch-science
Sparkfun
Suzusho
Datasheet Lib
Reddit Elec
Ermicro
Carnet du maker (fr)

Watchdog as interrupt

Last update: Thu Jun 5 22:25:41 2025
kalshagar - Watchdog as interrupt

Watchdog trigger every second sample

This example is working as-is for the Atmega328 or Atmega88 series...
README : must set the fuse WDTON to 0 (inverted logic) to enable the watchdog.
 
#include <avr/interrupt.h>
#include <avr/wdt.h>
 
ISR(WDT_vect){
   //interrupt code : do the job here
}
 
//setup the watchdog to timeout every second and make an interrupt (not a reset!)
void setupWatchdog(){
    //README : must set the fuse WDTON to 0 to enable the watchdog
 
    //disable interrupts
    cli();
 
    //make sure watchdod will be followed by a reset (must set this one to 0 because it resets the WDE bit)
    MCUSR &= ~(1 << WDRF);
    //set up WDT interrupt (from that point one have 4 cycle to modify WDTCSR)
    WDTCSR = (1<<WDCE)|(1<<WDE);
    //Start watchdog timer with 1s prescaller and interrupt only
    WDTCSR = (1<<WDIE)|(0<<WDE)|(1<<WDP2)|(1<<WDP1);
    //Enable global interrupts
    sei();
}
 
void main() {
   setupWatchdog();
 
   //...
}
All content on this site is shared under the MIT licence (do what u want, don't sue me, hat tip appreciated)
electrogeek.tokyo ~ Formerly known as Kalshagar.wikispaces.com and electrogeek.cc (AlanFromJapan [2009 - 2025])