setup:		clrwdt

;--------------------------------------------------

		banksel	option_reg

		movlw	b'10001111'	; wdt/128
		movwf	option_reg
;--------------------------------------------------

;--------------------------------------------------

		banksel	trisa

		movlw	b'00001000'
		movwf	trisa

		movlw	b'00111100'
		movwf	trisc

;-------------------- Timer_1 ---------------------

		banksel	t1con
	
		movlw	b'00110001'
		movwf	t1con
;--------------------------------------------------
		banksel	intcon

		movlw	b'01000000'
		movwf	intcon

bsf	intcon, gie
;-----------------
		banksel	pie1

		movlw	b'00000001'
		movwf	pie1

;---------------------- ADC -----------------------

		banksel	adcon0

		movlw	b'00011101'
		movwf	adcon0
;-----------------
		banksel	adcon1

		movlw	b'00100000'
		movwf	adcon1
;-----------------
		banksel	ansel

		movlw	b'11000000'
		movwf	ansel

;---------------- Comparator -----------------------

		banksel	cmcon

		movlw	b'00000111'
		movwf	cmcon
;---------------------------------------------------
		banksel	porta

		bcf	pin_rele_in
		bcf	pin_rele_out
		bcf	pin_rele_off

		bcf	pin_sv_max
		bcf	pin_sv_out

		bcf	out_ok
		movlw	0x03
		movwf	on_counter
		movwf	off_counter

		call	delay_1_sec

		btfsc	pin_jumper
		return
;-----------------
		bsf	pin_rele_off
		call	auto_setup

		bsf	intcon, gie
		return
;-------------------------------------------------------------------------



;-------------------------------------------------------------------------
auto_setup:	call	delay_1_sec	; 1-й этап
					; 210v -> flash
		btfsc	pin_jumper
		goto	auto_setup_1

		call	one_flash

		btfsc	pin_jumper
		goto	auto_setup_1
		goto	auto_setup

auto_setup_1:	call	setup_in_210
;--------------------------------------------
auto_setup_20:	clrwdt

		bsf	pin_sv_max	; 1-й этап закончен
		bsf	pin_sv_out

		btfsc	pin_jumper
		goto	auto_setup_20
		
		call	delay_1_sec

		btfsc	pin_jumper
		goto	auto_setup_20
		bcf	pin_sv_out
;--------------------------------------------
auto_setup_2:	call	delay_1_sec	; 2-й этап
					; out=187 -> flash
		call	one_flash
;-----------------
		btfsc	pin_jumper
		goto	auto_setup_21

		call	one_flash
;-----------------	
		btfsc	pin_jumper
		goto	auto_setup_21
		goto	auto_setup_2
;-----------------
auto_setup_21:	call	setup_out_min
;--------------------------------------------
auto_setup_30:	clrwdt

		bsf	pin_sv_max	; 2-й этап закончен
		bsf	pin_sv_out

		btfsc	pin_jumper
		goto	auto_setup_30
		
		call	delay_1_sec

		btfsc	pin_jumper
		goto	auto_setup_30
		bcf	pin_sv_out
;--------------------------------------------
auto_setup_3:	call	delay_1_sec	; 3-й этап
					; out=242v -> flash
		call	one_flash
;-----------------
		btfsc	pin_jumper
		goto	auto_setup_31

		call	one_flash
;-----------------
		btfsc	pin_jumper
		goto	auto_setup_31

		call	one_flash
;-----------------
		btfsc	pin_jumper
		goto	auto_setup_31
		goto	auto_setup_3
;-----------------
auto_setup_31:	call	setup_out_max

		bsf	pin_sv_max	
		bsf	pin_sv_out

		call	delay_1_sec
		call	delay_1_sec
		call	delay_1_sec
;-------------------------------------------------------------------------
auto_setup_end:	call	one_flash
		goto	auto_setup_end
;-------------------------------------------------------------------------




;-------------------------------------------------------------------------
setup_in_210:	call	adc_in		; 210v

		movf	adresh, 0
		movwf	adc_result_m

		bsf	status, rp0
		 movf	adresl, 0
		bcf	status, rp0
		movwf	adc_result_l
;-----------------
		movf	adc_result_l, 0
		sublw	porog_set_l
		movwf	adc_result_l	; const - result

		btfss	status, c
		incf	adc_result_m

		movf	adc_result_m, 0
		sublw	porog_set_m
		movwf	adc_result_m	; const - result
;-----------------
		movlw	flash_210_adr_l
		bsf	status, rp0
		 movwf	eeadr
		bcf	status, rp0

		movf	adc_result_l, 0
		bsf	status, rp0
		 movwf	eedata
		bcf	status, rp0

		call	flash_write
;-----------------
		movlw	flash_210_adr_m
		bsf	status, rp0
		 movwf	eeadr
		bcf	status, rp0

		movf	adc_result_m, 0
		bsf	status, rp0
		 movwf	eedata
		bcf	status, rp0

		call	flash_write

		return
;--------------------------------------------------------------------------


;-------------------------------------------------------------------------
setup_out_min:	call	adc_out		; min

		movf	adresh, 0
		movwf	adc_result_m

		bsf	status, rp0
		 movf	adresl, 0
		bcf	status, rp0
		movwf	adc_result_l
;-----------------
		movlw	flash_min_adr_l
		bsf	status, rp0
		 movwf	eeadr
		bcf	status, rp0

		movf	adc_result_l, 0
		bsf	status, rp0
		 movwf	eedata
		bcf	status, rp0

		call	flash_write
;-----------------
		movlw	flash_min_adr_m
		bsf	status, rp0
		 movwf	eeadr
		bcf	status, rp0

		movf	adc_result_m, 0
		bsf	status, rp0
		 movwf	eedata
		bcf	status, rp0

		call	flash_write

		return
;--------------------------------------------------------------------------

;-------------------------------------------------------------------------
setup_out_max:	call	adc_out		; 210v

		movf	adresh, 0
		movwf	adc_result_m

		bsf	status, rp0
		 movf	adresl, 0
		bcf	status, rp0
		movwf	adc_result_l
;-----------------
		movlw	flash_max_adr_l
		bsf	status, rp0
		 movwf	eeadr
		bcf	status, rp0

		movf	adc_result_l, 0
		bsf	status, rp0
		 movwf	eedata
		bcf	status, rp0

		call	flash_write
;-----------------
		movlw	flash_max_adr_m
		bsf	status, rp0
		 movwf	eeadr
		bcf	status, rp0

		movf	adc_result_m, 0
		bsf	status, rp0
		 movwf	eedata
		bcf	status, rp0

		call	flash_write

		return
;--------------------------------------------------------------------------

;--------------------------------------------------------------------------
one_flash:	bsf	pin_sv_max	
		bsf	pin_sv_out

		call	delay_200_ms	
		
		bsf	pin_sv_max
		bcf	pin_sv_out

		call	delay_200_ms	

		return
;--------------------------------------------------------------------------
