Return from Intterupt - I'm using PIC16F877A C language for my project

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
im doing my final year project and need help about a part in interrupt . I'm using PIC16F877A for my project . I'm using C language for programming and MikroC as my compiler . I'm trying to find a way so that when the microcontroller exits the interrupt service routine , it should go back to the starting of the main program rather than return to the address in the main program where the stack pointer is showing . In other words , the program counter should reset to the beginning of the main program .

IS there any way to do this ? if yes, please share. Thank you!
 

jpanhalt

Joined Jan 18, 2008
11,087
Are you wedded to that chip? If you can use an enhanced mid-range that has STKPTR, then it is relatively easy.

Here is a related thread from another forum on that point: http://www.electro-tech-online.com/threads/erasing-return-address-of-call.145388/#post-1228915

I only do assembly and have seen the need to return to a known point, but it really is not easy, unless you can access the stack.

John

Edit: An alternative is to set a flag in the interrupt and then check that flag in routines that are not stable to an interrupt (e.g., something that depends on an outside timed event).
 
Last edited:

dannyf

Joined Sep 13, 2015
2,197
What you are trying to do can be done. But it is difficult to do, especially in a high level language like C.

However, a better question for you is why do you want to or have to do that? Countless mcus work in the field flawlessly without your kind of return mechamism.

If I were you, I would seriously rethink about your approach.
 

Papabravo

Joined Feb 24, 2006
21,159
Only way I know is to keep interrupts disabled and force a watchdog RESET. This cleans up the stack and all the peripherals as well.
 

Picbuster

Joined Dec 2, 2013
1,047
im doing my final year project and need help about a part in interrupt . I'm using PIC16F877A for my project . I'm using C language for programming and MikroC as my compiler . I'm trying to find a way so that when the microcontroller exits the interrupt service routine , it should go back to the starting of the main program rather than return to the address in the main program where the stack pointer is showing . In other words , the program counter should reset to the beginning of the main program .

IS there any way to do this ? if yes, please share. Thank you!
set flag in interrupt when it returned to originated point pickup flag and go to main.
 

John P

Joined Oct 14, 2008
2,025
I was going to say what Papabrovo says--what you're trying to do is equivalent to resetting the processor. But as Dannyf said, what's the point of this? Nobody else in the world has a need for it.

Come to think of it, how about hara-kiri? Maybe the processor can trigger its own reset pin via an external output. I've never tried it, but I don't see why it wouldn't work.
 

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
thanks guys for the comments, if that would be difficult then i would prefer to stay away haha. but could i use a pushbutton to jump to a specific loop in a program with many loops , for example if rb0=1 jump to main while loop.
 

jpanhalt

Joined Jan 18, 2008
11,087
Yes, at least in Assembly that is possible. For example, I use a push button on a project to force re-calibration. I see no reason that can't be done in C.

John
 

dannyf

Joined Sep 13, 2015
2,197
could i use a pushbutton to jump to a specific loop in a program with many loops , for example if rb0=1 jump to main while loop.
Yes. But no of that requires returning to a particular point from the interrupt.

Whenever you run into something that's "unnatural" to a chip, you may want to rethink your approach.
 

nsaspook

Joined Aug 27, 2009
13,081
thanks guys for the comments, if that would be difficult then i would prefer to stay away haha. but could i use a pushbutton to jump to a specific loop in a program with many loops , for example if rb0=1 jump to main while loop.
What you are asking for is an unstructured goto spaghetti producer.
That's almost universally a bad thing in programming unless its for a fatal error handler.

For C you could use the structured goto statement called a 'switch' with a simple interrupt or event driven finite-state machine to design good embedded software.
http://www.barrgroup.com/Embedded-Systems/How-To/State-Machines-Event-Driven-Systems
 
Top