Introduction to Misunderstandings in Anti-interference Technology of MCS51 Series Microcontroller Software

Publisher:angelbabyLatest update time:2012-09-24 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:Introduction to Misunderstandings in Anti-interference Technology of MCS51 Series Microcontroller Software

Previous article:Research on Embedded Internet Technology of MCS-51 Single-Chip Microcomputer
Next article:Application of Clock Interrupt in MCU Programming

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号