	;; Test program of KASHIWADE switch
	;; For gpasm
        list p=16f84		;specify the micro controller use
status	equ	0x03
ra	equ	0x05		;specify the address of the ports
rb	equ	0x06
intcon	equ	0x0b
opt	equ	0x81
tra	equ	0x85
trb	equ	0x86

	;;SET val
w100us	equ	0x0c
w10ms	equ	0x0d
w100ms	equ	0x0e
w300ms	equ	0x0e
l1	equ	0x0f
l2	equ	0x10
l3	equ	0x11
l4	equ	0x12			
	;;
	;;Initialize
	;;	
ST	movlw	0x00		; Port A -> out		
	tris	ra
	movlw	0x01		; Port B -> in
	tris	rb
	movlw	0x10		; Clear Ra except bit4
	movwf	ra
	clrf	rb		; Clear RB
	;; 
	;;Start
	;;
;;; Wait 
	movlw	0x10		; INT pin Interruption ON
	movwf	intcon
	sleep			; Sleep until inserting SAISEN 
	call	MS100		; Wait
	movlw	0x08		; RB port Interruption ON
	movwf	intcon
	movlw	0x11
	movwf	ra		; Ra.0 ON
	movlw	0x10
	tris	rb		; Port B.4 -> in
	clrf	rb		; Clear RB
	sleep			; Sleep until detecting KASHIWA
	movlw	0x13
	movwf	ra		; Ra.1 ON	
	movlw	0x00		; RB port Interruption OFF
	movwf	intcon
	clrf	rb		; Clear RB	
	call	MS100
	movlw	0xff
	movwf	l1
L1	movlw	0xff
	movwf	l2
L2	movlw	0xff
	movwf	l3
L3	btfsc	rb,4		; Check
	goto	NEXT
	decfsz	l3,1	
	goto	L3
	decfsz	l2,1	
	goto	L2
	decfsz	l1,1	
	goto	L1
	goto	ST		; Reset if no second input
NEXT	bsf	ra,2		; Status LED ON
;;; Switch on	
	bcf	ra,4		; Sw on
SW	call	MS100		; wait
	bsf	ra,4
	call	MS100		; wait
	call	MS100		; wait
	movlw	0x1f
	movwf	ra		; TURI ON	
	call	MS100		; wait
	movlw	0x10		; Clear Ra except bit4
	movwf	ra	 
	bcf	opt,6		; Tachisagari
	movlw	0x20
	tris	rb	
	movlw	0x08		; RB port Interruption ON
	movwf	intcon	
	sleep			; Sleep until Power down
	goto	ST		; Program reset
	
	;;4MHz 1 clock= 0.25us
US100	movlw	0x28		;
	movwf	w100us
USLOOP	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	decfsz	w100us,1	
	goto	USLOOP
	return	

MS10	movlw	0xff		;
	movwf	w10ms
LOOP10	call	US100
	decfsz	w10ms,1	
	goto	LOOP10
	return			; (2clock)

MS100	movlw	0x0b
	movwf	w100ms		;
LOOP100	call	MS10
	decfsz	w100ms,1	
	goto	LOOP100
	return			; (2clock)
	
	;;
	;;Program end
	;; 
END	end







