PIC16F913/914/916/917/946
16.3.4
CONTEXT SAVING DURING
INTERRUPTS
During an interrupt, only the return PC value is saved
on the stack. Typically, users may wish to save key
registers during an interrupt (e.g., W and STATUS
registers). This must be implemented in software.
Since the lower 16 bytes of all banks are common in the
PIC16F91X/946 (see Figure 2-3), temporary holding
registers, W_TEMP and STATUS_TEMP, should be
placed in here. These 16 locations do not require
banking and therefore, make it easier to context save
and restore. The same code shown in Example 16-1
can be used to:
• Store the W register
• Store the STATUS register
• Execute the ISR code
• Restore the STATUS register (Bank Select bits)
• Restore the W register
Note:
The microcontroller does not normally
require saving the PCLATH register
unless it is modified in code either directly
or via the pagesel macro. Then, the
PCLATH register must be saved at the
beginning of the ISR, managed for CALLs
and GOTOs in the ISR and restored when
the ISR is complete to ensure correct
program flow.
EXAMPLE 16-1:
SAVING STATUS AND W REGISTERS IN RAM
MOVWF
SWAPF
CLRF
MOVWF
:
W_TEMP
STATUS,W
STATUS
;Copy W to TEMP register
;Swap status to be saved into W
;bank 0, regardless of current bank, Clears IRP,RP1,RP0
;Save status to bank zero STATUS_TEMP register
STATUS_TEMP
:(ISR)
:
;Insert user code here
SWAPF
STATUS_TEMP,W
;Swap STATUS_TEMP register into W
;(sets bank to original state)
;Move W into STATUS register
;Swap W_TEMP
MOVWF
SWAPF
SWAPF
STATUS
W_TEMP,F
W_TEMP,W
;Swap W_TEMP into W
© 2007 Microchip Technology Inc.
DS41250F-page 233