Datasheet

© 2007 Microchip Technology Inc. DS41250F-page 233
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
EXAMPLE 16-1: SAVING STATUS AND W REGISTERS IN RAM
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.
MOVWF W_TEMP ;Copy W to TEMP register
SWAPF STATUS,W ;Swap status to be saved into W
CLRF STATUS ;bank 0, regardless of current bank, Clears IRP,RP1,RP0
MOVWF STATUS_TEMP ;Save status to bank zero STATUS_TEMP register
:
:(ISR) ;Insert user code here
:
SWAPF STATUS_TEMP,W ;Swap STATUS_TEMP register into W
;(sets bank to original state)
MOVWF STATUS ;Move W into STATUS register
SWAPF W_TEMP,F ;Swap W_TEMP
SWAPF W_TEMP,W ;Swap W_TEMP into W