;;;;;;; int_tmr0 -- Uses timer0 interrupt to toggle LED RA4 once every 2.6 seconds
;;;;;;;  This is once every 65536 counts with a divide-by-64 prescale value
;;;;;;; Assembler directives ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        list  P=PIC18F452, F=INHX32, C=160, N=0, ST=OFF, MM=OFF, R=DEC, X=ON
        #include P18F452.inc
        __CONFIG  _CONFIG1H, _HS_OSC_1H  ;HS oscillator
        __CONFIG  _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_42_2L  ;Reset
        __CONFIG  _CONFIG2H, _WDT_OFF_2H  ;Watchdog timer disabled
        __CONFIG  _CONFIG3H, _CCP2MX_ON_3H  ;CCP2 to RC1 (rather than to RB3)
        __CONFIG  _CONFIG4L, _LVP_OFF_4L  ;RB5 enabled for I/O

;;;;;;; Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        cblock  0x000           ;Beginning of Access RAM
        sta_temp      ;temp storage for STATUS during interrupts
        w_temp        ;temp storage for WREG during interrupts

MOVLF   macro   literal,dest
        movlw   literal
        movwf   dest

        org  0x0000             ;Reset vector
        goto  Mainline

        org  0x0008             ;High priority interrupt vector
        goto  $                 ;Trap

        org  0x0018             ;Low priority interrupt vector
        goto inttmr0            ;handler for timer0 interrupt

;;;;;;; Mainline program ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        MOVLF  B'10001110',ADCON1  ;Enable PORTA & PORTE digital I/O pins
        MOVLF  B'11100001',TRISA   ;Set I/O for PORTA
               MOVLF  B'00010000',PORTA  ;Turn off all four LEDs driven from PORTA
        bsf    RCON,IPEN          ;2-level priority int scheme
        bsf    INTCON,GIEL        ;global enable of low priority ints
        bsf    INTCON,GIEH        ;global enable of high priority ints
        MOVLF  B'10000101',T0CON  ;configure timer0 divide_by_64
        bcf    INTCON2,TMR0IP     ;set low priority for TIMR0 int
        bsf    INTCON,TMR0IE      ;enable TIMR0 int
        bcf    INTCON,TMR0IF      ;start TMR0
xxx     goto   xxx                ;program just loops forever waiting for interrupt from timr0

inttmr0 movff  STATUS,sta_temp    ;save registers
        movwf  w_temp
        btfss  INTCON,TMR0IF      ;was it the timer0 interrupt?
        bra    done
        btg    PORTA,RA4          ;toggle LED on RA4
        bcf    INTCON,TMR0IF      ;restart TMR0
done    movf   w_temp,W           ;restore registers
        movff  sta_temp,STATUS
        retfie                    ;return from interrupt, enabling interrupts