About MCU soft reset

Publisher:脑力驿站Latest update time:2016-09-08 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Array positioning

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

Keywords:MCU Reference address:About MCU soft reset

Previous article:The role of microcontroller emulator
Next article:Communication between MCU and PLC: About 485 interface

Latest Microcontroller Articles
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号