Microchip Technology DM164130-9 User Manual

Page of 101
Lessons
 2012 Microchip Technology Inc.
DS41628B-page 77
3.11.5
New Instructions
3.11.5.1
BOTH
3.11.5.1.1 retfie
The retfie instruction exits the ISR by popping the previous address from the stack 
and setting the GIE bit. The PIC18 requires the retfie, fast instruction to restore 
the saved context, whereas the enhanced mid-range does not have this distinction.
3.11.6
Assembly
3.11.6.1
BOTH
EXAMPLE 3-37: 
By using interrupts, the main loop can spend time doing other things such as crunching 
numbers or writing to an LCD. The program no longer needs to wait for the flag to 
become set to continue like the previous lesson did. This example code will simply 
branch to MainLoop indefinitely, doing nothing while waiting for the interrupt. 
3.11.6.2
ENHANCED MID-RANGE
EXAMPLE 3-38: 
This jumps to the ISR routine. Notice how the goto statement is directly after the inter-
rupt vector address. 
EXAMPLE 3-39: 
Inside the ISR, the cause of the interrupt is determined. Once determined, one of the 
services that must be completed is clearing the interrupt flag so that the ISR can be 
successfully left. The retfie instruction exits the ISR by restoring the saved context, 
re-enables the GIE bit and returns to the instruction following the last instruction 
executed when the interrupt occurred.
TABLE 3-35:
NEW INSTRUCTIONS FOR BOTH DEVICES
Instruction
English
Purpose
retfie
Return from interrupt
Return to normal execution
MainLoop:
    bra       MainLoop        ;can spend rest of time doing something critical here
Org 0x0                   ;Reset Vector starts at 0x0000
bra        Start          ;main code execution
ORG        0x0004         ;Interrupt Vector starts at address 0x0004
goto       ISR
;Enter here if an interrupt has occurred
;First, check what caused the interrupt by checking the ISR flags
;This lesson only has 2 flags to check
ISR:
    banksel     IOCAF           ;bank7
    btfsc       IOCAF, 3        ;check the interrupt-on-change flag
    bra         Service_SW1     ;switch was pressed
    bra         Service_TMR0    ;Timer0 overflowed