.include "m8def.inc"

.def reg1 = r16
.def reg2 = r17 
.def reg3 = r18

.equ data = portb
.equ e = 3
.equ rs = 2

.org 0x000
	rjmp reset

;INITIALISIERUNGEN FÜR AVR
reset:
	ldi reg1, high(ramend)		;stack init.
	out sph, reg1
	ldi reg1, low(ramend)
	out spl, reg1

	ldi reg1, 0xff				;portb ist ausgang
	out ddrb, reg1
				
	cbi portb, e
	cbi portb, rs
;WARTEN BIS LCD RESET BEENDET IST
wait_100ms:	
	ldi reg3, 0x0f
w1:	ldi reg2, 0xff
w2:	ldi reg1, 0xff
w3:	dec reg1
	brne w3
	dec reg2
	brne w2
	dec reg3
	brne w1	
;INITIALISIERUNDEN FÜR LCD
;init_lcdd:
	
	cbi data, e			;8bit ohne untere nibbel
	ldi reg1, 0x30
	out data, reg1
	rcall ena

	cbi data, e			;4bit, 2 zeilen, 5x8
	ldi reg1, 0x28
	rcall comand

	cbi data, e			;ebenso
	ldi reg1, 0x28
	rcall comand

	cbi data, e			;display:an, cursor:an, blinken:aus
	ldi reg1, 0x0E
	rcall comand

	cbi data, e			;display löschen
	ldi reg1, 0x01
	rcall comand

	cbi data, e			;inc cursor
	ldi reg1, 0x06
	rcall comand

;_________________TEXT____________
	rcall ms

	ldi reg1, 'a'
	rcall datai

	ldi reg1, 'b'
	rcall datai

	ldi reg1, 'c'
	rcall datai

	ldi reg1, 'd'
	rcall datai

	ldi reg1, 'e'
	rcall datai

	ldi reg1, 'f'
	rcall datai

	ldi reg1, 'g'
	rcall datai

	ldi reg1, 'h'
	rcall datai

	ldi reg1, 'i'
	rcall datai

	ldi reg1, 'j'
	rcall datai
loop:

	rjmp loop

comand:
	;1.NIBBEL
	cbi data, e
	cbi data, rs
	mov reg2, reg1
	andi reg1, 0b11110000;0xf0
	out data, reg1
	rcall ena
	rcall ms
	;2.NIBBEL
	swap reg2
	andi reg2, 0b11110000;0xf0
	out data, reg2
	rcall ena
	rcall ms
	ret

datai:
	;1.NIBBEL
	cbi data, e
	mov reg2, reg1
	andi reg1, 0b11110000;0xf0
	sbr reg1, 1<<rs
	out data, reg1
	rcall ena
	rcall ms
	;2.NIBBEL
	swap reg2
	andi reg2, 0b11110000;0xf0
	sbr reg2, 1<<rs
	out data, reg2
	rcall ena
	rcall ms
	ret

ena:
	sbi data, e
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	cbi data, e
	;rcall ms
	ret

ms:
	ldi reg1, 0xff
w4:	ldi reg2, 0xc8
w5:	dec reg2
	brne w5
	dec reg1
	brne w4
	ret