;***********************************************************************
;* mega16
;* f osc = 8 MHz internal RC
;* fuses=0 CKSEL3 CKSEL1 CKSEL0 SUT0
;***********************************************************************
.include	"m16def.inc"
.nolist
;			R0
;			R1
;			R2		;mult
.def		EncCount = R5	;
.def		timrU10	 = R6	;
.def		timrU20	 = R7	;
.def		timrU30	 = R8	;
.def		filtradr = R9	;
.def		ADCcnt	 = R10	;
.def		keycnt	 = R11	;
.def		morgalka = R12	;
.def		msec	 = R13	; 10Hz смена значений на индикаторе
.def		enccnt	 = R14
.def		resetFM  = R15	; сбросить флаг моргалки
.def		temp     = R16
.def		tempH	 = R17
.def		flags	 = R18
.equ		f_MV	 = 0
.equ		f_MA	 = 1
.equ		f_delay	 = 2
.equ		f_dir	 = 3
.equ		f_5tr	 = 4
.equ		f_OFF	 = 5
.def		razryad	 = R19
;.def		 = R20
;.def		 = R21
;.def		 = R22
;.def		 = R23
.def		buferL   = R24
.def		buferH   = R25
;			XL		 = R26
;			XH		 = R27
;			YL		 = R28
;			YH		 = R29
;			ZL		 = R30		uses in conversion
;			ZH		 = R31

;---------- описание портов ----------------
.equ	S_Port	= PORTC			; сегменты
;					76543210
.equ	S_a		= 0b00100000
.equ	S_b		= 0b10000000
.equ	S_c		= 0b00000010
.equ	S_d		= 0b00001000
.equ	S_e		= 0b00010000
.equ	S_f		= 0b01000000
.equ	S_g		= 0b00000001
.equ	S_h		= 0b00000100
.equ	Off		= 0b00000000

.equ	R_Port	= PORTB			; разряды
.equ	R_V1	= 2
.equ	R_V2	= 1
.equ	R_V3	= 0
.equ	R_I1	= 3
.equ	R_I2	= 4
.equ	R_I3	= 5

.equ	B_Port	= PORTB			; порт кнопки
.equ	button	= 6				; пин кнопки
.equ	OFFbutt	= 5
.equ	Ibutt	= 0
.equ	Ubutt	= 2
.equ	notbutt	= 3
.equ	E_Port	= PORTD			; энкодеры
.equ	E_INT1	= 3				; INT1
.equ	E_INT0	= 2				; INT0
.equ	E_Volt	= 1				; второй на INT1
.equ	E_Amper	= 0				; второй на INT0
.equ	ADC_Port= PORTA			; ацп
.equ	Voltage_ADC =2			; был канал 0
.equ	Current_ADC =1			; канал 1
.equ	Temper_ADC  =0			; был канал 2
.equ	O_Port	= PORTA			; Opto
.equ	U36		= 5
.equ	U24		= 6
.equ	U12		= 7				;U30
.equ	OFF_STAB= 4

;---------Constants-------

;------RAM--------
;* $060-0FF empty
;0x01xx
.equ	RAM_Disp		=0x00	;to 0x05 Data to display
.equ	RAM_Segm		=0x08	;to 0x0D Segmented data
.equ	RAM_Hex			=0x10	;to 0x14 Result of hex2dec
.equ	RAM_VA			=0x18	;to 0x1B E_VoltL,E_VoltH,E_AmpL,E_AmpH,
.equ	RAM_ADVA		=0x20	;to 0x25 ADC_VoltL,ADC_VoltH,ADC_AmpL,ADC_AmpH,ADCTempL,H
.equ	RAM_ADC			=0x80	;to 0x2FF
;---------Macros-------

.MACRO macroName
		nop
.ENDM

.cseg						;выбор сегмента програмного кода
.org 0						;установка текущего адреса на 0

;********************************************************************
;*               Векторы прерываний 								*        
;********************************************************************
		jmp		RESET	;0x000 Pin, Power-on, Brown-out, Watchdog Reset

		jmp		I_INT0	;0x002 INT0 External Interrupt Request 0

		jmp		I_INT1	;0x004 INT1 External Interrupt Request 1

		reti			;0x006 TIMER2_COMP Timer/Counter2 Compare Match
		nop
		reti			;0x008 TIMER2_OVF Timer/Counter2 Overflow
		nop
		reti			;0x00A TIMER1_CAPT Timer/Counter1 Capture Event
		nop
		reti			;0x00C TIMER1_COMPA Timer/Counter1 Compare Match A
		nop
		reti			;0x00E TIMER1_COMPB Timer/Counter1 Compare Match B
		nop
		reti			;0x010 TIMER1_OVF Timer/Counter1 Overflow
		nop
		reti			;0x012 TIMER0_OVF Timer/Counter0 Overflow
		nop
		reti			;0x014 SPI_STC Serial Transfer Complete
		nop
		reti			;0x016 USART_RXC USART, Rx Complete
		nop
		reti			;0x018 USART_UDRE USART Data Register Empty
		nop
		reti			;0x01A USART_TXC USART, Tx Complete
		nop
		jmp		ADC_COMP;0x01C ADC ADC Conversion Complete

		reti			;0x0 EE_RDY EEPROM Ready
		nop
		reti			;0x0 ANA_COMP Analog Comparator
		nop
		reti			;0x0 TWI Two-wire Serial Interface
		nop
		reti			;0x0 INT2 External Interrupt Request 2
		nop
		jmp		T0_COMP	;0x0 TIMER0_COMP Timer/Counter0 Compare Match

		reti			;0x0 SPM_RDY Store Program Memory Ready
		nop

SIMV:
;they will be ON (255-... inverse)
;   -a-
;  f   b
;  |-g-|  
;  e   c
;   -d-
	.db	(S_a|S_b|S_c|S_d|S_e|S_f)		,(S_b|S_c)				  ;0,1
	.db	(S_a|S_b|S_d|S_e|S_g)			,(S_a|S_b|S_c|S_d|S_g)	  ;2,3
	.db	(S_b|S_c|S_f|S_g)				,(S_a|S_c|S_d|S_f|S_g)	  ;4,5
	.db	(S_a|S_c|S_d|S_e|S_f|S_g)		,(S_a|S_b|S_c)			  ;6,7
	.db	(S_a|S_b|S_c|S_d|S_e|S_f|S_g)	,(S_a|S_b|S_c|S_d|S_f|S_g);8,9
	.db	(S_a|S_d|S_e|S_f|S_g)			,(S_e|S_g)				  ;E,r
	.db	(S_g)							,(S_g|S_f|S_e|S_d)		  ;-,t
	.db	(S_b|S_c|S_e|S_f|S_g)			,Off					  ;H,space
;--------------------------------------------------------
conversion:		;Disp->temp->Segm
		ldi		ZH,1
		ldi		ZL,RAM_Disp
CONV_LOOP:
		ld		temp,Z+
		push	ZH
		push	ZL
		ldi		ZH,High(2*SIMV)
		ldi		ZL,Low (2*SIMV)
		cbr		temp,0xF0							;ограничить символы 0-F
		add		ZL,temp
		lpm		temp,Z
		pop		ZL
		pop		ZH

		sbrc	flags,f_OFF
		rjmp	nopoint
		cpi		ZL,RAM_Disp+4						;point Amp
		brne	PC+2
		sbr		temp,S_h
		cpi		ZL,RAM_Disp+2						;point Volt
		brne	nopoint
		sbr		temp,S_h
nopoint:
		std		Z+(RAM_Segm-RAM_Disp-1),temp		;сохранить в новой области памяти
		cpi		ZL,(RAM_Disp+6)
		brne	CONV_LOOP
		ret
;----------------------------------------------
hex2dec:		;in buferH,buferL out RAM_Hex 5 byte (used temp) 
		ldi		ZH,$01
		ldi		ZL,RAM_Hex
		clr		temp
five:
		subi	buferL,LOW(10000)	
		sbci	buferH,HIGH(10000)
		inc		temp
		brcc	five
		subi	temp,(-$2F)
		st		Z+,temp
		subi	buferL,LOW(-10000)	
		sbci	buferH,HIGH(-10000)
		clr		temp
four:
		subi	buferL,LOW(1000)	
		sbci	buferH,HIGH(1000)
		inc		temp
		brcc	four
		subi	temp,(-$2F)
		st		Z+,temp
		subi	buferL,LOW(-1000)	
		sbci	buferH,HIGH(-1000)
		clr		temp
tres:
		subi	buferL,LOW(100)	
		sbci	buferH,HIGH(100)
		inc		temp
		brcc	tres
		subi	temp,(-$2F)
		st		Z+,temp
		subi	buferL,LOW(-100)	
		clr		temp
dous:
		subi	buferL,10
		inc		temp
		brcc	dous
		subi	temp,(-$2F)
		st		Z+,temp
		subi	buferL,-$3A
		st		Z+,buferL
		ret
;----------------------------------------------
writeEEPROM:
		ld		temp,Y+		
		out		eedr,temp			;data
		out		eearl,buferL		;adr L
		out		eearh,buferH		;adr H
		sbi		eecr,eemwe			;Master Write Enable
		sbi		eecr,eewe			;Write Enable
WaitWriteComplite:
		sbic	eecr,eewe			;WaitWriteComplite
		rjmp	WaitWriteComplite
		ret

;**********************************************************************
;*       НАЧАЛО ПРОГРАММЫ											  *
;**********************************************************************
RESET:
		cli							;запретить все прерывания
		clr		temp
		out		EECR, temp			;обнулить EEPROM Control Register
		out		GICR, temp			;interrupt vectors at start of FlashMEM
		out		MCUCR,temp			;Sleep Disable ;Sleep Mode = Idle
									;Interrupt Sense Control = Low level of INT1(0)
;		out		WDTCR, R30			;Watchdog Disable
		ldi		temp,0x18
		out		WDTCR,temp
		ldi		temp,0x0E			;Watchdog Enable 1sec
		out		WDTCR,temp
;--------Очистка SRAM-------------------------------
		ldi		temp,0
		ldi		YL, LOW(RAMEND-0x60)
		ldi		YH, HIGH(RAMEND-0x60)
		ldi		XL, 0x60			;X=0x0060
CLEAR_SRAM:
		st		X+, temp			;R30=0 adress 060..45F
		sbiw	YL, 1				;3FF..0
		brne	CLEAR_SRAM			;branch if not zero

;--------Инициализация стека------------------------
		ldi		temp, LOW(RAMEND)	;выбор адреса вершины стека
		out		SPL, temp			;запись его в регистр стека
		ldi		temp, HIGH(RAMEND)	;выбор адреса вершины стека
		out		SPH, temp			;запись его в регистр стека

;--------Инициализация портов-----------------------
		ldi		temp, 0b11111111	
		out		S_Port-1,temp		;DDR 1=out сегменты
		ldi		temp, 0b11111111	
		out		R_Port-1,temp		;DDR 1=out разряды
		ldi		temp, 0b00000000	
		out		E_Port-1, temp		;DDR 0=in датчики
		ldi		temp, 0b00000000	
		out		ADC_Port-1, temp	;DDR 0=in датчики

		ldi		temp, 0b00000000	
		out		S_Port,temp			;Segment off
		ldi		temp, 0b11111111	
		out		R_Port,temp			;Razryad off
		ldi		temp, 0b00000000	
		out		ADC_Port,temp		;1= pull-up

		cbi		B_Port-1,button		;buttons in
		cbi		B_Port,button		;buttons no pullup
		sbi		O_Port-1,U36		;OPTOCOUP1 out
		sbi		O_Port-1,U24		;OPTOCOUP2 out
		sbi		O_Port-1,U12		;OPTOCOUP3 out
		cbi		O_Port,U12			;OPTOCOUP1 of
		cbi		O_Port,U24			;OPTOCOUP2 of
		cbi		O_Port,U36			;OPTOCOUP2 of
		sbi		O_Port-1,OFF_STAB	;OFF out
		sbi		O_Port,OFF_STAB		;OFF =1
		sbi		E_Port,E_INT0		;pullup
		sbi		E_Port,E_INT1		;pullup
		sbi		E_Port,E_Volt		;pullup
		sbi		E_Port,E_Amper		;pullup
		sbi		PortD-1,4			;OC1B - current
		sbi		PortD-1,5			;OC1A - voltage
		sbi		PortD-1,7			;OC2 - vent

;-------- Инициализация Ext INT -----------------
		ldi		temp,0b11000000
		out		GICR,temp
		out		GIFR,temp

		ldi		temp,0b00000101		;int0,int1 any change
		out		MCUCR,temp
;-------- Инициализация таймера TMR0 -----------------
		ldi		temp,(1<<WGM01)|(1<<CS01)|(1<<CS00)		;WGM=ctc CLK/64
		out		TCCR0,temp
		ldi		temp,208			;~600Hz @8MHz					
		out		OCR0,temp
		ldi		temp,(1<<OCIE0)
;		clr		temp	
		out		TIMSK,temp			;off
;  CS02 01  00 и CS12 11  10
;	0	0	0	no
;	0	0	1	Clk/1
;	0	1	0	Clk/8
;	0	1	1	Clk/64
;	1	0	0	Clk/256
;	1	0	1	Clk/1024
;-------- Инициализация таймера TMR1 -----------------
		ldi		temp,(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11)		
		out		TCCR1A,temp					;WGM=Fast PWM, 9-bit CLKio/1
		ldi		temp,(1<<WGM12)|(1<<CS10)	;non-inverting mode
		out		TCCR1B,temp					;15k6 @8WHz

		ldi		temp,high(0) 
		out		OCR1AH,temp
		ldi		temp,low(0)	
		out		OCR1AL,temp			;volt

		ldi		temp,high(0)
		out		OCR1BH,temp
		ldi		temp,low(0)		
		out		OCR1BL,temp			;amp

;		in		temp,TIMSK
;		ori		temp,(1<<OCIE1A)|(1<<OCIE1B)
;		out		TIMSK,temp			;compa,b
;-------- Инициализация таймера TMR2 -----------------
;		ldi		temp,(1<<CS22)|(1<<CS21)|(1<<CS20)		;WGM=norm CLK/1024
;		out		TCCR2,temp			;32.8ms
;		in		temp,TIMSK
;		ori		temp,(1<<TOIE2)
;		out		TIMSK,temp			;ovf
		ldi		temp,(1<<WGM20)|(1<<WGM21)|(1<<COM21)|(1<<CS20)
		out		TCCR2,temp			;non-inv fast PWM 31kHz
		ldi		buferH,120
		out		OCR2,buferH
;-------- Инициализация АЦП --------------------------
		ldi		temp,(1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1) 	;ADC=On 
		out		ADCSRA,temp			;	Int Enable Prescaler=64 (125kHz @ 8MHz)
		ldi		temp,Voltage_ADC|(1<<REFS0)	; right adjusted AVCC=AREF
		out		ADMUX,temp
;-------- Read EEPROM mega8 ----------------------------
		ldi		YH,1
		ldi		YL,RAM_VA
		ldi		temp,0				;EEPROM adress H
		out		eearh,temp			
		ldi		temp,16				;EEPROM adress L
loadEEPROM:
		out		eearl,temp			
		sbi		eecr,eere			;Read Enable
		in		buferL,eedr			;read data
		sbrc	temp,0
		andi	buferL,1
		st		Y+,buferL
		inc		temp
		cpi		temp,20
		brne	loadEEPROM
; установка ШИМ
		ldi		YL,RAM_VA
		ld		temp,Y+
		ld		tempH,Y+
		out		OCR1AH,tempH
		out		OCR1AL,temp			;volt
		ld		temp,Y+
		ld		tempH,Y+
		out		OCR1BH,tempH
		out		OCR1BL,temp			;amper
;-------- Установка начальных значений -----------------
		clr		morgalka
		clr		msec
		clr		resetFM
		clr		ADCcnt
		clr		filtradr
		ldi		flags,(1<<f_OFF)
		ldi		temp,0xFF
		mov		keycnt,temp
		ldi		buferL,20				;timer for hysteresis
		mov		timrU10,buferL
		mov		timrU20,buferL
		mov		timrU30,buferL

		ldi		YH,1
		ldi		YL,RAM_ADVA				;ADCvolt
		ldi		temp,99
		st		Y+,temp
		ldi		temp,0
		st		Y+,temp
		ldi		temp,low(199)
		st		Y+,temp
		ldi		temp,high(199)
		st		Y+,temp					;10.0,2.00

		ldi		YL,RAM_Disp			; ' -    '
		ldi		buferL,0x0F			; ' '
		ldi		buferH,0x0C			; '-'
		st		Y+,buferL
		st		Y+,buferH
		st		Y+,buferL
		st		Y+,buferL
		st		Y+,buferL
		st		Y+,buferL
		rcall	conversion
		ldi		razryad,0b11111110

		sei							;enable all interrupt

;*****************************************************
;*-------- Основной цикл ----------------------------*
;*****************************************************

START:
		WDR
		rjmp	Start

;*****************************************************
;*--------- Прерывание ------------------------------*
;*****************************************************
T0_COMP:								;8M/64/208=601Hz
		push	temp
		in		temp,SREG
		push	temp
		push	buferH
		push	buferL
		ldi		YH,1

		sbi		ADCSRA,ADSC			;начать новое преобразование

		sbrs	enccnt,7				;<128 => +1
		inc		enccnt
		sbrs	enccnt,3
		cbr		flags,(1<<f_delay)		;13ms

; стереть
		ldi		temp,off
		out		S_Port,temp
; проверить кнопки
		sbrc	razryad,notbutt
		rjmp	skipinc
		inc		keycnt
		brne	PC+2
		dec		keycnt					;оставить на 255
		rjmp	endbutton
skipinc:
		sbic	B_port-2,button			;бежит 0
		rjmp	endbutton

		ldi 	temp,255
		cp 		keycnt,temp
		brne	PC+2 
		clr		keycnt 					;первый запуск

;button is pressed 
		sbrc	razryad,OFFbutt			;бежит 0
		rjmp	IbuttT

		ldi		temp,2
		cp 		keycnt,temp
		brne	endbutton
; поменять ON OFF

		sbrc	flags,f_OFF
		rjmp	setON
		sbr		flags,(1<<f_OFF)
		ldi		YL,RAM_Disp			; ' -    '
		ldi		buferL,0x0F			; ' ' заменить на 0С для х6 чёрточек
		ldi		buferH,0x0C			; '-'
		st		Y+,buferL
		st		Y+,buferH
		st		Y+,buferL
		st		Y+,buferL
		st		Y+,buferL
		st		Y+,buferL
		rcall	conversion

		sbi		O_PORT,OFF_STAB
		rjmp	endbutton
setON:
		cbr		flags,(1<<f_OFF)
		ldi		YL,RAM_VA
		ld		temp,Y+
		ld		tempH,Y+
		out		OCR1AH,tempH
		out		OCR1AL,temp				;volt
		ld		temp,Y+
		ld		tempH,Y+
		out		OCR1BH,tempH
		out		OCR1BL,temp				;amper
		cbi		O_PORT,OFF_STAB
		rjmp	endbutton
IbuttT:
		sbrc	flags,f_OFF
		rjmp	endbutton
		sbrc	razryad,Ibutt
		rjmp	VbuttT
		sbr		flags,(1<<f_MA)
		clr		resetFM	
		
		ldi		temp,200				;2sec
		cp 		keycnt,temp
		brne	endbutton

		ldi		buferH,0				;EEPROM adress
		ldi		buferL,18				;AMP_L
		ldi		YL,RAM_VA+2
		rcall	writeEEPROM
		ldi		buferL,19				;AMP_H
		ldi		YL,RAM_VA+3
		rcall	writeEEPROM
		sbr		flags,(1<<f_5tr)
		rjmp	endbutton
VbuttT:
		sbrc	razryad,Ubutt
		rjmp	endbutton
		sbr		flags,(1<<f_MV)
		clr		resetFM	
		
		ldi		temp,200				;2sec
		cp 		keycnt,temp
		brne	endbutton

		ldi		buferH,0				;EEPROM adress
		ldi		buferL,16				;VoltageL
		ldi		YL,RAM_VA
		rcall	writeEEPROM
		ldi		buferL,17				;VoltageH
		ldi		YL,RAM_VA+1
		rcall	writeEEPROM
		sbr		flags,(1<<f_5tr)
endbutton:
; сменить разряды
		sec		
		rol		razryad					;<--
		sbrc	razryad,6
		rjmp	incmsecend
		ldi		razryad,0b11111110
		inc		resetFM	
		brne	PC+2
		cbr		flags,(1<<f_MV)|(1<<f_MA)|(1<<f_5tr)



		ldi		temp,20					;5Hz
		inc		msec	
		cp		temp,msec
		brmi	PC+2
		rjmp	incmsecend
		clr		msec
; пересчитать скользящий фильтр
		ldi		YH,high(0x280 - Temper_ADC*0x80)		;Begin of Temp filter 
		ldi		YL, low(0x280 - Temper_ADC*0x80)
		clr		buferH
		clr		buferL
calcsumT:
		ld		temp,Y+
		add		buferL,temp
		ld		temp,Y+
		adc		buferH,temp
		cpi		YL,0x00
		brne	calcsumT

		ldi		YH,1					;ADCTemp
		ldi		YL,RAM_ADVA+4
		clr		temp
		rol		buferL
		rol		buferH
		rol		temp
		st		Y+,buferH				;L
		st		Y,temp					;H

		cpi		buferH,low(273+80)
		ldi		buferL,high(273+80)		;80gr cels
		cpc		temp,buferL
		brcs	setventspeed
;teplo off
		sbi		O_PORT,OFF_STAB
		sbr		flags,(1<<f_OFF)
		ldi		YL,RAM_Disp			; ' - H0t'
		ldi		buferL,0x0F			; ' ' 
		ldi		buferH,0x0C			; '-'
		st		Y+,buferL
		st		Y+,buferH
		st		Y+,buferL
		ldi		buferH,0x0E			; 'H'
		st		Y+,buferH
		ldi		buferH,0x00			; '0'
		st		Y+,buferH
		ldi		buferH,0x0D			; 't'
		st		Y+,buferH
		rcall	conversion
; recall temperature
		ldi		YL,RAM_ADVA+4
		ld		buferH,Y+				;L
		ld		temp,Y					;H
setventspeed:
		ldi		buferL,high(273+46)
		cpi		buferH,low(273+46)
		cpc		temp,buferL
		brcs	tempernothigh
		subi	buferH,50
		cpi		buferH,31
		brmi	PC+2
		ldi		buferH,31			;70gr
		lsl		buferH
		lsl		buferH
		lsl		buferH
		out		OCR2,buferH			;46gr= 41% 64gr =99%
		ldi		temp,(1<<WGM20)|(1<<WGM21)|(1<<COM21)|(1<<CS20)
		out		TCCR2,temp			;non-inv fast PWM 31kHz
		rjmp	ventend
tempernothigh:
		cpi		buferH,low(273+42)
		ldi		buferL,high(273+42)
		cpc		temp,buferL
		brcc	ventend
		ldi		temp,0
		out		TCCR2,temp			;no tmr
ventend:



		ldi		YH,high(0x280 - Current_ADC*0x80)	;Begin of I filter 0x200
		ldi		YL, low(0x280 - Current_ADC*0x80)
		clr		buferH
		clr		buferL
calcsumI:
		ld		temp,Y+
		add		buferL,temp
		ld		temp,Y+
		adc		buferH,temp
		cpi		YL,low(0x80+0x280 - Current_ADC*0x80)
		brne	calcsumI

		clr		temp
		rol		buferL
		rol		buferH
		rol		temp
		ldi		YH,1					;ADCcur
		ldi		YL,RAM_ADVA+2
		st		Y+,buferH				;L
		st		Y,temp					;H


		ldi		YH,high(0x280 - Voltage_ADC*0x80)	;Begin of U filter 0x180
		ldi		YL, low(0x280 - Voltage_ADC*0x80)
		clr		buferH
		clr		buferL
calcsumU:
		ld		temp,Y+
		add		buferL,temp
		ld		temp,Y+
		adc		buferH,temp
		cpi		YL,low(0x80+0x280 - Voltage_ADC*0x80)
		brne	calcsumU

		clr		temp
		rol		buferL
		rol		buferH
		rol		temp
		ldi		YH,1					;ADCvolt
		ldi		YL,RAM_ADVA
		st		Y+,buferH				;L
		st		Y,temp					;H
; new adc result ready

;ADCvolt+55
		mov		tempH,temp
		ldi		temp,55
		add		temp,buferH				;L
		ldi		buferH,0
		adc		tempH,buferH			;H
		ldi		buferL,20				;timer for hysteresis

;if volt>14,5 then opto1 on
;if volt>28,5 then opto2 on
;if volt>42,5 then opto3 on
		cpi		temp,low(145)
		ldi		buferH,high(145)
		cpc		tempH,buferH
		brcs	PC+3
		sbi		O_Port,U12
		mov		timrU10,buferL

		cpi		temp,low(285)
		ldi		buferH,high(285)
		cpc		tempH,buferH
		brcs	PC+3
		sbi		O_Port,U24
		mov		timrU20,buferL

		cpi		temp,low(425)
		ldi		buferH,high(425)
		cpc		tempH,buferH
		brcs	PC+3
		sbi		O_Port,U36
		mov		timrU30,buferL

;if volt<14,0 then opto1 off
;if volt<28,0 then opto2 off
;if volt<42,0 then opto3 off
		cpi		temp,low(420)
		ldi		buferH,high(420)
		cpc		tempH,buferH
		brcc	lowerend
		dec		timrU30				;-1
		brne	PC+3				;=0
		cbi		O_Port,U36
		inc		timrU30				;+1

		cpi		temp,low(280)
		ldi		buferH,high(280)
		cpc		tempH,buferH
		brcc	lowerend
		dec		timrU20				;-1
		brne	PC+3				;=0
		cbi		O_Port,U24
		inc		timrU20				;+1


		cpi		temp,low(140)
		ldi		buferH,high(140)
		cpc		tempH,buferH
		brcc	lowerend
		dec		timrU10				;-1
		brne	PC+3				;=0
		cbi		O_Port,U12
		inc		timrU10				;+1
lowerend:
; *******************************************************
; индикация токов и напряжений 
; в зависимости от флага f_MV и f_MV 
; показывает измеренное или заданное 
; *******************************************************
		ldi		YH,1
		sbrc	flags,f_OFF
		rjmp	incmsecend
		ldi		YL,RAM_ADVA				;ADCvolt
		sbrs	flags,f_MV
		rjmp	Show_volt
		ldi		YL,RAM_VA				;Evolt
		sbrs	flags,f_5tr
		rjmp	Show_volt
		ldi		YL,RAM_Disp				;VoltInd
		ldi		buferL,5				;s
		st		Y+,buferL
		ldi		buferL,0x0D				;t
		st		Y+,buferL
		ldi		buferL,0x0B				;r
		st		Y+,buferL
		rjmp	Show_voltEnd
Show_volt:
		ld		buferL,Y+
		ld		buferH,Y+
		sbrc	flags,f_MV
		adiw	buferL,1
		rcall	hex2dec
		ldi		YL,RAM_Disp				;VoltInd
		ldd		buferL,Y+(RAM_Hex-RAM_Disp+2)
		cpi		buferL,0x30
		brne	PC+2
		ldi		buferL,0x0F				;пробел
		st		Y+,buferL
		ldd		buferL,Y+(RAM_Hex-RAM_Disp+2)
		st		Y+,buferL
		ldd		buferL,Y+(RAM_Hex-RAM_Disp+2)
		st		Y+,buferL
Show_voltEnd:
		ldi		YL,RAM_ADVA+2			;ADC_Amp
		sbrs	flags,f_MA
		rjmp	Show_Amp
		ldi		YL,RAM_VA+2				;E_Amp
		sbrs	flags,f_5tr
		rjmp	Show_Amp
		ldi		YL,RAM_Disp+3			;AmpInd
		ldi		buferL,5				;s
		st		Y+,buferL
		ldi		buferL,0x0D				;t
		st		Y+,buferL
		ldi		buferL,0x0B				;r
		st		Y+,buferL
		rjmp	Show_AmpEnd
Show_Amp:
		ld		buferL,Y+
		ld		buferH,Y
		sbrc	flags,f_MA
		adiw	buferL,1
		rcall	hex2dec
		ldi		YL,RAM_Disp+3			;AmpInd
		ldd		buferL,Y+(RAM_Hex-RAM_Disp-3+2)
		st		Y+,buferL
		ldd		buferL,Y+(RAM_Hex-RAM_Disp-3+2)
		st		Y+,buferL
		ldd		buferL,Y+(RAM_Hex-RAM_Disp-3+2)
		st		Y+,buferL
Show_AmpEnd:

		rcall	conversion
incmsecend:

		out		R_Port,razryad

		inc		morgalka
		sbrs	morgalka,6
		rjmp	indikac
		sbrs	morgalka,5
		rjmp	indikac

		sbrs	flags,f_MV
		rjmp	testMA
		sbrs	razryad,R_V1
		rjmp	indikacend
		sbrs	razryad,R_V2
		rjmp	indikacend
		sbrs	razryad,R_V3
		rjmp	indikacend
testMA:
		sbrs	flags,f_MA
		rjmp	indikac
		sbrs	razryad,R_I1
		rjmp	indikacend
		sbrs	razryad,R_I2
		rjmp	indikacend
		sbrs	razryad,R_I3
		rjmp	indikacend
indikac:
		sbrs	razryad,R_V1
		ldi		YL,RAM_Segm
		sbrs	razryad,R_V2
		ldi		YL,RAM_Segm+1
		sbrs	razryad,R_V3
		ldi		YL,RAM_Segm+2
		sbrs	razryad,R_I1
		ldi		YL,RAM_Segm+3
		sbrs	razryad,R_I2
		ldi		YL,RAM_Segm+4
		sbrs	razryad,R_I3
		ldi		YL,RAM_Segm+5
		ld		temp,Y
		out		S_Port,temp

indikacend:		


interuptend:
		pop		buferL
		pop 	buferH
		pop		temp
		out		SREG,temp
		pop		temp
		reti						;закончить прерывание


I_INT1:
		push	temp
		in		temp,SREG
		push	temp
		push	tempH
		
		sbrc	flags,f_delay
		rjmp	endint1
		sbrc	flags,f_OFF
		rjmp	endint1

		ldi		temp,200
delay100us:
		nop
		dec		temp
		brne	delay100us

		in		temp,E_Port-2

		sbrc	temp,E_INT1
		rjmp	secondchng
		cbr		flags,(1<<f_dir)	;E_INT0=0	E_Volt=0 fdir=0
		sbrc	temp,E_Volt
		sbr		flags,(1<<f_dir)	;E_INT0=0	E_Volt=1 fdir=1
		rjmp	endint1
secondchng:
		ldi		XH,1
		ldi		XL,RAM_VA


		sbrc	flags,f_dir
		rjmp	down1
		sbrs	temp,E_Volt
		rjmp	endint1
up1:
		ld		temp,X+
		ld		tempH,X+
;setspeed
		ldi		buferL,120
		cp		enccnt,buferL		;20ms
		ldi		buferL,5
		brlo	PC+2
		ldi		buferL,1
;add
		add		temp,buferL
		ldi		buferL,0
		adc		tempH,buferL

		cpi		temp,low(499)
		ldi		buferL,high(499)
		cpc		tempH,buferL
		brcs	notmax1
		ldi		temp,low(499)
		ldi		tempH,high(499)
notmax1:
		st		-X,tempH
		st		-X,temp
		rjmp	succes
down1:
		sbrc	temp,E_Volt
		rjmp	endint1
		ld		temp,X+
		ld		tempH,X+
;setspeed
		ldi		buferL,120
		cp		enccnt,buferL		;20ms
		ldi		buferL,5
		brlo	PC+2
		ldi		buferL,1
;sub
		sub		temp,buferL
		sbci	tempH,0
		cpi		tempH,-1
		breq	min1reach
		st		-X,tempH
		st		-X,temp
succes:
		out		OCR1AH,tempH
		out		OCR1AL,temp			;volt
		clr		temp
		out		TCNT2,temp
min1reach:
		clr		resetFM				; мигание
		clr		enccnt
		sbr		flags,(1<<f_MV)|(1<<f_delay)
endint1:
		ldi		temp,100
delay50us:
		nop
		dec		temp
		brne	delay50us

		pop		tempH
		pop		temp
		out		SREG,temp
		pop		temp
		reti						;закончить прерывание



I_INT0:
		push	temp
		in		temp,SREG
		push	temp
		push	tempH
		push	buferL


		sbrc	flags,f_delay
		rjmp	endint0
		sbrc	flags,f_OFF
		rjmp	endint0

		ldi		temp,150
delay75us:
		nop
		dec		temp
		brne	delay75us


		in		temp,E_Port-2

		sbrc	temp,E_INT0
		rjmp	secondchng0
		cbr		flags,(1<<f_dir)	;E_INT0=0	E_Volt=0 fdir=0
		sbrc	temp,E_Amper
		sbr		flags,(1<<f_dir)	;E_INT0=0	E_Volt=1 fdir=1
		rjmp	endint0
secondchng0:
		ldi		XH,1
		ldi		XL,RAM_VA+2
		sbrs	flags,f_dir
		rjmp	down0
		sbrc	temp,E_Amper
		rjmp	endint0
up0:
		ld		temp,X+
		ld		tempH,X+
;setspeed
		ldi		buferL,120
		cp		enccnt,buferL		;20ms
		ldi		buferL,5
		brlo	PC+2
		ldi		buferL,1
;add
		add		temp,buferL
		ldi		buferL,0
		adc		tempH,buferL

		cpi		temp,low(509)
		ldi		buferL,high(509)
		cpc		tempH,buferL
		brcs	notmax
		ldi		temp,low(509)
		ldi		tempH,high(509)
notmax:
		st		-X,tempH
		st		-X,temp
		rjmp	succes0
down0:
		sbrs	temp,E_Amper
		rjmp	endint0
		ld		temp,X+
		ld		tempH,X+

;setspeed
		ldi		buferL,120
		cp		enccnt,buferL		;20ms
		ldi		buferL,5
		brlo	PC+2
		ldi		buferL,1
;sub
		sub		temp,buferL
		sbci	tempH,0
		cpi		tempH,-1
		breq	min0reach
		st		-X,tempH
		st		-X,temp
succes0:
		out		OCR1BH,tempH
		out		OCR1BL,temp			;amper
		clr		temp
		out		TCNT2,temp
min0reach:
		clr		resetFM				; мигание
		clr		enccnt
		sbr		flags,(1<<f_MA)|(1<<f_delay)
endint0:
		ldi		temp,100
delay51us:
		nop
		dec		temp
		brne	delay51us

		pop		buferL
		pop		tempH
		pop		temp
		out		SREG,temp
		pop		temp
		reti						;закончить прерывание


ADC_COMP:
		push	temp
		in		temp,SREG
		push	temp
		push	XL
		push	XH

		sbrc	ADCcnt,1
		inc		filtradr
		mov		temp,filtradr
		lsl		temp
		andi	temp,0x7E
				
		ldi		XH,2				;ADCcnt 0   1   2
		sbrc	ADCcnt,1			;X      280 200 180
		ldi		XH,1		
		ldi		XL,0x80
		sbrc	ADCcnt,0
		ldi		XL,0
		
		add		XL,temp		

		in		temp,ADCL
		st		X+,temp
		in		temp,ADCH				
		st		X+,temp				;added one new elem to array



		ldi		temp,3
		inc		ADCcnt
		cp		ADCcnt,temp
		brne	PC+2
		clr		ADCcnt				;ADCcnt=0..2
		mov		temp,ADCcnt
		sbr		temp,(1<<REFS0)	
		out		ADMUX,temp


		pop		XH
		pop		XL
		pop		temp
		out		SREG,temp
		pop		temp
		reti						;закончить прерывание

