You are on page 1of 3

The interrupt handler, also called the interrupt service routine or

the ISR, is the code that receives control upon occurrence of the
interrupt. Most of the programming that goes into the service
routine is specific to the application; however, there are certain
housekeeping operations that should be included. The following
list describes the structure of an interrupt service routine for the
mid-range PICs:
1. Preserve the value in the w register.
2. Preserve the value of the STATUS register.
3. Execute the application-specific operations.
4. Restore the value of the STATUS register at the time of the
interrupt.
5. Restore the value of the w register at the time of the interrupt.
6. Issue the RETFIE instruction to end the interrupt handler.
In the PIC 16F84, the interrupt service routine must be located at
offset 0x004 in
code memory. A simple org directive takes care of ensuring this
location, as in the following code fragment:

Alternatively, code can place a jump at offset 0x004 and locate


the Service Routine elsewhere in the code. In this case, it is
important to remember not to call the Service Routine, but to
access it with a goto instruction. The reason is that the call
opcode places a return address in the stack, which then polls for
the retfie instruction.

11.2.1 Context Saving Operations


The only value automatically preserved by the interrupt
mechanism is PC (the Program Counter), which is stored in the
stack. Applications often need to restore the processor to the
same state as when the interrupt took place, so the first operation
of most interrupt handlers is saving the processors context. This
usually includes the w and the STATUS registers and occasionally
others used by the specific implementation.
Saving w and STATUS Registers
Saving the w and the STATUS registers requires using register
variables, but the process requires special care. Saving the w
register is simple enough: its value at the start of the Service
Routine is stored in a local variable from which it is restored at
termination. But saving the STATUS register cannot be done with
the MOVF instruction, since this instruction changes the zero flag.
The solution is to use the SWAPF instruction which does not
affect any of the flags. Of course, SWAPF inverts the nibbles in
the operand, so it must be repeated so as to restore the original
state. The following code fragment assumes that file register
variables named old_w and old_status were previously created.

You might also like