
;C = keys
;B = lcd
;D0-D3 = lcd too

.include "m16def.inc"
.def   temp=r16
;.def   cols=r18
;.def   rots=r21
;.def   key=r19
;.def   scancod=r20
.def   lcd=r22
.def   count_lcd=r23
.def   temp2=r24

.equ rs=2
.equ rw=1
.equ e=0

.org  $000
rjmp init
;.org $007
;rjmp scankeys

init:


;stack
ldi temp,low(RAMEND)
out spl,temp
ldi temp,high(RAMEND)
out sph,temp 

;ports
ser  temp 
ser temp2
;out DDRC, temp2
out ddrD,temp
out DDRB,temp
ldi temp,$00


;timer
ldi temp, $05 
out TCCR0,temp
ldi temp, (1<<TOIE0)
out TIMSK, temp

;lcd
rcall delay2ms
rcall delay2ms

ldi lcd, $38
rcall lcdcom
ldi lcd, $06

;cursor - right
rcall lcdcom
ldi lcd, $0C
rcall lcdcom
ldi lcd, $01

;clear lcd
rcall lcdcom
rcall delay2ms
rcall delay2ms

;top string
ldi lcd, $80
rcall lcdcom
ldi count_lcd,12
ldi zl, low(str_0*2)
ldi zh, high(str_0*2)

out0: lpm
adiw zl, 1
mov lcd, r0
rcall lcddat
dec count_lcd
brne out0
;rcall scankeys
;bottom string

;ldi lcd, $C0
;rcall lcdcom
;ldi count_lcd, 17
;sei

loop: rjmp loop



lcdcom:
out PORTB, lcd
ldi temp, 0x01 ;*
out portD, temp
cbi portD, e
rcall delay40us
rcall delay40us
ret

lcddat:
out PORTB, lcd
ldi temp, 0x05 ;*
OUT portD, temp
cbi portD, e
rcall delay40us
rcall delay40us
ret

delay40us:
ldi r18, 48
d0: dec r18
brne d0
ret

delay2ms:
ldi r17, 48
d1: rcall delay40us
dec r17
brne d1
ret



str_0: .db 'L','C','D', ' ','W','o','r','k','s',' ','=',')',' ',0xF0  

