
.equ	LCDRS_PORT=PORTA	;Kimeneti port választása
.equ	LCDRS_DIR=DDRA		;Register Select 1=data 0=command
.equ	LCDRS=0

.equ	LCDEN_PORT=PORTA	;Kimeneti port választása
.equ	LCDEN_DIR=DDRA		;Enable - data clocked on falling edge
.equ	LCDEN=1

.equ	LCDDAT_PORT=PORTA	;LCD Data PortA[4..7]
.equ	LCDDAT_DIR=DDRA		;LCD adatbitek a PortA 4-7 biteken
.equ	LCDDAT_PIN=PINA




; bemeneti változók
;	temp		- kiírandó karakter / sor+oszlopcím




;*****************************************************
;** LCD_Init
;**
;**  In: -
;**
;**  Out: -
;**
;**
;** Alt:
;**
;** Description:  Initialize LCD in 4 bit mode
;*

Lcd_init:
	ldi temp,50
	call wait1ms
	;in temp,porta
	;ori temp,48
	;out porta,temp
	;ldi temp,5
	;call wait1ms
	
		; *** Send the 'FUNCTION SET' command
		;                 +------ Data:  0 = 4-bit; 1 = 8-bit
		;                 |+----- Lines: 0 = 1; 1 = 2
		;   	          ||+---- Font:  0 = 5x8; 1 = 5x11
		ldi	temp,0b00101000
		call	LCD_SendCmd
		;ldi	temp,1
		;call	Wait1ms			;wait 5ms

		; *** Send the 'CURSOR/DISPLAY SHIFT' command
		;                  +----- S/C:  0 = cursor; 1 = display
		;    	           |+---- R/L:  0 = left; 1 = right
		ldi	temp,0b00010100
		call	LCD_SendCmd
		;ldi	temp,1
		;call	Wait1ms			;wait 5ms

		; *** Send the 'ENTRY MODE' command
		;      			     +--- Direction: 0 = left; 1 = right
		;  	        	     |+-- Shift Dislay: 0 = off; 1 = on
		ldi	temp,0b00000110
		call	LCD_SendCmd
		;ldi	temp,1
		;call	Wait1ms			;wait 5ms

		; *** Send the 'DISPLAY ON/OFF' command
		;     		        +---- Display: 0 = off; 1 = on
		;,          		    |+--- Cursor: 0 = off; 1 = on
		;     		  	    ||+-- Blink: 0 = off; 1 = on
		ldi	temp,0b00001100
		call	LCD_SendCmd
		;ldi	temp,1
		;call	Wait1ms			;wait 5ms

		call	LCD_Clear
		ret

;*****************************************************
;** LCD_pulseE
;**
;**  In: -
;**
;**  Out: R0 - Data on LCDDat pin
;**
;**
;** Alt
;**
;** Description:  Generate ENABLE signal to LCD
;*
LCD_pulseE:
		sbi	LCDEN_PORT,LCDEN	;Enable high
		nop				;Wait 500ns @16MHz
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		cbi	LCDEN_PORT,LCDEN	;Enable low, latch data
		ret

;*****************************************************
;** LCD_SendCmd
;**
;**  Bem: R16 - Command byte
;**
;**  Kim: -
;**
;**
;** Haszn: R0, R16, R17
;**
;** Leírás:
;** A rutin beírja a parancsot az utasításregiszterbe

;
LCD_SendCmd:
		push temp
		in sitt,pina
		andi sitt,0b00001111
		andi temp,0b11110000
		or	sitt,temp
		out	LCDDAT_PORT,sitt
		cbi	LCDRS_PORT,LCDRS	;Set to Command Reg.
		rcall	LCD_PulseE		;Latch upper nibble


		pop temp		
		swap temp
		in sitt,pina
		andi sitt,0b00001111
		andi temp,0b11110000
		or	sitt,temp
		out	LCDDAT_PORT,sitt
		cbi	LCDRS_PORT,LCDRS	;Set to Command Reg.
		rcall	LCD_PulseE		;Latch lower nibble
		ldi temp,1
		call wait1ms
	ret



;*****************************************************
;** LCD_SendChar
;**
;**  In: R16 - character code
;**
;**  Out: -
;**
;**
;** Alt: R0,R1, R16, R17
;**
;** Description:
;** Routine to write a character to the data register.
;
LCD_SendChar:
		push temp
		push temp
		in sitt,pina
		andi sitt,0b00001111
		andi temp,0b11110000
		or	sitt,temp
		out	LCDDAT_PORT,sitt
		sbi	LCDRS_PORT,LCDRS	;Set to data Reg.
		rcall	LCD_PulseE		;Latch upper nibble

		pop temp
		swap temp
		in sitt,pina
		andi sitt,0b00001111
		andi temp,0b11110000
		or	sitt,temp
		out	LCDDAT_PORT,sitt
		rcall	LCD_PulseE
		cbi	LCDRS_PORT,LCDRS	;Set to Data Reg.
;		mov temp,r1
		ldi temp,1
		call wait1ms
		pop temp
	ret

;*****************************************************
;** LCD_Clear
;**
;**  In: -
;**
;**  Out: -
;**
;**
;** Alt: 
;**
;** Description:
;** Clears the display and sends the cursor home.
;
LCD_Clear:
		ldi	temp,0b00000001		;Clear LCD
		call	LCD_SendCmd
		;ldi temp,1
		;call wait1ms
		ldi	temp,0b00000010		;Send the cursor home
		rcall	LCD_SendCmd
		ldi temp,1
		call wait1ms
	ret

;*****************************************************
;** LCD_Goto
;**
;**  In: R16 - position
;**		0x00 - 1.row, 1.col
;**		0x40 - 2.row, 1.col
;**
;**  Out: -
;**
;**
;** Alt: R16
;**
;** Description:
;**
;
LCD_Goto:
		;ldi temp,0
		ori	temp,0b10000000		;Set address Command
		;or temp,oszlop
		;mov karakter,temp		;itt adjuk át a temp értékét a karakter-nak,mert az LCD_SendCmd csak karaktert fogad
		call	LCD_SendCmd
		ldi temp,1
		call wait1ms
	ret
