Interrupt Service subroutine
Based on the figures above, when the interrupt signal is generated, the microcontroller finishes first execution of the instruction that is currently being processed, then stores on the stack memory the address of the next instruction code and later calls the "interrupt service subroutine", beginning in the address 0x008H of code memory, for the 18F2550 microcontroller.
At the end of the interrupt service subroutine, using the statement "RETFIE" (Return From Interrupt), the program flow returns to the main program through the same mechanism used by the subroutines "CALL", ie recovering the return address previously stored on the stack.
The 18F2550 Bolt system uses the first 2K of memory to store its Bootloader code. In this program, there is a vector for redirecting the interrupt address to 0x808, so that the user has sufficient memory space to program their interrupt service subroutines. Next is an example of interrupt handling in the Bolt system, using the timer interrupt 0.
The example combines two tasks: in Task 1, the processor turns the RB7 led on and off to a period of 1 second. This task is performed by the TIMER interrupt 0. In Task 2, which is in charge of the main program, the microcontroller performs a binary count in bits RB0 ... RB3. The period of the counter in this case is 200 ms.
DO NOT FORGET TO STORE IN RAM MEMORY, W AND STATUS REGISTERS ON ENTERING THE INTERRUPT SUBROUTINE.
Once inside the interrupt subroutine, the programmer must immediately save W and STATUS registers to avoid losing its contents upon return from the subroutine. SWAPF instruction, which performs an exchange between the most significant bits and least significant of a memory location is used because its implementation does not alter the STATUS register. In the following examples, we use the locations "SALVAW" to store the W register and "SALVASTATUS" to save the STATUS register:
;SAVING W IN MEMORY.
Then we write here the service subroutine interruption, which performs a specific task. At the end of the subroutine, before returning to the main program, we retrieve the values of W and STATUS registers like this:
stored back from memory to W
;w value is stored back. STATUS is not affected