ARM Technical Support Knowledge Articles

SOFTWARE RESET IN C

Applies to: C51 C Compiler

Answer


Information in this article applies to:


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

Article last edited on: 2006-05-03 23:05:11

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential