;************************************************************************
;                                                                       *
;   Filename:      BA_L3-Flash_LED-vector.asm                           *
;   Date:          9/9/07                                               *
;   File Version:  1.0                                                  *
;                                                                       *
;   Author:        David Meiklejohn                                     *
;   Company:       Gooligum Electronics                                 *
;                                                                       *
;************************************************************************
;                                                                       *
;   Architecture:  Baseline PIC                                         *
;   Processor:     12F508/509                                           *
;                                                                       *
;************************************************************************
;                                                                       *
;   Files required: none                                                *
;                                                                       *
;************************************************************************
;                                                                       *
;   Description:    Lesson 3, example 2                                 *
;                                                                       *
;   Illustrates use of subroutine vector table for long calls           *
;                                                                       *
;   Flashes a LED at approx 1 Hz, with 20% duty cycle                   *
;   LED continues to flash until power is removed                       *
;                                                                       *
;************************************************************************
;                                                                       *
;   Pin assignments:                                                    *
;       GP1 - flashing LED                                              *
;                                                                       *
;************************************************************************

    list        p=12F509      
    #include    <p12F509.inc>


;***** CONFIGURATION
                ; ext reset, no code protect, no watchdog, 4Mhz int clock
    __CONFIG    _MCLRE_ON & _CP_OFF & _WDT_OFF & _IntRC_OSC


;***** VARIABLE DEFINITIONS
        UDATA
dc1     res 1                   ; delay loop counters
dc2     res 1
dc3     res 1


;************************************************************************
RESET   CODE    0x000           ; effective reset vector
        movwf   OSCCAL          ; update OSCCAL with factory cal value 
        pagesel start
        goto    start           ; jump to main code

;***** Subroutine vectors
delay10                         ; delay W x 10ms
        pagesel delay10_R
        goto    delay10_R       


;***** MAIN PROGRAM
MAIN    CODE

;***** Initialisation
start
        movlw   b'111101'       ; configure GP1 (only) as an output
        tris    GPIO

;***** Main loop
flash
        movlw   b'000010'       ; set bit corresponding to GP1 (bit 1)
        movwf   GPIO            ; write to GPIO to turn on LED
        movlw   .20             ; stay on for 0.2s:
        pagesel delay10
        call    delay10         ;   delay 20 x 10ms = 200ms
        clrf    GPIO            ; clear GPIO to turn off LED
        movlw   .80             ; stay off for 0.8s:
        call    delay10         ;   delay 80 x 10ms = 800ms
        pagesel flash
        goto    flash           ; repeat forever


;***** Subroutines
SUBS    CODE

delay10_R                       ; delay W x 10ms
        banksel dc3             ; -> ?+1+Wx(3+10009+3)-1+4 = Wx10.015ms
        movwf   dc3             
dly2    movlw   .13             ; repeat inner loop 13 times
        movwf   dc2             ; -> 13x(767+3)-1 = 10009 cycles
        clrf    dc1             ; inner loop = 256x3-1 = 767 cycles
dly1    decfsz  dc1,f           
        goto    dly1
        decfsz  dc2,f           ; end middle loop
        goto    dly1            
        decfsz  dc3,f           ; end outer loop
        goto    dly2

        retlw   0


        END
