;
;  PIC12C509
;    Gullwing Door Mirror Controller  Ver 0.1
;		Programmed by DD
;       2005/10/9
;
;	Data Format
;	+---------------+
;	| | |O|O|X|X|X|I|   GPIO  I:Input   O:Output   X:Don't Use
;       +---------------+   
;       Input Data
;                           GPO => Open/Close Switch (Open:1, Close:0) 
;
; 	Output Data
;		            GP4 => Open Signal
;		            GP5 => Close Signal
;
;       Function
;           
;       Flow Chart
;           (START):
;                    GP4=0, GP5=0
;                    If GPWUF=1 then 
;			if GP0=0 go to OPEN.
;                       else go to CLOSE.
;           (SLP):
;                    GP4=0, GP5=0
;		     Read GP0  
;                    Sleep z.z.z....
;                    Go to START
;           (OPEN):
;                    GP4=1
;		     Wait for 5 sec.
;		     if GP0=1 go to CLOSE
;                    Go to SLP
;           (CLOSE):
;                    GP5=1
;		     Wait for 5 sec.
;		     if GP0=0 go to OPEN
;                    Go to SLP


	list    p=pic12C509A
	include "p12c509a.inc"
;
	__FUSES _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC
;	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  0x08    ;Store variables above control registers
	wcnt1		;Wait Counter1 (For 200ms Wait)
	wcnt2		;Wait Counter2 (For 200ms Wait)
	waitcnt		;Wait Counter
	endc


	org 0x00		;Start of Code Space
;=========================================
;  Start Program (& Wake up from Sleep)
;=========================================
START	movlw	b'00000001'	;GP5,4,3,2,1:Output  GP0:Input
	tris	6		;Set GPIO Input/Output
	movlw   b'00000000'	;Wakeup & Pull-up Enable
	option
	movlw	b'00000000'	;GP5,4=0
	movwf	GPIO
	btfss	STATUS,GPWUF	;If GPWUF=0
	goto	SLP		;  go to SLP
	btfss	GPIO,0		;If GP0=0
	goto	OPEN		;  go to OPEN
	goto	CLOSE		;  go to CLOSE
SLP	movlw	b'00000000'	;GP5,4=0
	movwf	GPIO
	movf	GPIO,W		;Read GPIO
	sleep

;===========================
;  Open Door Mirror
;===========================
OPEN	movlw	b'00010000'	;GP5=0,GP4=1
	movwf	GPIO
	movlw	d'25'		;200ms*25 Wait
	movwf	waitcnt
Openlp	call	wait200ms
	decfsz	waitcnt,F
	goto	Openlp
	btfsc	GPIO,0		;If GP0=1
	goto	CLOSE		; go to CLOSE
	goto	SLP		;Go to SLP

;===========================
;  Close Door Mirror
;===========================
CLOSE	movlw	b'00100000'	;GP5=1,GP4=0
	movwf	GPIO
	movlw	d'25'		;200ms*25 Wait
	movwf	waitcnt
Closelp	call	wait200ms
	decfsz	waitcnt,F
	goto	Closelp
	btfss	GPIO,0		;If GP0=0
	goto	OPEN		; go to OPEN
	goto	SLP		;Go to SLP
	

;Subroutine

;================================
;  Wait for 200ms (197.4 msec)
;================================
wait200ms
	movlw	d'0'		;*256
	movwf	wcnt2
waitlp2	clrf	wcnt1
waitlp1	decfsz	wcnt1,F
	goto	waitlp1
	decfsz	wcnt2,F
	goto	waitlp2
	retlw	0

	end


