| Electronics > Beginners |
| Sorry to interrupt ... |
| (1/7) > >> |
| PerranOak:
As I understand it, when an interrupt occurs (I’m using a PIC 16F1827) the programme counter (PC) is pushed to the stack and the programme jumps to 0004h to start the interrupt code. Programmes that I have seen always seem to use a subroutine ending in “RETFIE” so that the programme runs this then gets back to what it was doing. In my project the programme will be in a loop displaying the current result when an interrupt occurs. I then want that interrupt to jump to executing code part the way down the main loop and carry one, not acting as a subroutine at all - more of a "GOTO". Is this “allowed”? Will it cause unpredictability? What is it that triggers the return of the PC from the stack, is it “RETFIE”? Also, I note in the datasheet that the “GIE: Global Interrupt Enable bit” of INTCON0 is cleared on interrupt. Is this normal/usual? Thank you. |
| mikerj:
The RETFIE instruction re-enables interrupts and pops the last address off the stack and loads it to the program counter. It's quite possible to use a normal "RETURN" at the end of an interrupt, the only difference is that interrupts will not be automatically re-enabled. If you jump to a specific location at the end of your inetrrupt rather than executing a RETFIE/RETURN then you will need to manually "pop" the saved address from the hardware stack by decrementing the STKPTR register. If you don't do this then the stack will quickly overflow. Note this is only possible on later versions of the 14 bit PIC core (such as the 16F1827) that allows access to the stack pointer, early devices had no way of doing this. GIE is disabled when servicing an interrupt to prevent further interrupts occurring (re-entering the ISR) and again quickly filling up the stack. It's possible to re-enable interrupts whilst in the interrupt handler, but you really need to understand the implications of this. |
| langwadt:
--- Quote from: mikerj on May 31, 2018, 05:04:25 pm ---The RETFIE instruction re-enables interrupts and pops the last address off the stack and loads it to the program counter. It's quite possible to use a normal "RETURN" at the end of an interrupt, the only difference is that interrupts will not be automatically re-enabled. If you jump to a specific location at the end of your inetrrupt rather than executing a RETFIE/RETURN then you will need to manually "pop" the saved address from the hardware stack by decrementing the STKPTR register. If you don't do this then the stack will quickly overflow. Note this is only possible on later versions of the 14 bit PIC core (such as the 16F1827) that allows access to the stack pointer, early devices had no way of doing this. --- End quote --- if the mainloop just pushed something on the stack before the interrupt, then returning from the interrupt to a different place in main those will never be popped off the stack and it'll just keep growing |
| mikerj:
--- Quote from: langwadt on May 31, 2018, 05:14:38 pm --- --- Quote from: mikerj on May 31, 2018, 05:04:25 pm ---The RETFIE instruction re-enables interrupts and pops the last address off the stack and loads it to the program counter. It's quite possible to use a normal "RETURN" at the end of an interrupt, the only difference is that interrupts will not be automatically re-enabled. If you jump to a specific location at the end of your inetrrupt rather than executing a RETFIE/RETURN then you will need to manually "pop" the saved address from the hardware stack by decrementing the STKPTR register. If you don't do this then the stack will quickly overflow. Note this is only possible on later versions of the 14 bit PIC core (such as the 16F1827) that allows access to the stack pointer, early devices had no way of doing this. --- End quote --- if the mainloop just pushed something on the stack before the interrupt, then returning from the interrupt to a different place in main those will never be popped off the stack and it'll just keep growing --- End quote --- Yes, that's what I said.... |
| langwadt:
--- Quote from: mikerj on May 31, 2018, 05:19:46 pm --- --- Quote from: langwadt on May 31, 2018, 05:14:38 pm --- --- Quote from: mikerj on May 31, 2018, 05:04:25 pm ---The RETFIE instruction re-enables interrupts and pops the last address off the stack and loads it to the program counter. It's quite possible to use a normal "RETURN" at the end of an interrupt, the only difference is that interrupts will not be automatically re-enabled. If you jump to a specific location at the end of your inetrrupt rather than executing a RETFIE/RETURN then you will need to manually "pop" the saved address from the hardware stack by decrementing the STKPTR register. If you don't do this then the stack will quickly overflow. Note this is only possible on later versions of the 14 bit PIC core (such as the 16F1827) that allows access to the stack pointer, early devices had no way of doing this. --- End quote --- if the mainloop just pushed something on the stack before the interrupt, then returning from the interrupt to a different place in main those will never be popped off the stack and it'll just keep growing --- End quote --- Yes, that's what I said.... --- End quote --- you said pop the saved address, but the main loop could have pushed other things on the stack at the moment the interrupt happens if you return to a different place the code that pops those off again will never happen |
| Navigation |
| Message Index |
| Next page |