Keil™, An ARM® Company

Technical Support

C51: SOFTWARE RESET IN C


Information in this article applies to:

  • C51 All Versions

QUESTION

How can I put a software reset into my source code?

ANSWER

You may type cast the address of the reset vector (0x0000) to a function pointer and call that from C using the following:

((void (code *) (void)) 0x0000) ();

For example, the following program continuously resets itself.

void reset (void)
{
((void (code *) (void)) 0x0000) ();
}

void main (void)
{
reset ();
}

You should note that the software reset sequence above does not clear the 8051 interrupt system or reset any 8051 peripherals. When the above code is executed inside an interrupt routine, the 8051 blocks subsequent interrupts. Therefore, this sequence cannot be used in interrupt service routines.

The following small assembly function may be called from interrupt routines or from your main program. It works by pushing a return of 0x0000 onto the stack and executing an RETI instruction (to return from interrupt). This clears any interrupt conditions and starts program execution 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

This works well when register bank 0 is selected. If this routine is called and register bank 0 is not selected, the program may not function as expected. You should add the following instruction to the above routine or to the startup code to select register bank 0.

       MOV  PSW, #0

SEE ALSO

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Wednesday, May 03, 2006


Did this article provide the answer you needed?
 
Yes
No
Not Sure