;#include <P16F690.INC>
  ; __config (_XT_OSC & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

;#include <P16F690.INC>
 ; __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
 
#include <P16F690.INC> ;includes libraries for pic690
  __config (_HS_OSC & _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  ;to save fast clock
  t1 equ 26  ;to save second fast clock
  t2 equ 27  ;to save third fast clock
  t3 equ 28  ;to save fourth fast clock


 org 00

 goto Start
  org 04
  goto INT_ROUTINE

Start

 BCF   STATUS,RP0   ;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'00001010' ;sets baud rate for 115.2k
 movwf SPBRG
 BSF   TRISB,6   ;set portb pin 11 as input
 bcf   TRISB,4   ;set portb pin 13 as output
 bcf STATUS,RP0 ;set bank 0 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
 movwf k1   ; start k1 at 0
 movwf k2   ; start k2 at 0
 movwf k3   ; start k3 at 0

loop
  bsf PORTB,4   ;turns on the led on pin 13
  btfss PORTB,6  ;repeat until pin 11 goes high 
  goto$-1
  movf TMR0,W; load clock into W
  movwf t0; save tmro
  movf k1,W ; load k1 into W
  movwf t1 ; saved t1
  movf k2,W ; load k2 into w
  movwf t2  ; saved t2
  movf k3,W ; load k2 into w
  movwf t3  ; saved t3
  movf t0,W; load clock 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
  bcf PORTB,4  ;turn off led on pin 13
  btfsc PORTB,6  ;repeat until pin 11 goes low 
  goto$-1
  movf TMR0,W; load clock into w
  movwf t0; save fast time
  movf k1,W ;load k1 into w
  movwf t1 ; save clock time
  movf k2,W ; load k2 into w
  movwf t2 ; save t2
  movf k3,W ; load k3 into w
  movwf t3 ; save t3
  movf t0,W; load clock 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
  goto loop

INT_ROUTINE
   movwf dum ;save W
   movf STATUS, W ;save Status in W
   bcf STATUS,RP0 ;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

