11.10.1 Interrupt Handler Example

The following example provides a Reset function and a default interrupt handler in assembly language. The default interrupt handler uses persistent data storage to keep a count of unexpected interrupts and/or error traps.

       .include "xc.inc"
       .text

       .global __reset
__reset:
       ;; takes control at device reset/power-on
       mov   #__SP_init,w15      ; initialize stack pointer
       mov   #__SPLIM_init,w0    ;  and stack limit register
       mov   w0,SPLIM          ;

       btst  RCON,#POR         ; was this a power-on reset?
       bra   z,start           ;  branch if not

       clr   FaultCount        ; else clear fault counter
       bclr  RCON,#POR         ;  and power-on bit
start:
       goto  main              ; start application

       .global __T1Interrupt
__T1Interrupt:
       ;; services timer 1 interrupts
       bclr  IFS0,#T1IF        ; clear the interrupt flag
       retfie                  ;  and return from interrupt

       .global __DefaultInterrupt
__DefaultInterrupt:
       ;; services all other interrupts & traps
       inc  FaultCount         ; increment the fault counter
       reset                   ;  and reset the device

       .section .pbss,persist  ; persistent data storage
       .global FaultCount      ;  is not affected by reset
FaultCount:
       .space 2                ; count of unexpected interrupts

The standard naming conventions for interrupt handlers are described in the sections below.

Note: The compiler requires only one leading underscore before any of the interrupt handler names. The assembler requires two leading underscores before any of the interrupt handler names. The compiler format is shown in tables in the following sections.