	LIST      P=16F88
	#INCLUDE  P16F88.INC

	__CONFIG  _CONFIG1, _CP_OFF & _WDT_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _INTRC_IO & _LVP_OFF & _CPD_OFF & _DEBUG_OFF


	CBLOCK	0x20
	pw_status
	acc_tim0
	acc_tim1
	acc_tim2
	main_tim0
	main_tim1
	main_tim2
	led_tim0
	led_tim1
	led_tim2
	wt_ctr0
	ad_vol		;A/D変換した電圧
	ENDC

ACC_PORT		EQU	PORTA
ACC_PIN		EQU	2
LED_PORT		EQU	PORTB
LED_PIN		EQU	0
PW_PORT		EQU	PORTB
PW_GOOD		EQU	1
PW_ACC		EQU	2
PW_MAIN		EQU	3
NT_PORT		EQU	PORTA
NT_ACC		EQU	0
st_power_low	EQU	0
st_power_down	EQU	1
st_acc_up		EQU	2

	ORG	0		;リセットの入口
	GOTO	init
; -----------------------------------------------------
; 初期処理
init
	CLRF	ad_vol
	MOVLW	5
	CLRF	acc_tim0
	CLRF	acc_tim1
	MOVWF	acc_tim2
	CLRF	main_tim0
	CLRF	main_tim1
	MOVWF	main_tim2
	MOVLW	0x07
	MOVWF	pw_status
	CLRF	PORTA
	CLRF	PORTB

	BSF		STATUS,RP0
	MOVLW	B'01110000'
	MOVWF	OSCCON
	MOVLW	B'0011111'
	MOVWF	OSCTUNE

	; PIN I/O 設定
	MOVLW	B'00001100'
	MOVWF	TRISA		;RA2-3は入力,RA0は出力(RA1,4,5,6,7は未使用）
	MOVLW	B'11110000'
	MOVWF	TRISB		;RB0-3は出力,(RB4-7は未使用）
	BCF		STATUS,RP0
	CLRF	PORTA
	CLRF	PORTB

	; A/D設定
	MOVLW	B'01011001'
	MOVWF	ADCON0		;16Tosc(adcs1=0,adcs0=1),CH3(chs=011),0,0,ADON=1
	BSF		STATUS,RP0
	MOVLW	B'01000000'			
	CLRF	ADCON1		;左詰め(ADFM=0),16Tosc(adcs2=1), Vref=Vdd Vref-=Vss(VCFG1,0=00),-,-,-,-
	MOVLW	B'00001000'	
	MOVWF	ANSEL		;-,ANS6=0,ANS5=0,ANS4=0,ANS3=1,ANS2=0,ANS1=0,ANS0=0(AN2,AN3使用)
	BCF		STATUS,RP0


; -----------------------------------------------------
; メイン処理

main
main_loop
	; sens main voltage
	BSF		ADCON0,GO		; AD変換開始
main_ch2_loop
	BTFSC	ADCON0,GO		; AD変換完了？
	GOTO	main_ch2_loop	; NO
	MOVF	ADRESH,W		; AD変換結果の退避
	MOVWF	ad_vol		; 上位8bit
	MOVLW	168
	SUBWF	ad_vol,W		; ad_vol < 168(13.0v base=20v)
	BTFSS	STATUS,C
	GOTO	main_down
	MOVLW	172
	SUBWF	ad_vol,W		; ad_vol < 172(13.5v base=20v)
	BTFSS	STATUS,C
	GOTO	main_low
main_hi
	BCF		pw_status,st_power_low
	BCF		pw_status,st_power_down
	CLRF	main_tim0
	CLRF	main_tim1
	MOVLW	5
	MOVWF	main_tim2
	BSF		PW_PORT,PW_MAIN	; power on 3.7v
	BSF		PW_PORT,PW_GOOD	; power good sign on
;	BCF		NT_PORT,NT_ACC	; notify acc down
	GOTO	main_next
main_low
	BSF		pw_status,st_power_low
	BCF		pw_status,st_power_down
	DECFSZ	main_tim0,F
	GOTO	main_next
	DECFSZ	main_tim1,F
	GOTO	main_next
	BSF		NT_PORT,NT_ACC	; notify acc down
	DECFSZ	main_tim2,F
	GOTO	main_next
	BSF		PW_PORT,PW_MAIN	; power on 3.7v
	BCF		PW_PORT,PW_GOOD	; power good sign off
	GOTO	main_next
main_down
	BSF		pw_status,st_power_low
	BSF		pw_status,st_power_down
	DECFSZ	main_tim0,F
	GOTO	main_next
	DECFSZ	main_tim1,F
	GOTO	main_next
	BSF		NT_PORT,NT_ACC	; notify acc down
	DECFSZ	main_tim2,F
	GOTO	main_next
	BCF		PW_PORT,PW_MAIN	; power off 3.7v
	BCF		PW_PORT,PW_GOOD	; power good sign off
	BCF		PW_PORT,PW_ACC	; power off 5.0v
	GOTO	main_next
main_next

	; sens acc power
acc_sens
	BTFSS	PW_PORT,PW_MAIN
	GOTO	acc_next
	BTFSS	ACC_PORT,ACC_PIN
	GOTO	acc_down
acc_up
	BSF		pw_status,st_acc_up
	CLRF	acc_tim0
	CLRF	acc_tim1
	MOVLW	5
	MOVWF	acc_tim2
	BSF		PW_PORT,PW_ACC	; acc on singan 
	BCF		NT_PORT,NT_ACC	; notify acc down
	GOTO	acc_next	
acc_down
	DECFSZ	acc_tim0,F
	GOTO	acc_next
	BSF		NT_PORT,NT_ACC	; notify acc down
	DECFSZ	acc_tim1,F
	GOTO	acc_next
	BCF		pw_status,st_acc_up
	DECFSZ	acc_tim2,F
	GOTO	acc_next
;	DECFSZ	acc_tim3,F
;	GOTO	acc_next
	BCF		PW_PORT,PW_ACC		; acc off singan 
acc_next

	; led
	BTFSC	pw_status,st_power_down
	GOTO	led_pw_down				; 主電源 電圧低下
	BTFSC	pw_status,st_power_low
	GOTO	led_pw_low				; 主電源 電圧低下警告
led_pw_good
	BTFSS	pw_status,st_acc_up
	GOTO	led_pw1_acc0			; ACC OFF

;	主電源 電圧正常 ACC ON
	BSF		LED_PORT,LED_PIN
	GOTO	led_next

;	主電源 電圧正常 ACC OFF 
led_pw1_acc0
	BTFSC	LED_PORT,LED_PIN
	GOTO	led_pw1_acc0_on_loop
led_pw1_acc0_off_loop
	DECFSZ	led_tim0,F
	GOTO	led_next
	DECFSZ	led_tim1,F
	GOTO	led_next
	BSF		LED_PORT,LED_PIN
	GOTO	led_next
led_pw1_acc0_on_loop
	DECFSZ	led_tim0,F
	GOTO	led_next
	BCF		LED_PORT,LED_PIN
	GOTO	led_next

;	主電源 電圧低下	
led_pw_down
	DECFSZ	led_tim0,F
	GOTO	led_next
	DECFSZ	led_tim1,F
	GOTO	led_next
	BTFSC	LED_PORT,LED_PIN
	GOTO	led_pw_down_on
	BSF		LED_PORT,LED_PIN
	MOVLW	10
	MOVWF	led_tim1
	GOTO	led_next
led_pw_down_on
	BCF		LED_PORT,LED_PIN
	MOVLW	10
	MOVWF	led_tim1
	GOTO	led_next

;	主電源 電圧警告
led_pw_low
	DECFSZ	led_tim0,F
	GOTO	led_next
	DECFSZ	led_tim1,F
	GOTO	led_next
	BTFSC	LED_PORT,LED_PIN
	GOTO	led_pw_low_on
	BSF		LED_PORT,LED_PIN
	MOVLW	30
	MOVWF	led_tim1
	GOTO	led_next
led_pw_low_on
	BCF		LED_PORT,LED_PIN
	MOVLW	30
	MOVWF	led_tim1
	GOTO	led_next

led_next
	GOTO	main_loop
