main()
{
unsigned char code rst[]={0xe4,0xc0,0xe0,0xc0,0xe0,0x32}; // reset code
(*((void (*)())(rst)))(); // execute the previous line of code, call the rst array as a function
}
Originally I told him to embed the following code:
clr a
push acc
push acc
reti
But he played with the previous part, and the content in the array rst[] was exactly the assembly machine code above. What he did was to
save the data in the rst array as code, then use the absolute address method to point to the array, and run the code in the array as
a function. It actually passed!
l A better way to reset the microcontroller
The assembly language explanation in the post is as follows:
clr a // Clear ACC = 0
push acc // Push 0 to the stack - 8 bits
push acc // Push 0 to the stack again - 8 bits
reti // Return to address 0, and execute.
The analysis method of this sentence is the same as above, but it is more refined and there are no redundant assembly statements.
There is a big difference between software reset and real power-on reset: most registers have a certain reset value during power-on reset; software
reset is only equivalent to starting execution from address 0, and the registers will not change to the certain reset value.
Example: 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 changed from the following three instructions
NOP
NOP
LJMP STAT to
:
NOP
NOP
LJMP ERR
to achieve the purpose.
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. Interested readers can replace the infinite loop with software traps, replace LJMP ERR with LJMP STAT and LJMP ERR1 respectively, and then set the interference detection in the low-level interrupt and the main program respectively. The experimental results will inevitably prove that only LJMP ERR can achieve software reset without fail, so that the system can get rid of interference and return to normal. In the software reset process of the MCS-51 microcontroller, the interrupt return instruction RETI must be executed twice in succession to ensure that the system returns to normal.
2. C language reset
void reset (void){((void (code *) (void)) 0x0000) ();
The reset procedure cannot clear the interrupt system of 8051 and some peripheral devices of 8051. When you call the above software reset procedure in the interrupt procedure, the interrupt will no longer be triggered. Therefore, the above soft reset procedure cannot be called in the interrupt subroutine.
You can use the following program to jump to 0000H to implement a soft reset. The following program is actually a function pointer, which points to the address 0000H. ((void (code *) (void)) 0x0000) ();
The following example implements software self-reset
void reset (void)
{
((void (code *) (void)) 0x0000) ();
}
void main (void)
{
reset ();
}
You may notice that the above soft reset procedure cannot clear the 8051 interrupt system and some 8051 peripherals. When you call the above software reset procedure in the interrupt routine, the interrupt will no longer be triggered. Therefore, the above soft reset procedure cannot be called in the interrupt subroutine.
The following small assembly function can be called in the interrupt program or the main program. This function pushes 0x0000 onto the stack and then pops it with "RETI", which will clear the interrupt environment and restart the program from 0000H.
?PR?RESET SEGMENT CODE RSEG ?PR?RESET
; C prototype: void reset (void); PUBLIC reset reset: POP ACC; pop return address POP ACC CLR A ; push 0 as new PUSH ACC; return address to stack PUSH ACC RETI; execute return of interrupt END The above program works well when bank 0 is selected. If a register bank other than bank 0 is selected, you may not get the expected results. You should add "MOV PSW, #0" in the above program or startup code to select bank 0.
My soft reset has been used for many years without any problems
clr EA
call reset
call reset ; call reset as many times as the interrupt priority is used
jmp start ; jump to 0000h
Previous article:The role of microcontroller emulator
Next article:Communication between MCU and PLC: About 485 interface
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- [FreeRTOS check-in station 3 is open] Task status and switching, closing time is August 20
- New feature! You can add videos directly to your posts~~~~ No worries~~
- Application of Standing Wave Ratio Analysis Module in Leaky Cable Monitoring System
- DSP system design-DSP development dynamics issues
- What is the function of the INCREMENT COMPONENT PART NUMBER button in PROTEL99?
- MicroPython simple task scheduler
- [2022 Digi-Key Innovation Design Competition] Distributed Temperature and Humidity Acquisition System - Brief Design Overview
- CC2541 Bluetooth Watchdog Mode
- The weekly review information is here~ Several activities are online this week~
- 【AT-START-F403A Review】1. Environment setup (including all required files + Stdlib simplified version)