Watchdog Timer
Watchdog Timer
enabled it ensures that a certain number of instructions execute within a pre-defined time frame. This
time frame or the time delay can be configured/set using the registers of the watchdog timer. In case
the instructions execute within the time frame, watchdog timer needs to be turned off and the program
execution continues. However, if the instructions fail to execute within this time frame (this conditions
is called time out condition) the entire system reboots thus avoiding any system crash or hang up.
Watchdog Timer in ATmega16:
The Watchdog timer of Atmega16 can be configured by using WDTCR register of AVR
microcontroller. When the time out condition is set, the watchdog timer starts counting clock cycles.
The watchdogs timer is clocked from separate on-chip watchdog oscillator of 1MHz frequency. The
time out condition is set by configuring prescaler bits of WDTCR register.
WDTCR (Watch Dog Timer Control Register):
WDTOE
Bit 7
Bit 6
Bit 5
Bit 4
WDE
Bit 3
WDP2
Bit 2
WDP1
Bit 1
WDP0
Bit 0
WDTOE (Watch Dog Turn-Off Enable) The watchdog timer is disabled by configuring WDTOE and
WDE bits (explained later in the article).
WDE (Watch Dog Enable) Watchdog timer is enabled by writing 1 to WDE bit.
WDP [2:0] (Watch Dog Prescaler) bits - These three bits determine the watchdog time out condition.
The table below shows prescaler bits combination and their corresponding time out period:
WDP2
WDP1
WDP0
0
0
0
0
1
1
1
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
Number of WDT
oscillator cycles
16K (16,384)
32K (32,768)
64K (65,536)
128K (131,072)
256K (262,144)
512K (524,288)
1024K (1,048,576)
2048K (2,097,152)
Typical Time-out
at Vcc=3.0V
17.1 ms
34.3 ms
68.5 ms
0.14 s
0.27 s
0.55 s
1.1 s
2.2 s
Typical Time-out
at Vcc=5.0V
16.3 ms
32.5 ms
65 ms
0.13 s
0.26 s
0.52 s
1.0 s
2.1 s
The connection for above mentioned objective is shown in circuit diagram. An LED is connected at pin
PB0 which blinks continuously. Another LED is connected to PB1 pin. This LED will glow when
watchdog time out condition occurs.
Programming steps:
1. Check the status of WDRF bit. If this bit is high, set the PB1 pin of the controller.
2. Set the WDE bit in WDTCR register to activate the watchdog timer.
3. Set the prescaler bits WDP [2:0] = 111 for the time-out condition of 2 sec.
4. Write an infinite loop to toggle the PB0 bits toggles with a certain delay.
5. On time-out condition, watchdog timer resets the controller and this event reinitialize the program
counter to go back at step 1
{
DDRB=0x03;
if(bit_is_set(MCUCSR,WDRF))
{
PORTB|=(1<<PB1);
_delay_ms(1000);
}
PORTB&=~(1<<PB1);
WDTCR=0x0F;
while(1)
{
PORTB|=(1<<PB0);
_delay_ms(400);
PORTB&=~(1<<PB0);
_delay_ms(400);
}
}