Skip to content

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_wSWAPF STATUS,WMOVWF temp_status

On exit (before RETFIE): SWAPF temp_status,WMOVWF STATUSMOVF temp_w,W

Without this, main code may break after the first interrupt.

  1. Between TMR0IF check and flag BCF, add W and STATUS save.
  2. Before fin_isr, add restore in reverse order.
  3. Build with no errors.
  4. 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
				
  • 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.