;************************************************************************
;                                                                       *
;   Filename:      BA_L7-WDTdemo.asm                                    *
;   Date:          10/6/09                                              *
;   File Version:  1.3                                                  *
;                                                                       *
;   Author:        David Meiklejohn                                     *
;   Company:       Gooligum Electronics                                 *
;                                                                       *
;************************************************************************
;                                                                       *
;   Architecture:  Baseline PIC                                         *
;   Processor:     12F508/509                                           *
;                                                                       *
;************************************************************************
;                                                                       *
;   Files required: delay10.asm         (provides W x 10ms delay)       *
;                   stdmacros-base.inc  (provides DelayMS macro)        *
;                                                                       *
;************************************************************************
;                                                                       *
;   Description:    Lesson 7, example 3a                                *
;                                                                       *
;   Demonstrates use of watchdog timer                                  *
;                                                                       *
;   Turn on LED for 1s, turn off, then enter endless loop               *
;   LED stays off if watchdog not enabled, flashes if WDT set to 2.3s   *
;                                                                       *
;************************************************************************

    list        p=12F509 
    #include    <p12F509.inc>
    #include    <stdmacros-base.inc>    ; DelayMS - delay in milliseconds

    radix       dec


;***** EXTERNAL LABELS
    EXTERN      delay10_R       ; W x 10ms delay


;***** CONFIGURATION
    #define     WATCHDOG        ; define to enable watchdog timer

    IFDEF WATCHDOG
                    ; ext reset, no code protect, watchdog, 4MHz int clock
        __CONFIG    _MCLRE_ON & _CP_OFF & _WDT_ON & _IntRC_OSC
    ELSE
                    ; ext reset, no code protect, no watchdog, 4MHz int clock
        __CONFIG    _MCLRE_ON & _CP_OFF & _WDT_OFF & _IntRC_OSC
    ENDIF

; pin assignments
    #define     LED     GPIO,1      ; indicator LED on GP1
    constant    nLED=1              ;   (port bit 1)


;************************************************************************
RESET   CODE    0x000           ; effective reset vector
        movwf   OSCCAL          ; update OSCCAL with factory cal value 
        pagesel start
        goto    start           ; jump to main program

;***** Subroutine vectors
delay10                         ; delay W x 10ms
        pagesel delay10_R
        goto    delay10_R   
    

;***** MAIN PROGRAM
MAIN    CODE

;***** Initialisation
start
        movlw   1<<PSA | b'111'     ; configure watchdog timer:
                                    ;   prescaler assigned to WDT (PSA = 1)
                                    ;   prescale = 128 (PS = 111)
        option                      ;   -> WDT period = 2.3 s

        movlw   ~(1<<nLED)          ; configure LED pin (only) as an output
        tris    GPIO

;***** Main code
        bsf     LED                 ; turn on LED

        DelayMS 1000                ; delay 1s

        bcf     LED                 ; turn off LED

        goto    $                   ; wait forever


        END


