Keil Logo

C51: Software Reset in C


Information in this article applies to:

  • C51

QUESTION

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

ANSWER

You may typecast 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


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.