;  http://electronix.ru/forum/index.php?s=370bfe50ace4d6660364ceb3beb20be6&showtopic=60502&st=120&p=565035&#entry565035

;  http://roboforum.ru/viewtopic.php?f=2&t=5548&sid=d57224b87e8c653934a0ea8a71a1ea2d&start=15

;     


.def	_PvEnc	= r14 	;    
.def	_PvDir		= r13 	;    

.def	_PosH		= r12 	; 24-bit    
.def	_PosM		= r11 	;
.def	_PosL		= r10 	;

=================================================== 
; *********     **********
	RJMP 	INT_Encoder		; External Interrupt Request 0

;---------------------------------------------------------------------------------------------------------------------------------
; **************    INT0 *************
	ldi		r16,0b00000001		; INT0     (Encoder)
	sts		EICRA, r16			; 
	
	ldi		r16,0b00000001		;    INT0 (B_Int Encoder)
	out		EIMSK, r16			; 
;-------------------------------------------------------------------------------------------------------------------------------



;===========================================================
;    INT0 -  
;===========================================================
INT_Encoder:
	cli
	push	r0
	push	r16
	push	zl
	push	zh
	in		r0, SREG

	mov		zl, _PvEnc			;   zl[1:0]

	clr		r16					;  
	sbic	PINC,3				;   (3=, PD2=B_Int)
	sbr		r16,(1<<1)			;   
	sbic	PIND,2				;  (=1, B_Int=0)  
	sbr		r16,(1<<0)			;  _PvEnc
	mov		_PvEnc,r16			;	

	sub		zl, _PvEnc			; Decode motion (zl = zl - _PvEnc)
	andi	zl, 3				; zl = zl and b11
	breq	enc_ext				; 00 -  
	cpi		zl, 1
	breq	enc_inc				; 01 -  
	cpi		zl, 3
	breq	enc_dec				; 11 -  
								; 10 - 
	mov		zl, _PvDir
	mov		zh, _PvDir			; Count by 2 for previous direction
	lsl		zl					; zl=zl shl 1, bit0 cleared
	asr		zh					; zh=zh shr 1, bit7=const, bit0 -> carry flag	
	rjmp	enc_add

enc_dec:
	ldi		zl, low(-1)
	ldi		zh, high(-1)
	rjmp	enc_setpvdir

enc_inc:
	ldi		zl, low(1)
	ldi		zh, high(1)
enc_setpvdir:
	mov		_PvDir, zl
enc_add:
	add		_PosL, zl			;  
	adc		_PosM, zh
	adc		_PosH, zh

enc_ext:
	out		SREG, r0
	pop		zh
	pop		zl
	pop		r16
	pop		r0
	sei
	reti






/* 


==============================  

  -   AVR ATmega ATtiny   - www.AVR123.nm.ru


  . 
http://www.roboforum.ru/viewtopic.php?f=2&t=1070

"  "    !!!         - 
      .   avr123.nm.ru/05.htm  
     
       
CVAVR 1.25.9 - CodeVisinAVR -          .
avr123.nm.ru        avr123.nm.ru/06.htm 

CVAVR  -      .      

; ************************************************************

      VMLAB     4.3 Mb  
(   www.amctools.com -  )  :

avr123.nm.ru/z2.htm
avr123.nm.ru/z3.htm
avr123.nm.ru/z9.htm

        VMLAB

; ************************************************************

  "-"    
  -   :  avr123.nm.ru/z4.htm

///////////////////////////////////////////////////////////////

//      - PROTEUS  !!! 
//    - http://www.oProteus.narod.ru
     
///////////////////////////////////////////////////////////////


     :

  CVAVR 1.25.9       avr123.nm.ru      2 
 
  PROTEUS 7.4 sp3 PRO    1.6.5

     http://www.roboforum.ru/viewtopic.php?f=2&t=2398

        PROTEUS 7.5 -  
    http://kazus.ru/forum/topic_13350-600.html

==============================

     -    - www.AVR123.nm.ru

 187   +  CD c     - -  .
   http://torrents.ru/forum/viewtopic.php?t=1652142

==============================

AVR Studio 4.16  build 628. : 127 MB     :
     http://www.efo.ru/ftp/pub/atmel/_AVR_MCUs_8bit/AvrStudio/AVR%20Studio%204.16%20628/AvrStudio416Setup.exe

*/