Step 8 of 11
Save and restore context
Goal
Preserve WREG and STATUS on ISR entry and restore them before RETFIE.
Course minimum sequence (uses temp_w and temp_status from step 3):
On entry: MOVWF temp_w → SWAPF STATUS,W → MOVWF temp_status
On exit (before RETFIE): SWAPF temp_status,W → MOVWF STATUS → MOVF temp_w,W
Without this, main code may break after the first interrupt.
Follow these steps
- Between TMR0IF check and flag BCF, add W and STATUS save.
- Before
fin_isr, add restore in reverse order. - Build with no errors.
- More detail: context guide and theory.
isr_alta:
BTFSS INTCON, TMR0IF
GOTO fin_isr
MOVWF temp_w
SWAPF STATUS, W
MOVWF temp_status
BCF INTCON, TMR0IF
SWAPF temp_status, W
MOVWF STATUS
MOVF temp_w, W
fin_isr:
RETFIE
Checklist before you continue
- I save WREG and STATUS on ISR entry.
- I restore WREG and STATUS before RETFIE.
- Builds with no errors.
Tip: Never put code between restoring STATUS and RETFIE — STATUS must be correct on return.