Electronic – Returning from interrupts at a different location from where the interrupt occurs. (PIC16F877A)

cinterruptsmicrocontrollerpic

Is there a way to return from interrupts at a different location from where the interrupt occurred in Microchip PIC16F877A?

I have a problem where I need my code restarts at a specific point always an external interrupt occurs and the ISR is just finished.

I'm thinking in use in-line assembly instructions in my C code to get the address I want to return and to change the top of stack to that address, but I guess the PIC16F877A doesn't support that.

Best Answer

You are correct, PICs before PIC18 didn't give a way to change the top of the stack.

You are asking one of the classic questions which has caused me to avoid PIC's, and especially everything pre-PIC18 like the proverbial plague. wikipedia PIC stack

It is a perfectly normal thing to do, providing you understand what you are doing, and not illogical. It is a core mechanism for multi-tasking, or muti-process operating systems.

The normal reason for doing something like that is to do an operating system (OS) context switch. This would enable software to create the illusion of many concurrent processes. When an interrupt has been serviced, the OS might not want to return to the interrupted process, but may need to run a different piece of code. Part of the mechanism is to change the stack pointer to the stack of a different process, and then execute the return from interrupt.

See AN818 Manipulating the Stack of the PIC18 Microcontroller for how to change the return address. Note it is a PIC18 document.

Sadly AFAICT that PIC is not capable of doing that.

An inferior alternative technique is to have your main control loop check a variable to detect when it needs to get out, and execute that other code.

Is there a way to force a RESET?