;************************************************************************
;                                                                       *
;   Filename:       BA_L4-Toggle_LED-delay.asm                          *
;   Date:           15/9/07                                             *
;   File Version:   1.0                                                 *
;                                                                       *
;   Author:         David Meiklejohn                                    *
;   Company:        Gooligum Electronics                                *
;                                                                       *
;************************************************************************
;                                                                       *
;   Architecture:   Baseline PIC                                        *
;   Processor:      12F508/509                                          *
;                                                                       *
;************************************************************************
;                                                                       *
;   Files required: delay10.asm     (provides W x 10 ms delay)          *
;                                                                       *
;************************************************************************
;                                                                       *
;   Description:    Lesson 4, example 3                                 *
;                                                                       *
;   Demonstrates use of a simple delay for debouncing                   *
;                                                                       *
;   Toggles LED when pushbutton is pressed (low) then released (high)   *
;   Uses 20 ms debounce delay                                           *
;                                                                       *
;************************************************************************
;                                                                       *
;   Pin assignments:                                                    *
;       GP1 - LED                                                       *
;       GP3 - pushbutton switch                                         *
;                                                                       *
;************************************************************************

    list        p=12F509       
    #include    <p12F509.inc>  

                ; int reset, no code protect, no watchdog, 4Mhz int clock
    __CONFIG    _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC


;***** EXTERNAL LABELS
    EXTERN      delay10_R       ; W x 10 ms delay


;***** VARIABLE DEFINITIONS
        UDATA_SHR
sGPIO   res 1                   ; shadow copy of GPIO


;************************************************************************
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 10 ms
        pagesel delay10_R
        goto    delay10_R       


;***** MAIN PROGRAM
MAIN    CODE

;***** Initialisation
start
        clrf    GPIO            ; start with LED off
        clrf    sGPIO           ;   update shadow	
        movlw   b'111101'       ; configure GP1 (only) as an output
        tris    GPIO            ; (GP3 is an input)

;***** Main loop
loop

waitdn  btfsc   GPIO,3          ; wait until button pressed (GP3 low)
        goto    waitdn

        movf    sGPIO,w
        xorlw   b'000010'       ; toggle LED on GP1
        movwf   sGPIO           ;   using shadow register
        movwf   GPIO

        movlw   .2              
        pagesel delay10
        call    delay10         ; delay 20 ms to debounce (GP3 low)
        pagesel $

waitup  btfss   GPIO,3          ; wait until button released (GP3 high)
        goto    waitup          ;   before continuing

        movlw   .2              
        pagesel delay10
        call    delay10         ; delay 20ms to debounce (GP3 high)
        pagesel $

        goto    loop            ; repeat forever


        END


