;************************************************************************
;                                                                       *
;   Filename:      BA_L7-Wakeup.asm                                     *
;   Date:          9/6/09                                               *
;   File Version:  1.3                                                  *
;                                                                       *
;   Author:        David Meiklejohn                                     *
;   Company:       Gooligum Electronics                                 *
;                                                                       *
;************************************************************************
;                                                                       *
;   Architecture:  Baseline PIC                                         *
;   Processor:     12F508/509                                           *
;                                                                       *
;************************************************************************
;                                                                       *
;   Files required: stdmacros-base.inc   (provides DbnceHi macro)       *
;                                                                       *
;************************************************************************
;                                                                       *
;   Description:    Lesson 7, example 2a                                *
;                                                                       *
;   Demonstrates wake up on change                                      *
;                                                                       *
;   Turn on LED, debounce then wait for button press                    *
;   turn off LED, debounce, then sleep                                  *
;                                                                       *
;************************************************************************

    list        p=12F509 
    #include    <p12F509.inc>
    #include    <stdmacros-base.inc>    ; DbcneHi - debounce switch, wait for high

    radix       dec


;***** CONFIGURATION
                ; int reset, no code protect, no watchdog, 4MHz int clock
    __CONFIG    _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC

; pin assignments
    #define     LED     GPIO,1      ; indicator LED on GP1
    constant    nLED=1              ;   (port bit 1)
    #define     BUTTON  GPIO,3      ; pushbutton on GP3


;************************************************************************
RESET   CODE    0x000           ; effective reset vector
        movwf   OSCCAL          ; update OSCCAL with factory cal value 


;***** MAIN PROGRAM

;***** Initialisation
start
        movlw   ~(1<<nLED)      ; configure LED pin (only) as an output
        tris    GPIO
        movlw   b'01000111'     ; configure wake-up on change and Timer0:
                ; 0-------          enable wake-up on change (NOT_GPWU = 0)
                ; --0-----          timer mode (T0CS = 0)
                ; ----0---          prescaler assigned to Timer0 (PSA = 0)
                ; -----111          prescale = 256 (PS = 111)
        option                  ;   -> increment every 256 us

;***** Main code
        bsf     LED             ; turn on LED

        DbnceHi BUTTON          ; wait for stable button high
                                ;   (in case restarted after button press)
waitlo  btfsc   BUTTON          ; wait for button press (low)
        goto    waitlo

        bcf     LED             ; turn off LED

        DbnceHi BUTTON          ; wait for stable button release

        sleep                   ; enter sleep mode


        END