;
;  PIC12F675
;    Room Lamp Controller  Rev 0.1
;		Programmed by DD
;       2009/6/13
;    Rev 0.1 : The fade-in time is 1.3 sec.
;              The fade-out time is 4.0 sec.
;
;	Data Format
;	+---------------+
;	| | |X|O|X|I|X|I|   GPIO  I:Input   O:Output
;       +---------------+   
;       Input Data
;                           GP4 => CTRL Signal (0:ON, 1:OFF)
;
; 	Output Data
;		            GP2 => Lamp Control (0:OFF, 1:ON)
;
;
;       Function
;           
;       Flow Chart
;           (START):
;                    GP2=0
;		     pwm=0
;	    (MAIN):
;		     if (GP4==1) then
;			if (pwm <> 0) pwm=pwm-1
;		        call  PULSE
;			call  PULSE
;			call  PULSE
;		        go to MAIN
;		     else
;			pwm = pwm + 1
;			if (pwm == 128) pwm=127
;			call  PULSE
;		     end if
;		     go to MAIN
;
;       Subroutines
;	    (PULSE):
;		     count=126
;	    (PULSE1):
;		     if (count < pwm) GP2=1
;		     else  GP2=0
;		     call  Wait10ms
;		     count = count-1
;		     if (count>=0) go to PULSE1
;		     return
;
;	    (Wait):
;		     Wait for a while


	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
	wcnt		;Wait Counter1
	pwm		;Pulse width (0-127)
	count		;Pulse counter
	endc


	org 0x00		;Start of Code Space
;=========================================
;  Start Program (& Wake up from Sleep)
;=========================================
	bsf	STATUS,RP0	;Set Bank1
	movlw	b'00010000'	;Output: GP2 Input:GP4
	movwf	TRISIO		;Set GPIO Input/Output
	movlw   b'00000000'	;Pull-up Enable
	movwf	OPTION_REG
	movlw	b'00010000'	;Weak pull-up enable for GP4
	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,2		;GP2(LED)=0(OFF)
	clrf	pwm		;pwm=0
MAIN	btfss   GPIO,4          ;If GP4 = 0,
	goto	MAIN1		;  go to MAIN1
	movf	pwm,F		;Test pwm
	btfss	STATUS,Z	;If (pwm <> 0),
	decf	pwm,F		;  pwm=pwm-1
	call	PULSE		;Call PULSE
	call	PULSE		;Call PULSE
	call	PULSE		;Call PULSE
	goto	MAIN		;Go to MAIN
MAIN1:	incf	pwm,F		;pwm=pwm+1
	btfsc	pwm,7		;if (pwm>=128)
	decf	pwm,F		;pwm=pwm-1
	call	PULSE		;Call PULSE
	goto	MAIN
	
;================================
;  Output depending on pulse width
;================================
PULSE:	movlw	d'126'
	movwf	count		;count=126
PULSE1:	movf	count,W		;W=count
	subwf	pwm,W		;W=pwm-count
	btfss	STATUS,C	;if (W>0)
	goto	PULSE2		;  go to PULSE2
	bsf	GPIO,2		;GP2=1
	goto	PULSE3		;Go to PULSE3
PULSE2:	bcf	GPIO,2		;GP2=0
PULSE3:	call	wait	;Wait
	decfsz	count,F		;count=count-1
	goto	PULSE1
	retlw	0

;================================
;  Wait
;================================
wait
	movlw	d'22'		;22
	movwf	wcnt
waitlp1	decfsz	wcnt,F
	goto	waitlp1
	retlw	0

	end
