	list	P=PIC10F200
	include  "P10F200.INC"

	__CONFIG _CP_OFF & _MCLRE_ON & _WDT_OFF & _OSC_IntRC


;#define	DebugKeys

  cblock	0x010
	bt1shr
	bt2shr
	time
	flags
	TMR0Prev
	subsec
	sec

	keys

LastBank0:
  endc

  IF LastBank0 >= 0x20
		ERROR "To many variables used"
  ENDIF

	org		0x0
Start:
	clrf	GPIO
	clrf	time
	clrf	sec

	movlw	0xFF
	movwf	bt1shr
	movwf	bt2shr

  ifdef	DebugKeys
	movwf	keys
  endif

	movlw	.61
	movwf	subsec

	movlw	0xFE
	tris	GPIO

	movlw	0xC5
	option

	clrf	TMR0Prev
	clrf	TMR0

Loop:
	movf	TMR0,w
	xorwf	TMR0Prev,w
	btfsc	STATUS,Z
	goto	Loop
	xorwf	TMR0Prev,f

	movf	TMR0Prev,w
	btfss	STATUS,Z
	goto	Loop

							; 16ms key debounce

	bcf		STATUS,C
  ifdef	DebugKeys
	btfsc	keys,GP1
  else
	btfsc	GPIO,GP1
  endif
	bsf		STATUS,C
	rlf		bt1shr,f
	movlw	0x01
	xorwf	bt1shr,w
	btfsc	STATUS,Z
	call	Bt1Pressed

	bcf		STATUS,C
  ifdef	DebugKeys
	btfsc	keys,GP2
  else
	btfsc	GPIO,GP2
  endif
	bsf		STATUS,C
	rlf		bt2shr,f
	movlw	0x01
	xorwf	bt2shr,w
	btfsc	STATUS,Z
	call	Bt2Pressed

	nop

	decfsz	subsec,f
	goto	Loop
						; 1 sec
	movlw	.61
	movwf	subsec

	incf	sec,f
	movlw	.60
	xorwf	sec,w
	btfss	STATUS,Z
	goto	Loop
						; 1 minute
	clrf	sec

	movf	time,w
	btfss	STATUS,Z
	decfsz	time,f
	goto	Loop

	bcf		GPIO,GP0
	goto	Loop

Bt1Pressed
	movlw	.61
	movwf	subsec
	clrf	sec
	movlw	.5
	addwf	time,w
	btfsc	STATUS,C
	movlw	0xFF
	movwf	time
	bsf		GPIO,GP0
	retlw	0

Bt2Pressed
	clrf	time
	bcf		GPIO,GP0
	retlw	0

	END