#include <p16f690.inc>
  __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
 
  k1 equ 20   ;assign memory addresses for k1
  k2 equ 21   ;assign memory addresses for k2
  k3 equ 22   ;assign memory addresses for k3
  dum equ 23  ;dummy variable
  dum2 equ 24  ;dummy for saving status reg
  t0 equ 25  ;assign memory address for t0
  t1 equ 26  ;assign memory address for t1
  t2 equ 27  ;assign memory address for t2
  t3 equ 28  ;assign memory address for t3
  c1 equ 29  ; counter 1
  c2 equ 30  ; counter 2
 org 00

 goto Start
  org 04
  goto INT_ROUTINE

Start
 bcf   STATUS,RP0
 bcf   STATUS,RP1   ;set bank 0 for registers
 movlw b'10010000' ;enables serial port, continuous receive  
 movwf RCSTA
 bsf   STATUS,RP0  ;sets bank 1 registers 
 movlw  b'00100100' ;sets serial transfer parameters
 movwf TXSTA
 movlw b'00011001' ;sets baud rate for 9600 (spbrg = 25)
 movwf SPBRG
 bcf STATUS,RP0  ; set bank 0 for registers
; bcf PIR1,RCIF ;sets receive interupt flag
 movlw b'10100000' ; enables trm0 interupt
 movwf INTCON 
 bsf STATUS,RP0; sets bank 1
 movlw b'00000000'
 movwf OPTION_REG; fastest timer rate
 bcf STATUS,RP0; set bank 0
 clrf PORTA  ;  init portA
 bsf STATUS, RP1  ; sets bank 2
 movlw b'00000011'
 movwf ANSEL  ; make pins an0 and an1 analog, all others digital
 bcf STATUS, RP1  ;
 bsf STATUS, RP0  ; sets bank 1
 movlw b'00000011'
 movwf TRISA  ; makes pins an0 and an1 input, all others output
 clrf TRISC   ; makes pins for port C all output pins
 bcf STATUS, RP0
 bsf STATUS, RP1 ; sets bank 2
 movlw b'10000000'  ; sets up comparitor
 movwf CM1CON0   ; comparitor c1
 bcf STATUS, RP1 ; set bank 0
 clrf PORTC ; init portc
 clrf TMR0  ; init timer
 movwf k1   ; start k1 at 0
 movwf k2   ; start k2 at 0
 movwf k3   ; start k3 at 0

loop
  bcf STATUS, RP0
  bsf STATUS, RP1  ; set bank 2
  btfss CM1CON0,C1OUT  ;repeat until pin C1OUT goes high 
  goto$-1
  bcf STATUS, RP1  ; set bank 0
  movf TMR0,W; load clock into w
  movwf t0;  move tmr0 into t0
  movf k1,W
  movwf t1 ; move k1 into t1
  movf k2,W
  movwf t2  ; move k2 into t2
  movf k3,W 
  movwf t3  ; move k3 into t3
  movf t0,W ; move t0 back into W
  bsf STATUS,RP0 ;set bank 1 registers
  btfss TXSTA,TRMT ;check if txreg is empty
  goto $-1
  bcf STATUS,RP0 ;set bank 0 registers
  movwf TXREG ;put W data into txreg
  movf t1,W ;load counter into w
  bsf STATUS,RP0 ;set bank 1 registers
  btfss TXSTA,TRMT ;check if txreg is empty
  goto $-1
  bcf STATUS,RP0 ;set bank 0 registers
  movwf TXREG ;put W data into txreg
  movf t2,W ;load counter k2 into w
  bsf STATUS,RP0 ;set bank 1 registers
  btfss TXSTA,TRMT ;check if txreg is empty
  goto $-1
  bcf STATUS,RP0 ;set bank 0 registers
  movwf TXREG ;put W data into txreg
  movf t3,W ;load counter k3 into w
  bsf STATUS,RP0 ;set bank 1 registers
  btfss TXSTA,TRMT ;check if txreg is empty
  goto $-1
  bcf STATUS,RP0 ;set bank 0 registers
  movwf TXREG ;put W data into txreg

  bsf PORTC,5   ;turns on the led on pin 5
  call delay1        ;delay around 10 msec
  bcf PORTC,5   ;turn off the led on pin 5
  goto loop

delay1               ; a delay of around 10 msec
 movlw b'00010000'
 bcf STATUS,RP0
 movwf c2            ; set counter 2 to 16
lc0
 movlw b'11111111'
 movwf c1            ; set counter 1 to 255
lc1
 decfsz c1,1         ; decrease c1 by one
 goto lc1            ; if not zero, go to lc1
 decfsz c2,1         ; if c1=0, decrease c2 by one
 goto lc0            ; if c2 is not zero, go to lc0
 return              ; if c2 =0, return

INT_ROUTINE
   movwf dum ;save W
   movf STATUS, W ;save Status in W
   bcf STATUS,RP0 ;set bank 0
   bcf STATUS,RP1 ;set bank 0
   movwf dum2   ;save status in dum2
   movlw b'11111111'
   subwf k1,W   ;subtract k1
   btfsc STATUS,Z  ;see if k1=255
   goto minute
   goto hour
minute
   movlw b'11111111'
   subwf k2,W   ;subtract k2
   btfsc STATUS,Z  ;see of k2=255
   incf k3,1
   incf k2,1  ;increment k2
hour  incf k1,1
   movf dum2, W ;returning Status to original value
   movwf STATUS
   movf dum,W ; returning dum to W
   bcf INTCON,T0IF
   retfie
 end

