Dear Everyone! Here I am try to summarize my study regarding "Using Watchdog features on Arduino (Nano) etc where Bootloader not present"
Please note that bootloaders have some initializing codes which missing in avr/wdt.h
The watchdog timer is managed by the WDTCSR register.
Two FUSE BITS are important WDTON and BOOTRST.
WDTON set (0) will generate system reset when Dog Bite else depends on WDTCSR register.
BOOTRST should not be programmed if no bootloader present, to get start from address zero.
// Definition of interrupt names
#include <avr/io.h>
// ISR interrupt service routine
#include <avr/interrupt.h>
#define wdt_reset() __asm__ __volatile__ ("wdr")
const int WD_LED = 13; // LED at Pin13
int counts;
// Jump to address zero
// void(* resetFunc) (void) = 0;//declare soft reset function at address 0
//Watchdog Interrupt
ISR(WDT_vect)
{
flash();
}
void setup()
{
Serial.begin(9600);
pinMode(WD_LED, OUTPUT);
digitalWrite(WD_LED, LOW);
cli();
wdt_reset();
// Enable Watchdog Configuration mode for 4 clock cycles only:
WDTCSR |= (1<<WDCE) | (1<<WDE); // WDTCSR |= B00011000;
// Configur Watchdog
// WDIE = 1: WDT Interrupt Enable
// WDE = 1 :System Reset Enable
WDTCSR = (1<<WDIE) | (1<<WDE) | (0<<WDP3) | (1<<WDP2) | (1<<WDP1) | (0<<WDP0); // 1-Sec WDT with Interrupt and System Reset
sei();
Serial.print("WDTCSR: ");
Serial.println(WDTCSR, HEX);
Serial.println("Setup finished.");
}
void loop()
{
Serial.println(counts++); // do anything
delay(1500); // change argument to 1500 -> watchdog will be active DOG-BITE
wdt_reset();
}
void flash()
{
static boolean output = HIGH;
digitalWrite(WD_LED, output);
output = !output;
// resetFunc(); //Jump to address / soft reset
}