Watchdog Timer and Sleep Mode of Microcontroller

In this article, we will explore the concept of a watchdog timer and its application in the context of PIC microcontrollers. A watchdog timer is an essential component that ensures the stability and reliability of a microcontroller system. By continually monitoring the system’s operation and taking necessary actions, such as resetting the microcontroller when needed, the watchdog timer plays a crucial role in preventing system failures or undesirable behavior. We will delve into the functionality, control registers, and operation of watchdog timers, as well as discuss the sleep mode of PIC microcontrollers in conjunction with the watchdog timer. Join us as we unravel the intricacies of this indispensable feature in the world of microcontroller programming.

Watchdog Timer Introduction

A watchdog timer is an essential component in microcontroller systems that ensures the stability and reliability of the system. It continuously monitors the operation of the microcontroller and takes necessary actions, such as resetting the microcontroller when needed, to prevent system failures or undesirable behavior. The watchdog timer acts as a safety mechanism that guards against software or hardware failures that could cause the microcontroller to become unresponsive or behave unpredictably. It is especially useful in applications where the microcontroller is running critical tasks or operating in harsh or unpredictable environments.

The watchdog timer operates independently of the main program execution and has its own dedicated hardware. Its typical operation involves counting down from a predetermined value to zero, and if the timer reaches zero without being reset, it triggers a system reset. The microcontroller effectively demonstrates its correct functionality by periodically resetting the watchdog timer in the main program code. If, for any reason, the main program code becomes stuck or encounters an error that prevents it from resetting the watchdog timer, the timer will eventually reach zero and reset the microcontroller, returning it to a known and stable state.

Sleep Mode Introduction

Sleep mode is a power-saving mode in microcontrollers where the device is in a low-power state with reduced functionality. In this mode, the microcontroller consumes minimal power while maintaining important operations such as preserving the internal register values.

The microcontroller enters sleep mode to optimize power consumption when it is idle or not actively performing any tasks. Entering sleep mode halts unnecessary operations, enabling the microcontroller to conserve power and extend battery life in portable devices.

The microcontroller turns off the oscillator and stops executing instructions when it enters sleep mode. However, the microcontroller’s peripherals, such as timers, UARTs, and external interrupts, can still be active to wake up the device when necessary. The sleep mode enables the microcontroller to quickly resume its normal operation upon receiving an interrupt or meeting specific conditions.

It’s important to note that sleep mode doesn’t completely shut down the microcontroller but rather puts it into a low-power state. While in sleep mode, the microcontroller can still respond to external events and perform specific tasks assigned to its peripherals.

Overall, sleep mode is a useful feature in microcontrollers that helps optimize energy consumption by reducing power usage during periods of inactivity, without completely shutting down the device.

You may also like to read articles related to sleep mode:

Watchdog Timer in Pic Microcontrollers

In this article, we will explore how PIC microcontrollers utilize the watchdog timer and sleep mode. PIC microcontrollers have a built-in feature called the watchdog timer, which employs an on-chip RC oscillator, eliminating the need for external components. Even when sleep mode is enable and the OSC2 and OSC1 pins are inactive, the RC oscillator runs continuously. The device’s configuration bits can enable or disable the watchdog timer, and if it is enable, the software execution cannot override its functionality. The figure below illustrates the block diagram of the watchdog timer.

Block Diagram of Watchdog Timer

Figure 1 The Block Diagram of Watchdog Timer

By properly utilizing the watchdog timer and sleep mode, PIC microcontrollers can achieve enhanced power management and ensure system stability.

Watchdog Timer Control Registers

The watchdog timer consists of several types of control registers, such as OPTION_REG, that contain different types of configure and control bits, like TMR0 prescaler or WDT postscaler, INT, the external interrupt, TMR0, and weak pull-ups on PORT B. All the bits are readable and writable. The watchdog timer with all the registers and control bits is shown below.

Watchdog Timer Control Registers

Bit 7: The RBPU is weak pull-up enable bit. The digit 1 is to disable the weak pull-up, and the digit 0 is to enable the weak pull-up. These are the latch values of the individual port.

Bit 6: INTEDG, which is the interrupt edge select bit, uses digit 1 for the rising edge of the INT pin and digit 0 for the falling edge of the INT pin.

Bit5: T0CS, which is the clock source select bit, the digit 1 is for transitions on the T0CKI pin, and the digit 0 is for selecting the internal instruction clock cycle.

Bit 4: TOSE is the source edge select bit. The value 1 is for an incremental transition from high to low on the T0CKI pin, while the value 0 is for an incremental transition from low to high on the T0CKI pin.

Bit 3: PSA, , is to assign the pre-scaler value to the WDT (Watchdog Timer) when it is set to 1. When it is set to 0, it assigns the pre-scaler value to Timer 0.

Bit 2: PS2 and PS0 are the prescaler and postscaler rates of the watchdog timer. These bits select the rates. Please refer to Table 1 for more information.

Watchdog Timer Rates

Table 1 Watchdog Timer Rates

Operation of Watchdog Timer

When the device is in normal operating condition, the watchdog timer (WDT) simply resets the device. Similarly, when the device is in sleep mode, it wakes up the device to ensure that it continues to operate normally. This wakeup is referred to as watchdog timer wakeup. By clearing the configured bit WDTE, the watchdog timer can be permanently disabled.

The software fully controls the pre and post scalar assignments during program execution. You can change them only at that time. Even if the watchdog timer is disabled, you must execute the instruction to change the pre-scaler from Timer0 to WDT in order to prevent accidentally resetting the device.

WDT Frequency

The watchdog timer has an optimum timeout period of 18 ms, but it does not have a post scaler. This time period also changes with VDD, variation in process from part to part, and variation in temperature. If you require a longer time for adequate output, you can assign the time division ratio up to 1:128 to the post scaler via software control by writing in the OPTION_REG register. You can realize the timeout period up to 2.3 sec.

To clear the watchdog timer and post scaler, you use the instructions SLEEP and SLRWDT. These instructions also ensure that the device doesn’t time out and reset. It’s essential to remember that in the most unfavorable conditions, when VDD is at its minimum, temperature is at its maximum, and the watchdog timer post scaler is set to the maximum value, the device might take several seconds before the WTD timeout actually happens.

Sleep Mode or Power Down Mode

The sleep mode signifies a state of inactivity. In simpler terms, the device rests in this mode, remaining idle without any ongoing processes. Consequently, power consumption reaches its minimum level, and the oscillator becomes deactivated. To initiate this mode, you employ the SLEEP instruction. When this instruction activates, it clears both the watchdog timer and the PD bit in the status register, while the watchdog timer continues to operate. The TO bit is then set, and the oscillator deactivates. Furthermore, the input and output pins maintain their prior states.

During this mode, characterized by the lowest current consumption, all input and output pins assume either VDD or VSS levels. External circuitry draws no current, and the module, which typically incurs delta sleep current, renders inactive.

 Wake UP from Sleep Mode

The device which has the watchdog timer wakes up one of the following bellow conditions.

  • When any device is reset.
  • When enabled the watchdog timer.

Any related module, such as an external input pin, a change in the port pin, a comparator, an analog-to-digital (A/D) converter, a timer, a liquid crystal display (LCD), a serial synchronous port (SSP), or a capture module, can interrupt the sleeping mode and awaken the watchdog timer. Once it wakes up, the watchdog timer resumes program execution. You can determine the reason for the device wake-up or reset by examining the TO and PD bits in the status register.

Upon device power-up, it clears the PD bit and enters reset mode. The watchdog timer awakens when the TO bits clears. After waking up, the device automatically resumes operation and proceeds to execute the next instruction. If the device wakes up due to an external event interrupt, you must enable the corresponding bit. The GIE bit remains unaffected during device wake-up, so if it’s enabled, the device will execute the instruction following the sleep instruction.

Wake up from sleep mode by Using Interrupts

When an interrupt occurs before executing the SLEEP instruction, the microcontroller will not clear the WDT and WDT postscaler. The TO and PD bits are also not cleared. If the interrupt occurs after executing the SLEEP instruction, the microcontroller immediately wakes up from sleep mode and fully executes the SLEEP instruction. In this case, the microcontroller clears the WDT and WDT postscaler.

Pic Microcontroller Watchdog Timer Example with MikroC

Here’s an example of how to use the watchdog timer in a PIC microcontroller using the MikroC for PIC compiler. In this example, we’ll configure the watchdog timer to generate a reset if it’s not periodically reset within a specific time frame:

// Include the necessary header file
#include <built_in.h>

// Define the configuration bits for the PIC microcontroller
__CONFIG(FOSC_HS & WDTE_ON & PWRTE_ON & CP_OFF);

void main() {
    // Initialize the watchdog timer
    OPTION_REG &= 0x7F; // Enable the Watchdog Timer with a prescaler of 1:128

    // Your main program code

    while (1) {
        // Place your application code here

        // Reset the watchdog timer to prevent a timeout
        asm CLRWDT; // Clear the watchdog timer
    }
}

In this code:

We include the necessary header files and set the configuration bits for your specific PIC microcontroller. Make sure to adjust the configuration bits to match your PIC model and requirements.

In the main() function, we initialize the watchdog timer by modifying the OPTION_REG register. We enable the watchdog timer with a prescaler of 1:128. You can adjust the prescaler value to change the timeout period.

Inside the while(1) loop, your main program code should be placed. It’s essential to reset the watchdog timer periodically to prevent a timeout. We use the assembly instruction asm CLRWDT; to clear the watchdog timer. You should include this line at appropriate intervals in your code to avoid a watchdog timer reset.

Your application-specific code can run within the while(1) loop. Be sure to reset the watchdog timer as needed to prevent unintended resets.

Applications of Watchdog Timer Mode

  1. This timer finds its applications where the user continuously sets and resets the operating system while it is running.
  2. The primary application of a watchdog timer is to monitor the system, detect any issues, and control the microprocessor.
  3. When the code in a microcontroller is not executing properly, the watchdog timer resets the microcontroller to ensure proper code execution.

Conclusion

In conclusion, the watchdog timer and sleep mode are vital features in the world of microcontrollers, specifically in the context of PIC microcontrollers. The watchdog timer ensures the stability and reliability of the system by continuously monitoring the microcontroller’s operation and taking necessary actions, such as resetting the microcontroller when needed, to prevent system failures or undesirable behavior. On the other hand, sleep mode allows the microcontroller to conserve power by entering a low-power state when idle, while still maintaining important operations. By properly utilizing these features, PIC microcontrollers can achieve enhanced power management and ensure system stability. The watchdog timer and sleep mode are indispensable tools for microcontroller programmers, offering a balance between functionality and energy efficiency.

You may also like to read:

Leave a Comment