;-------------------------------------------------

;        QUICK BATTERY CHARGER

;				S.Ishijima

;	http://www.csc.titech.ac.jp/~s60196

;                                    Ver 1.15

;-------------------------------------------------



PORTA  EQU     05H		; I/O PORT ADDRESS

PORTB  EQU     06H

OPTION EQU	01H

TMR0	EQU	01H





WREG	EQU	0

FILEREG	EQU	1





; STATUS

STATUS          EQU     03H

C		EQU	0

DC		EQU	1

Z		EQU	2

PD		EQU	3

TO		EQU	4

RP0		EQU	5



TRISA	EQU	5H

TRISB	EQU	6H



;---- 16c84 EEPROM/DATA -----

EEDATA		EQU	08H		; c84	EE-PROM

EECON1		EQU	08H		; c84	EE-PROM		FSR=1

EEADR		EQU	09H		; c84	EE-PROM

EECON2		EQU	09H		; c84	EE-PROM		FSR=1

;

; EECON1 value define

RD		EQU	0

WR		EQU	1

WREN		EQU	2

WRERR		EQU	3

EEIF		EQU	4





;  TIMER

DOWN_TIME	EQU	6	; ?TIMES DOWN -> STOP

END_TIME	EQU	70	; END_TIME -> STOP

WAIT_TIME	EQU	2	; 23 -> 1min   69 -> 3min

G_TIME		EQU	5



; RAM ADDRESS



TIMER1	EQU	10H

TIMER2	EQU	11H



TMP	EQU	12H

TMP2	EQU	13H



CHARGE	EQU	14H

VCOUNT	EQU	15H

VREND	EQU	16H



VCV1	EQU	17H

VCV2	EQU	18H

VCV3	EQU	19H

VCV4	EQU	20H



VMAX1	EQU	21H

VMAX2	EQU	22H

VMAX3	EQU	23H

VMAX4	EQU	24H



DCOUNT1	EQU	25H	; DOWN COUNT

DCOUNT2	EQU	26H

DCOUNT3	EQU	27H

DCOUNT4	EQU	28H



CTIME1	EQU	29H	; CHARGE TIME

CTIME2	EQU	2AH

CTIME3	EQU	2BH

CTIME4	EQU	2CH



VPRE1	EQU	2DH

VPRE2	EQU	2EH

VPRE3	EQU	2FH

VPRE4	EQU	30H



GTIMER	EQU	31H



VIN1	EQU	0

VIN2	EQU	1

VIN3	EQU	2

VIN4	EQU	3

BAT1	EQU	4

BAT2	EQU	5

BAT3	EQU	6

BAT4	EQU	7



CCHAR	EQU	0

DCCHAR	EQU	4



	ORG	0h

	GOTO	START

	

	ORG	8h

START

	CALL	INIT

	

;	FOR DEBUG

;	MOVLW	60

;	MOVWF	EEADR

	

	CALL	WAIT

	CALL	WAIT

	CALL	WAIT

	CALL	DISCHARGE



LOP	

	CALL 	PRECHARGE

	CALL	VREAD

	CALL	DOWN_CONTROL

	CALL	DISCHARGE

	CALL	TIMER_CONTROL

	

;	FOR DEBUG

;	MOVF	VMAX1, WREG

;	MOVWF	EEDATA

;	CALL	WRDATA

;	DECF	EEADR, FILEREG

	CALL	WAIT

;	NOP

	GOTO	LOP

	

INIT

	BSF	STATUS, RP0

	MOVLW	00H

	MOVWF	TRISA

	MOVLW	0FH

	MOVWF	TRISB

	MOVLW	11000100B

	MOVWF	OPTION

	BCF	STATUS, RP0



	CLRF	TMR0

	MOVLW	G_TIME

	MOVWF	GTIMER



	CALL	INIT1

	CALL	INIT2

	CALL	INIT3

	CALL	INIT4

	RETURN

	

INIT1

	CLRF	VMAX1

	CLRF	VPRE1

	MOVLW	DOWN_TIME

	MOVWF	DCOUNT1

	CLRF	CTIME1

	BSF	PORTB, BAT1

	RETURN

	

INIT2

	CLRF	VMAX2

	MOVLW	DOWN_TIME

	MOVWF	DCOUNT2

	CLRF	CTIME2

	BSF	PORTB, BAT2

	RETURN



INIT3

	CLRF	VMAX3

	MOVLW	DOWN_TIME

	MOVWF	DCOUNT3

	CLRF	CTIME3

	BSF	PORTB, BAT3

	RETURN



INIT4

	CLRF	VMAX4

	MOVLW	DOWN_TIME

	MOVWF	DCOUNT4

	CLRF	CTIME3

	BSF	PORTB, BAT4

	RETURN





TIMER_CONTROL

	DECFSZ	GTIMER

	RETURN

	MOVLW	G_TIME

	MOVWF	GTIMER

	

	BTFSC	PORTB, BAT1	;Џ[“d‚µ‚Д‚ў‚И‚ў‚Ж‚«TIMER‚рђi‚Я‚И‚ў

	INCF	CTIME1

	BTFSC	PORTB, BAT2

	INCF	CTIME2

	BTFSC	PORTB, BAT3

	INCF	CTIME3

	BTFSC	PORTB, BAT4

	INCF	CTIME4

	

	MOVLW	END_TIME

	SUBWF	CTIME1, WREG	; CTIME - END_TIME -> WREG

	BTFSC	STATUS, C

	BCF	PORTB, BAT1	; CTIME >= END_TIME  ( C = 1)

	MOVLW	END_TIME

	SUBWF	CTIME2, WREG	; CTIME - END_TIME -> WREG

	BTFSC	STATUS, C

	BCF	PORTB, BAT2	; CTIME >= END_TIME  ( C = 1)	

	MOVLW	END_TIME

	SUBWF	CTIME3, WREG	; CTIME - END_TIME -> WREG

	BTFSC	STATUS, C

	BCF	PORTB, BAT3	; CTIME >= END_TIME  ( C = 1)

	MOVLW	END_TIME

	SUBWF	CTIME4, WREG	; CTIME - END_TIME -> WREG

	BTFSC	STATUS, C

	BCF	PORTB, BAT4	; CTIME >= END_TIME  ( C = 1)

	

	RETURN





DOWN_CONTROL

RENEW_MAX1

	COMF	VCV1, WREG	; BATTERY EXISTS ?

	BTFSC	STATUS, Z

	GOTO	NO_BATTERY1

	MOVF	VCV1, WREG

	SUBWF	VMAX1, WREG	; VMAX - VCV

	BTFSC	STATUS, C

	GOTO	DOWN_COUNT1	; VMAX >= VCV

	MOVF	VCV1, WREG	; VMAX < VCV

	MOVWF	VMAX1

	GOTO	RENEW_MAX2

NO_BATTERY1

	CALL	INIT1

	GOTO	RENEW_MAX2

DOWN_COUNT1

	BTFSC	STATUS, Z

	GOTO	RENEW_MAX2	; VMAX = VCV

	DECFSZ	DCOUNT1		; VMAX > VCV

	GOTO	RENEW_MAX2

	BCF	PORTB, BAT1	; CHARGEING -> OFF

	

RENEW_MAX2

	COMF	VCV2, WREG

	BTFSC	STATUS, Z

	GOTO	NO_BATTERY2

	MOVF	VCV2, WREG

	SUBWF	VMAX2, WREG	; VMAX - VCV

	BTFSC	STATUS, C

	GOTO	DOWN_COUNT2	; VMAX >= VCV

	MOVF	VCV2, WREG	; VMAX < VCV

	MOVWF	VMAX2

	GOTO	RENEW_MAX3

NO_BATTERY2

	CALL	INIT2

	GOTO	RENEW_MAX3

DOWN_COUNT2

	BTFSC	STATUS, Z

	GOTO	RENEW_MAX3	; VMAX = VCV

	DECFSZ	DCOUNT2

	GOTO	RENEW_MAX3

	BCF	PORTB, BAT2

RENEW_MAX3

	COMF	VCV3, WREG

	BTFSC	STATUS, Z

	GOTO	NO_BATTERY3

	MOVF	VCV3, WREG

	SUBWF	VMAX3, WREG	; VMAX - VCV

	BTFSC	STATUS, C

	GOTO	DOWN_COUNT3	; VMAX >= VCV

	MOVF	VCV3, WREG	; VMAX < VCV

	MOVWF	VMAX3

	GOTO	RENEW_MAX4

NO_BATTERY3

	CALL	INIT3

	GOTO	RENEW_MAX4

DOWN_COUNT3

	BTFSC	STATUS, Z

	GOTO	RENEW_MAX4	; VMAX = VCV

	DECFSZ	DCOUNT3

	GOTO	RENEW_MAX4

	BCF	PORTB, BAT3

RENEW_MAX4

	COMF	VCV4, WREG

	BTFSC	STATUS, Z

	GOTO	NO_BATTERY4

	MOVF	VCV4, WREG

	SUBWF	VMAX4, WREG	; VMAX - VCV

	BTFSC	STATUS, C

	GOTO	DOWN_COUNT4	; VMAX >= VCV

	MOVF	VCV4, WREG	; VMAX < VCV

	MOVWF	VMAX4

	RETURN

NO_BATTERY4

	CALL	INIT4

	RETURN

DOWN_COUNT4

	BTFSC	STATUS, Z

	RETURN			; VMAX = VCV

	DECFSZ	DCOUNT4

	RETURN

	BCF	PORTB, BAT4

	RETURN





PRECHARGE

	BSF	STATUS, RP0

	MOVLW	11000111B

	MOVWF	OPTION

	BCF	STATUS, RP0

	

	; 011 ...60-> 500ms, 0.8V   85->1.15V  240-> 2.6V

	; 100 ...160-> 2.4V  220->2.9V

	; 111 ...250-> 3.1V

	MOVLW	110

	MOVWF	TIMER1

	

	CLRF	TMR0

	BSF	PORTA, CCHAR

	BSF	PORTA, DCCHAR

PRECHARGE_LOOP

	MOVF	TMR0,WREG

	SUBLW	0FFH

	BTFSS	STATUS, Z

	GOTO	PRECHARGE_LOOP



	DECFSZ	TIMER1, FILEREG

	GOTO	PRECHARGE_LOOP

	RETURN



VREAD

	BSF	STATUS, RP0

	MOVLW	11000100B

	MOVWF	OPTION

	BCF	STATUS, RP0

	

	MOVLW	0FFH

	MOVWF	VCOUNT

	CLRF	VREND

	

	CLRF	TMR0

	BSF	PORTA, CCHAR

	BSF	PORTA, DCCHAR

VREAD_LOOP

	MOVF	TMR0,WREG

	SUBLW	0FBH

	BTFSS	STATUS, Z

	GOTO	VREAD_LOOP



	BTFSS	PORTB, VIN1

	CALL	WRCOUNTV1

	

	BTFSS	PORTB, VIN2

	CALL	WRCOUNTV2

	

	BTFSS	PORTB, VIN3

	CALL	WRCOUNTV3

	

	BTFSS	PORTB, VIN4

	CALL	WRCOUNTV4

	

	DECFSZ	VCOUNT, FILEREG

	GOTO	VREAD_LOOP

	RETURN



WRCOUNTV1

	BTFSC	VREND, VIN1

	RETURN

	BSF	VREND, VIN1

	MOVF	VCOUNT, WREG

	MOVWF	VCV1

	RETURN

	

WRCOUNTV2

	BTFSC	VREND, VIN2

	RETURN

	BSF	VREND, VIN2

	MOVF	VCOUNT, WREG

	MOVF	VCV2

	RETURN

	

WRCOUNTV3

	BTFSC	VREND, VIN3

	RETURN

	BSF	VREND, VIN3

	MOVF	VCOUNT, WREG

	MOVF	VCV3

	RETURN

	

WRCOUNTV4

	BTFSC	VREND, VIN4

	RETURN

	BSF	VREND, VIN4

	MOVF	VCOUNT, WREG

	MOVF	VCV4

	RETURN





	

DISCHARGE

	BSF	STATUS, RP0

	MOVLW	11000011B

	MOVWF	OPTION

	BCF	STATUS, RP0

	

	MOVLW	60		; 60-> = 500ms

	MOVWF	TIMER1

	

	CLRF	TMR0

	BCF	PORTA, CCHAR

	BCF	PORTA, DCCHAR

DISCHARGE_LOOP1

	MOVF	TMR0, WREG

	SUBLW	0FFH

	BTFSS	STATUS, Z

	GOTO	DISCHARGE_LOOP1



	DECFSZ	TIMER1, FILEREG

	GOTO	DISCHARGE_LOOP1

	RETURN



WRDATA

	BSF	STATUS, RP0

	BSF	EECON1, WREN

	MOVLW	55H

	MOVWF	EECON2

	MOVLW	0AAH

	MOVWF	EECON2

	BSF	EECON1, WR

WRDATA_LOOP

	BTFSS	EECON1, EEIF

	GOTO	WRDATA_LOOP

	BCF	EECON1, EEIF

	BCF	EECON1, WREN

	BCF	STATUS, RP0

	RETURN





WAIT

	BSF	STATUS, RP0

	MOVLW	11000111B

	MOVWF	OPTION

	BCF	STATUS, RP0

	CLRF	TMR0

	MOVLW	255

	MOVWF	TIMER1

	MOVLW	WAIT_TIME

	MOVWF	TIMER2

WAIT_LOOP1

	MOVF	TMR0, WREG

	SUBLW	0FFH

	BTFSS	STATUS, Z

	GOTO	WAIT_LOOP1

WAIT_LOOP2

	DECFSZ	TIMER1,FILEREG

	GOTO	WAIT_LOOP1

WAIT_LOOP3

	DECFSZ	TIMER2, FILEREG

	GOTO	WAIT_LOOP1

	RETURN

	

	

	END

