;CAR ALARM EXTENSION - written by Geert Van Espen
;================================================
;
;This program is for use with the corresponding schematic diagram which can be
;found elsewhere on this site.
;
;Introduction
;------------
;This program demonstrates the wake-up on input change feature of the PIC16C84
;or PIC16F84. Port B pins RB4 - RB7 can be configured as inputs with internal
;pull up resistors, also the interrupt associated with the change on input
;on RB4 - RB7 can be set up to wake the chip from sleep. If the 
;global interrupt is enabled just before sleep, the program will vector to
;the interrupt vector (0004). If not the chip will continue execution
;just after the next instruction following sleep.
;In this example code, the port B is initalized to input 2 situations wich
;can happen in a simple car alarm system: the alarm on-off situation and
;the alarm situation (horn or other device is working). These situations
;are reflected at ports RB4 - RB5. RA0 is configured as output and drives
;a relay which in turn can drive some device to inform the user i.e. the
;car's turning lights. RA1 is also configured as an output to drive the
;status LED. This LED is for debugging purposes and works like this:
; - flashes twice when connecting the device to power, to confirm correct
;   functioning
; - flashes once if input RB4 (horn) changes from low to high
; - flashes twice if input RB4 (horn) changes from high to low
; - goes on and off when in the 90 seconds alarm_count time-out mode
;     (see below)
; - flashes briefly when the alarm count is incremented
;
;RA0 is output to the relay for the turning signals.
;RA1 is output to the status LED.
;
;RB4 is input from horn
;RB5 is input from alarm on-off switch (DIS switch on Velleman alarm kits)
;
;There is two extra RA2 - RA3 outputs which are not really necessary but
;just for extra information: these two outputs can be connected to a
;speaker (with 50 nF capacitor in series) which will play a melody when the
;relay goes on and off.
;
;Working
;-------
;When there is an alarm, this is counted. The alarm horn will go on and off
;for about 1 minute on most alarm systems, but this will only be counted as
;one alarm event. After 90 seconds, if another alarm goes off, this will be
;counted as the second one and so on.
;
;The device is programmed so that it works most of the time in sleep mode thus
;consuming very few power. When using a low-drop low-quescing-current voltage
;regulator (like LP2950 or 4805CV) the power consumpion from the car's battery
;should be below 1 mA most of the time.
;Sleep is executed. When any input change occurs, the processor wakes
;up and jumps to the interrupt vector. The appropriate action is than taken.
;See corresponding schematic diagram.
;
;
;You can use a home-made programmer to program this code into the
;PIC16F84.
;Use TOPIC -RG ALARMEXT.HEX as command for programming.
;
;       Program:          ALARMEXT.ASM 
;       Revision Date:    25-05-2001
;       First release:    14-05-2001
;
;******************************************************************************
;
; PSEUDO code
;
; important variables
;   dis = 0
;   alarm_count = 0
;
;
; LUS:
;         enable timer interrupts, timer interval 0.5 second
;         time = 0
;         verwerk_inputs
;         do until time = 90
;           if alarm, do not break
;           if DIS, break
;         enddo
;         verwerk_inputs
;         disable timer interrupts
;         time = 0
;         SLEEP
;         goto LUS
;;
; verwerk_inputs
;         if dis != previous_dis
;           previous_dis = dis
;           if dis = 1, read out alarm_count, alarm_count = 0
;           if dis = 0, kojak once, alarm_count = 0
;;
;
; read out alarm_count
;         delay 1 sec
;         read out alarm_count
;
; timer interrupt
;         timing = 1
;;
; input change interrupt
;         if RB4 (alarm) and dis = 1, ignore
;         if RB4 (alarm) and not timing, increment alarm_count
;         dis = RB5 (dis)
;
  LIST P=16C84

; define DEBUG for debug mode, or define NODEBUG for no debug mode
#define NODEBUG



; define KOJAKMODE if device is used with sound information device, or
; define NOKOJAKMODE if device is used with turning lights
#define KOJAKMODE



;
z       equ     2               ;
RBPU    equ     7               ;
temp    equ     10h             ;

teller  equ     11h             ;
timing  equ     12h             ;
InTimer equ     13h             ;

melodySelect equ 14h            ;
tempVar equ     15h             ;

INPUT1  equ     16h             ;
INPUT1P equ     17h             ;
INPUT1D equ     18h             ;

KojakCnt equ    19h             ;
TimeoutCntInner equ 1ah         ;
TimeoutCntOuter equ 1bh         ;

dis     equ     1ch             ;

INPUT2  equ     1dh             ;
INPUT2P equ     1eh             ;
INPUT2D equ     1fh             ;

DISP2   equ     20h             ;
In2TO   equ     21h             ;

freq    equ     22h             ; passes frequency value to beep
duratn  equ     23h             ; passes duration value to beep
d_hi    equ     24h             ; temporary counter (high byte of duration)
d_lo    equ     25h             ; temporary counter (low byte of duration)
tgl     equ     26h             ; temporary variable
f_temp  equ     27h             ; temporary counter (frequency)
t_pat   equ     28h             ; temporary variable

w_safe  equ     29h             ;
status_safe equ 2ah             ;

alarm_count equ 2bh             ;



OptionReg equ   1h              ;
F       EQU     1               ;
;
;
        include "p16c84.inc"
;
        org     0               ;
        goto    init            ;
;
        org     4               ;
        goto    ServiceInterrupt;

;
;
;
ServiceInterrupt
        btfsc   INTCON, RBIF    ; change on rb int?
        goto    RBWakeUpService ; yes then service
        btfsc   INTCON, T0IF    ; timer int?
        goto    TimerService    ; yes then service

; we are now on INT interrupt
        bcf     INTCON, INTE    ; clear TMR0 int mask
        bcf     INTCON, INTF    ; clear flag
        retfie                  ; return from int interrupt

TimerService
;  we are now on timer interrupt: indicate that we did service!
        bcf     INTCON, T0IE    ; clear TMR0 int mask
        bcf     INTCON, T0IF    ; clear flag

        movwf   w_safe          ; save context
        swapf   STATUS, w       ;
        movwf   status_safe     ;

        bsf     InTimer, 0      ; indicate that we are in timer interrupt

        swapf   status_safe, w  ; restore context
        movwf   STATUS          ;
        swapf   w_safe, f       ;
        swapf   w_safe, w       ;


        retfie                  ; return from timer interrupt
;
; This routine checks which input is changed and stores it
; into the corresponding variables.
; This service is the only one that can wake up the processor
; from sleep mode.
RBWakeUpService
        bcf     INTCON, RBIE    ; clear mask
        bcf     INTCON, RBIF    ; clear flag


        movwf   w_safe          ; save context
        swapf   STATUS, w       ;
        movwf   status_safe     ;

        bcf     INTCON, RBIE    ; clear mask
        bcf     STATUS, RP0     ; select page 0
        movf    PORTB, W        ; read PORTB
        bcf     INTCON, RBIF    ; clear flag
        call    delay3          ; do de-bounce for 32mSecs
        movf    PORTB, W        ; read port B again
        andlw   B'00110000'     ; mask outputs
        movwf   temp            ; save in temp
        swapf   temp, W         ; switch low and high
        movwf   INPUT1D         ;
        movwf   INPUT2D         ;
        rrf     INPUT2D, f      ;
        movlw   1               ;
        andwf   INPUT1D, f      ;
        andwf   INPUT2D, f      ;


        btfsc   INPUT2D, 0      ;
        goto    no_alarm        ;
        btfss   INPUT1D, 0      ;
        goto    no_alarm        ;
        btfsc   timing, 0       ;
        goto    no_alarm        ;
        bsf     timing, 0       ; extra debounce
        incf    alarm_count, f  ;
        movlw   0f              ;
        andwf   alarm_count, f  ; limit alarm count

        bsf     PORTA, 1        ; status LED flash to
        call    delay3          ;   indicate that alarm_count
        bcf     PORTA, 1        ;   has been incremented

no_alarm

        swapf   status_safe, w  ; restore context
        movwf   STATUS          ;
        swapf   w_safe, f       ;
        swapf   w_safe, w       ;


        retfie                  ; return from interrupt
;
;
;
;**********************************************************************
;*                                                                    *
;*     InitPorts                                                      *
;*                                                                    *
;*     Function: this function initializes PortA and PortB.           *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************
; This sub-routine initializes PortA and PortB.
InitPorts
        clrf    PORTA           ;
        bsf     STATUS, RP0     ; select bank 1
        movlw   0               ;
        movwf   TRISA           ; set port a as outputs
        clrf    PORTB           ;
        movlw   B'00110000'     ; RB0-RB3, 6, 7 outputs
        movwf   TRISB           ; RB4-RB5 inputs
        bsf     OptionReg, RBPU ; disable pull up
        bcf     STATUS, RP0     ; select page 0
        clrf    PORTB           ; init port B
        clrf    PORTA           ; make port a all low
        bcf     INTCON, RBIE    ; disable mask
        movf    PORTB, W        ; read port
        bcf     INTCON, RBIF    ; clear flag
        bsf     INTCON, RBIE    ; enable mask
        retfie                  ; enable global and return
;
;
;
;
;
;
;**********************************************************************
;*                                                                    *
;*     delay                                                          *
;*                                                                    *
;*     Function: waits for approx 1 sec using TMR0 interrupts, at     *
;*     fosc speed of 2 MHz.                                           *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************
delay
        call    delay2          ;
        call    delay2          ;
        return                  ;



;**********************************************************************
;*                                                                    *
;*     delay2                                                         *
;*                                                                    *
;*     Function: waits for approx 0.5 sec using TMR0 interrupts, at   *
;*     fosc speed of 2 MHz.                                           *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************
delay2
        movlw   4c              ;
        movwf   teller          ;
delay2_lus
        bcf     InTimer, 0      ; clear flag
        bsf     INTCON, GIE     ; globally allow interrupts
        bsf     INTCON, T0IE    ; enable mask
CheckAgain2
        btfss   InTimer, 0      ; timer overflowed?
        goto    CheckAgain2     ; no check again
        decfsz  teller, f       ;
        goto    delay2_lus      ;
        return                  ;



; delay3: very short delay
delay3
        movlw   0               ;
#ifdef DEBUG
        movlw   1               ;
#endif
        movwf   teller          ;
dd3     nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        nop                     ;
        decfsz  teller, f       ;
        goto    dd3             ;
        return                  ;



;**********************************************************************
;*                                                                    *
;*     KOJAK                                                          *
;*                                                                    *
;*     Function: fires off the turning signals or siren x times (x in *
;*     w register).                                                   *
;*                                                                    *
;*                                                                    *
;*     params: w, number of times to flash                            *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************
; this function fires off the turning signals x times (x in w register)
KOJAK
        movwf   KojakCnt        ; w in count register
        clrf    melodySelect    ;
        movlw   1               ;
        subwf   KojakCnt, w     ;
        btfss   STATUS, Z       ;
        bsf     melodySelect, 0 ;
        call    delay2          ;
KojakLus
#ifdef KOJAKMODE
        bsf     PORTA, 0        ; power to the amplifier
#endif
        call    delay2          ;
        bsf     PORTA, 0        ; relay turning signals ON
;        call    delay           ;
        btfss   melodySelect, 0 ;
        goto    KojakMelo2      ;
        call    Melodie1        ;
        goto    KojakMeloSam    ;
KojakMelo2
        call    Melodie2        ;
KojakMeloSam
#ifdef KOJAKMODE
#else
        bcf     PORTA, 0        ; relay turning signals OFF
#endif
        decfsz  KojakCnt, f     ; loop
        goto    KojakLus        ;
#ifdef KOJAKMODE
        bcf     PORTA, 0        ; power off (amplifier)
#endif
        return                  ;
;
;
;**********************************************************************
;*                                                                    *
;*     beep                                                           *
;*                                                                    *
;*     Function: beeps a speaker which is connected between RA2 and   *
;*     RA3 (with 50 nF capacitor in between). Upon entry, the         *
;*     variables duratn and freq must be filled with duration and     *
;*     frequency.                                                     *
;*                                                                    *
;*                                                                    *
;*     params: duratn and freq, see description                       *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************
;
; The beep routine beeps a speaker which is connected between
; RA2 and RA3 (with 50nF capacitor in between). Upon entry,
; the variables duratn and freq must be filled with duration and
; frequency.
beep    bcf     PORTA, 2        ; start with alternating pattern
        bsf     PORTA, 3        ;
        movlw   0fc             ; all ones to invert all bits in port A
        movwf   t_pat           ;
        movf    freq, w         ;
        movwf   f_temp          ;
        movf    freq, f         ;
        btfsc   STATUS, Z       ; if freq is zero, fill pattern with
                                ; zeros
        clrf    t_pat           ; ..for a silent pause
        movf    duratn, w       ; variable duratn goes into the
        movwf   d_hi            ; high byte
        clrf    d_lo            ; ..of a 16-bit counter
        movf    t_pat, w        ;
        movwf   tgl             ;
beeplus movf    tgl, w          ;
        xorwf   PORTA, f        ; XORing the speaker bits with 1s
                                ; inverts them
        nop                     ; zeros have no effect
        nop                     ; nops pad main loop to 20µs
        nop                     ;
        decfsz  f_temp, f       ; when f_temp reaches zero,
        goto    noRoll1         ;
        movf    freq, w         ; ..reload the freq. value and put
        movwf   f_temp          ; ..tgl_pat into tgl in order to
        movf    t_pat, w        ; ..invert the speaker bits on the
        movwf   tgl             ; ..next loop
dur_lo  movlw   1               ; decrement low byte of duration
        subwf   d_lo, f         ;
        btfsc   STATUS, C       ; if no borrow, go to noRoll2
        goto    noRoll2         ;
        decfsz  d_hi, f         ; decrement the high byte of duration
        goto    beeplus         ; ..and if overflow, routine is done
        bcf     PORTA, 2        ; no volts on speaker when leaving..
        bcf     PORTA, 3        ;
        return                  ;
noRoll1 clrf    tgl             ; if f_temp is not zero, don't
        goto    dur_lo          ; ..pluck the speaker
noRoll2 nop                     ; waste time to ensure that all
        nop                     ; ..paths through the routine take 20µs
        goto    beeplus         ;



;**********************************************************************
;*                                                                    *
;*     KNIPPER1                                                       *
;*                                                                    *
;*     Function: blinks status led once.                              *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************
KNIPPER1
        bcf     PORTA, 1        ;
        call    delay3          ;
        bsf     PORTA, 1        ; light STATUS led
        call    delay3          ;
        bcf     PORTA, 1        ; dim STATUS led
        return                  ;        

;
;**********************************************************************
;*                                                                    *
;*     KNIPPER2                                                       *
;*                                                                    *
;*     Function: blinks status led twice.                             *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************
KNIPPER2
        bcf     PORTA, 1        ;
        call    delay3          ;
        bsf     PORTA, 1        ; light STATUS led
        call    delay3          ;
        bcf     PORTA, 1        ; dim STATUS led
        call    delay3          ;
        bsf     PORTA, 1        ; light STATUS led
        call    delay3          ;
        bcf     PORTA, 1        ; dim STATUS led
        return                  ;        

;
;**********************************************************************
;*                                                                    *
;*     KNIPPER3                                                       *
;*                                                                    *
;*     Function: blinks status led three times.                       *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************
KNIPPER3
        call    KNIPPER1        ;
        call    KNIPPER2        ;
        return                  ;        

;
;**********************************************************************
;*                                                                    *
;*     KNIPPER4                                                       *
;*                                                                    *
;*     Function: blinks status led 4 times.                           *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************

; this function makes the STATUS led flash four times
KNIPPER4
        call    KNIPPER2        ;
        call    KNIPPER2        ;
        return                  ;        



;**********************************************************************
;*                                                                    *
;*     Melodie1                                                       *
;*                                                                    *
;*     Function: plays a melody.                                      *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************

Melodie1
        movlw   d'70'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'60'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'50'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'60'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'70'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        return                  ;

;**********************************************************************
;*                                                                    *
;*     Melodie2                                                       *
;*                                                                    *
;*     Function: plays a melody.                                      *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************

Melodie2
        movlw   d'23'           ;
        movwf   freq            ;
        movlw   d'18'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'47'           ;
        movwf   freq            ;
        movlw   d'8'            ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'27'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'47'           ;
        movwf   freq            ;
        movlw   d'8'            ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        return                  ;



;**********************************************************************
;*                                                                    *
;*     Melodie3                                                       *
;*                                                                    *
;*     Function: plays a melody.                                      *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************

Melodie3
        movlw   d'43'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'35'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        movlw   d'27'           ;
        movwf   freq            ;
        movlw   d'11'           ;
        movwf   duratn          ;
        call    beep            ;
        call    delay3          ;
        return                  ;


;**********************************************************************
;*                                                                    *
;*     VerwerkInput                                                   *
;*                                                                    *
;*     Function: this function checks if inputs have changed.         *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************

VerwerkInput
; check if inputs have changed
; input1 = RB4 (horn)
        movf    INPUT1P, w      ; check if input1 has changed
        subwf   INPUT1D, w      ;
        btfss   STATUS, Z       ;
        goto    not_equal1      ;
        goto    ddd2            ;
not_equal1
        movf    INPUT1D, w      ; previous = current
        movwf   INPUT1P         ;
        btfss   INPUT1D, 0      ;
        goto    ddd1_1          ;
        ;call    KNIPPER1        ;
        goto    ddd1_2          ;
ddd1_1
        ;call    KNIPPER2        ;
ddd1_2

; input2 = RB5 (DIS)
ddd2
        movf    INPUT2P, w      ; check if input2 has changed
        subwf   INPUT2D, w      ;
        btfss   STATUS, Z       ;
        goto    not_equal2      ;
        goto    ddd3            ;
not_equal2
        call    delay           ; extra debounce for input2!
        call    delay           ;
        call    delay           ;

        movf    PORTB, W        ; read port B again
        andlw   B'00110000'     ; mask outputs
        movwf   tempVar         ; save in temp
        swapf   tempVar, W      ; switch low and high
        movwf   INPUT2D         ;
        rrf     INPUT2D, f      ;
        movlw   1               ;
        andwf   INPUT2D, f      ;

        movf    INPUT2P, w      ; again check if input2 has changed
        subwf   INPUT2D, w      ;
        btfss   STATUS, Z       ;
        goto    not_equal2_2    ;
        goto    ddd3            ;
not_equal2_2
        movf    INPUT2D, w      ; previous = current
        movwf   INPUT2P         ;
        btfss   INPUT2D, 0      ;
        goto    ddd2_1          ;
        call    KNIPPER3        ;
        movf    alarm_count, f  ;
        btfss   STATUS, Z       ;
        incf    alarm_count, f  ;
        movlw   2               ; kojak 2 times + alarm_count + 1
        addwf   alarm_count, w  ;
        andlw   7               ; limit number of kojaks
        call    KOJAK           ; signal alarm put off by user
        clrf    alarm_count     ;
        bcf     timing, 0       ;
        goto    ddd2_2          ;
ddd2_1
        call    KNIPPER4        ;
        movlw   1               ;
        call    KOJAK           ; signal alarm put on by user
        clrf    alarm_count     ;
        bcf     timing, 0       ;
ddd2_2
ddd3
; end check if inputs have changed

        return                  ;





;**********************************************************************
;*                                                                    *
;*     01  Program                     parent = xx  none              *
;*                                                                    *
;*     Function: main program.                                        *
;*                                                                    *
;*     params: none                                                   *
;*     return: none                                                   *
;*                                                                    *
;**********************************************************************

init
        call    InitPorts       ; initalize ports

        bsf     INTCON, GIE     ; globally allow interrupts
        bsf     INTCON, RBIE    ; enable interrupt on change

        movf    PORTB,w         ;

        clrf    timing          ;
        clrf    dis             ;
        clrf    alarm_count     ;

        clrf    INPUT1D         ;
        clrf    INPUT2D         ;

        clrf    INPUT1P         ;
        clrf    INPUT2P         ;

        clrf    TimeoutCntInner ;
        clrf    TimeoutCntOuter ;
        clrf    InTimer         ;

; set timer
        bsf     STATUS, RP0     ; select page 1
        movlw   B'00000001'     ; fosc/8 --> TMR0
#ifdef DEBUG
        movlw   B'00000000'     ;
#endif
        movwf   OptionReg       ;
        bsf     OptionReg, RBPU ; disable pull up
        bcf     STATUS, RP0     ; select page 0
        clrf    TMR0
        bcf     INTCON, T0IF    ; clear flag
        bsf     INTCON, T0IE    ; enable mask



; self-test result on display
        call    KNIPPER2        ;
#ifdef DEBUG
        goto    end_self_test   ;
#endif
#ifdef KOJAKMODE
        bsf     PORTA, 0        ; power to the amplifier
        call    delay2          ;
#endif
        call    Melodie3        ;
#ifdef KOJAKMODE
        bcf     PORTA, 0        ; power off (amplifier)
#endif

end_self_test

        clrf    PORTB           ;
        movf    PORTB, W        ; read PORTB
        clrf    alarm_count     ;
        bcf     timing, 0       ;




start



        call   VerwerkInput     ;



;;;;;;;;;;;;;;;

; TIME OUT sequence

        btfss   INPUT1D, 0      ; is there a burglar attempt?
        goto    Timeout_af      ; no: so don't do time sequence

        bsf     timing, 0       ; signal that we are here


        movlw   2               ;
#ifdef DEBUG
        movlw   1               ;
#endif
        movwf   TimeoutCntOuter ;

TimeoutLusOuter
        movlw   0f0             ; about 90 seconds
#ifdef DEBUG
        movlw   10              ;
#endif
        movwf   TimeoutCntInner ;
TimeoutLusInner
        call    delay2          ;
        btfsc   INPUT2D, 0      ;
        goto    TimeoutLus_af   ;
        btfss   TimeoutCntInner, 0;
        goto    TO1             ;
        bsf     PORTA, 1        ; light STATUS led
        goto    TO_sam          ;
TO1     bcf     PORTA, 1        ; dim STATUS led
TO_sam
        decfsz  TimeoutCntInner, f;
        goto    TimeoutLusInner ;
        decfsz  TimeoutCntOuter, f;
        goto    TimeoutLusOuter ;
TimeoutLus_af
        call    VerwerkInput    ;
        bcf     PORTA, 1        ; dim STATUS led
Timeout_af
        bcf     PORTA, 1        ; dim STATUS led
        bcf     timing, 0       ; signal that we are done
; END TIME OUT sequence

;;;;;;;;;;;;;;;








        bcf     timing, 0       ;

        bsf     INTCON, GIE     ; globally allow interrupts
        bsf     INTCON, RBIE    ; enable interrupt on change
        ;bcf     INTCON, T0IE    ; disable timer during sleep
        sleep                   ;
        nop                     ;



        goto    start           ;

;
  end



  



