There is a widespread misunderstanding in the microcontroller: in the MCS-51 series microcontroller, as long as the program is executed from the starting address with an instruction, the microcontroller can be reset to get rid of interference. Through a simple experiment, a reliable method of software reset is revealed.
Some MCUs (such as 8098) have special reset instructions. Although some enhanced MCS-51 system MCUs do not have reset instructions, they have integrated WATCHDOG circuits, so anti-interference is not a problem. Since the popular MCS-51 series MCUs (such as 8031 and 8032) do not have reset instructions and do not have hardware WATCHDOS, if there is no external hardware WATCHDOG circuit, software anti-interference technology must be used. Commonly used software anti-interference technologies include: software traps, instruction redundancy, software WATCHDOG, etc. Their role is to detect when the system is disturbed in time, and then use software methods to reset the system. The so-called software reset is to use a series of instructions to imitate the reset operation. This is the software reset technology unique to the MCS-51 series MCU.
Now let's use a simple experiment to illustrate. The experimental circuit is shown in the attached figure. The light-emitting diode LED0 connected to the simulation socket P1.0 is used to indicate the working status of the main program, the light-emitting diode LED1 connected to P1.1 is used to indicate the working status of the low-level interrupt subroutine, the light-emitting diode LED2 connected to P1.2 is used to indicate the working status of the high-level interrupt subroutine, and the button connected to the P3.2 port is used to set up an interference flag. After the program detects the interference flag, it deliberately enters an infinite loop or falls into a trap to simulate the interference situation, thereby verifying the actual effects of various reset methods. The initialization program for the test is as follows:
ORG 0000H
STAT: LJMP MAIN Reset entry address
LJMP PX0 Button interrupt vector (low-level interrupt)
ORG 000BH
LJMP PT0 t0 interrupt vector (low-level interrupt)
ORG 001BH
LJMP PT1 T1 interrupt vector (high-level interrupt)
ORG 0030H
MAIN:
CLR EA
MOV SP, #7
MOV P1, #0FFH
MOV P3, #0FFH
MOV TMOD, #11H
CLR 00H Disturbance flag initialization
SETB ET0
SETB ET1
SETB EX0
SETB PT1
SETB TR0
SETB TR1
SETB EA
LOOP: CPL P1.0 Main program LED flashing
MOV R6, #80H
MOV R7, #0
TT1:
DJNZ R7, TT1
DJNZ R6, TT1
SJMP LOOP
PX0:
SETB 00H Set up interference flag to simulate interference
PT0: CPL P1.1 Low-level interrupt program LED1 flashes
RETI
PT1: CPL P1.2 High-level interrupt program LED2 flashes
RETI
END
The experimental steps are as follows:
1. Start the execution according to the above program, and all three LEDs should flash (otherwise, the fault should be eliminated first), indicating that the main program and each interrupt subroutine are normal. Because the analog interference mark is not detected, it is not affected by the button.
2. Modify the main program as follows. After pressing the button, the main program will fall into an infinite loop.
LOOP: CPL P1.0
MOV R6,#80H
MOV R7,#0H
TT1: DJNZ R7,TT1
DJNZ R6,TT1
JNB 00H,LOOP Is it disturbed?
STOP: LJMP STOP Falling into an infinite loop.
At this time, you can see that the main program stops working (LED0 stops flashing), while the two interrupt subroutines continue to run (LED1 and LED2 continue to flash).
3. Use timer T1 as software WATCHDOG and use unit 30H as software WATCHDOG counter. Add an instruction to reset software WATCHDOG in the main program.
LOOP: CPL P1.0
MOV 30H,#0 Reset software WATCHDOG counter
LOOP: CPL P1.0
MOV R6,#80H
MOV R7,#0H
TT1: DJNZ R7,TT1
DJNZ R6,TT1
JNB 00H,LOOP Is it disturbed?
STOP: LJMP STOP Fall into an infinite loop.
The T1 interrupt subroutine is modified as follows:
PT1: CPL P1.2 High-level interrupt program LED flashes
INC 30H
MOV A,30H
ADD A,#0FDH
JC ERR Reach 3 times?
RETI
ERR: LJMP STAT Software WATCHDOG action [page]
Before pressing the button, the program runs normally (all three LEDs flash). After pressing the button, the main program can quickly resume work, but the two interrupt subroutines are blocked and no longer work. The process is as follows: After the main program detects interference, it enters an infinite loop and cannot perform the operation of resetting the 30H unit. The T1 interrupt causes 30H to increase in value continuously. When the count reaches 3, the software WATCHDOG performs an action and executes an LJMP instruction to execute the program from the beginning. The interference flag is cleared in the MAIN process (indicating that the interference has passed), so that the main program can quickly resume work. It stands to reason that the MAIN process also resets each interrupt and opens them. Why can't the interrupt resume work? This is because the reset of the interrupt activation flag has been forgotten, because it has no clear bit address to be programmed, and directly turning to the 0000H address cannot complete the real reset. Software reset is a must-do work after using software traps and software WATCHDOG. At this time, it is entirely possible that a program error occurs. In the interrupt subroutine, the interrupt activation flag is set, which will prevent the same-level interrupt response. Since the software WATCHDOG is a high-level interrupt, it will prevent all interrupt responses. This shows the importance of clearing the interrupt activation flag. Many authors of literature have made mistakes because they did not realize this.
4. Among all the instructions, only RETI instruction can clear the interrupt activation flag. The error handling procedure ERR mainly completes this function, and other follow-up work is completed by the reset system. For this reason, we redesign the T1 interrupt subroutine as follows:
PT1: CPL P1.2 High-level interrupt program LED flashes
INC 30H Software WATCHDOG counter increments
MOV A,30H
ADD A,#0FD
JC ERR Reach 3 times?
RETI
ERR: CLR EA Disable interrupt
CLR A Prepare reset address (0000H)
PUSH ACC
PUSH ACC
RETI Clear interrupt activation flag and reset
This program first disables the interrupt so that subsequent processing can proceed smoothly, and then replaces the LJMP instruction with the RETI instruction, thereby clearing the interrupt activation flag and completing the task of turning to 0000H. After the program is modified in this way, the result is still not ideal: after pressing the button, sometimes only the main program and the high-level interrupt subroutine can quickly return to normal, while the low-level interrupt may still be disabled. If the interference is transferred to the low-level interrupt as follows, the low-level interrupt will be turned off after pressing the button:
LOOP: CPL P1.0
MOV R6,#80H
MOV R7,#0H
TT1: DJNZ R7,TT1
DJNZ R6,TT1
SJMP LOOP
PT0: CPL P1.1
JB 00H,STOP
RETI
STOP: LJMP STOP Falling into an infinite loop.
After careful analysis, we may conclude that when the software WATCHDOG is nested in a low-level interrupt, only the high-level interrupt activation flag is cleared after reset, and the low-level interrupt flag is still set, so that the low-level interrupt is always disabled.
5. Modify the error handling as follows:
ERR: CLR EA Correct software reset entry
MOV 66H,#0AAH Rebuild the power-on flag
MOV 67H,#55H
MOV DPTR,#ERR1 Prepare the first return address
PUSH DPL
PUSH DPH
RETI Clear the high-level interrupt activation flag
ERR1: CLR A
PUSH ACC
PUSH ACC
RETI Clear the low-level interrupt activation flag
At this time, RETI must be executed twice to reach 0000H to ensure that all interrupt activation flags are cleared to achieve the same effect as hardware reset. Similarly, the software trap must also be achieved by the following three instructions.
NOP
NOP
LJMP STAT
is changed to:
NOP
NOP
LJMP ERR
Conclusion: When the main program is disturbed and captured by the software trap, the interrupt flag is not set. During the execution of ERR, the RETI instruction is equivalent to the RET instruction, which can also achieve the purpose of software reset. The experimental results show that only LJMP ERR can realize software reset without fail, so that the system can get rid of interference and return to normal. In the software reset process of MCS-51 microcontroller, the interrupt return instruction RETI must be executed twice in succession to ensure that the system returns to normal.
Previous article:Research on Embedded Internet Technology of MCS-51 Single-Chip Microcomputer
Next article:Application of Clock Interrupt in MCU Programming
- 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
- Testing solutions for redundant link networks
- Key wireless technologies for 5G systems
- [NXP Rapid IoT Review] + Rapid IoT App Running Error
- How to Design an RF Power Amplifier: The Basics
- What is the principle of touch switch?
- What is jitter and phase noise?
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board evaluation] + OLED screen display driver
- Tailing Micro B91 Development Kit Burning Pitfalls Record
- Antai Testing - Sharing of Maintenance Experience of Tektronix AFG3021 Arbitrary Signal Generator
- Interrupt vectors for ARM (Cortex-M3)