ATMEGA128 Watchdog Timer

Publisher:温柔阳光Latest update time:2017-11-23 Source: eefocusKeywords:ATMEGA128 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. ATMEGA128 Data Sheet

Watchdog Timer

       The watchdog timer is driven by an independent 1 MHz on-chip oscillator. This is a typical value at VCC = 5V. See the characteristic data for typical values ​​at other VCC levels. The time interval of the watchdog reset can be adjusted by setting the prescaler of the watchdog timer, as shown in Table 22 on P53. The watchdog reset instruction WDR is used to reset the watchdog timer. In addition, the timer is reset when the watchdog timer is disabled or a reset occurs. There are 8 options for the reset time. If the timer is not reset in time, once the time exceeds the reset period, the ATmega128 is reset and executes the program pointed to by the reset vector. The specific watchdog reset timing is described on P50.

       To prevent the watchdog timer from being accidentally disabled or the reset time changed, the chip provides three different protection levels based on the fuse bits M103C and WDTON, as shown in Table 21. Security level 0 corresponds to the ATmega103 settings. Enabling the watchdog timer has no restrictions. Please refer to P 54 “Changing the timing sequence of the watchdog timer configuration”.

ATmega128 WDT Configuration Table Watchdog Timer

Watchdog Timer Control Register - WDTCR

ATmega128 Watchdog Timer Control Register - WDTCR

• Bits 7..5 – Res: Reserved.
Reserved bits. A read operation returns a value of zero.

• Bit 4 – WDCE: Watchdog Modification Enable
When clearing WDE, WDCE must be set first, otherwise the watchdog cannot be disabled. Once set, the hardware will clear it after the next 4 clock cycles. Please refer to the description of WDE to disable the watchdog. WDCE must also be set to modify the prescaler data when operating in security levels 1 and 2, as shown in P 54 “Time sequence for changing the watchdog timer configuration”.

• Bit 3 – WDE: Watchdog Enable
When WDE is "1", the watchdog is enabled, otherwise the watchdog will be disabled. WDE can only be cleared when WDCE is "1". The following are the steps to turn off the watchdog:
1. Write "1" to WDCE and WDE in the same instruction, even if WDE is already "1".
2. Write "0" to WDE within the next 4 clock cycles.
The watchdog timer can never be disabled when working at security level 2. Refer to P 54 "Timing sequence for changing the watchdog timer configuration".

• Bits 2..0 – WDP2, WDP1, WDP0: Watchdog Timer Prescaler 2, 1, and 0
WDP2, WDP1, and WDP0 determine the prescaler for the Watchdog Timer as shown in Table 22.

ATmega128 Watchdog Timer Prescaler Options

       The following examples implement the operation of turning off the WDT in assembly and C. It is assumed that the interrupt is under user control (for example, global interrupt is disabled), so the interrupt will not occur when executing the following program.

Assembly code examples
WDT_off:
; Set WDCE and WDE
ldi r16, (1<out WDTCR, r16
; Turn off WDT
ldi r16, (0<out WDTCR, r16
ret

C code example

void WDT_off(void)
{

WDTCR = (1<
WDTCR = 0x00;
}

       Watchdog initialization routine      

C code example

 void WDT_Init(void)

{

  _WDR();//reset watchdog timer
 WDTCR |= (1< WDTCR = 0x0F;//enable watchdog timer andset timeout value 1.9 sencond

}

Changing the watchdog timer configuration timing sequence

The sequence for changing the configuration varies slightly depending on the security level. We will explain each one below.

Security Level 0

This mode is compatible with the watchdog operation of the ATmega103. The initial state of the watchdog is disabled. You can enable it without restriction by setting WDE and changing the timer overflow period. When disabling the watchdog timer, you need to follow the instructions about WDE.

Security Level 1

In this mode, the watchdog timer is initially disabled and can be enabled without restriction by setting WDE (e.g., WDTCR |= (1<1. Writing "1" to WDCE and WDE in the same instruction, even if WDE is already "1".
2. Writing "0" to WDE and appropriate data to WDP simultaneously within the next 4 clock cycles, while writing "0" to WDCE.

Security Level 2

In this mode, the watchdog timer is always enabled and the read return value of WDE is "1". Changing the timer overflow period requires executing a specific timing sequence:
1. Write "1" to WDCE and WDE in the same instruction. Although WDE is always set, "1" must be written to start the timing.
2. Write "0" to WDCE and write appropriate data to WDP simultaneously within the next 4 clock cycles. The value of WDE can be arbitrary.

 

2. Watchdog AVR-GCC routine

       Header file: #include, including the watchdog reset command _WDR();

      avr-libc provides three APIs to support the operation of the internal Watchdog of the device, namely:
      wdt_reset() // Watchdog reset
      wdt_enable(timeout) // Watchdog enable
      wdt_disable() // Watchdog disable
      Before calling the above functions, you must include the header file wdt.h. wdt.h also defines the Watchdog timer timeout symbol constants, which are used to provide timeout values ​​for the wdt_enable function. The symbolic constants are as follows:
      Symbolic constant Value Meaning
      WDTO_15MS Watchdog timer 15 milliseconds timeout
      WDTO_30MS Watchdog timer 30 milliseconds timeout
      WDTO_60MS Watchdog timer 60 milliseconds timeout
      WDTO_120MS Watchdog timer 120 milliseconds timeout
      WDTO_250MS Watchdog timer 250 milliseconds timeout
      WDTO_500MS Watchdog timer 500 milliseconds timeout
      WDTO_1S Watchdog timer 1 second timeout
      WDTO_2S Watchdog timer 2 second timeout
      Watchdog test program:
      When executing this program on CA-M8, S1-8 (LEDY) should be turned on to connect the yellow LED to the PB0 pin.

#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define SET_LED PORTB&=0XFE //PB0 connected to the yellow LED

#define CLR_LED PORTB|=0X01
//1ms delay so the error will not be too big function
void DelayMs(uint ms)
{
     uint i;
     for(i=0;i
     _delay_loop_2(4 *250);
}
int main(void)
{
     DDRB=_BV(PB0);
     PORTB=_BV(PB0); //CLR_LED
     //WDT counter synchronization is one second
     wdt_enable(WDTO_1S);
     wdt_reset();//Feed the dog
     DelayMs(500);
     SET_LED;
     //Wait for the dog to starve to death
     DelayMs(5000);
     SET_LED;
     while(1)
     wdt_reset();
}
      Execution result:
      The yellow LED on CA-M8 keeps flashing, proving that Watchdog makes the MCU reset continuously.

3. Notes

      If the time of each cycle in the loop body does not exceed the reset time of the watchdog, it is sufficient to feed the dog once, otherwise it needs to be fed multiple times.


Keywords:ATMEGA128 Reference address:ATMEGA128 Watchdog Timer

Previous article:12864 LCD display picture-CVAVR program
Next article:Questions about using internal pull-up resistors in AVR microcontrollers

Recommended ReadingLatest update time:2024-11-16 13:01

stm8s development (V) Use of TIMER: timing!
STM8S provides three types of TIM timers: advanced control type (TIM1), general type (TIM2/TIM3/TIM5) and basic timer (TIM4/TIM6). Although they have different functions, they are all based on a common architecture. This common architecture makes it very easy and convenient to design applications using various timers
[Microcontroller]
stm8s development (V) Use of TIMER: timing!
S3C6410 WATCHDOG TIMER (watchdog timer) driver under Linux (1)
Let's talk about the overall structure first. We have to talk about the platform device that everyone is familiar with. The watchdog timer also exists as a platform device, but the difference from the previous one is that the watchdog timer is a hybrid device. Let's introduce the hybrid device first. 1. Mixed equipm
[Microcontroller]
ATmega128 Stack Pointer
The stack pointer is mainly used to store temporary data, local variables and the return address of interrupts/self-routines. The stack pointer always points to the top of the stack. Note that the AVR stack grows downward, that is, when new data is pushed into the stack, the value of the stack pointer will decrease.
[Microcontroller]
ATmega128 Stack Pointer
System tick Timer (SYSTICK Timer) of STM32F10xxx
background When studying the STM32F10xxx timer, I accidentally saw the System tick Timer, so I took a deeper look at it and made a record here. text System tick timer is a 24-bit system timer of Cotex-M core. Its auto-reload value can be configured and changed at any time, and if the system timer is configured, its in
[Microcontroller]
STM8L timer2 generates PWM
Introduction This article introduces how to use timer2 in the STM8L series to generate 38K frequency PWM. Among them, this article uses the first channel (PB0) of timer2. experiment platform Compiler software: IAR for STM8 1.42.2 Hardware platform: stm8l101f3p6 development board Emulator: ST-LINK Library version: ST
[Microcontroller]
STM8L timer2 generates PWM
Watchdog circuit
[Microcontroller]
Watchdog circuit
Design of key authentication system based on ATSHA204
Introduction: The design cost of electronic systems is getting higher and higher, but PCB copying has become easier, and the programs in Flash can also be easily copied, so the probability of the entire electronic product system being cloned is getting higher and higher. In view of this phenomenon, an embedded key aut
[Microcontroller]
Design of key authentication system based on ATSHA204
ATmega128 Status Register
The status register contains information about the results of the most recently executed arithmetic instruction. This information can be used to change the program flow to implement conditional operations. The contents of the status register are updated only after the ALU operation is completed. In this way, in most c
[Microcontroller]
ATmega128 Status Register
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号