;************************************************************************
;                                                                       *
;   Filename:       BA_L4-Toggle_LED-no_db.asm                          *
;   Date:           15/9/07                                             *
;   File Version:   1.0                                                 *
;                                                                       *
;   Author:         David Meiklejohn                                    *
;   Company:        Gooligum Electronics                                *
;                                                                       *
;************************************************************************
;                                                                       *
;   Architecture:   Baseline PIC                                        *
;   Processor:      12F508/509                                          *
;                                                                       *
;************************************************************************
;                                                                       *
;   Files required: none                                                *
;                                                                       *
;************************************************************************
;                                                                       *
;   Description:    Lesson 4, example 2                                 *
;                                                                       *
;   Demonstrates the need for debouncing                                *
;                                                                       *
;   Toggles LED when pushbutton is pressed (low) then released (high)   *
;       (no debounce)                                                   *
;                                                                       *
;************************************************************************
;                                                                       *
;   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


;***** 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 


;***** MAIN PROGRAM

;***** 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

waitup  btfss   GPIO,3          ; wait until button released (GP3 high)
        goto    waitup          ;   before continuing

        goto    loop            ; repeat forever


        END

