Rebooting a PIC, the crude nasty way :)

Status
Not open for further replies.

bsodmike

New Member
Hiyas,

I've got my PIC running @ 20Mhz and don't want to be littering my code with clrwdt()'s just yet...

Basically during my PS/2 transmit operation if it fails 5 times I want to reboot the pic, what I've done is this:

Code:
		if (retry > 4)
		{
			asm("org 0x00");
		}

I'm using the PICC compiler by Hi-TECH and was wondering if this crude way of manually setting the program counter back to 0x00 is ok

Cheers,
Mike
 
Jumping to the reset vector will re-run the program from the beginning, but it's not performing a reset! - the stack will still be set at where ever it was previously!, and any other registers you assume reset values for will not be valid.
 
Hi, bsodmike

Well then one can always connect the /MCLR to an unused port pin and output a LOW to reset the PIC.

As you have mentioned in the title, you wanted a crude and nasty method. :roll:
 
Which PIC are you using

Hi,

If you are using a 18F series then it has got a software RESET instruction. Tying a port to MCLR is a good idea but keep in mind to use a RC Delay network. Normally when the PIC starts the data direction registers are set as input so no prob. Still in some cases you find your PIC freeze.

Thanks and Regards

Sougata
 
bsodmike said:
Code:
		if (retry > 4)
		{
			asm("org 0x00");
		}

I'm using the PICC compiler by Hi-TECH and was wondering if this crude way of manually setting the program counter back to 0x00 is ok

Cheers,
Mike

It is not unheard of to reset the PIC from code, however, you method is unacceptable. For one, as Nigel points out, the stack remains full thus an overflow could occur.

The second point is this is a compiled language, thus the complier MUST know what's going on. Jumping to other places in assembly code without returning will likely break it since the compiler has no understanding of what the asm block does. I believe it is very ill-advised to use the PICC18's reset asm instruction directly as well. The asm reset will work at runtime, the compiler can't mess this up, but the compiler will think it needs to build code that expects to keep executing from there, which may confuse it. It may not compile as well as it could. As a minimum example, say you have this in a function, it'll build code for returning from it even though the code is unreachable.

PICC has a reset instruction. I forget whether it's capitalized or if it has a "()" at the end. But anyways, the compiler will take care of how to implement it whether your part has a reset assembly instruction or not. In any case the compiler should be fully aware what it needs to do and make optimum code. It should clear the stack out, probably TRIS the ports, jump to the start of code, and will reinitialize variables as necessary.
 
Thanks, I'll look up the reset function...I've gone back to letting the WDT timeout...I trust this clears the stack etc?
 
bsodmike said:
Thanks, I'll look up the reset function...I've gone back to letting the WDT timeout...I trust this clears the stack etc?

I would check the datasheets! - on later PIC's it continues from where it stopped (from a SLEEP instruction) when the WDT times out. Earlier devices did a soft reset from a SLEEP/WDT - I spent ages trying to get a 16F628 to wake from sleep and couldn't understand why my code wasn't working properly :lol:
 
Could you...?

Is it any chance that the reset part happens when you know that your stack is not compromised :?:

In my case, in an application I've finished just few days ago, the reset happens when the stack is empty.
 
On the 16 series chips the stack is a circular buffer and so it doesn't matter if it gets reset or not it will still work fine. I'm not sure what the emulator will make of it though.

Mike.
Edit, just tried it in the emulator and it works as expected.
 
Errr...Am I half asleep or doing a "org 0x00" will not reset anything, including the PC ? :roll:

The org "instruction" is not an instruction interpreted by the CPU, it's a pseudo-instruction that tells the assembler to place the following instructions from location 0x00.

As it was already pointed out, the RESET instruction is what you are looking for, even if I don't think that a correct way to resume from an abnormal condition is to reset everything... doing so is, I think, a bad habit... a PIC is not running on Windows

uski
 
Guys. To get the PIC to reboot you force a watchdog RESET. How?

1. Disable interupts
2. Code a goto instruction which jumps to itself.
3. In C this would look like
while(1) ;

which translates as "while 1 is TRUE execute the null statement"

Works every time
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…