RETFIE Control Flow

Return from an interrupt and enable interrupts again.

SyntaxRETFIE
Operandsnone
Operationtop of stack → PC, 1 → GIE
Status bitsNone
Words / cycles1 word, 2 cycles
Encoding00 0000 0000 1001

How it works

When an interrupt is taken, the PIC clears GIE, pushes the return address and jumps to address 0x0004. The interrupt routine ends with RETFIE, which pops the address and sets GIE again in one step.

Clear the interrupt flag that caused the interrupt (T0IF, INTF, RBIF...) before RETFIE, or the interrupt fires again immediately.

Example

; RETFIE example from picsimulator.com/instructions/retfie.html
    list p=16f84a
    include <p16f84a.inc>

    org 0
    goto start
    org 4             ; interrupt vector
    incf 0x20, f      ; count interrupts
    bcf INTCON, INTF  ; clear the flag
    retfie            ; return, GIE = 1 again
start:
    clrf 0x20
    bsf INTCON, INTE  ; enable the RB0/INT interrupt
    bsf INTCON, GIE
    bsf INTCON, INTF  ; set the flag in software: the interrupt is taken
wait:
    goto wait

The routine runs once and 0x20 becomes 1. Clicking RB0 in the GPIO widget triggers it again on a rising edge.

Open this example in the simulator

Watch out for

  • Save W and STATUS at the start of the routine and restore them before RETFIE (with SWAPF for STATUS), or the main program sees them change unexpectedly.

Related instructions