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

Discussion in 'Embedded Systems and Microcontrollers' started by rocker123uk, Mar 9, 2016.

  1. rocker123uk

    Thread Starter Member

    Dec 6, 2015
    33
    0
    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!
     
  2. jpanhalt

    Expert

    Jan 18, 2008
    6,448
    1,251
    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: Mar 9, 2016
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    2,196
    421
    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.
     
  4. Papabravo

    Expert

    Feb 24, 2006
    11,737
    2,483
    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.
     
  5. Picbuster

    Well-Known Member

    Dec 2, 2013
    818
    101
    set flag in interrupt when it returned to originated point pickup flag and go to main.
     
  6. John P

    AAC Fanatic!

    Oct 14, 2008
    1,727
    258
    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.
     
  7. rocker123uk

    Thread Starter Member

    Dec 6, 2015
    33
    0
    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.
     
  8. jpanhalt

    Expert

    Jan 18, 2008
    6,448
    1,251
    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
     
  9. dannyf

    Well-Known Member

    Sep 13, 2015
    2,196
    421
    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.
     
  10. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    4,906
    5,348
    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
     
Loading...