.include"m16def.inc"

.equ	LCD_RS = 0
.equ	LCD_RW = 1
.equ	LCD_EN = 2

.def	temp = r16
.def	argument = r17
.def	return = r18

.org	0
rjmp	reset


reset:
ldi		temp, low(RAMEND)
out		SPL, temp
ldi		temp, high(RAMEND)
out		SPH, temp


rcall	LCD_init
	

rcall	LCD_wait
ldi		argument,'A'	;write 'A' to the LCD char data RAM
rcall	LCD_putchar  
		
loop:	rjmp loop	


;fce inicializace displeje
LCD_init:
rcall 	LCD_delay

ldi		temp, 0b00000111   ;nastavení rw,rs a en na výstupy
out 	DDRD,temp
cbi 	PORTD,LCD_RW
cbi 	PORTD,LCD_RS

ldi 	argument,0x30
rcall 	LCD_command8
rcall 	LCD_delay

ldi 	argument,0x30
rcall 	LCD_command8
rcall 	LCD_delay

ldi 	argument,0x30
rcall 	LCD_command8
rcall 	LCD_delay

ldi 	argument,0x20
rcall 	LCD_command8
rcall 	LCD_delay

rcall 	LCD_wait
ldi 	argument, 0x28; 2 lajny 5x7 font 4bit
rcall 	LCD_command
rcall 	LCD_delay

rcall 	LCD_wait
ldi 	argument, 0x08;vypnutí displeje
rcall 	LCD_command
rcall 	LCD_delay

rcall 	LCD_wait
ldi 	argument, 0x0F;zapnutí displeje,kurzoru,blikání kurzoru
rcall 	LCD_command
rcall 	LCD_delay

rcall 	LCD_wait
ldi 	argument, 0x01; vyčisti display a kursor z5 na 1,1
rcall 	LCD_command
rcall 	LCD_delay

rcall 	LCD_wait
ldi 	argument, 0x06; 2 lajny 5x7 font 4bit
rcall 	LCD_command
rcall 	LCD_delay
ret

	
;zdržovací funkce -> smyčka
LCD_delay:

clr r2	
LCD_delay_loop1:
        clr r3
		LCD_delay_loop2:
		dec r3
		brne LCD_delay_loop2
dec r2
brne LCD_delay_loop1
ret

//funkce pro vykonání potřebných intrukcí pro přepnutí z 8bit do 4bit
LCD_command8:

in 		temp,DDRD
sbr 	temp,0b11110000 /*nastaví datové vodiče do 1 jako výstupy, 
3bity v nižší polovině bytu nechá nastavené tak jak byly viz LCD_init*/
out 	DDRD,temp

in 		temp,PORTD
cbr 	temp,0b11110000;vyčistí databity PD4-PD7
cbr 	argument,0b00001111;vyčistí en,rw,rs

or 		temp,argument/*argument=horní polovina bytu s 0x20 viz inicializace  
temp dolní polovina bytu z PortD logický OR*/

out 	PORTD,temp

sbi 	PORTD,LCD_EN
rcall 	delay5us
cbi 	PORTD,LCD_EN
in 		temp,DDRD
cbr 	temp,0b11110000
out 	DDRD,temp
ret

;LCD čekej funkce, hlídá jestli je LCD zaneprázdněný když není BF=0 return
LCD_wait:
rcall 	LCD_write_bf1 ; test BF v log.1
rcall 	LCD_getaddr
andi 	return, 0x80 ;dokud nebude 8.bit tj,0bx0000000 v returnu 0,tak smyčka
brne 	LCD_wait
ret

;to samé jako LCD_getchar akroát s RS=0  
LCD_getaddr:

in 		temp,DDRD
andi 	temp,0b00001111
out 	DDRD,temp
cbi 	PORTD,LCD_RS
sbi 	PORTD,LCD_EN
sbi 	PORTD,LCD_RW
nop
in 		temp,PIND
andi 	temp,0b11110000
mov 	return, temp
cbi 	PORTD,LCD_EN
rcall 	delay5us
sbi 	PORTD,LCD_EN
nop
in 		temp,PIND
andi 	temp,0b11110000
swap 	temp
or 		return,temp
cbi 	PORTD,LCD_EN
cbi 	PORTD,LCD_RW
ret

LCD_getchar:

in 		temp,DDRD
andi 	temp,0b00001111
out 	DDRD,temp
sbi 	PORTD,LCD_RS
sbi 	PORTD,LCD_EN
sbi 	PORTD,LCD_RW
nop
in 		temp,PIND
andi 	temp,0b11110000
mov 	return, temp
cbi 	PORTD,LCD_EN
rcall 	delay5us
sbi 	PORTD,LCD_EN
nop
in 		temp,PIND
andi 	temp,0b11110000
swap 	temp
or 		return,temp
cbi 	PORTD,LCD_EN
cbi 	PORTD,LCD_RW
cbi 	PORTD,LCD_RS
ret

;příkaz na displej
LCD_command:

push 	argument
in 		temp,DDRD
sbr 	temp,0b11110000
out 	DDRD,temp
in 		temp,PORTD
cbr 	temp,0b11110111
cbr 	argument,0b00001111
or 		temp,argument

out 	PORTD, temp
sbi 	PORTD,LCD_EN
rcall 	delay5us
cbi 	PORTD,LCD_EN
pop 	argument
cbr 	temp,0b11110000
swap 	argument
cbr 	argument,0b00001111
or 		temp,argument
out 	PORTD,temp
sbi 	PORTD,LCD_EN
rcall 	delay5us
cbi 	PORTD,LCD_EN	
in 		temp,DDRD
cbr 	temp,0b11110000
out 	DDRD,temp
ret
	
lcd_putchar:
push 	argument		;uloží argument
in 		temp,DDRD		;dostane bity
sbr		temp,0b11110000	;nastaví výstupy
out		DDRD,temp		;volží je do DDRD
in 		temp,PORTD		;dostane bity z portu
cbr		temp,0b11110111	;vyčistí všechny bity LCD
cbr		argument,0b00001111	;chceme vyšší polovinu bytu do argumentu
or 		temp,argument		;log.OR s tempem
out		PORTD,temp		;zapíšeme data na PORTD
sbi		PORTD,LCD_RS		;nastavíme RS=1 pro přístup do registru LCD
sbi		PORTD,LCD_EN		; 
rcall 	delay5us                    
cbi		PORTD,LCD_EN        ;
pop		argument		;načtení argumentu ze zásobníku
cbr		temp,0b11110000	;čtení dolní poloviny bytu, vyčistíme horní polovinu
swap 	argument		;prohození dat PD4-7 = PD0-3 a naopak
cbr		argument,0b00001111	;vyčistění dolní poloviny bytu
or 		temp,argument		;log OR
out		PORTD,temp		;zápis dat na PORTD
sbi		PORTD,LCD_RS		;zas RS=1
sbi		PORTD,LCD_EN		; 
rcall 	delay5us              
cbi		PORTD,LCD_EN        ;
cbi		PORTD,LCD_RS
in 		temp,DDRD
cbr		temp,0b11110000	;změnění vlastností bitů
out		DDRD,temp
ret

delay5us:
nop 
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop 
nop
nop 
nop 
nop
nop
nop
nop
ret

;test jestli je BF=1
LCD_write_bf1:
rcall 	LCD_getaddr
andi 	return,0x80
breq	LCD_write_bf1
ret

