0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Configure Watchdog Timer in Embedded C: Simple Guide

To configure a watchdog timer in embedded C, you typically enable the watchdog, set its timeout period, and start it by writing to specific hardware registers. Then, you regularly reset the watchdog timer in your code to prevent system reset. This setup depends on your microcontroller's datasheet and watchdog registers.
📐

Syntax

The basic steps to configure a watchdog timer in embedded C involve:

  • Enable the watchdog timer: Set the control register bit to turn on the watchdog.
  • Set the timeout period: Configure the timer period by writing to the watchdog timeout register.
  • Start the watchdog: Begin the countdown by enabling the timer.
  • Reset the watchdog regularly: Clear the watchdog counter in your main loop or interrupt to avoid reset.

These steps use specific registers defined by your microcontroller, for example WDT_CTRL, WDT_TIMEOUT, and WDT_RESET.

c
/* Example syntax for watchdog timer configuration */
WDT_CTRL |= ENABLE_WDT;       // Enable watchdog timer
WDT_TIMEOUT = TIMEOUT_VALUE;   // Set timeout period
WDT_CTRL |= START_WDT;        // Start watchdog timer

// In main loop or timer interrupt
WDT_RESET = RESET_VALUE;       // Reset watchdog timer counter
💻

Example

This example shows how to configure and use a watchdog timer on a generic microcontroller. It enables the watchdog with a 2-second timeout and resets it inside the main loop to prevent system reset.

c
#include <stdint.h>
#include <stdbool.h>

// Mock hardware registers
volatile uint8_t WDT_CTRL = 0;
volatile uint8_t WDT_TIMEOUT = 0;
volatile uint8_t WDT_RESET = 0;

#define ENABLE_WDT 0x01
#define START_WDT  0x02
#define RESET_VALUE 0xAA
#define TIMEOUT_VALUE 200  // Example timeout value

void watchdog_init(void) {
    WDT_CTRL |= ENABLE_WDT;       // Enable watchdog
    WDT_TIMEOUT = TIMEOUT_VALUE;  // Set timeout
    WDT_CTRL |= START_WDT;        // Start watchdog
}

void watchdog_reset(void) {
    WDT_RESET = RESET_VALUE;      // Reset watchdog timer
}

int main(void) {
    watchdog_init();

    while (true) {
        // Application code here

        watchdog_reset();  // Reset watchdog regularly
    }

    return 0;
}
⚠️

Common Pitfalls

Common mistakes when configuring watchdog timers include:

  • Not enabling the watchdog before setting timeout, causing no effect.
  • Forgetting to reset the watchdog regularly, which causes unwanted system resets.
  • Setting an incorrect timeout value that is too short or too long for your application.
  • Not consulting the microcontroller datasheet for correct register names and bits.

Always check your hardware manual for exact register details.

c
/* Wrong way: Forgetting to reset watchdog */
WDT_CTRL |= ENABLE_WDT;
WDT_TIMEOUT = TIMEOUT_VALUE;
WDT_CTRL |= START_WDT;

// No watchdog_reset() call here causes reset

/* Right way: Reset watchdog regularly */
while (true) {
    // Your code
    watchdog_reset();
}
📊

Quick Reference

StepActionDescription
1Enable WatchdogSet control register bit to turn on watchdog timer
2Set TimeoutWrite timeout value to watchdog timeout register
3Start WatchdogEnable watchdog timer counting
4Reset WatchdogRegularly clear watchdog counter to prevent reset

Key Takeaways

Enable the watchdog timer before setting its timeout period.
Set an appropriate timeout value based on your application's needs.
Regularly reset the watchdog timer in your main loop or interrupt.
Consult your microcontroller datasheet for exact register names and bits.
Forgetting to reset the watchdog causes unexpected system resets.