Make your own free website on Tripod.com

The AVR Assembler Site

HOME
AVR ASM TUTOR
ASM FORUM
AVR BEGINNERS NET
TUTORIAL #2
MUL & DIV
FAST MUL & DIV
16 BIT MUL
16 BIT ADD & SUB
32 BIT MATH
16 BIT MATH
16 BIT DIV
24 BIT DIV
32 BIT DIV
FLOAT MATH
SQRT16
BCD CONVERSIONS
16 BIT BCD
DEC TO ASCII
INTEGER TO ASCII
HEX TO ASCII
MOVING AVG
FAST FOURIER
BLOCK COPY
LOAD PROG MEM
EPROM STORAGE
SERIAL EPROM
AT45 DATAFLASH
FLASH CARD
VFX SMIL
VFX MEM
BUBBLE SORT
CRC CHECK
XMODEM REC
UART 304
UART 305
UART 128
UART BUFF
USB TO RS232
AVR ISP
ISP 2313
ISP 1200
AVR SPI
I2C 300
I2C 302
I2C TWI26
I2C/TWI 128
I2C/TWI AT8
DALLAS-1W
DALLAS CRC
ETHERNET DRIVER
TEA PROTOCOL
ADC
10 BIT ADC
CHEAP ADC
PRECISION 8 BIT ADC
THERMOMETER
INFARED DECODER
LCD DRIVER FOR HD44xxx
LCD DRIVER FOR HD44780
LCD DRIVER FOR HD44780 #2
4x4 KEYPAD
KEYPAD LED MUX
AT/PS2 KEYBOARD
AT KEYBOARD
PS2 KEYBOARD
MEGA 8 BOOTLOADER
BOOTLOADER
ALARM CLOCK
REAL TIME CLOCK
90 DAY TIMER
DELAY ROUTINE
CALLER ID
DTMF GENERATOR
6 CHAN PWM
PWM 10K
ENCODER
STH-11
ATMEL CORP
AVR BUTTERFLY
AVR BOOK

PS2 KEYBOARD EMULATOR FOR 8515

                                    ;***********************************************************************
                                    ;*
                                    ;* Number		: **.**
                                    ;* File Name		:"Main.asm"
                                    ;* Title		:Keymatrix interface to AT PS/2
                                    ;* 
                                    ;* Date (Orig.)		:2001.09.13.
                                    ;* Date (Last)		:2001.10.14.
                                    ;* Version 		:1.1.0
                                    ;* Support telephone	:+36-30-9541-658
                                    ;* Support fax		:+36-30-9809-372
                                    ;* Support E-mail	:info@vfx.hu
                                    ;* Target MCU		:AT90S8515
                                    ;*
                                    ;* DESCRIPTION
                                    ;* kezeli a kovetkezo eszkozoket:
                                    ;*  - 84 key  
                                    ;*  - 6 LED 
                                    ;*  - 16,11,11x Jacksly??
                                    ;*  - PS/2 style interface to AT or MARGOR CP-01
                                    ;*
                                    ;*	AT90S8515 PLCC Pin discriptions
                                    ;*
                                    ;*	ROW0	 T0 (PB0) 	 VCC
                                    ;*	ROW1     T1 (PB1) 	 (PA0) AD0  D0
                                    ;*	ROW2   AIN0 (PB2) 	 (PA1) AD1  D1
                                    ;*	ROW3   AIN1 (PB3) 	 (PA2) AD2  D2
                                    ;*      ROW4     SS (PB4) 	 (PA3) AD3  D3
                                    ;*      LED3 * MOSI (PB5) 	 (PA4) AD4  D4
                                    ;*	LED4 * MISO (PB6) 	 (PA5) AD5  D5
                                    ;*	LED5  * SCK (PB7) 	 (PA6) AD6  D6
                                    ;*		    RESET 	 (PA7) AD7  D7
                                    ;*	LED0    RxD (PD0) 	 ICP 
                                    ;*      LED1    TxD (PD1) 	 ALE
                                    ;*     	KCLK   INT0 (PD2) 	 OC1B
                                    ;*     	KDATA  INT1 (PD3) 	 (PC7) A15  D8
                                    ;*	LED2  	    (PD4) 	 (PC6) A14  D9
                                    ;*	ROW5   OC1A (PD5) 	 (PC5) A13  D10
                                    ;*	ROW6     WR (PD6) 	 (PC4) A12  D11
                                    ;*	ROW7     RD (PD7) 	 (PC3) A11  D12
                                    ;*	            XTAL2 	 (PC2) A10  D13
                                    ;*                  XTAL1 	 (PC1) A9   D14
                                    ;*     	              GND 	 (PC0) A8   D15
                                    ;*
                                    ;*	Quarc: f=3.6864 MHz (T=0.27126 us)
                                    ;*
                                    ;***********************************************************************
                                    .include "8515def.inc"
                                    
                                    
                                    
                                    ;**********************!!!!!!!!!!!!!!!!!!!!!!***************************
                                    
                                    ;* R15 regisztert modositani tilos!
                                    ;* Atjaro az interrupt rutinok es a foprogram kozott!!!!
                                    				; 0. = 1 ismetles kell a bill.nyomas utan, Timer lejart
                                    
                                    
                                    ;**********************!!!!!!!!!!!!!!!!!!!!!!***************************
                                    
                                    
                                    
                                    ;**************************************************************************
                                    ;* Const Def
                                    .EQU	BaudRate=23		;23 - 9600 @3.6864MHz
                                    
                                    .EQU	T0Freq=227		;220 @1.8432MHz/1024 = 50Hz
                                    				;184 @3.6864MHz/1024 = 50Hz
                                    				;220 @7.3728MHz/1024 = 200Hz
                                    				;227 @7.3728MHz/64   = 4000Hz/250us
                                    
                                    
                                    				;238 @3.6864MHz/1024 = 200Hz
                                    				;250 @3.6864MHz/1024 = 600Hz
                                    
                                    
                                    .EQU	ONESEC=200		;for 1s
                                    
                                    .EQU	FlashPeriod=1000	;LEDek villogasi periodusa ~250us egysegekben
                                    
                                    
                                    ;***************************************************************************
                                    ;***** Hardware Abstraction Layer
                                    
                                    .EQU	DTLo_PORT = DDRA
                                    .EQU	DTHi_PORT = DDRC			;2x8 bit adat port Pull-upos kell
                                    
                                    
                                    
                                    .EQU	Row0  = 0	;PortB kiosztasa
                                    .EQU	Row1  = 1
                                    .EQU	Row2  = 2
                                    .EQU	Row3  = 3
                                    .EQU	Row4  = 4
                                    .EQU	LED3 = 5				;LED3
                                    .EQU	LED4 = 6				;LED4
                                    .EQU	LED5 = 7				;LED5
                                    
                                    .EQU	LED345 = PORTB
                                    .EQU	Row01234 = PORTB
                                    
                                    
                                    .EQU	LED0  = 0	;PortD kiosztasa	 LED0
                                    .EQU	LED1  = 1				;LED1
                                    .EQU	LED2  = 4				;LED2
                                    .EQU	Row5  = 5
                                    .EQU	Row6  = 6
                                    .EQU	Row7  = 7
                                    
                                    .EQU	LED012 = PORTD
                                    .EQU	Row567 = PORTD
                                    
                                    
                                    ;******************************************************************************
                                    ;**** VARIABLES
                                    .DSEG
                                    Flag1:	 .BYTE 1		;Flagbitek
                                    				;2   = paros/paratlan szamlalo led villogtatashoz
                                    				;1:0 = scan mode 01 = scan1, 10 =  scan2, 11 = scan3
                                    
                                    CNT1L:	 .BYTE 1
                                    CNT1H:	 .BYTE 1		;software Timer
                                    
                                    CNT2L:	 .BYTE 1
                                    CNT2H:	 .BYTE 1		;LED villogtatas freki
                                    
                                    
                                    KeyHead: .BYTE 1		;Bill. puffer eleje
                                    KeyTrail:.BYTE 1		;Bill. puffer vege
                                    
                                    
                                    WTime:	 .BYTE 2		;Bill.nyomas utan a varakozas ido
                                    WRep:	 .BYTE 2		;Bill.nyomas utan a ismetlesi ido 
                                    
                                    PS2Flag: .BYTE 1		;PS2 flag csak az int. rutin hasznalhatja
                                    				;3. = 1 Parameterre var, komunikacio kozben vagyunk
                                    				;2. = 1 Request resend
                                    				;1. = 1 Resend last data to host
                                    				;0. = 1 Send ACK
                                    				
                                    PS2NextA:.BYTE 2		;PS2 program cim, ahova a kovetkezo int ugrik
                                    LastSend:.BYTE 1		;PS2 Utoljara kuldott kod
                                    RepChar: .BYTE 1		;karakter sorszama amit ismetelni kell
                                    
                                    
                                    StatLEDs:.BYTE 2		;STAUS LEDek  allapota eg / nem eg
                                    				;masodik byte a villogas mask byte-ja
                                    
                                    
                                    ParamErr:.BYTE 1		;parameterre max 4ms ideig var utana hiba
                                    
                                    
                                    
                                    .EQU	KeyBuffLen = 17
                                    KeyBuff:  .BYTE KeyBuffLen		;Transmit key-buffer
                                    
                                    
                                    
                                    .EQU	KeyCount = 128			;max 128 bill.,de csak 119 db billenyut kell kezelni
                                    Matrix:	  .BYTE KeyCount		;Keys allapota
                                    
                                    
                                    
                                    ;**************************************************************************************
                                    .ESEG
                                    	.db	"MAGOR HI-01 Keyboard-matrix Controller by VFX. V1.1.0 [orig. 2001.09.16] "
                                    	.db	"Last Upd. [2001.10.14]  Info: info@vfx.hu  Data area>>"
                                    
                                    ;*****************************************************************************
                                    ;****		I N T E R R U P T S
                                    ;****
                                    ;*****************************************************************************
                                    .CSEG
                                    		rjmp RESET	 ; Reset Handler
                                    		rjmp EXT_INT0	 ; IRQ0 Handler
                                    		rjmp EXT_INT1	 ; IRQ1 Handler
                                    		rjmp TIM1_CAPT	 ; Timer1 Capture Handler
                                    		rjmp TIM1_COMPA	 ; Timer1 CompareA Handler
                                    		rjmp TIM1_COMPB	 ; Timer1 CompareB Handler
                                    		rjmp TIM1_OVF	 ; Timer1 Overflow Handler
                                    		rjmp TIM0_OVF	 ; Timer0 Overflow Handler
                                    		rjmp SPI_INT	 ; SPI Transfer Complete Handler
                                    		rjmp UART_RXC	 ; UART RX Complete Handler
                                    		rjmp UART_DRE	 ; UDR Empty Handler
                                    		rjmp UART_TXC	 ; UART TX Complete Handler
                                    		rjmp ACI_INT	 ; Analog Comparator Handler
                                    
                                    
                                    ;*********************
                                    EXT_INT0:	RETI
                                    EXT_INT1:	RETI
                                    TIM1_CAPT:	RETI
                                    ;************************
                                    ;* Key Deleay & Rep Time
                                    ;************************
                                    TIM1_COMPA:    	PUSH	ZL
                                    		IN	ZL,SREG
                                    		PUSH	ZL
                                    
                                    		ldd	ZL,Y+WRep+1-RAMSTRT
                                    		out	OCR1AH,ZL
                                    		ldd	ZL,Y+WRep+0-RAMSTRT
                                    		out	OCR1AL,ZL		;rep. timer beallitasa
                                    		ldi	ZL,1
                                    		or	R15,Zl			;timer lejart ismetles mehet Flag
                                    		
                                    		POP	ZL
                                    		OUT	SREG,ZL
                                    		POP	ZL
                                    		RETI
                                    ;**
                                    TIM1_COMPB:	RETI
                                    TIM1_OVF:	RETI
                                    
                                    ;*********************
                                    ;* RealTime Clock Int
                                    ;*********************
                                    ;* 
                                    TIM0_OVF:	PUSH	ZL
                                    		PUSH	ZH
                                    		IN	ZL,SREG
                                    		PUSH	ZL
                                    
                                    		LDI	ZL,T0Freq	
                                    		OUT	TCNT0,ZL		;Reload  Timer
                                    
                                    		LDD	ZL,Y+CNT1L-RAMSTRT
                                    		LDD	ZH,Y+CNT1H-RAMSTRT
                                    		OR	ZL,ZH
                                    		BREQ	TIM_01
                                    		LDD	ZL,Y+CNT1L-RAMSTRT
                                    		SBIW	ZL,1
                                    		STD	Y+CNT1L-RAMSTRT,ZL
                                    		STD	Y+CNT1H-RAMSTRT,ZH
                                    
                                    TIM_01:
                                    		LDD	ZL,Y+CNT2L-RAMSTRT
                                    		LDD	ZH,Y+CNT2H-RAMSTRT
                                    		OR	ZL,ZH
                                    		BREQ	TIM_02
                                    		LDD	ZL,Y+CNT2L-RAMSTRT
                                    		SBIW	ZL,1
                                    		STD	Y+CNT2L-RAMSTRT,ZL
                                    		STD	Y+CNT2H-RAMSTRT,ZH
                                    
                                    TIM_02:		POP	ZL
                                    		OUT	SREG,ZL
                                    		POP	ZH
                                    		POP	ZL
                                    		RETI
                                    
                                    ;********************
                                    ;* SPI Int Handle
                                    ;********************
                                    SPI_INT:	RETI 
                                    UART_RXC:	RETI
                                    UART_DRE:	RETI
                                    UART_TXC:	RETI
                                    EPR_INT:	RETI
                                    ACI_INT:	RETI
                                    
                                    
                                    
                                    
                                    ;**********************************************************************
                                    ;* Iclude files 
                                    
                                    .include	"keyb.asm"
                                    .include	"ps2.asm"
                                    .include	"EEprom.asm"
                                    
                                    
                                    ;**********************************************************************
                                    ;* RESET 
                                    ;**********************************************************************
                                    RESET:	CLI			;GLOBAL INTERRUP DIS.	
                                    	clr	R15		;Flags!!!
                                    
                                    
                                    	ldi r16, high(RAMEND)	;setup stack
                                    	out SPH,r16
                                    	ldi r16, low(RAMEND)
                                    	out SPL,r16
                                    
                                    	LDI	R16,0b00101010	;Sleep enable, Idle mode,INT0, INT1 fall-edge sens.
                                    	OUT	MCUCR,R16	;7. =1 SRE External SARAM Enabled, Alternate Function 
                                    				;6. =1 SRW External SRAM WaitStates Enabled
                                    				;5. = 1 Sleep Enable
                                    				;4. = 0 SLEEP => Idle Mode
                                    				;     1 SLEEP => Power Down Mode
                                    				;3:2 = Inerrupt Sense Control for INT1
                                    				; 00 -> Low Level INT1
                                    				; 01 -> Reserved
                                    				; 10 -> Falling Edge INT1
                                    				; 11 -> Rising Edge INT1
                                    				;1:0 = Inerrupt Sense Control for INT0
                                    				; 00 -> Low Level INT0
                                    				; 01 -> Reserved
                                    				; 10 -> Falling Edge INT0
                                    				; 11 -> Rising Edge INT0
                                    
                                    
                                    	LDI	R16,0b00000000		;INT0, INT1 Disabled
                                    	OUT	GIMSK,R16	;7. = 1 EXT INT1 ACTIVE
                                    				;6. = 1 EXT INT0 ACTIVE
                                    
                                    
                                    
                                    	LDI	R16,0b01000010
                                    	OUT	TIMSK,R16 	;7. = 1 TC1 OWRF EN.  *
                                    				;6. = 1 TC1 COMP MATCH EN.
                                    				;3. = 1 TC1 INP CAPTURE EN.
                                    				;1. = 1 TC0 OWRF EN.   *
                                    	
                                    
                                    	LDI	YL,LOW(RAMSTRT)
                                    	LDI	YH,HIGH(RAMSTRT) ;BAZIS 'Y' +0
                                    
                                    
                                    ;************************************************************************
                                    ;*Prot/Keymatrix and Hardware Init
                                    ;*
                                    
                                    	ldi	R16,255
                                    	STD	Y+RepChar-RAMSTRT,R16 		;ismetelhetettlen karakter sorszam
                                    
                                    	clr	R17
                                    
                                    	out	DTLo_PORT,R17
                                    	out	DTLo_PORT+1,R16			;Lower 8 data bit Input & Pull up
                                    
                                    	out	DTHi_PORT,R17
                                    	out	DTHi_PORT+1,R16			;Higher 8 data bit Input & Pull up
                                    
                                    
                                    	out	DDRB,R16
                                    	out	DDRB+1,R16			;Row0..4 output & H, LED3,4,5 Nem vilagit
                                    
                                    
                                    	ldi	R17,255-(1< OC1 not connected   ; Disconnect OC1A
                                    				;    01 => OC1 not connected   ; Togle OC1A
                                    				;    10 => OC1 non-inv  PWM    ; Clear OC1A
                                    				;    11 => OC1 inverted PWM    ; Set OC1A
                                    				;5:4 00 => OC1 not connected       ""  OC1B
                                    				;    01 => OC1 not connected       ""
                                    				;    10 => OC1 non-inverted PWM    ""
                                    				;    11 => OC1 inverted PWM        ""
                                    				;1:0 00 => PWM Disabled
                                    				;    01 =>  8 bit PWM
                                    				;    10 =>  9 bit PWM
                                    				;    11 => 10 bit PWM
                                    
                                    
                                    	LDI	R17,0b00001000
                                    	OUT	TCCR1B,R17	;7. = 0 Input Capture Noise Canceller Disabled
                                    				;6. = 0 InputCapture ICP Falling edge
                                    				;3. = 1 Reset TC1 when copare match
                                    				;2:0 Clock select
                                    				;   000 => TC1 Stop
                                    				;   001 => Ck
                                    				;   010 => Ck/8
                                    				;   011 => Ck/64
                                    				;   100 => Ck/256
                                    				;   101 => Ck/1024
                                    				;   110 => External T1 Falling Edge
                                    				;   111 => External T1 rising Edge
                                    
                                    
                                    ;************************************
                                    ;**  Timer0 Init		*****
                                    ;**
                                    	LDI	R16,T0Freq
                                    	OUT	TCNT0,R16	;megszakitas frekvenciaja
                                    
                                    	LDI	R16,0b00000011
                                    	OUT	TCCR0,R16	;TC0 Control Register
                                    				;2:0 Clock Select
                                    				; 000 -> Stop
                                    				; 001 -> CK
                                    				; 010 -> CK/8
                                    				; 011 -> CK/64
                                    				; 100 -> CK/256
                                    				; 101 -> CK/1024
                                    				; 110 -> External pin T0 falling edge
                                    				; 111 -> External pin T0 risinrg edge
                                    
                                    
                                    
                                    
                                    		SEI				;SET GLOBAL INT EN.
                                    
                                    
                                    
                                    		CBI	LED012,LED0		;LEDx vilagit
                                    		CBI	LED012,LED1
                                    		CBI	LED012,LED2
                                    		CBI	LED345,LED3
                                    		CBI	LED345,LED4
                                    		CBI	LED345,LED5
                                    
                                    		ldi	R16,Low(ONESEC)
                                    		STD	Y+CNT1L-RAMSTRT,R16	;software down counter, wait for 1s 
                                    		ldi	R16,High(ONESEC)
                                    		STD	Y+CNT1H-RAMSTRT,R16 
                                    
                                    
                                    wait1:		LDD	R16,Y+CNT1L-RAMSTRT 
                                    		LDD	R17,Y+CNT1H-RAMSTRT 
                                    		or	R16,R17
                                    		brne	wait1
                                    
                                    
                                    		SBI	LED012,LED0		;LEDx nem vilagit
                                    		SBI	LED012,LED1
                                    		SBI	LED012,LED2
                                    		SBI	LED345,LED3
                                    		SBI	LED345,LED4
                                    		SBI	LED345,LED5
                                    
                                    		ldi	ZL,low(0)			;led villogtatas kb 0.5s
                                    		STD	Y+CNT2L-RAMSTRT,ZL
                                    		ldi	ZH,high(0)
                                    		STD	Y+CNT2H-RAMSTRT,ZH
                                    
                                    
                                    ;***********************************************************************
                                    ;***********************************************************************
                                    ;********     M     A      I      N				********
                                    ;********							********
                                    ;***********************************************************************
                                    WWW:		ldi	ZL,low(40)
                                    		STD	Y+CNT1L-RAMSTRT,ZL
                                    		ldi	ZH,high(40)
                                    		STD	Y+CNT1H-RAMSTRT,ZH
                                    
                                    Main: 		SLEEP
                                    		rcall	PS2_Com
                                    
                                    		LDD	ZL,Y+CNT2L-RAMSTRT
                                    		LDD	ZH,Y+CNT2H-RAMSTRT
                                    		OR	ZL,ZH
                                    		BRNE	LEDkihagy
                                    
                                    		ldi	ZL,low(FlashPeriod)			;led villogtatas kb 0.5s
                                    		STD	Y+CNT2L-RAMSTRT,ZL
                                    		ldi	ZH,high(FlashPeriod)
                                    		STD	Y+CNT2H-RAMSTRT,ZH
                                    
                                    		LDD	R16,Y+Flag1-RAMSTRT
                                    		ldi	R17,0b0000100
                                    		eor	R16,R17
                                    		STD	Y+Flag1-RAMSTRT,R16		;paros/paratlan bit valtas
                                    	
                                    		rcall	Puts_LEDs
                                    
                                    
                                    LEDkihagy:	LDD	R16,Y+CNT1L-RAMSTRT
                                    		cpi	R16,0
                                    		brne	Main
                                    
                                    		rcall	ScanMatrix			;keymatrix beolvasasa a memoriaba
                                    		rcall	MakeScanC
                                    		rjmp	WWW
                                    
                                    
                                    
                                    
                                    
                                    ;* LEd 7  6  5  4  3  2  1  0
                                    ;*           3P 2P 1P 3Z 2Z 1Z
                                    
                                    Puts_LEDs:	LDD	R16,Y+Flag1-RAMSTRT
                                    		andi	R16,4				;D0 = Paros/paratlan bit
                                    		ldi	R18,0b00000111			;csak zoldek villognak
                                    		breq	paros
                                    		ldi	R18,0b00111000			;csak pirosak villognak
                                    
                                    paros:		LDD	R16,Y+StatLEDs+1-RAMSTRT	;villogas mask = 1 villog
                                    		and	R18,R16				
                                    
                                    		LDD	R16,Y+StatLEDs+0-RAMSTRT	;LED data = 1 villagit
                                    		or	R16,R18
                                    		com	R16
                                    		andi	R16,0b00111111			;inv. kell mert kozos anodosak a ledek
                                    
                                    		mov	R17,R16
                                    		rol	R17
                                    		rol	R17
                                    		andi	R17,0b00010000
                                    		mov	R18,R16
                                    		andi	R18,0b00000011
                                    		or	R17,R18				;ez megy ki a PD portra, zold ledek
                                    		in	R18,LED012
                                    		andi	R18,0b11101100
                                    		or	R18,R17
                                    		out	LED012,R18			;LED0,1,2 kikuldve
                                    		
                                    		rol	R16
                                    		rol	R16
                                    		andi	R16,0b11100000
                                    		in	R17,LED345
                                    		andi	R17,0b00011111
                                    		or	R17,R16
                                    		out	LED345,R17			;LED3,4,5 kikuldve
                                    
                                    		ret
                                    
                                    
                                    ;***********************************************************************
                                    ;*
                                    ;* File Name		:"PS2.asm"
                                    ;* Title		:PS/2 communication protocoll
                                    ;* 
                                    ;* Date (Orig.)		:2001.10.06.
                                    ;* Date (Last)		:2001.10.12.
                                    ;* Version 		:1.0.10
                                    ;* Support telephone	:+36-30-9541-658
                                    ;* Support fax		:+36-30-9809-372
                                    ;* Support E-mail	:info@vfx.hu
                                    ;* Target MCU		:AT90S8515
                                    ;*
                                    ;* DESCRIPTION
                                    ;*  - PS/2 style communication to AT or MARGOR CP-01
                                    ;*
                                    ;*	CLK & DATA = H	-> Two way communication 
                                    ;*	DATA = L	-> PC (MAGOR) sends to Keyb
                                    ;*	CLK = L		-> PC (MAGOR) inhibits communiaction
                                    
                                    ;**************************************************************************
                                    ;* Const Def
                                    
                                    
                                    ;***************************************************************************
                                    ;***** Hardware Abstraction Layer
                                    
                                    .EQU	PS2_CLKPort = DDRD
                                    .EQU	PS2_DATPort = DDRD
                                    
                                    .EQU	KCLK  = 2
                                    .EQU	KDATA = 3
                                    
                                    
                                    ;******************************************************************************
                                    ;**** VARIABLES
                                    .DSEG
                                    
                                    .EQU	NoError   = 0		;No Error
                                    .EQU	RxF_Abort = 2		;Abort communication
                                    .EQU	TxBreak   = 3		;Abort Transmit
                                    .EQU	Parity_Err= 4		;Parity Error
                                    
                                    
                                    ;******************************************************************************
                                    .ESEG
                                    
                                    
                                    ;******************************************************************************
                                    .CSEG
                                    
                                    Init_PS2:	CBI	PS2_CLKPort,KCLK
                                    	CBI	PS2_CLKPort+1,KCLK	;CLK pin tre-stated external pull-up res!!
                                    
                                    		CBI	PS2_DATPort,KDATA
                                    	CBI	PS2_DATPort+1,KDATA	;DATA pin tre-stated external pull-up res!!
                                    
                                    		ldi	R16,0
                                    		STD	Y+PS2NextA+0-RAMSTRT,R16
                                    		STD	Y+PS2NextA+1-RAMSTRT,R16 ;nincs aktiv komunikacio folyamatban
                                    		STD	Y+PS2Flag-RAMSTRT,R16 
                                    
                                    		ret
                                    
                                    
                                    ;******************************************************************************
                                    ;** Communication between Host and Keyboard
                                    ;**
                                    ;******************************************************************************
                                    PS2_Com:	sbis	PS2_CLKPort-1,KCLK
                                    		 ret				;if clk pin low, comm. inhibit
                                    		sbis	PS2_DATPort-1,KDATA
                                    		 rjmp	RxCommand		;if data pin low, HOST sends command/data
                                    
                                    
                                    		LDD	R16,Y+PS2Flag-RAMSTRT	;Request Resend?
                                    		andi	R16,0b00000100
                                    		breq	Ps2_C2
                                    		ldi	R16,0xFE		;RESEND Command
                                    		mov	R1,R16
                                    		rcall	SendByte		;keres elkuldese
                                    		brcs	ResSendERR
                                    		LDD	R16,Y+PS2Flag-RAMSTRT	;Request Resend flag torlese
                                    		andi	R16,0b11111011
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    
                                    
                                    Ps2_C2:		LDD	R16,Y+PS2Flag-RAMSTRT	;Resend last data?
                                    		andi	R16,0b00000010
                                    		breq	Ps2_C0
                                    		ldd	R1,Y+LastSend-RAMSTRT	;utoljara kuldott karakter ismetleshez
                                    		rcall	SendByte		;ujrakuldes
                                    		brcs	ResSendERR
                                    		LDD	R16,Y+PS2Flag-RAMSTRT	;Resend flag torlese
                                    		andi	R16,0b11111101
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    ResSendERR:	ret				;ha hiba volt ujra kuldjuk
                                    
                                    Ps2_C0:		LDD	R16,Y+PS2Flag-RAMSTRT	;Send ACK flag active?
                                    		andi	R16,0b00000001
                                    		breq	Ps2_C1
                                    		ldi	R16,0xFA		;Send ACK to HOST
                                    		mov	R1,R16
                                    		rcall	SendByte
                                    		brcs	ResSendERR		;ha hiba van ujrakuldes lesz
                                    		LDD	R16,Y+PS2Flag-RAMSTRT	;Host Abort
                                    		andi	R16,0b11111110		;ACK flag torlove sikeresen elment		
                                    		STD	Y+PS2Flag-RAMSTRT,R16	;komunikacio nincs
                                    		ret
                                    
                                    Ps2_C1:		LDD	R16,Y+PS2Flag-RAMSTRT	;Parameterre var?
                                    		andi	R16,0b00001000
                                    		breq	Ps2_C3			;ha nem, akkor hadmenjen
                                    
                                    		LDD	R16,Y+ParamErr-RAMSTRT	;parameterr error counter>4ms -> igen, hiba
                                    inc	R16
                                    		cpi	R16,20			;20*250us ido ~4..5ms
                                    		brcs	Megidovan		;idotullepes!!
                                    
                                    		LDD	R16,Y+PS2Flag-RAMSTRT	;Parameterre var bit torlese, nem varunk tovabb!
                                    		andi	R16,0b11110111
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		clr	R16
                                    		STD	Y+PS2NextA+0-RAMSTRT,R16
                                    		STD	Y+PS2NextA+1-RAMSTRT,R16 ;alapallapotba hozas!
                                    Megidovan:	STD	Y+ParamErr-RAMSTRT,R16
                                    		ret
                                    
                                    
                                    Ps2_C3:		LDD	ZL,Y+PS2NextA+0-RAMSTRT
                                    		LDD	ZH,Y+PS2NextA+1-RAMSTRT
                                    		mov	R16,ZL
                                    		or	R16,ZH			;ha nincs komunikacio folyamatban
                                    		breq	SendScan		;akkor kuldjunk scan kodot
                                    		ijmp				;ugras a komunikacios rutinra
                                    
                                    ;********************************************
                                    ;** Send scan/fifo
                                    ;********************************************
                                    SendScan:	ldi	ZL,Low(KeyBuff)
                                    		ldi	ZH,High(KeyBuff)
                                    		LDD	R0,Y+KeyHead-RAMSTRT
                                    		LDD	R16,Y+KeyTrail-RAMSTRT
                                    		cp	R0,R16
                                    		brne	SendScn1
                                    		ret
                                    SendScn1:	clr	R0
                                    		add	Zl,R16
                                    		adc	ZH,R0
                                    		ld	R1,Z
                                    		inc	R16
                                    		cpi	R16,KeyBuffLen
                                    		brcs	SendScn2
                                    		clr	R16
                                    SendScn2:	STD	Y+KeyTrail-RAMSTRT,R16	;uj pointer elmentve
                                    		rcall	SendByte
                                    		brcc	NoSendErr		;nincs hiba siman elment
                                    		LDD	R16,Y+PS2Flag-RAMSTRT	;Resend flag beallitasa
                                    		ori	R16,0b00000010
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    NoSendErr:	ret
                                    
                                    
                                    ;*****************************************
                                    ;** Send R1 to Host
                                    ;** C=1 hiba tortent R17= hibakod
                                    ;*****************************************
                                    SendByte:	sbis	PS2_CLKPort-1,KCLK
                                    		 rjmp	SendByte		;wait for clk=hi
                                    		rcall	Delay45us
                                    		rcall	Delay5us		;wait 50us
                                    		sbis	PS2_CLKPort-1,KCLK
                                    		 rjmp	SendByte		;wait for clk=hi
                                    		sec
                                    		std	Y+LastSend-RAMSTRT,R1	;utoljara kuldott karakter
                                    		ldi	R17,TxBreak		;R17= Transmit break
                                    		sbis	PS2_DATPort-1,KDATA	;Is Data still hi?
                                    		 ret				;ha nem,vissza R17 hibakod!
                                    		rcall	Delay40us
                                    		clr	R0			;R0=0 start bit
                                    		rcall	SendBit
                                    		brcs	SendAborted
                                    
                                    		ldi	R16,8
                                    		mov	R3,R16
                                    		mov	R0,R1
                                    		clr	R2
                                    SendLoop:	rcall	SendBit
                                    		brcs	SendAborted
                                    		dec	R3
                                    		brne	SendLoop
                                    inc	R2			;paratlan paritas kell!!!!!???
                                    		mov	R0,R2
                                    		rcall	SendBit
                                    		clr	R0
                                    		dec	R0			;R0=255 stop bit =H
                                    		rcall	SendBit
                                    		rcall	Delay15us
                                    		rcall	Delay15us
                                    		ldi	R17,NoError
                                    clc
                                    		ret
                                    SendAborted:	ldi	R17,TxBreak		;R17= Transmit break
                                    		sec
                                    		ret		
                                    
                                    ;* C=1 break, C=0 normal transmit
                                    SendBit:	ror	R0			;0. bit -> C
                                    		brcc	bit0
                                    bit1:         	CBI	PS2_DATPort,KDATA	;DATA pin H
                                    		inc	R2			;paritas no egyel
                                    		rjmp	bitn
                                    bit0:          	SBI	PS2_DATPort,KDATA	;DATA pin L
                                    bitn:		rcall	Delay20us
                                    		SBI	PS2_CLKPort,KCLK	;clk = L
                                    		rcall	Delay40us
                                    		CBI	PS2_CLKPort,KCLK	;clk = H
                                    		rcall	Delay20us
                                    		clc
                                    		sbis	PS2_CLKPort-1,KCLK	;Clock=high?
                                    		 sec				;if no -> C=1
                                    		ret
                                    
                                    
                                    ;******************************************************************************
                                    ;* R E C E I V E 
                                    ;*
                                    ;* 80 us/bit device generated clock - host changes the Data line only when
                                    ;* the Clock line is low, and data is latched on the rising edge of the clock pulse
                                    ;* Host may abort transmission at time before the 11th clock pulse (acknowledge bit)
                                    ;* by holding Clock low for at least 100 microseconds. 
                                    ;*
                                    ;* Uses:R1,R2,R17,R18
                                    
                                    RxStream:	sbis	PS2_CLKPort-1,KCLK	;Wait for Clock=high 
                                    		 rjmp	RxStream
                                    
                                    		sbic	PS2_DATPort-1,KDATA	;Is Data still low?
                                    		 rjmp	RxErr			;ha nem, akkor hiba!
                                    
                                    ;Read each bit (8 data bits, parity bit, and stop bit) as follows: 
                                    		clr	R1			;data reg.
                                    		clr	R2			;parity reg.
                                    		ldi	R17,8			;8 bit
                                    
                                    RxBitLoop:	rcall	RxBit			;R18-ban a bit
                                    		add	R2,R18
                                    		ror	R18			;adatbit a C flagben
                                    		ror	R1
                                    		sbis	PS2_CLKPort-1,KCLK	;Clock=high?
                                    		 rjmp	RxAbort			;if no then Abort
                                    		dec	R17
                                    		brne	RxBitLoop
                                    
                                    						;Read parity bit 
                                    		rcall	RxBit			;R18-ban a bit
                                    		inc	R2			;paratlan paritas kell!!??!!!?????
                                    eor	R2,R18			;R2 = parity xor parity bit
                                    		sbis	PS2_CLKPort-1,KCLK
                                    		 rjmp	RxAbort
                                    		
                                    	;Read stop bit 
                                    		rcall	RxBit			;R18-ban a bit
                                    		sbis	PS2_CLKPort-1,KCLK
                                    		 rjmp	RxAbort
                                    
                                    		sbis	PS2_DATPort-1,KDATA	;Is Data still low?
                                    		 rjmp	RxErr1			;Data still equals 0?
                                    						;Output Acknowledge bit 
                                    		rcall	SendAckBit		;Acknowledge bit 
                                    		rcall	Delay45us		;Delay 45 microseconds
                                    						;(to give host time to inhibit
                                    						;next transmission.) 
                                    		ldi	R17,0			;no error flag
                                    		ror	R2			;C-flagben a parity
                                    		brcs	RxParErr
                                    		ret				;R1 = reseived data
                                    
                                    
                                    RxParErr:	ldi	R17,Parity_Err		;Parity Error
                                    		sec				;C flag =0
                                    		ret
                                    
                                    RxAbort:	ldi	R17,RxF_Abort		;Abort
                                    		sec				;C flag =0
                                    		ret
                                    
                                    ;*Keep clocking until Data=1 then generate an error
                                    
                                    RxErr1:		rcall	RxBit
                                    		sbis	PS2_DATPort-1,KDATA	;Is Data still low?
                                    		 rjmp	RxErr1			;ha nem, akkor hiba!						;Data still equals 0? Keep clocking until Data=1 then generate an error
                                    RxErr:		
                                    		ret
                                    
                                    ;Read a bit
                                    RxBit:		rcall	Delay20us
                                    		SBI	PS2_CLKPort,KCLK	;clk = L
                                    		rcall	Delay40us
                                    		CBI	PS2_CLKPort,KCLK	;clk = H
                                    		rcall	Delay20us
                                    		clr	R18			
                                    		sbic	PS2_DATPort-1,KDATA
                                    		 ldi	R18,1			;R18 a beolvasott adatbit
                                    		ret
                                    
                                    ;Send the acknowledge bit
                                    SendAckBit:	rcall	Delay15us
                                    		SBI	PS2_DATPort,KDATA	;data = L
                                    		rcall	Delay5us
                                    		SBI	PS2_CLKPort,KCLK	;clk = L
                                    		rcall	Delay40us
                                    		CBI	PS2_CLKPort,KCLK	;clk = H
                                    		rcall	Delay5us
                                    		CBI	PS2_DATPort,KDATA	;data = H
                                    		ret
                                    
                                    
                                    
                                    ;******************************************************************************
                                    ;* Delayxxus
                                    ;*
                                    ;*rcall [3] + wait [xx] +ret [4] = Delay
                                    ;*	@7.3728MHz
                                    ;*	45 us [332cycl 
                                    ;*	40 us [296cycl 
                                    ;*	20 us [148cycl
                                    ;*	10 us [74cycl]
                                    ;*	 5 us [37cycl]
                                    Delay45us:	ldi	R16,104		;[1] 104*3+10 cycl
                                    		rjmp	DelayLoop	;[2]
                                    Delay5us:	ldi	R16,9		;[1] 9*3+10 cycl
                                    		rjmp	DelayLoop	;[2]
                                    Delay15us:	ldi	R16,34		;[1] 34*3+10 cycl
                                    		rjmp	DelayLoop	;[2]
                                    Delay20us:	ldi	R16,46		;[1] 46*3+10 cycl
                                    		rjmp	DelayLoop	;[2]
                                    Delay40us:	ldi	R16,96		;[1] 96*3 cycl
                                    DelayLoop:	dec	R16		;[1]
                                    		brne	DelayLoop	;[1/2]
                                    		ret			;[4]
                                    
                                    
                                    
                                    ;*******************************************
                                    ;** Host request to send... receive data
                                    ;**
                                    ;*******************************************
                                    RxCommand:	LDD	ZL,Y+PS2NextA+0-RAMSTRT
                                    		LDD	ZH,Y+PS2NextA+1-RAMSTRT
                                    		mov	R16,Zl
                                    		or	R16,ZH
                                    		breq	NewCommand
                                    	ijmp				;ha nem uj, akkor ugras a vegrehajtasra
                                    
                                    NewCommand:	rcall	RxStream		;Read data R1, C=1 hiba, R17 hibakod
                                    		brcc	RxComNoErr
                                    
                                    ReqResend:	LDD	R16,Y+PS2Flag-RAMSTRT	;Host read error Resend needed
                                    		ori	R16,0b00000100		
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    NoValidComm:   	ret
                                    
                                    RxComNoErr:	ldi	ZL,Low(2*ComTab)	;uj parancs jott
                                    		ldi	ZH,High(2*ComTab)       		
                                    RxCommLoop:	lpm
                                    		tst	R0			;ha a tabla vege es nincs parancs kihagyjuk
                                    		breq	NoValidComm
                                    		cp	R0,R1		
                                    		breq	HitComm
                                    		adiw	ZL,2*(ComNex-ComTab)	;kovetkezo parancs
                                    		rjmp	RxCommLoop
                                    
                                    HitComm:	adiw	ZL,2			;pointerre mutat
                                    		lpm	
                                    		mov	R1,R0			;low byte
                                    		adiw	ZL,1
                                    		lpm
                                    		mov	ZH,R0			;hi byte
                                    		mov	ZL,R1
                                    		ijmp				;ugras a parancs vegrehajtasara
                                    
                                    ComTab:		.dw	0xED,STATUS_IND	;Turns on/off LEDs
                                    ComNex:		.dw	0xEE,ECHO	;Echo
                                    		.dw	0xF0,ALT_SCAN	;Set Scan Code Set
                                    		.dw	0xF3,TYPE_RATE	;Set repeat rate 
                                    		.dw	0xF4,KEN	;Enables keysacnning
                                    		.dw	0xF5,KDIS	;Disable keyscanning
                                    		.dw	0xF6,SET_DEF	;Set Default Value
                                    		.dw	0xF7,ALL_TYPE	;Set all keys - Typematic
                                    		.dw	0xF8,ALL_BREAK	;Set all keys - Make/Break
                                    		.dw	0xF9,ALL_MAKE	;Set all keys - Make
                                    		.dw	0xFA,ALL_MTB	;Set all keys - Typematic/Make/Break
                                    		.dw	0xFB,KEY_TYPE	;Set ket type - Typematic
                                    		.dw	0xFC,KEY_BREAK	;Set ket type - Make/Break
                                    		.dw	0xFD,KEY_MAKE	;Set ket type - Make
                                    		.dw	0xFE,RESEND	;Resend
                                    		.dw	0xFF,KRES	;Reset Command
                                    		.dw	0xF2,READID	;Read ID Command
                                    		.dw	0x00		;ez az utolso sor!
                                    
                                    
                                    
                                    
                                    Read_Data:	rcall	RxStream		;Read data R1, C=1 hiba, R17 hibakod
                                    		brcs	RD_Err
                                    		ret
                                    RD_Err:		LDD	R16,Y+PS2Flag-RAMSTRT	;Host read error Resend needed
                                    		ori	R16,0b00000100		
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		sec
                                    	   	ret
                                    
                                    
                                    ;******************************************************************************
                                    ;* C O M M A N D S   from host
                                    ;*
                                    ;******************************************************************************
                                    ;*Set/reset status indicators. A '1' bit turns the indicator ON.
                                    ;*	0. Scrollock,LED0 Green
                                    ;*	1. Numlock,  LED0 Red
                                    ;*	2. Capslock, LED1 Green
                                    ;*	3. 	     LED1 Red
                                    ;*	4. 	     LED2 Green
                                    ;*	5. 	     LED2 Red
                                    ;*	6. =1 Flash
                                    ;*	7. =0  
                                    ;*******************************************************
                                    STATUS_IND:	ldi	ZL,Low(StatInd1)
                                    		ldi	ZH,High(StatInd1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK & Parameter bit flag active
                                    		ori	R16,0b00001001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    StatInd1:	rcall	Read_Data
                                    		brcc	SatInd2
                                    		ret
                                    SatInd2:	mov	R16,R1
                                    		andi	R16,0b01000000			;flash bit active?
                                    		ldi	R16,0b00111111
                                    		breq	noflash		
                                    		and	R16,R1
                                    		STD	Y+StatLEDs+1-RAMSTRT,R16
                                    		rjmp	SatInd3
                                    noflash:	and	R16,R1
                                    		STD	Y+StatLEDs+0-RAMSTRT,R16
                                    SatInd3:	LDD	R16,Y+PS2Flag-RAMSTRT
                                    		andi	R16,0b11110111			;parameter renben megjott, torolve a bit
                                    		ori	R16,0b00000001			;ACK kuldes kell
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		rcall	Puts_LEDs			;uj led allapot kikuldese
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    ECHO:		ldi	ZL,Low(Echo1)
                                    		ldi	ZH,High(Echo1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		ret
                                    Echo1:		ldi	R16,0xEE			;Echo valasz a parancsra
                                    		mov	R1,R16
                                    		rcall	SendByte			;Send
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    
                                    ALT_SCAN:	ldi	ZL,Low(Altscan1)
                                    		ldi	ZH,High(Altscan1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK & Parameter bit flag active
                                    		ori	R16,0b00001001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    Altscan1:	rcall	Read_Data
                                    		brcc	Altscan2
                                    		ret
                                    Altscan2:	LDD	R16,Y+PS2Flag-RAMSTRT
                                    		andi	R16,0b11110111			;parameter renben megjott, torolve a bit
                                    		ori	R16,0b00000001			;ACK kuldes kell
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    
                                    		mov	R16,R1				;parameter byte
                                    		andi	R16,3				;csak az alo 2 bit kell
                                    		cpi	R16,0
                                    		breq	Lekerdezes
                                    		ldd	R17,Y+Flag1-RAMSTRT
                                    		andi	R17,0b11111100			;scan mode maskolasa 
                                    		or	R17,R16
                                    		STD	Y+Flag1-RAMSTRT,R17		;uj scan mode mentese
                                    		rjmp	EndBack
                                    
                                    Lekerdezes:	ldi	ZL,Low(Scanlek)
                                    		ldi	ZH,High(Scanlek)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		ret	
                                    
                                    Scanlek:	ldd	R16,Y+Flag1-RAMSTRT
                                    		andi	R16,0b00000011			;scan mode maskolasa 
                                    		inc	R16				;igy lesz 1,2,3 kodtabla
                                    		mov	R1,R16
                                    		rcall	SendByte			;Send scan tab kod
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    TYPE_RATE:	ldi	ZL,Low(TYPE_RATE1)
                                    		ldi	ZH,High(TYPE_RATE1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK & Parameter bit flag active
                                    		ori	R16,0b00001001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    TYPE_RATE1:	rcall	Read_Data
                                    		brcc	TR1
                                    		ret					;ha hiba volt: request to resend
                                    TR1:		mov	R16,R1				;Bit 7 is unimplemented. 
                                    		andi	R16,0b00011111			;Bits 6:5 - Repeat Delay
                                    		rcall	SetWait				;Bits 4:0 - Repeat Rate
                                    		mov	R16,R1
                                    		swap	R16
                                    		andi	R16,0b00000011
                                    		rcall	SetRep
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    
                                    KEN:		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    	;ide jon a enables code
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    
                                    
                                    KDIS:		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    	;ide jon disables code
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    
                                    SET_DEF:	LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		rcall	Scan_Init			;Default Scantable
                                    		rjmp	EndBack
                                    
                                    
                                    ;*******************************************************
                                    
                                    ALL_TYPE:	LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    	;ide jon a set code
                                    		rjmp	EndBack
                                    ;*******************************************************
                                    
                                    ALL_BREAK:	LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    	;ide jon a set code
                                    		rjmp	EndBack
                                    ;*******************************************************
                                    
                                    ALL_MAKE:	LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    	;ide jon a set code
                                    		rjmp	EndBack
                                    ;*******************************************************
                                    
                                    ALL_MTB:	LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    	;ide jon a set code
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    KEY_TYPE:	ldi	ZL,Low(Ktype1)
                                    		ldi	ZH,High(Ktype1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK & Parameter bit flag active
                                    		ori	R16,0b00001001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    Ktype1:	rcall	Read_Data
                                    		brcc	Ktype2
                                    		ret
                                    Ktype2:	mov	R16,R1				;parameter byte
                                    
                                    
                                    	;ide jon az feldolgozas!!
                                    
                                    		LDD	R16,Y+PS2Flag-RAMSTRT
                                    		andi	R16,0b11110111			;parameter renben megjott, torolve a bit
                                    		ori	R16,0b00000001			;ACK kuldes kell
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    KEY_BREAK:	ldi	ZL,Low(Kbreak1)
                                    		ldi	ZH,High(Kbreak1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK & Parameter bit flag active
                                    		ori	R16,0b00001001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    Kbreak1:	rcall	Read_Data
                                    		brcc	Kbreak2
                                    		ret
                                    Kbreak2:	mov	R16,R1				;parameter byte
                                    
                                    
                                    	;ide jon a feldolgozas!!
                                    
                                    		LDD	R16,Y+PS2Flag-RAMSTRT
                                    		andi	R16,0b11110111			;parameter renben megjott, torolve a bit
                                    		ori	R16,0b00000001			;ACK kuldes kell
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		rjmp	EndBack
                                    
                                    ;*******************************************************
                                    KEY_MAKE:	ldi	ZL,Low(Kmake1)
                                    		ldi	ZH,High(Kmake1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK & Parameter bit flag active
                                    		ori	R16,0b00001001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    Kmake1:	rcall	Read_Data
                                    		brcc	Kmake2
                                    		ret
                                    Kmake2:	mov	R16,R1				;parameter byte
                                    
                                    
                                    	;ide jon a feldolgozas!!
                                    
                                    		LDD	R16,Y+PS2Flag-RAMSTRT
                                    		andi	R16,0b11110111			;parameter renben megjott, torolve a bit
                                    		ori	R16,0b00000001			;ACK kuldes kell
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		rjmp	EndBack
                                    
                                    
                                    ;*******************************************************
                                    RESEND:		LDD	R16,Y+PS2Flag-RAMSTRT		;Resend last data!
                                    		ori	R16,0b00000010
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		rjmp	EndBack
                                    		
                                    ;*******************************************************
                                    ;* Keyboard RESET
                                    ;*******************************************************
                                    KRES:		ldi	ZL,Low(KRES1)
                                    		ldi	ZH,High(KRES1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    KRES1:		ldi	R16,0xAA			;Basic Assurance Test Result	
                                    		mov	R1,R16
                                    		rcall	SendByte			;Send Result of BAT
                                    		rcall	Scan_Init			;Re-init Scantable
                                    		ldi	R16,0
                                    		STD	Y+PS2Flag-RAMSTRT,R16 		;Calears Flags
                                    		STD	Y+StatLEDs+0-RAMSTRT,R16	;LED nem villagit
                                    		STD	Y+StatLEDs+1-RAMSTRT,R16	;LED nem villog
                                    		rjmp	EndBack
                                    
                                    
                                    ;*******************************************************
                                    READID:		ldi	ZL,Low(READID1)
                                    		ldi	ZH,High(READID1)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    		LDD	R16,Y+PS2Flag-RAMSTRT		;Make ACK flag active
                                    		ori	R16,0b00000001
                                    		STD	Y+PS2Flag-RAMSTRT,R16
                                    		ret
                                    READID1:	ldi	ZL,Low(READID2)
                                    		ldi	ZH,High(READID2)
                                    		STD	Y+PS2NextA+0-RAMSTRT,ZL
                                    		STD	Y+PS2NextA+1-RAMSTRT,ZH
                                    	ldi	R16,0xD0			;Eredeti: 0xAB
                                    		mov	R1,R16
                                    		rcall	SendByte			;Send Result of BAT
                                    		ret
                                    READID2: 	ldi	R16,0xDE			;Eredeti: 0x83
                                    		mov	R1,R16
                                    		rcall	SendByte
                                    EndBack:      	ldi	R16,0
                                    		STD	Y+PS2NextA+0-RAMSTRT,R16
                                    		STD	Y+PS2NextA+1-RAMSTRT,R16
                                    		ret
                                    ;*******************************************************
                                    
                                    ;**************************************************************************************
                                    .ESEG
                                    
                                    ;**************************************
                                    ;* Scan kod tabla Scan1 & Scan2 kodokkal
                                    ;*
                                    ;Keys allapota
                                    		;7. = 1 Enabled
                                    		;6. = 1 Ismetleses mod (Typematic)
                                    		;5. = 1 Break mode (felengedeskor scan kod kell)
                                    		;4. = 1 Make mode (lenyomaskor scan kod kell)
                                    		;3. = 1 Utolso ervenyes allapot
                                    		;2:1 = > counter prellegesmentesiteshez
                                    		;0. = 1 Utolso allapot
                                    		;	
                                    		; 6..4 = 1 Typematic/make/break mode
                                    		; 5..4 = 1 Make/break mode
                                    		; 4    = 1 Olny Make mode  
                                    
                                    
                                    MatrixTab:	.db	0xF0	;0. P
                                    		.db	0xF0	;1. L
                                    		.db	0xF0	;2. E
                                    		.db	0xF0	;3. A
                                    		.db	0xF0	;4. B
                                    		.db	0xF0	;5. %
                                    		.db	0xF0	;6. Space Bar
                                    		.db	0xF0	;7. 7
                                    		.db	0xF0	;8. Menu Up
                                    		.db	0xF0	;9. Menu Ok
                                    ;10.
                                    		.db	0xB0	;10. +C
                                    		.db	0xB0	;11. +Z
                                    		.db	0xB0	;12. +Y
                                    		.db	0xB0	;13. Used
                                    		.db	0xF0	;14. 9
                                    		.db	0xF0	;15. 8
                                    		.db	0xF0	;16. D
                                    		.db	0xF0	;17. H
                                    		.db	0xF0	;18. R
                                    		.db	0xF0	;19. I
                                    ;20.
                                    		.db	0xF0	;20. J
                                    		.db	0xF0	;21. K
                                    		.db	0xF0	;22. O
                                    		.db	0xF0	;23. 4
                                    		.db	0xF0	;24. Menu
                                    		.db	0xF0	;25. Menu Cancel
                                    		.db	0xB0	;26. Used
                                    		.db	0xB0	;27. +X
                                    		.db	0xB0	;28. Gyorsmenet
                                    		.db	0xB0	;29. -X
                                    ;30.
                                    		.db	0xF0	;30. 6
                                    		.db	0xF0	;31. 5
                                    		.db	0xF0	;32. N
                                    		.db	0xF0	;33. G
                                    		.db	0xF0	;34. M
                                    		.db	0xF0	;35. F
                                    		.db	0xF0	;36. S
                                    		.db	0xF0	;37. T
                                    		.db	0xF0	;38. Q
                                    		.db	0xF0	;39. 1
                                    ;40.
                                    		.db	0xF0	;40. Menu Down
                                    		.db	0xF0	;41. MODE
                                    		.db	0xB0	;42. -C
                                    		.db	0xB0	;43. Used
                                    		.db	0xB0	;44. -Y
                                    		.db	0xB0	;45. -Z
                                    		.db	0xF0	;46. 3
                                    		.db	0xF0	;47. 2
                                    		.db	0xF0	;48. X
                                    		.db	0xF0	;49. Y
                                    ;50.
                                    		.db	0xF0	;50. Z
                                    		.db	0xF0	;51. U
                                    		.db	0xF0	;52. V
                                    		.db	0xF0	;53. W
                                    		.db	0xF0	;54. C
                                    		.db	0xF0	;55. .
                                    		.db	0xF0	;56. End
                                    		.db	0xF0	;57. Up Arrow
                                    		.db	0xF0	;58. Home
                                    		.db	0xB0	;59. Rotate Right
                                    ;60.
                                    		.db	0xB0	;60. Rotate stop
                                    		.db	0xB0	;61. Rotate Left
                                    		.db	0xF0	;62. 0
                                    		.db	0xF0	;63. -
                                    		.db	0xF0	;64. S1
                                    		.db	0xF0	;65. S2
                                    		.db	0xF0	;66. S3
                                    		.db	0xF0	;67. S4
                                    		.db	0xF0	;68. S5
                                    		.db	0xB0	;69. START
                                    ;70.
                                    		.db	0xB0	;70. STOP
                                    		.db	0xF0	;71. DEL
                                    		.db	0xF0	;72. R Arrow
                                    		.db	0xF0	;73. Dn Arrow
                                    		.db	0xF0	;74. L Arrow
                                    		.db	0xF0	;75. ENTER
                                    		.db	0xF0	;76. CTRL
                                    		.db	0xF0	;77. SHIFT
                                    		.db	0xF0	;78. Backspace
                                    		.db	0xF0	;79. INS
                                    ;80.
                                    		.db	0xB0	;80. Yac-16[0]
                                    		.db	0xB0	;81. Yac-16[1]
                                    		.db	0xB0	;82. Yac-16[2]
                                    		.db	0xB0	;83. Yac-16[3]
                                    		.db	0xB0	;84. Yac-16[4]
                                    		.db	0xB0	;85. Yac-16[5]
                                    		.db	0xB0	;86. Yac-16[6]
                                    		.db	0xB0	;87. Yac-16[7]
                                    		.db	0xB0	;88. Yac-16[8]
                                    		.db	0xB0	;89. Yac-16[9]
                                    ;90.
                                    		.db	0xB0	;90. Yac-16[10]
                                    		.db	0xB0	;91. Yac-16[11]
                                    		.db	0xB0	;92. Yac-16[12]
                                    		.db	0xB0	;93. Yac-16[13]
                                    		.db	0xB0	;94. Yac-16[14]
                                    		.db	0xB0	;95. Yac-16[15]
                                    		.db	0xB0	;96. Yac-11[0]
                                    		.db	0xB0	;97. Yac-11[1]
                                    		.db	0xB0	;98. Yac-11[2]
                                    		.db	0xB0	;99. Yac-11[3]
                                    ;100
                                    		.db	0xB0	;100. Yac-11[4]
                                    		.db	0xB0	;101. Yac-11[5]
                                    		.db	0xB0	;102. Yac-11[6]
                                    		.db	0xB0	;103. Yac-11[7]
                                    		.db	0xB0	;104. Yac-11[8]
                                    		.db	0xB0	;105. Yac-11[9]
                                    		.db	0xB0	;106. Yac-11[10]
                                    		.db	0xB0	;107. 3 allasu kapcs1A
                                    		.db	0xB0	;108. 3 allasu kapcs1B
                                    		.db	0xB0	;109. 3 allasu kapcs2A
                                    ;110
                                    		.db	0xB0	;110. 3 allasu kapcs2B
                                    		.db	0xF0	;111. Not used
                                    		.db	0xB0	;112. Yac-12[0]
                                    		.db	0xB0	;113. Yac-12[1]
                                    		.db	0xB0	;114. Yac-12[2]
                                    		.db	0xB0	;115. Yac-12[3]
                                    		.db	0xB0	;116. Yac-12[4]
                                    		.db	0xB0	;117. Yac-12[5]
                                    		.db	0xB0	;118. Yac-12[6]
                                    		.db	0xB0	;119. Yac-12[7]
                                    		.db	0xB0	;120. Yac-12[8]
                                    		.db	0xB0	;121. Yac-12[9]
                                    		.db	0xB0	;122. Yac-12[10]
                                    		.db	0xB0	;123. Yac-12[11]
                                    		.db	0xF0	;124. Not used
                                    		.db	0xF0	;125. Not used
                                    		.db	0xF0	;126. Not used
                                    		.db	0xF0	;127. Not used
                                    DeleayT:	.db	0x01,0x0C		;Set 500ms rep., 10 char/s
                                    
                                    
                                    ;**************************************************************************************
                                    .CSEG
                                    
                                    Scan_Init:	ldi	XL,Low(Matrix)
                                    		ldi	XH,High(Matrix)
                                    		ldi	ZL,Low(MatrixTab)
                                    		ldi	ZH,High(MatrixTab)
                                    		out 	EEARH,ZH		;output address high for 8515
                                    		out	EEARL,ZL		;output address low for 8515
                                    		ldi	R17,KeyCount
                                    GetMatrix:	rcall	EERead_seq		;get EEPROM data
                                    		ST	X+,R0
                                    		dec	R17
                                    		brne	GetMatrix		;Scan filter torlese
                                    		ldi	ZL,Low(DeleayT)
                                    		ldi	ZH,High(DeleayT)
                                    		out 	EEARH,ZH
                                    		out	EEARL,ZL
                                    		rcall	EERead_seq		;get Dleay
                                    mov	R16,R0
                                    		rcall	SetWait
                                    		rcall	EERead_seq		;get Rep.Time
                                    mov	R16,R0
                                    		rcall	SetRep
                                    		clr	R16
                                    		STD	Y+ParamErr-RAMSTRT,R16 
                                    		ret
                                    
                                    
                                    ;**********************************
                                    ;* Beolvas a Keymatrixrol egy teljes matrixot a memoriaba
                                    ;* 
                                    
                                    ScanMatrix:	CBI	Row01234,Row0
                                    
                                    		ldi	ZL,Low(Matrix)
                                    		ldi	ZH,High(Matrix)		;a billentyu parametertabla cime
                                    
                                    
                                    Row0w:		SBIC	Row01234-2,Row0		;var mig a pin fizikailag nem lesz alacsony
                                    		 rjmp Row0w
                                    		IN	R0,DTLo_PORT-1		;Lower 8 bit 
                                                    rcall	HalfRow
                                    		IN	R0,DTHi_PORT-1		;Higher 8 bit 
                                    		SBI	Row01234,Row0
                                    		CBI	Row01234,Row1
                                                    rcall	HalfRow
                                    Row0u:		SBIS	Row01234-2,Row0		;var mig a pin fizikailag nem lesz magas
                                    		 rjmp Row0u
                                    
                                    
                                    Row1w:		SBIC	Row01234-2,Row1
                                    		 rjmp Row1w
                                    		IN	R0,DTLo_PORT-1		;Lower 8 bit 
                                                    rcall	HalfRow
                                    		IN	R0,DTHi_PORT-1		;Higher 8 bit 
                                    		SBI	Row01234,Row1
                                    		CBI	Row01234,Row2
                                                    rcall	HalfRow
                                    Row1u:		SBIS	Row01234-2,Row1
                                    		 rjmp Row1u
                                    
                                    
                                    
                                    Row2w:		SBIC	Row01234-2,Row2
                                    		 rjmp Row2w
                                    		IN	R0,DTLo_PORT-1		;Lower 8 bit 
                                                    rcall	HalfRow
                                    		IN	R0,DTHi_PORT-1		;Higher 8 bit 
                                    		SBI	Row01234,Row2
                                    		CBI	Row01234,Row3
                                                    rcall	HalfRow
                                    Row2u:		SBIS	Row01234-2,Row2
                                    		 rjmp Row2u
                                    
                                    
                                    Row3w:		SBIC	Row01234-2,Row3
                                    		 rjmp Row3w
                                    		IN	R0,DTLo_PORT-1		;Lower 8 bit 
                                                    rcall	HalfRow
                                    		IN	R0,DTHi_PORT-1		;Higher 8 bit 
                                    		SBI	Row01234,Row3
                                    		CBI	Row01234,Row4
                                                    rcall	HalfRow
                                    Row3u:		SBIS	Row01234-2,Row3
                                    		 rjmp Row3u
                                    
                                    
                                    
                                    Row4w:		SBIC	Row01234-2,Row4
                                    		 rjmp Row4w
                                    		IN	R0,DTLo_PORT-1		;Lower 8 bit 
                                                    rcall	HalfRow
                                    		IN	R0,DTHi_PORT-1		;Higher 8 bit 
                                    		SBI	Row01234,Row4
                                    		CBI	Row567,Row5
                                                    rcall	HalfRow
                                    Row4u:		SBIS	Row01234-2,Row4
                                    		 rjmp Row4u
                                    
                                    
                                    Row5w:		SBIC	Row567-2,Row5
                                    		 rjmp Row5w
                                    		IN	R0,DTLo_PORT-1		;Lower 8 bit 
                                                    rcall	HalfRow
                                    		IN	R0,DTHi_PORT-1		;Higher 8 bit 
                                    		SBI	Row567,Row5
                                    		CBI	Row567,Row6
                                                    rcall	HalfRow
                                    Row5u:		SBIS	Row567-2,Row5
                                    		 rjmp Row5u
                                    
                                    
                                    
                                    Row6w:		SBIC	Row567-2,Row6
                                    		 rjmp Row6w
                                    		IN	R0,DTLo_PORT-1		;Lower 8 bit 
                                                    rcall	HalfRow
                                    		IN	R0,DTHi_PORT-1		;Higher 8 bit 
                                    		SBI	Row567,Row6
                                    		CBI	Row567,Row7
                                                    rcall	HalfRow
                                    Row6u:		SBIS	Row567-2,Row6
                                    		 rjmp Row6u
                                    
                                    
                                    Row7w:		SBIC	Row567-2,Row7
                                    		 rjmp Row7w
                                    		IN	R0,DTLo_PORT-1		;Lower 8 bit 
                                                    rcall	HalfRow
                                    		IN	R0,DTHi_PORT-1		;Higher 8 bit 
                                    		SBI	Row567,Row7
                                                    rcall	HalfRow
                                    Row7u:		SBIS	Row567-2,Row7
                                    		 rjmp Row7u
                                    
                                    		ret				;filter bufferben a beolvasott matrix				
                                    
                                    
                                    ;*****************************
                                    ;xx. sor feldolgozasa
                                    
                                    HalfRow:	com	R0			;inv, ekkor 0 = nincs lenyomas, 1 = lenyomva
                                    		ldi	R16,8
                                    
                                    KeyCycl:	ld	R1,Z			;R1-ban a bill. parameter
                                    		mov	R19,R1
                                    		ror	R19
                                    		andi	R19,3			;a szamlalo allapota
                                    		cpi	R19,3
                                    		breq	kc1
                                    		inc	R19			;szamlalo+1
                                    kc1:		mov	R17,R1
                                    		andi	R17,1			;0. bit az elozo allapot
                                    		mov	R18,R0
                                    		andi	R18,1			;0. bit mostani allapot
                                    		eor	R17,R18			;azonos?
                                    		breq	kc2			;ha igen ugras
                                    		clr	R19			;szamlalo torlese, ha nem azonos
                                    kc2:		ror	R0
                                    		rol	R19
                                    		andi	R19,7			;csak a szamlalot es az uj allapotbit kell
                                    		mov	R17,R1
                                    		andi	R17,0b11111000		;a parameter byte also 3 bitje nem kell
                                    		or	R19,R17			;counter es uj allapot bemasolasa
                                    		st	Z+,R19
                                    
                                    		dec	R16
                                    		brne	KeyCycl
                                    		ret
                                    
                                    
                                    
                                    ;*****************************************************
                                    ;* Eloallitja a scankodokat a bill.matrix allapota alapjan
                                    ;*
                                    ;*****************************************************
                                    MakeScanC:	ldi	ZL,Low(2*ScanTabl)
                                    		ldi	ZH,High(2*ScanTabl)		;Scancode table
                                    		ldi	XL,Low(Matrix)
                                    		ldi	XH,High(Matrix)			;a billentyu parametertabla cime
                                    
                                    		clr	R1
                                    		ldd	R16,Y+Flag1-RAMSTRT
                                    		andi	R16,3				;scan mode kinyerese 
                                    		cpi	R16,1
                                    		breq	scanjo				;ha 1, akkor scan mode1
                                           		adiw	ZL,2				;scan mode2
                                    		cpi	R16,2
                                    		breq	scanjo				;ha 2, akkor scan mode2
                                    		adiw	ZL,2				;scan mode3
                                    
                                    scanjo:		ldi	R20,KeyCount							;Z-ben az aktualis scan table cime
                                    
                                    keyloop:	lpm
                                    		adiw	ZL,1
                                    		mov	R4,R0				;R4 low, R5 hi part
                                    		lpm
                                    		mov	R5,R0
                                    		adiw	ZL,5				;kovetkezo kod cime
                                    	
                                    
                                    		ld	R1,X			;allapot byte
                                    		mov	R17,R1
                                                    andi	R17,1			;R17 0. bit az uj allapot
                                    		mov	R18,R1
                                     		ror	R18
                                    		mov	R19,R18
                                    		ror	R19
                                    		ror	R19
                                    		andi	R19,1			;R19 elozo allapot
                                    		andi	R18,3			;counter erteke
                                    		cpi	R18,3			;counter<>3 nem tortenik semmi
                                    		breq	kl1
                                    		rjmp	nextkey		
                                    kl1:		eor	R19,R17			;ket allapot azonos?
                                    		brne	diffrent		;ha nem,akkor make scan/break kod
                                    		cpi	R17,1			;lenyomott allapot?
                                    		breq	kl2
                                    		rjmp	nextkey			;ha nem, akkor nem tortenik semmi
                                    
                                    kl2:		LDD	R9,Y+RepChar-RAMSTRT	;ismetelendo karakter sorszama
                                    		cp	R20,R9			;itt tartunk?
                                    		brne	nextkey			;ha nem kimarad
                                    
                                    
                                    		mov	R17,R15
                                    		andi	R17,1
                                    		breq	nextkey			;ismetles meg nem kell
                                    		ldi	R17,0b11111110
                                    		and	R15,R17			;flag torolve
                                    
                                    		mov	R16,R1
                                    		andi	R16,0b01000000			;typematic mode?
                                    		breq	nextkey				;ha igen ugras
                                    
                                    
                                            	ldi	R17,2			;ismetles mehet
                                    		mov	R1,R5
                                    		mov	R2,R4
                                    		rjmp	mb2
                                    
                                    diffrent:	mov	R18,R1
                                    		andi	R18,0b11110111
                                    		mov	R2,R17
                                    		lsl	R2
                                    		lsl	R2
                                    		lsl	R2
                                    		or	R2,R18
                                    		ST	X,R2
                                    
                                    		mov	R16,R1			;a gomb aktiv? kell figyelni?
                                    		andi	R16,0b10000000
                                    		breq	nextkey			;ha inaktiv, jon a kovetkezo
                                    		cpi	R17,1			;ha az uj bit 1, akkor lenyomas
                                    		breq	lenyom
                                    
                                    
                                    feleng:		LDD	R16,Y+RepChar-RAMSTRT	;ismetelendo karakter sorszama
                                    		cp	R20,R16			;itt tartunk?
                                    		brne	Fel1			;ha nem kimarad
                                    		rcall	RepTimerStop
                                    
                                    Fel1:		mov	R16,R1
                                    		andi	R16,0b00100000		;kell break kodot generalni?
                                    		breq	nextkey 
                                    
                                    		ldd	R16,Y+Flag1-RAMSTRT
                                    		andi	R16,3				;scan mode kinyerese 
                                    		cpi	R16,1
                                    		breq	scantab1			;ha 1, akkor scan mode1
                                    
                                    		ldi	R17,0xF0			;scantab2 & 3
                                    		mov	R1,R5
                                    		mov	R2,R17
                                    		mov	R3,R4
                                    		ldi	R17,3
                                    		rjmp	mb2
                                    
                                    scantab1:       ldi	R17,0x80			;use scantable 1 
                                    		or	R4,R17				;7. bit =1 break
                                    		mov	R1,R5
                                    		mov	R2,R4
                                    		ldi	R17,2
                                    		rjmp	mb2
                                    
                                    lenyom:		mov	R16,R1
                                    		andi	R16,0b00010000			;kell make kodot generalni?
                                    		breq	nextkey 
                                    
                                    		ldi	R17,2				;make code 0xE0xx 2 byte
                                    		mov	R1,R5
                                    		mov	R2,R4
                                    		rcall	RepTimerStart
                                    
                                    mb2:		tst	R1
                                    		brne	mb3
                                    		mov	R1,R2
                                    		mov	R2,R3
                                    		dec	R17
                                    mb3:		rcall	PutScanCodes
                                    
                                    
                                    nextkey:	adiw	XL,1				;kovetkezo matrix elem
                                    		dec	R20				;kovetkezo scan kod cime
                                    		breq	keyextit
                                    		rjmp	keyloop
                                    keyextit:	ret
                                    
                                    
                                    
                                    
                                    ;************************************************
                                    ;* Put Scan code into FIFO
                                    ;* in: R17 - mennyi hely kell, R1,R2,R3 parameterek ami a pufferbe kerul
                                    ;* out C=1 hiba
                                    ;************************************************
                                    PutScanCodes:	push	ZL
                                    		push	ZH
                                    
                                    		ldd	R18,Y+KeyTrail-RAMSTRT	;R18=txtail
                                    		ldd	R19,Y+KeyHead-RAMSTRT	;R19=txhead
                                    		sub	R18,R19
                                    		breq	Sntolc1			;T=H, akkor TXbuffSize hely van
                                    		brsh	Sndjoc1      		
                                    
                                    Sntolc1:	subi	R18,(-1*KeyBuffLen)	;(T-H)+32
                                    
                                    Sndjoc1:	cp	R18,R17			;R18 hely a pufferben 
                                    		brcs	SendchrExit1		;ha <1 nincs elg hely! Hiba!
                                    
                                    		clr	R18			;R18=0	
                                    		ldi	zl,low(KeyBuff)		;Pointer to the txbuffer (FIFO)
                                    		ldi	zh,high(KeyBuff)
                                    		add	zl,R19
                                    		adc	zh,R18			; add address offset...  z = @Buf[txhead]
                                    		st	z+,R1			;Store first data 
                                    		inc	R19
                                    		cpi	R19,KeyBuffLen
                                    		brcs	Snd2c			;TxHead<32 akkor jo
                                    		clr	R19			;TxHead=0
                                    		ldi	zl,low(KeyBuff)		;Pointer to the txbuffer (FIFO)
                                    		ldi	zh,high(KeyBuff)
                                    
                                    Snd2c:		dec	R17
                                    		breq	stend
                                    		st	z+,R2			;Store 2. data 
                                    		inc	R19
                                    		cpi	R19,KeyBuffLen
                                    		brcs	Snd3c
                                    		clr	R19			;TxHead=0
                                    		ldi	zl,low(KeyBuff)		;Pointer to the txbuffer (FIFO)
                                    		ldi	zh,high(KeyBuff)
                                    Snd3c:		dec	R17
                                    		breq	stend
                                    
                                    		st	z+,R3			;Store 3. data 
                                    		inc	R19
                                    		cpi	R19,KeyBuffLen
                                    		brcs	stend
                                    		clr	R19			;TxHead=0
                                    
                                    stend:		std	Y+KeyHead-RAMSTRT,R19
                                    		clc				; Nincs hiba Addr eltarolva, kuldes folya,atban
                                    		rjmp	putend
                                    
                                    SendChrExit1:	sec				; Return with error
                                    		clr	R18			;R18=0	
                                    		ldi	zl,low(KeyBuff)		;Pointer to the txbuffer (FIFO)
                                    		ldi	zh,high(KeyBuff)
                                    		add	zl,R19
                                    		adc	zh,R18			; add address offset...  z = @Buf[txhead]
                                    		st	z+,R18			;Store error marker (0) 
                                    
                                    putend:		pop	ZH
                                    		pop	ZL
                                    		ret
                                    
                                    
                                    
                                    ;**************************************
                                    ;* Scan kod tabla Scan1 & Scan2 kodokkal
                                    ;*
                                    ScanTabl:	.dw	0x0019,0x004d,0x004d	;0. P
                                    		.dw	0x0026,0x004b,0x004b	;1. L
                                    		.dw	0x0012,0x0024,0x0024	;2. E
                                    		.dw	0x001E,0x001C,0x001C	;3. A
                                    		.dw	0x0030,0x0032,0x0032	;4. B
                                    		.dw	0x0029,0x000E,0x000E	;5. %
                                    		.dw	0x0039,0x0029,0x0029	;6. Space Bar
                                    		.dw	0x0008,0x003D,0x003D	;7. 7
                                    		.dw	0xE049,0xE07D,0x006F	;8. Menu Up
                                    		.dw	0x0041,0x0083,0x0037	;9. Menu Ok
                                    ;10.
                                    		.dw	0x004E,0x0079,0x0079	;10. +C
                                    		.dw	0x0049,0x007D,0x007D	;11. +Z
                                    		.dw	0x0048,0x0075,0x0077	;12. +Y
                                    		.dw	0x0047,0x006C,0x006C	;13. Used
                                    		.dw	0x000A,0x0046,0x0046	;14. 9
                                    		.dw	0x0009,0x003E,0x003E	;15. 8
                                    		.dw	0x0020,0x0023,0x0023	;16. D
                                    		.dw	0x0023,0x0033,0x0033	;17. H
                                    		.dw	0x0013,0x002D,0x002D	;18. R
                                    		.dw	0x0017,0x0043,0x0043	;19. I
                                    ;20.
                                    		.dw	0x0024,0x003B,0x003B	;20. J
                                    		.dw	0x0025,0x0042,0x0042	;21. K
                                    		.dw	0x0018,0x0044,0x0044	;22. O
                                    		.dw	0x0005,0x0025,0x0025	;23. 4
                                    		.dw	0x0043,0x0001,0x0047	;24. Menu
                                    		.dw	0x0040,0x000B,0x002F	;25. Menu Cancel
                                    		.dw	0x0053,0x0071,0x0071	;26. Used
                                    		.dw	0x004D,0x0074,0x0074	;27. +X
                                    		.dw	0x0038,0x0011,0x0019	;28. Gyorsmenet
                                    		.dw	0x004B,0x006B,0x006B	;29. -X
                                    ;30.
                                    		.dw	0x0007,0x0036,0x0036	;30. 6
                                    		.dw	0x0006,0x002E,0x002E	;31. 5
                                    		.dw	0x0031,0x0031,0x0031	;32. N
                                    		.dw	0x0022,0x0034,0x0034	;33. G
                                    		.dw	0x0032,0x003A,0x003A	;34. M
                                    		.dw	0x0021,0x002B,0x002B	;35. F
                                    		.dw	0x001F,0x001B,0x001B	;36. S
                                    		.dw	0x0014,0x002C,0x002C	;37. T
                                    		.dw	0x0010,0x0015,0x0015	;38. Q
                                    		.dw	0x0002,0x0016,0x0016	;39. 1
                                    ;40.
                                    		.dw	0xE051,0xE07A,0x006D	;40. Menu Down
                                    		.dw	0x000F,0x000D,0x000D	;41. MODE
                                    		.dw	0x004A,0x007B,0x0084	;42. -C
                                    		.dw	0x0051,0x007A,0x007A	;43. Used
                                    		.dw	0x0050,0x0072,0x0072	;44. -Y
                                    		.dw	0x004F,0x0069,0x0069	;45. -Z
                                    		.dw	0x0004,0x0026,0x0026	;46. 3
                                    		.dw	0x0003,0x001E,0x001E	;47. 2
                                    		.dw	0x002D,0x0022,0x0022	;48. X
                                    		.dw	0x0015,0x0035,0x0035	;49. Y
                                    ;50.
                                    		.dw	0x002C,0x001A,0x001A	;50. Z
                                    		.dw	0x0016,0x003C,0x003C	;51. U
                                    		.dw	0x002F,0x002A,0x002A	;52. V
                                    		.dw	0x0011,0x001D,0x001D	;53. W
                                    		.dw	0x002E,0x0021,0x0021	;54. C
                                    		.dw	0x0034,0x0049,0x0049	;55. .
                                    		.dw	0xE04F,0xE069,0x0065	;56. End
                                    		.dw	0xE048,0xE075,0x0063	;57. Up Arrow
                                    		.dw	0xE047,0xE06C,0x006E	;58. Home
                                    		.dw	0x0058,0x0007,0x005E	;59. Rotate Right
                                    ;60.
                                    		.dw	0x0001,0x0076,0x0008	;60. Rotate stop
                                    		.dw	0x0044,0x0009,0x004F	;61. Rotate Left
                                    		.dw	0x000B,0x0045,0x0045	;62. 0
                                    		.dw	0x000C,0x004E,0x004E	;63. -
                                    		.dw	0x003B,0x0005,0x0007	;64. S1
                                    		.dw	0x003C,0x0006,0x000F	;65. S2
                                    		.dw	0x003D,0x0004,0x0017	;66. S3
                                    		.dw	0x003E,0x000C,0x001F	;67. S4
                                    		.dw	0x003F,0x0003,0x0027	;68. S5
                                    		.dw	0xE065,0xE010,0xE010	;69. START
                                    ;70.
                                    		.dw	0xE064,0xE008,0xE008	;70. STOP
                                    		.dw	0xE053,0xE071,0x0064	;71. DEL
                                    		.dw	0xE04D,0xE074,0x006A	;72. R Arrow
                                    		.dw	0xE050,0xE072,0x0060	;73. Dn Arrow
                                    		.dw	0xE04B,0xE06B,0x0061	;74. L Arrow
                                    		.dw	0x001C,0x005A,0x005A	;75. ENTER
                                    		.dw	0x001D,0x0014,0x0011	;76. CTRL
                                    		.dw	0x002A,0x0012,0x0012	;77. SHIFT
                                    		.dw	0x000E,0x0066,0x0066	;78. Backspace
                                    		.dw	0xE052,0xE070,0x0067	;79. INS
                                    ;80.
                                    		.dw	0xE066,0xE018,0xE018	;80. Yac-16[0]
                                    		.dw	0xE067,0xE020,0xE020	;81. Yac-16[1]
                                    		.dw	0xE068,0xE028,0xE028	;82. Yac-16[2]
                                    		.dw	0xE069,0xE030,0xE030	;83. Yac-16[3]
                                    		.dw	0xE06A,0xE038,0xE038	;84. Yac-16[4]
                                    		.dw	0xE06B,0xE040,0xE040	;85. Yac-16[5]
                                    		.dw	0xE06C,0xE048,0xE048	;86. Yac-16[6]
                                    		.dw	0xE06D,0xE050,0xE050	;87. Yac-16[7]
                                    		.dw	0xE06E,0xE057,0xE057	;88. Yac-16[8]
                                    		.dw	0xE06F,0xE06F,0xE06F	;89. Yac-16[9]
                                    ;90.
                                    		.dw	0xE070,0xE013,0xE013	;90. Yac-16[10]
                                    		.dw	0xE071,0xE019,0xE019	;91. Yac-16[11]
                                    		.dw	0xE072,0xE039,0xE039	;92. Yac-16[12]
                                    		.dw	0xE074,0xE053,0xE053	;93. Yac-16[13]
                                    		.dw	0xE075,0xE05C,0xE05C	;94. Yac-16[14]
                                    		.dw	0xE076,0xE05F,0xE05F	;95. Yac-16[15]
                                    		.dw	0xE056,0xE061,0x0061	;96. Yac-11[0]
                                    		.dw	0xE02C,0xE01A,0x001A	;97. Yac-11[1]
                                    		.dw	0xE02D,0xE022,0x0022	;98. Yac-11[2]
                                    		.dw	0xE02E,0xE021,0x0021	;99. Yac-11[3]
                                    ;100
                                    		.dw	0xE02F,0xE02A,0x002A	;100. Yac-11[4]
                                    		.dw	0xE030,0xE032,0x0032	;101. Yac-11[5]
                                    		.dw	0xE031,0xE031,0x0031	;102. Yac-11[6]
                                    		.dw	0xE032,0xE03A,0x003A	;103. Yac-11[7]
                                    		.dw	0xE033,0xE041,0x0041	;104. Yac-11[8]
                                    		.dw	0xE034,0xE049,0x0049	;105. Yac-11[9]
                                    		.dw	0xE035,0xE04A,0x004A	;106. Yac-11[10]
                                    		.dw	0x0000,0x0000,0x0000	;107. Kapcs 1A
                                    		.dw	0x0000,0x0000,0x0000	;108. Kapcs 1B
                                    		.dw	0x0000,0x0000,0x0000	;109. Kapcs 2A
                                    ;110
                                    		.dw	0x0000,0x0000,0x0000	;110. Kapcs 2B
                                    		.dw	0x0000,0x0000,0x0000	;111. Not used
                                    		.dw	0xE03A,0xE058,0xE058	;112. Yac-12[0]
                                    		.dw	0xE01E,0xE01C,0xE01C	;113. Yac-12[1]
                                    		.dw	0xE01F,0xE01B,0xE01B	;114. Yac-12[2]
                                    		.dw	0xE020,0xE023,0xE023	;115. Yac-12[3]
                                    		.dw	0xE021,0xE02B,0xE02B	;116. Yac-12[4]
                                    		.dw	0xE022,0xE034,0xE034	;117. Yac-12[5]
                                    		.dw	0xE023,0xE033,0xE033	;118. Yac-12[6]
                                    		.dw	0xE024,0xE03B,0xE03B	;119. Yac-12[7]
                                    		.dw	0xE025,0xE042,0xE042	;120. Yac-12[8]
                                    		.dw	0xE026,0xE04B,0xE04B	;121. Yac-12[9]
                                    		.dw	0xE027,0xE04C,0xE04C	;122. Yac-12[10]
                                    		.dw	0xE028,0xE052,0xE052	;123. Yac-12[11]
                                    		.dw	0x0000,0x0000,0x0000	;124. Not used
                                    		.dw	0x0000,0x0000,0x0000	;125. Not used
                                    		.dw	0x0000,0x0000,0x0000	;126. Not used
                                    		.dw	0x0000,0x0000,0x0000	;127. Not used
                                    
                                    
                                    ;**************
                                    ;** Kivarasi idotablazat
                                    ;**
                                    ;** f=3.6864MHz, fc=f/256 = 14400Hz => 69.44us
                                    ;**
                                    WaitTime:	.dw	0x0E10	;250ms
                                    		.dw	0x1C20	;500ms
                                    		.dw	0x2A30	;750ms
                                    		.dw	0x3840	;1000ms
                                    
                                    ;**ismetlesi ido idotablazat
                                    
                                    RepTime:	.dw	0x01E0	;00000 30.0 char/s
                                    		.dw	0x021B	;00001 26.7 char/s
                                    		.dw	0x0258	;00010 24.0 char/s
                                    		.dw	0x0295	;00011 21.8 char/s
                                    		.dw	0x02D0	;00100 20.0 char/s
                                    		.dw	0x030A	;00101 18.5 char/s
                                    		.dw	0x034A	;00110 17.1 char/s
                                    		.dw	0x0384	;00111 16.0 char/s
                                    		.dw	0x03C0	;01000 15.0 char/s
                                    		.dw	0x043A	;01001 13.3 char/s
                                    		.dw	0x04B0	;01010 12.0 char/s
                                    		.dw	0x0529	;01011 10.9 char/s
                                    		.dw	0x05A0	;01100 10.0 char/s
                                    		.dw	0x061D	;01101  9.2 char/s
                                    		.dw	0x068A	;01110  8.6 char/s
                                    		.dw	0x0708	;01111  8.0 char/s
                                    		.dw	0x0780	;10000  7.5 char/s
                                    		.dw	0x0865	;10001  6.7 char/s
                                    		.dw	0x0960	;10010  6.0 char/s
                                    		.dw	0x0A3A	;10011  5.5 char/s
                                    		.dw	0x0B40	;10100  5.0 char/s
                                    		.dw	0x0C3A	;10101  4.6 char/s
                                    		.dw	0x0D14	;10110  4.3 char/s
                                    		.dw	0x0E10	;10111  4.0 char/s
                                    		.dw	0x0F33	;11000  3.7 char/s
                                    		.dw	0x110B	;11001  3.3 char/s
                                    		.dw	0x12C0	;11010  3.0 char/s
                                    		.dw	0x14D5	;11011  2.7 char/s
                                    		.dw	0x1680	;11100  2.5 char/s
                                    		.dw	0x1874	;11101  2.3 char/s
                                    		.dw	0x1AC9	;11110  2.1 char/s
                                    		.dw	0x1C20	;11111  2.0 char/s
                                    
                                    
                                    
                                    
                                    
                                    ;************************************************
                                    ;* Set Deleay Time
                                    ;* in: R16 Wait time selector 0..3
                                    ;*  
                                    ;************************************************
                                    SetWait:	ldi	ZL,Low(2*WaitTime)
                                    		ldi	ZH,High(2*WaitTime)
                                    		clr	R0
                                    		andi	R16,3
                                    		add	R16,R16
                                    		add	ZL,R16
                                    		adc	ZH,R0
                                    
                                    		LDI	R16,0b00001000	;Timer Stop
                                    		OUT	TCCR1B,R16	;7. = 0 Input Capture Noise Canceller Disabled
                                    					;6. = 0 InputCapture ICP Falling edge
                                    					;3. = 1 Reset TC1 when copare match
                                    					;2:0 Clock select
                                    					;   000 => TC1 Stop
                                    					;   001 => Ck
                                    					;   010 => Ck/8
                                    					;   011 => Ck/64
                                    					;   100 => Ck/256
                                    					;   101 => Ck/1024
                                    					;   110 => External T1 Falling Edge
                                    					;   111 => External T1 rising Edge
                                    
                                    		lpm
                                    		adiw	ZL,1
                                    		add	R0,R0		;mert a tablazat csak 3.68MHz-re keszult mi meg 7.37MHz-en ketyegunk
                                    		std	Y+WTime+0-RAMSTRT,R0
                                    		lpm
                                    		adc	R0,R0
                                    		std	Y+WTime+1-RAMSTRT,R0
                                    		ret
                                    
                                    
                                    ;************************************************
                                    ;* Set Repeat Time
                                    ;* in: R16 rep time selector 0..31
                                    ;*  
                                    ;************************************************
                                    SetRep: 	andi	R16,0b00011111
                                    		add	R16,R16			;mert word meretu adatok kellenek
                                    		ldi	ZL,Low(2*RepTime)
                                    		ldi	ZH,High(2*RepTime)
                                    		clr	R0
                                    		add	ZL,R16
                                    		adc	ZH,R0
                                    
                                    		LDI	R16,0b00001000	;Timer Stop
                                    		OUT	TCCR1B,R16	;7. = 0 Input Capture Noise Canceller Disabled
                                    					;6. = 0 InputCapture ICP Falling edge
                                    					;3. = 1 Reset TC1 when copare match
                                    					;2:0 Clock select
                                    					;   000 => TC1 Stop
                                    					;   001 => Ck
                                    					;   010 => Ck/8
                                    					;   011 => Ck/64
                                    					;   100 => Ck/256
                                    					;   101 => Ck/1024
                                    					;   110 => External T1 Falling Edge
                                    					;   111 => External T1 rising Edge
                                    
                                    		lpm
                                    		adiw	ZL,1
                                    		add	R0,R0		;mert a tablazat csak 3.68MHz-re keszult mi meg 7.37MHz-en ketyegunk
                                    		std	Y+WRep+0-RAMSTRT,R0
                                    		lpm
                                    		adc	R0,R0
                                    		std	Y+WRep+1-RAMSTRT,R0
                                    		ret
                                    
                                    
                                    
                                    ;****************************************************
                                    ;* Billentyu nyomas utani elso ismetles timer inditas
                                    ;* R20 - ismetlendo billentyu sorszama
                                    ;****************************************************
                                    RepTimerStart:	STD	Y+RepChar-RAMSTRT,R20	;ismetelendo karakter sorszama
                                    		LDI	R16,0b00000000		;Timer Stop
                                    		OUT	TCCR1B,R16      
                                    		OUT	TCNT1H,R16
                                    		OUT	TCNT1L,R16		;Counter torlese
                                    		in	R16,TIFR
                                    		andi	R16,255-(1< TC1 Stop
                                    						;   001 => Ck
                                    						;   010 => Ck/8
                                    						;   011 => Ck/64
                                    						;   100 => Ck/256
                                    						;   101 => Ck/1024
                                    						;   110 => External T1 Falling Edge
                                    						;   111 => External T1 rising Edge
                                    		ret
                                    
                                    
                                    RepTimerStop:	LDI	R16,0b00000000		;Timer Stop
                                    		OUT	TCCR1B,R16
                                    		in	R16,TIFR
                                    		andi	R16,255-(1<
                                 

Programming the AVR Microcontrollers in Assember Machine Language

This site is a member of WebRing.
To browse visit Here.

Atmel AVR From Wikipedia, the free encyclopedia (Redirected from Avr) Jump to: navigation, search The AVRs are a family of RISC microcontrollers from Atmel. Their internal architecture was conceived by two students: Alf-Egil Bogen and Vegard Wollan, at the Norwegian Institute of Technology (NTH] and further developed at Atmel Norway, a subsidiary founded by the two architects. Atmel recently released the Atmel AVR32 line of microcontrollers. These are 32-bit RISC devices featuring SIMD and DSP instructions, along with many additional features for audio and video processing, intended to compete with ARM based processors. Note that the use of "AVR" in this article refers to the 8-bit RISC line of Atmel AVR Microcontrollers. The acronym AVR has been reported to stand for Advanced Virtual RISC. It's also rumoured to stand for the company's founders: Alf and Vegard, who are evasive when questioned about it. Contents [hide] 1 Device Overview 1.1 Program Memory 1.2 Data Memory and Registers 1.3 EEPROM 1.4 Program Execution 1.5 Speed 2 Development 3 Features 4 Footnotes 5 See also 6 External Links 6.1 Atmel Official Links 6.2 AVR Forums & Discussion Groups 6.3 Machine Language Development 6.4 C Language Development 6.5 BASIC & Other AVR Languages 6.6 AVR Butterfly Specific 6.7 Other AVR Links [edit] Device Overview The AVR is a Harvard architecture machine with programs and data stored and addressed separately. Flash, EEPROM, and SRAM are all integrated onto a single die, removing the need for external memory (though still available on some devices). [edit] Program Memory Program instructions are stored in semi-permanent Flash memory. Each instruction for the AVR line is either 16 or 32 bits in length. The Flash memory is addressed using 16 bit word sizes. The size of the program memory is indicated in the naming of the device itself. For instance, the ATmega64x line has 64Kbytes of Flash. Almost all AVR devices are self-programmable. [edit] Data Memory and Registers The data address space consists of the register file, I/O registers, and SRAM. The AVRs have thirty-two single-byte registers and are classified as 8-bit RISC devices. The working registers are mapped in as the first thirty-two memory spaces (000016-001F16) followed by the 64 I/O registers (002016-005F16). The actual usable RAM starts after both these sections (address 006016). (Note that the I/O register space may be larger on some more extensive devices, in which case memory mapped I/O registers will occupy a portion of the SRAM.) Even though there are separate addressing schemes and optimized opcodes for register file and I/O register access, all can still be addressed and manipulated as if they were in SRAM. [edit] EEPROM Almost all devices have on-die EEPROM. This is most often used for long-term parameter storage to be retrieved even after cycling the power of the device. [edit] Program Execution Atmel's AVRs have a single level pipeline design. The next machine instruction is fetched as the current one is executing. Most instructions take just one or two clock cycles, making AVRs relatively fast among the eight-bit microcontrollers. The AVR family of processors were designed for the efficient execution of compiled C code. The AVR instruction set is more orthogonal than most eight-bit microcontrollers, however, it is not completely regular: Pointer registers X, Y, and Z have addressing capabilities that are different from each other. Register locations R0 to R15 have different addressing capabilities than register locations R16 to R31. I/O ports 0 to 31 have different addressing capabilities than I/O ports 32 to 63. CLR affects flags, while SER does not, even though they are complementary instructions. CLR set all bits to zero and SER sets them to one. (Note though, that neither CLR nor SER are native instructions. Instead CLR is syntactic sugar for [produces the same machine code as] EOR R,R while SER is syntactic sugar for LDI R,$FF. Math operations such as EOR modify flags while moves/loads/stores/branches such as LDI do not.) [edit] Speed The AVR line can normally support clock speeds from 0-16MHz, with some devices reaching 20MHz. Lower powered operation usually requires a reduced clock speed. All AVRs feature an on-chip oscillator, removing the need for external clocks or resonator circuitry. Because many operations on the AVR are single cycle, the AVR can achieve up to 1MIPS per MHz. [edit] Development AVRs have a large following due to the free and inexpensive development tools available, including reasonably priced development boards and free development software. The AVRs are marketed under various names that share the same basic core but with different peripheral and memory combinations. Some models (notably, the ATmega range) have additional instructions to make arithmetic faster. Compatibility amongst chips is fairly good. See external links for sites relating to AVR development. [edit] Features Current AVRs offer a wide range of features: RISC Core Running Many Single Cycle Instructions Multifunction, Bi-directional I/O Ports with Internal, Configurable Pull-up Resistors Multiple Internal Oscillators Internal, Self-Programmable Instruction Flash Memory up to 256K In-System Programmable using ICSP, JTAG, or High Voltage methods Optional Boot Code Section with Independent Lock Bits for Protection Internal Data EEPROM up to 4KB Internal SRAM up to 8K 8-Bit and 16-Bit Timers PWM Channels & dead time generator Lighting (PWM Specific) Controller models Dedicated IC Compatible Two-Wire Interface (TWI) Synchronous/Asynchronous Serial Peripherals (UART/USART) (As used with RS-232,RS-485, and more) Serial Peripheral Interface (SPI) CAN Controller Support USB Controller Support Proper High-speed hardware & Hub controller with embedded AVR. Also freely available low-speed (HID) software emulation Ethernet Controller Support Universal Serial Interface (USI) for Two or Three-Wire Synchronous Data Transfer Analog Comparators LCD Controller Support 10-Bit A/D Converters, with multiplex of up to 16 channels Brownout Detection Watchdog Timer (WDT) Low-voltage Devices Operating Down to 1.8v Multiple Power-Saving Sleep Modes picoPower Devices Atmel AVR assembler programming language Atmel AVR machine programming language Atmel AVR From Wikipedia, the free encyclopedia (Redirected from Avr) Jump to: navigation, search The AVRs are a family of RISC microcontrollers from Atmel. Their internal architecture was conceived by two students: Alf-Egil Bogen and Vegard Wollan, at the Norwegian Institute of Technology (NTH] and further developed at Atmel Norway, a subsidiary founded by the two architects. Atmel recently released the Atmel AVR32 line of microcontrollers. These are 32-bit RISC devices featuring SIMD and DSP instructions, along with many additional features for audio and video processing, intended to compete with ARM based processors. Note that the use of "AVR" in this article refers to the 8-bit RISC line of Atmel AVR Microcontrollers. The acronym AVR has been reported to stand for Advanced Virtual RISC. It's also rumoured to stand for the company's founders: Alf and Vegard, who are evasive when questioned about it. Contents [hide] 1 Device Overview 1.1 Program Memory 1.2 Data Memory and Registers 1.3 EEPROM 1.4 Program Execution 1.5 Speed 2 Development 3 Features 4 Footnotes 5 See also 6 External Links 6.1 Atmel Official Links 6.2 AVR Forums & Discussion Groups 6.3 Machine Language Development 6.4 C Language Development 6.5 BASIC & Other AVR Languages 6.6 AVR Butterfly Specific 6.7 Other AVR Links [edit] Device Overview The AVR is a Harvard architecture machine with programs and data stored and addressed separately. Flash, EEPROM, and SRAM are all integrated onto a single die, removing the need for external memory (though still available on some devices). [edit] Program Memory Program instructions are stored in semi-permanent Flash memory. Each instruction for the AVR line is either 16 or 32 bits in length. The Flash memory is addressed using 16 bit word sizes. The size of the program memory is indicated in the naming of the device itself. For instance, the ATmega64x line has 64Kbytes of Flash. Almost all AVR devices are self-programmable. [edit] Data Memory and Registers The data address space consists of the register file, I/O registers, and SRAM. The AVRs have thirty-two single-byte registers and are classified as 8-bit RISC devices. The working registers are mapped in as the first thirty-two memory spaces (000016-001F16) followed by the 64 I/O registers (002016-005F16). The actual usable RAM starts after both these sections (address 006016). (Note that the I/O register space may be larger on some more extensive devices, in which case memory mapped I/O registers will occupy a portion of the SRAM.) Even though there are separate addressing schemes and optimized opcodes for register file and I/O register access, all can still be addressed and manipulated as if they were in SRAM. [edit] EEPROM Almost all devices have on-die EEPROM. This is most often used for long-term parameter storage to be retrieved even after cycling the power of the device. [edit] Program Execution Atmel's AVRs have a single level pipeline design. The next machine instruction is fetched as the current one is executing. Most instructions take just one or two clock cycles, making AVRs relatively fast among the eight-bit microcontrollers. The AVR family of processors were designed for the efficient execution of compiled C code. The AVR instruction set is more orthogonal than most eight-bit microcontrollers, however, it is not completely regular: Pointer registers X, Y, and Z have addressing capabilities that are different from each other. Register locations R0 to R15 have different addressing capabilities than register locations R16 to R31. I/O ports 0 to 31 have different addressing capabilities than I/O ports 32 to 63. CLR affects flags, while SER does not, even though they are complementary instructions. CLR set all bits to zero and SER sets them to one. (Note though, that neither CLR nor SER are native instructions. Instead CLR is syntactic sugar for [produces the same machine code as] EOR R,R while SER is syntactic sugar for LDI R,$FF. Math operations such as EOR modify flags while moves/loads/stores/branches such as LDI do not.) [edit] Speed The AVR line can normally support clock speeds from 0-16MHz, with some devices reaching 20MHz. Lower powered operation usually requires a reduced clock speed. All AVRs feature an on-chip oscillator, removing the need for external clocks or resonator circuitry. Because many operations on the AVR are single cycle, the AVR can achieve up to 1MIPS per MHz. [edit] Development AVRs have a large following due to the free and inexpensive development tools available, including reasonably priced development boards and free development software. The AVRs are marketed under various names that share the same basic core but with different peripheral and memory combinations. Some models (notably, the ATmega range) have additional instructions to make arithmetic faster. Compatibility amongst chips is fairly good. See external links for sites relating to AVR development. [edit] Features Current AVRs offer a wide range of features: RISC Core Running Many Single Cycle Instructions Multifunction, Bi-directional I/O Ports with Internal, Configurable Pull-up Resistors Multiple Internal Oscillators Internal, Self-Programmable Instruction Flash Memory up to 256K In-System Programmable using ICSP, JTAG, or High Voltage methods Optional Boot Code Section with Independent Lock Bits for Protection Internal Data EEPROM up to 4KB Internal SRAM up to 8K 8-Bit and 16-Bit Timers PWM Channels & dead time generator Lighting (PWM Specific) Controller models Dedicated IC Compatible Two-Wire Interface (TWI) Synchronous/Asynchronous Serial Peripherals (UART/USART) (As used with RS-232,RS-485, and more) Serial Peripheral Interface (SPI) CAN Controller Support USB Controller Support Proper High-speed hardware & Hub controller with embedded AVR. Also freely available low-speed (HID) software emulation Ethernet Controller Support Universal Serial Interface (USI) for Two or Three-Wire Synchronous Data Transfer Analog Comparators LCD Controller Support 10-Bit A/D Converters, with multiplex of up to 16 channels Brownout Detection Watchdog Timer (WDT) Low-voltage Devices Operating Down to 1.8v Multiple Power-Saving Sleep Modes picoPower Devices Atmel AVR assembler programming language Atmel AVR machine programming language Atmel AVR From Wikipedia, the free encyclopedia (Redirected from Avr) Jump to: navigation, search The AVRs are a family of RISC microcontrollers from Atmel. Their internal architecture was conceived by two students: Alf-Egil Bogen and Vegard Wollan, at the Norwegian Institute of Technology (NTH] and further developed at Atmel Norway, a subsidiary founded by the two architects. Atmel recently released the Atmel AVR32 line of microcontrollers. These are 32-bit RISC devices featuring SIMD and DSP instructions, along with many additional features for audio and video processing, intended to compete with ARM based processors. Note that the use of "AVR" in this article refers to the 8-bit RISC line of Atmel AVR Microcontrollers. The acronym AVR has been reported to stand for Advanced Virtual RISC. It's also rumoured to stand for the company's founders: Alf and Vegard, who are evasive when questioned about it. Contents [hide] 1 Device Overview 1.1 Program Memory 1.2 Data Memory and Registers 1.3 EEPROM 1.4 Program Execution 1.5 Speed 2 Development 3 Features 4 Footnotes 5 See also 6 External Links 6.1 Atmel Official Links 6.2 AVR Forums & Discussion Groups 6.3 Machine Language Development 6.4 C Language Development 6.5 BASIC & Other AVR Languages 6.6 AVR Butterfly Specific 6.7 Other AVR Links [edit] Device Overview The AVR is a Harvard architecture machine with programs and data stored and addressed separately. Flash, EEPROM, and SRAM are all integrated onto a single die, removing the need for external memory (though still available on some devices). [edit] Program Memory Program instructions are stored in semi-permanent Flash memory. Each instruction for the AVR line is either 16 or 32 bits in length. The Flash memory is addressed using 16 bit word sizes. The size of the program memory is indicated in the naming of the device itself. For instance, the ATmega64x line has 64Kbytes of Flash. Almost all AVR devices are self-programmable. [edit] Data Memory and Registers The data address space consists of the register file, I/O registers, and SRAM. The AVRs have thirty-two single-byte registers and are classified as 8-bit RISC devices. The working registers are mapped in as the first thirty-two memory spaces (000016-001F16) followed by the 64 I/O registers (002016-005F16). The actual usable RAM starts after both these sections (address 006016). (Note that the I/O register space may be larger on some more extensive devices, in which case memory mapped I/O registers will occupy a portion of the SRAM.) Even though there are separate addressing schemes and optimized opcodes for register file and I/O register access, all can still be addressed and manipulated as if they were in SRAM. [edit] EEPROM Almost all devices have on-die EEPROM. This is most often used for long-term parameter storage to be retrieved even after cycling the power of the device. [edit] Program Execution Atmel's AVRs have a single level pipeline design. The next machine instruction is fetched as the current one is executing. Most instructions take just one or two clock cycles, making AVRs relatively fast among the eight-bit microcontrollers. The AVR family of processors were designed for the efficient execution of compiled C code. The AVR instruction set is more orthogonal than most eight-bit microcontrollers, however, it is not completely regular: Pointer registers X, Y, and Z have addressing capabilities that are different from each other. Register locations R0 to R15 have different addressing capabilities than register locations R16 to R31. I/O ports 0 to 31 have different addressing capabilities than I/O ports 32 to 63. CLR affects flags, while SER does not, even though they are complementary instructions. CLR set all bits to zero and SER sets them to one. (Note though, that neither CLR nor SER are native instructions. Instead CLR is syntactic sugar for [produces the same machine code as] EOR R,R while SER is syntactic sugar for LDI R,$FF. Math operations such as EOR modify flags while moves/loads/stores/branches such as LDI do not.) [edit] Speed The AVR line can normally support clock speeds from 0-16MHz, with some devices reaching 20MHz. Lower powered operation usually requires a reduced clock speed. All AVRs feature an on-chip oscillator, removing the need for external clocks or resonator circuitry. Because many operations on the AVR are single cycle, the AVR can achieve up to 1MIPS per MHz. [edit] Development AVRs have a large following due to the free and inexpensive development tools available, including reasonably priced development boards and free development software. The AVRs are marketed under various names that share the same basic core but with different peripheral and memory combinations. Some models (notably, the ATmega range) have additional instructions to make arithmetic faster. Compatibility amongst chips is fairly good. See external links for sites relating to AVR development. [edit] Features Current AVRs offer a wide range of features: RISC Core Running Many Single Cycle Instructions Multifunction, Bi-directional I/O Ports with Internal, Configurable Pull-up Resistors Multiple Internal Oscillators Internal, Self-Programmable Instruction Flash Memory up to 256K In-System Programmable using ICSP, JTAG, or High Voltage methods Optional Boot Code Section with Independent Lock Bits for Protection Internal Data EEPROM up to 4KB Internal SRAM up to 8K 8-Bit and 16-Bit Timers PWM Channels & dead time generator Lighting (PWM Specific) Controller models Dedicated IC Compatible Two-Wire Interface (TWI) Synchronous/Asynchronous Serial Peripherals (UART/USART) (As used with RS-232,RS-485, and more) Serial Peripheral Interface (SPI) CAN Controller Support USB Controller Support Proper High-speed hardware & Hub controller with embedded AVR. Also freely available low-speed (HID) software emulation Ethernet Controller Support Universal Serial Interface (USI) for Two or Three-Wire Synchronous Data Transfer Analog Comparators LCD Controller Support 10-Bit A/D Converters, with multiplex of up to 16 channels Brownout Detection Watchdog Timer (WDT) Low-voltage Devices Operating Down to 1.8v Multiple Power-Saving Sleep Modes picoPower Devices Atmel AVR assembler programming language Atmel AVR machine programming language