Ir al contenido

Paso 8 de 11

Guardar y restaurar contexto

Objetivo

Preservar WREG y STATUS al entrar a la ISR y restaurarlos antes de RETFIE.

Secuencia minima del curso (usa las variables temp_w y temp_status del paso 3):

Al entrar: MOVWF temp_wSWAPF STATUS,WMOVWF temp_status

Al salir (antes de RETFIE): SWAPF temp_status,WMOVWF STATUSMOVF temp_w,W

Sin esto, el programa principal puede fallar despues de la primera interrupcion.

  1. Entre la comprobacion de TMR0IF y el BCF de la bandera, agrega guardado de W y STATUS.
  2. Antes de fin_isr, agrega la restauracion en orden inverso.
  3. Compila sin errores.
  4. Detalle extra: guia contexto y teoria.

					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
				
  • Guardo WREG y STATUS al entrar a la ISR.
  • Restauro WREG y STATUS antes de RETFIE.
  • Compila sin errores.

Consejo: Nunca pongas codigo entre restaurar STATUS y RETFIE — STATUS debe quedar correcto al volver.