;
;  PIC12F675
;    Door Handle Light LED Controller  Rev 0.1
;		Programmed by DD
;       2007/10/29
;
;	Data Format
;	+---------------+
;	| | |X|O|X|I|X|I|   GPIO  I:Input   O:Output
;       +---------------+   
;       Input Data
;                           GP0 => LOCK Signal
;			    GP2 => UNLOCK Signal
;
; 	Output Data
;		            GP4 => LED Control (0:OFF, 1:ON)
;
;
;       Function
;           
;       Flow Chart
;           (START):
;                    GP4=0
;		     IF GP2=1 & GP0=0 then goto UNLOCK
;		     goto START
;           (UNLOCK):
;                    for (i=10; i>=0; i--)
;		       Wait for 100msec
;		       IF GP0=1 goto START
;
;		     for (i=252; i>=0; i=i-4)
;			GP4=1
;			for (j=256-i; j>=0; j--)
;			   wait for 0.04ms
;			GP4=0
;			for (j=i; j>=0; j--)
;			   wait for 0.04ms
;			IF GP0=1 goto START
;		     GP4=1
;
;		     for (i=100; i>=0; i--)
;		        Wait for 100msec
;			IF GP0=1 goto START
;
;		     for (i=255; i>=0; i--)
;			GP4=1
;			for (j=i; j>=0; j--)
;			   wait for 0.04ms
;			GP4=0
;			for (j=256-i; j>=0; j--)
;			   wait for 0.04ms
;			IF GP0=1 goto START
;		     GP4=0
;		     goto START
;
;       Subroutines
;	    (Wait100ms):
;		     Wait for 100msec
;	    (Wait004ms):
;		     Wait for 0.04msec


	list    p=pic12F675
	include "p12f675.inc"

; Defining constant

;
	__FUSES _CPD_OFF & _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
;	Configuration Code = 0xFFEA
;	When burning PIC, the following configuration is necessary
;		MCLR is OFF
;		Code Protection(CP) is OFF
;		Watch Dog Timer(WDT) is OFF
;		Clock is Internal RC Oscillator

	cblock  0x20    ;Store variables above control registers
	wcnt1		;Wait Counter1 (For 100ms Wait)
	wcnt2		;Wait Counter2 (For 100ms Wait)
	wcnt3		;Wait Conuter3 (For 0.04ms Wait)
	i		;Loop counter
	j		;Loop counter
	endc


	org 0x00		;Start of Code Space
;=========================================
;  Start Program (& Wake up from Sleep)
;=========================================
	bsf	STATUS,RP0	;Set Bank1
	movlw	b'00100111'	;Output: GP4,GP3 Input:GP5,GP2,GP1,GP0
	movwf	TRISIO		;Set GPIO Input/Output
	movlw   b'00000000'	;Pull-up Enable
	movwf	OPTION_REG
	movlw	b'00000000'	;No weak pull-up
	movwf	WPU
	movlw	b'00000000'	;All pins are digital I/O
	movwf	ANSEL
	bcf	STATUS,RP0	;Set Bank0
	movlw	b'00000111'	;
	movwf	CMCON		;CMCON=00000111

START	bcf	GPIO,4		;GP4(LED)=0(OFF)
	call	wait100ms
	btfss   GPIO,2          ;If GP2 = 0,
	goto	START		;  go to START
	btfsc   GPIO,0          ;If GP0 = 1,
	goto	START		;  go to START

UNLOCK	movlw	d'10'
	movwf	i		;i=10
UNLOCK1	call	wait100ms
	btfsc   GPIO,0          ;If GP0 = 1,
	goto	START		;  go to START
	decfsz	i,F		;Loop 10 times
	goto	UNLOCK1

	movlw	d'252'
	movwf	i		;i=252
UNLOCK2	bsf	GPIO,4		;GP4(LED)=1(ON)
	clrf	j		;j=0
	movf	i,W		;W=i
	subwf	j,F		;j=0-i
UNLOCK3	call	wait004ms	;Wait for 0.04ms
	decfsz	j,F		;Loop j times
	goto	UNLOCK3
	bcf	GPIO,4		;GP4(LED)=0(OFF)
	movf	i,W		;W=i
	movwf	j		;j=i
UNLOCK4	call	wait004ms	;Wait for 0.04ms
	decfsz	j,F		;Loop j times
	goto	UNLOCK4
	btfsc   GPIO,0          ;If GP0 = 1,
	goto	START		;  go to START
	decf	i,F		;
	decf	i,F		;
	decf	i,F		;
	decfsz	i,F		;i=i-4
	goto	UNLOCK2
	bsf	GPIO,4		;GP4(LED)=1(ON)

	movlw	d'100'
	movwf	i		;i=100
UNLOCK5	call	wait100ms	;Wait for 100msec
	btfsc   GPIO,0          ;If GP0 = 1,
	goto	START		;  go to START
	decfsz	i,F		;i=i-1
	goto	UNLOCK5

	movlw	d'255'
	movwf	i		;i=255
UNLOCK6	bsf	GPIO,4		;GP4(LED)=1(ON)
	movf	i,W		;W=i
	movwf	j		;j=i
UNLOCK7	call	wait004ms	;Wait for 0.04ms
	decfsz	j,F		;Loop j times
	goto	UNLOCK7
	bcf	GPIO,4		;GP4(LED)=0(OFF)
	clrf	j		;j=0
	movf	i,W		;W=i
	subwf	j,F		;j=0-i
UNLOCK8	call	wait004ms	;Wait for 0.04ms
	decfsz	j,F		;Loop j times
	goto	UNLOCK8
	btfsc   GPIO,0          ;If GP0 = 1,
	goto	START		;  go to START
	decfsz	i,F		;i=i-1
	goto	UNLOCK6
	bcf	GPIO,4		;GP4(LED)=0(OFF)
	goto START

;================================
;  Wait for 100ms
;================================
wait100ms
	movlw	d'128'		;*128
	movwf	wcnt2
waitlp2	clrf	wcnt1
waitlp1	decfsz	wcnt1,F
	goto	waitlp1
	decfsz	wcnt2,F
	goto	waitlp2
	retlw	0


;================================
;  Wait for 0.04ms
;================================
wait004ms
	movlw	d'11'		;11
	movwf	wcnt3
waitlp3	decfsz	wcnt3,F
	goto	waitlp3
	retlw	0

	end
