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
#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.
Previous article:Comparison between 51 MCU and AVR MCU
Next article:Research on Functional Simulation Verification Based on 82C52
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Save 50 (last 4 places): Buy STM32WB Nucleo Bluetooth Kit (including USB Dongle)
- DSP28335 interrupt configuration
- Why is the voltage after the op amp voltage division not 12V?
- Quickly get a fuel gauge working from scratch
- Experiment on the use of voltage sensor and current sensor
- Meaning of the tail vertebra
- Why can't I open the TI reference design?
- Show off the PSSD mobile hard drive
- ST's three French factories are on strike, and the chip shortage may become more serious
- What should I pay attention to when migrating a project from Embedded Studio V4 to V5?