The use and application of STC89 series microcontroller watchdog

Publisher:as8849402Latest update time:2012-12-01 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The concept of "watchdog" and its application in the system composed of single-chip microcomputer. Since the operation of the single-chip microcomputer may be interfered by the external electromagnetic field, causing the program to run away and fall into an infinite loop, the normal operation of the program is interrupted, and the system controlled by the single-chip microcomputer cannot continue to work. This will cause the entire system to stagnate and have unpredictable consequences. Therefore, out of consideration for real-time monitoring of the operating status of the single-chip microcomputer, a chip specifically used to monitor the operating status of the single-chip microcomputer program was developed, commonly known as a "watchdog".
The purpose of adding a watchdog circuit is to enable the microcontroller to work continuously in an unmanned state. The working process is as follows: the watchdog chip is connected to an I/O pin of the microcontroller. The I/O pin is controlled by the program of the microcontroller so that it regularly sends a high level (or low level) to the pin of the watchdog chip. This program statement is scattered among other control statements of the microcontroller. Once the microcontroller is trapped in a certain program segment and enters an infinite loop state due to interference, the program that sends a level to the watchdog pin cannot be executed. At this time, the watchdog circuit will send a reset signal to the pin connected to the reset pin of the microcontroller because it cannot get the signal sent by the microcontroller, so that the microcontroller will reset, so that the microcontroller will restart the execution of the program from the starting position of the program memory, thus realizing the automatic reset of the microcontroller.
Usually, the watchdog circuit needs a special watchdog chip connected to the microcontroller to realize it. This has been introduced in our microcontroller tutorial website http://www.51hei.com. However, this will complicate the circuit design. The STC microcontroller has a built-in watchdog. The watchdog application can be realized by setting the corresponding special function register. The STC89 series microcontroller has a special watchdog timer register, the Watch Dog Timer register. Its corresponding function is shown in the next knowledge point.
Watchdog Timer Register (WDT_CONTR)
The byte address of the STC microcontroller watchdog timer register in the special function register is E1H, which cannot be bit-addressed. This register is used to manage the watchdog control part of the STC microcontroller, including starting and stopping the watchdog, setting the watchdog overflow time, etc. When the microcontroller is reset, the register is not necessarily cleared to 0. On the STC download program software interface, you can set the reset to turn off the watchdog or only turn off the watchdog when the power is off. You can make a choice that suits your own design system according to your needs. The definition of each bit is shown in Table 4.2.1.

Table 1 Watchdog Timer Register (WDT_CONTR)

Bit number D7 D6 D5 D4 D3 D2 D1 D0
Bit Notation -- -- EN_WDT CLR_WDT IDLE_WDT PS2 PS1 PS0

EN_WDT: Watchdog enable bit. When set to "1", the watchdog is started.
CLR_WDT: Watchdog clear "0" bit. When set to "1", the watchdog timer will restart counting. Hardware automatically clears this bit to "0".
IDLE_WDT: Watchdog "IDLE" mode bit. When set to "1", the watchdog timer counts in the "idle mode" of the microcontroller. When cleared to "0", the watchdog timer does not count in the "idle mode" of the microcontroller.
PS2, PS1, PS0: Watchdog timer prescaler value. Different values ​​correspond to prescaler numbers as shown in Table 4.2.2.


Table 2 12M crystal watchdog timer prescaler value

PS2 ps1 ps0 Prescaler Watchdog overflow time
0 0 0 2 65.5ms
0 0 1 4 131.0ms
0 1 0 8 262.1ms
0 1 1 16 524.2ms
1 0 0 32 1.0485s
1 0 1 64 2.0971s
1 1 0 128 4.1943s
1 1 1 256 8.3886s

The watchdog overflow time is directly related to the prescaler number, the formula is as follows:

Watchdog overflow time = (N × pre-scaling number × 32768) / crystal frequency
In the above formula, N represents the clock mode of the STC microcontroller. The STC microcontroller has two clock modes: single speed, that is, 12 clock mode. In this clock mode, the STC microcontroller has the same machine cycle as other companies' 51 microcontrollers, that is, 12 oscillation cycles are one machine cycle; the other is double speed, also known as 6 clock mode. In this clock mode, the STC microcontroller runs twice as fast as other companies' 51 microcontrollers. There are settings for single speed and double speed on the download program software interface. You can download the test program to run at your own speed. The value of the pre-scaling number is determined by the combination of PS2, PS1 and PS0, as shown in Table 4.2.2. The crystal frequency is the clock frequency of the current system.
Below we use two examples to further explain the difference between program operation when using a watchdog and when not using a watchdog. Due to the high anti-interference characteristics of the STC microcontroller, I have never encountered a situation where the program runs wild, so it is difficult for us to artificially create a situation where the microcontroller program runs wild. The following program demonstrates the use of the watchdog overflow time to automatically reset the program.
[Example]: Implement the following description on the TX-1C experimental board: After the program starts, set the watchdog overflow time to about 2 seconds, then light up the first LED, delay for a while, and then turn off the LED, so that the program enters a waiting dead loop state, and feeds the dog approximately every 1 second in the dead loop to see if the program runs normally.
Create a new file part3.4.2.c, and the program code is as follows:

#include //52 series MCU header file
#define uchar unsigned char
#define uint unsigned int
sfr WDT_CONTR=0xe1;
sbit led1=P1^0;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--) //i=xms means delay of about xms milliseconds
for(j=110;j>0;j--);
}
void main()
{
WDT_CONTR=0x35;
led1=0;
delayms(500);
led1=1;
while(1)
{
delayms(1000);
WDT_CONTR=0x35;
}
}
Analysis:
(1) “sfr WDT_CONTR=0xe1;” defines the newly added watchdog register in the STC microcontroller. Since this register is not defined in the reg52.h header file, the method for defining other new registers in the future is the same. Of course, it can also be written in the reg52.h header file.
(2) After the program starts, the LED is lit and then turned off after a delay of about 500ms. The delay here should not be too long. If the total time of the program running between two feedings exceeds the overflow time of the watchdog, the watchdog will reset the microcontroller. You can adjust this delay time to see the actual demonstration effect.
(3) When feeding the dog, use the same statement as setting the watchdog register. As long as the CLR_WDT bit in the watchdog register is set to 1, the watchdog timer will count again. After the CLR_WDT bit is set to 1, it will be automatically cleared by the hardware.
(4) The demonstration result shows that the LED lights up and then turns off immediately and will never light up again. This shows that the program has not been reset and always stops in the while (1) loop. The watchdog is in a normal feeding state.
[Example] Just delete the sentence "WDT_CONTR=0x35;" in the while(1) loop, and the demonstration result is that the small light keeps flashing. Because of the role of the watchdog, when the watchdog timer overflows, the microcontroller is reset and the program is re-executed from the beginning, so the small light flashes.
This is an original article of 51hei microcontroller. Please keep the original source of this article for reprinting: http://www.51hei.com/mcu/637.html.
When using the watchdog, it is necessary to feed the dog at different positions in the entire large program. The time interval between feeding the dog twice must not be less than the overflow time of the watchdog timer, otherwise the program will reset continuously.

Reference address:The use and application of STC89 series microcontroller watchdog

Previous article:Comparison between 51 MCU and AVR MCU
Next article:Research on Functional Simulation Verification Based on 82C52

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号