Search code examples
embeddedpicmicrochip

How to reset a PIC18 in C?


What is the best way to reset a PIC18 using C code with the HiTech Pic18 C compiler?

Edit:

I am currenlty using

void reset()
{
#asm 
  reset
#endasm
}

but there must be a better way


Solution

  • There's a FAQ here.

    Q: How do I reset the micro?

    One way is to reset all variables to their defaults, as listed in the PIC manual. Then, use assembly language to jump to location 0x0000 in the micro.

    #asm ljmp 0x0000

    #endasm

    This is quite safe to use, even when called within interrupts or procedures. The PIC 16x series micros have 8 stack levels. Each time a procedure is called, one stack level is used up for the return address. It is a circular buffer, so even if the micro is 7 procedure levels deep and in an interrupt when a reset is called, this is the new start of the stack buffer, and the micro will continue as per normal.

    Another way is to set watchdog the timer when the chip is programmed, and use CLRWDT() instructions all through the code. When you want the micro to reset, stop clearing the watchdog bit and the micro will reset after around 18ms to 2 seconds depending on the prescaler.