views:

1088

answers:

5

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

A: 

Unless there's a library function defined by the compiler vendor's runtime library (if such a lib even exists in the microcontroller world ... but it should), then no. C itself certainly won't help you out, doing "a reset" is far too much of a platform-specific issue for C to cover it.

unwind
A: 

I use the ccsinfo.com compiler, which has a similar API call for resetting the PIC, but I would think the compiler's solution would do the right thing.

kenny
+3  A: 

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.

Eli Bendersky
The jump to 0x0000 won't reset any hardware peripherals.I'd use the watchdog or the reset assembly instruction.
Robert
+3  A: 

The compilers usually have their own reset() function built in, but it just does exactly what your function does, and the actual name may vary from compiler to compiler.

You are already doing it the best possible way.

MrZebra
+2  A: 

Your answer is the best way I know of. The key is that you have the assembly instruction inside a function call, all by itself. The compiler will not optimize a function that has inline assembly in it, so if you include the reset instruction inline to a very large function, the compiler will not optimize any of the code in that function. You have avoided this by putting Reset in its own function. The code in this function won't be optimized, but who cares, since it is such a small function.

Brent