.include "8515def.inc"
.equ	inPort=PINC
.equ	outPort=PORTA
.equ	DriveA_1=0
.equ	DriveA_2=1
.equ	DriveB_1=2
.equ	DriveB_2=3
.equ	Servo=4
.equ	U_SensorA_out=5
.equ	U_SensorB_out=6
.equ	D_SensorA=0
.equ	D_SensorB=1
.equ	U_SensorA_in=3
.equ	U_SensorB_in=2

.equ	DriveA_status=0
.equ	DriveB_status=1
.equ	DriveA_direction=2
.equ	DriveB_direction=3
.equ	DSR=2
.equ	CL=3 ;LCD
.equ	RW=4 ;LCD
.equ	RS=5 ;LCD
.equ	BTN_CANCEL=4
.equ	BTN_UP=5
.equ	BTN_DOWN=6
.equ	BTN_OK=7

;**************** LCD Characters ******************
.equ	B_A=0x41
.equ	B_B=0x42
.equ	B_C=0x43
.equ	B_D=0x44
.equ	B_E=0x45
.equ	B_F=0x46
.equ	B_G=0x47
.equ	B_H=0x48
.equ	B_I=0x49
.equ	B_J=0x4A
.equ	B_K=0x4B
.equ	B_L=0x4C
.equ	B_M=0x4D
.equ	B_N=0x4E
.equ	B_O=0x4F
.equ	B_P=0x50
.equ	B_Q=0x51
.equ	B_R=0x52
.equ	B_S=0x53
.equ	B_T=0x54
.equ	B_U=0x55
.equ	B_V=0x56
.equ	B_W=0x57
.equ	B_X=0x58
.equ	B_Y=0x59
.equ	B_Z=0x5A
;kleine Buchstaben + 32

.equ	Z_X=0x30
;Zahl + Z_X für Zahlenausgabe

.equ	doppelpunkt=0x3A
.equ	leer=0x20

.def	status=r1
.def	temp=r16
.def	drive_status=r17
.def	weg=r18
.def	data=r19
;def  	temp=r20-r22
.def	eehigh=r23
.def	eelow=r24
.def	max_ee=r25
.def	wait_object=r26
;def	temp=r27
.def	pos=r28	
.def	LCD=r29

.cseg
.org $000                        ;System-Reset-Interrupt
rjmp RESET                       ;           "
.org $009                        ;Serial-Interrupt
rjmp RxD                         ;           "

RESET:
	ldi 	temp,low(RAMEND) ;Initialises controller
	out	SPL,temp         ;           "
	ldi	temp,high(RAMEND);           "
	out	SPH,temp         ;           "
	ldi	temp,$FF	 ;Initialises Outputports
	out	outPort,temp	 ;           "
	out	DDRA,temp	 ;           "
	out	PORTB,temp
	out	DDRB,temp
	ldi 	temp,0           
	out	outPort,temp      
	out	PORTB,temp
	ldi	temp,0b00111000
	out	PORTD,temp
	out	DDRD,temp
	ldi	temp,0
	out	PORTD,temp
	ldi	temp,25		 ;Sets baudrate
	out 	UBRR,temp	 ;           "
	ldi	temp,0b10011000	 ;Enables serialport-interupts
	out	UCR,temp	 ;           "
	clr	pos
	clr	drive_status
	ldi	eelow,0
	clr	eehigh
	sei			 ;Enables interups
	cbi	PORTD,RW
	cbi	PORTD,RS
	cbi	PORTD,CL
	sbi	outPort,U_SensorA_out
	sbi	outPort,U_SensorB_out

	rcall	osd

	ldi	LCD,0x38
	rcall Befehl

	ldi	LCD,0x38
	rcall Befehl

	ldi	LCD,0x38
	rcall Befehl

	ldi	LCD,0x0C
	rcall Befehl

	ldi	LCD,0x02
	rcall Befehl

	ldi	LCD,0x01
	rcall Befehl
	rcall	cls
	ldi	ZH,high(2*System_load)	
	ldi	ZL,low(2*System_load)
	rcall	writebyteslcd
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*VERS)	
	ldi	ZL,low(2*VERS)
	rcall	writebyteslcd
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*Serial_speed)	
	ldi	ZL,low(2*Serial_speed)
	rcall	writebyteslcd
	rcall	osd
	SBIS	PIND,DSR
	rjmp	IR_ofl
	rcall	cls
	ldi	ZH,high(2*IR_online)	
	ldi	ZL,low(2*IR_online)
	rcall	writebyteslcd
	rcall	osd
	rjmp	ready
IR_ofl:
	rcall	cls
	ldi	ZH,high(2*IR_offline)	
	ldi	ZL,low(2*IR_offline)
	rcall	writebyteslcd
	rcall	osd
ready:
	rcall	cls
	ldi	ZH,high(2*ongel_ready)	
	ldi	ZL,low(2*ongel_ready)
	rcall	writebyteslcd
Mainloop:
	SBIS	inPort,BTN_CANCEL
	clr	drive_status
	SBIS	inPort,BTN_OK
	rjmp	Menu
	SBRC	drive_status,5
	rcall	check_next
	SBRC	drive_status,4
	rjmp	Mainloop
	SBRS	drive_status,DriveA_status
	rcall	driveA_stop
	SBRS	drive_status,DriveB_status
	rcall	driveB_stop
	SBRC	drive_status,DriveA_status
	rcall	Go
	SBRC	drive_status,DriveB_status
	rcall	Go
	SBIC	inPort,D_SensorA
	rjmp	Wait_low
	rjmp	Mainloop
Wait_low:
	SBIS	inPort,BTN_OK
	rjmp	Menu
	SBIS	inPort,BTN_CANCEL
	clr	drive_status
	cpi	weg,0
	breq	Mainloop
	SBIC	inPort,D_SensorA
	rjmp	Wait_low
	dec	weg
	cpi	weg,0
	brne	mainloop
	rcall	cls
	ldi	temp,115	
	out	UDR,temp
	cbr	drive_status,0b00001111
	sbr	drive_status,0b01000000
	rcall	driveA_stop
	rcall	driveB_stop
	rjmp	mainloop

Menu:
	rcall	cls
	ldi	ZH,high(2*buttons)	
	ldi	ZL,low(2*buttons)
	rcall	writebyteslcd
	rcall	osd
	rcall	osd
	

sa_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+1
	rcall	wtt
	ldi	ZH,high(2*sa_msg)	
	ldi	ZL,low(2*sa_msg)
	rcall	writebyteslcd
sa_btns:
	SBIS	inPort,BTN_OK
	rjmp	standalone_on
	SBIS	inPort,BTN_UP
	rjmp	ab_menu
	SBIS	inPort,BTN_DOWN
	rjmp	demo_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	sa_btns	

ab_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+2
	rcall	wtt
	ldi	ZH,high(2*ab_msg)	
	ldi	ZL,low(2*ab_msg)
	rcall	writebyteslcd
ab_btns:
	SBIS	inPort,BTN_OK
	rjmp	ab_show
	SBIS	inPort,BTN_UP
	rjmp	mc_menu
	SBIS	inPort,BTN_DOWN
	rjmp	sa_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	ab_btns	

mc_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+3
	rcall	wtt
	ldi	ZH,high(2*mc_msg)	
	ldi	ZL,low(2*mc_msg)
	rcall	writebyteslcd
mc_btns:
	SBIS	inPort,BTN_OK
	rjmp	missiondataclear
	SBIS	inPort,BTN_UP
	rjmp	f_menu
	SBIS	inPort,BTN_DOWN
	rjmp	ab_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	mc_btns	


f_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+4
	rcall	wtt
	ldi	ZH,high(2*f_msg)	
	ldi	ZL,low(2*f_msg)
	rcall	writebyteslcd
f_btns:
	SBIS	inPort,BTN_OK
	rjmp	dir_f
	SBIS	inPort,BTN_UP
	rjmp	z_menu
	SBIS	inPort,BTN_DOWN
	rjmp	mc_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	f_btns	

z_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+5
	rcall	wtt
	ldi	ZH,high(2*z_msg)	
	ldi	ZL,low(2*z_msg)
	rcall	writebyteslcd
z_btns:
	SBIS	inPort,BTN_OK
	rjmp	dir_z
	SBIS	inPort,BTN_UP
	rjmp	l_menu
	SBIS	inPort,BTN_DOWN
	rjmp	f_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	z_btns	

l_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+6
	rcall	wtt
	ldi	ZH,high(2*l_msg)	
	ldi	ZL,low(2*l_msg)
	rcall	writebyteslcd
l_btns:
	SBIS	inPort,BTN_OK
	rjmp	dir_l
	SBIS	inPort,BTN_UP
	rjmp	r_menu
	SBIS	inPort,BTN_DOWN
	rjmp	z_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	l_btns
r_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+7
	rcall	wtt
	ldi	ZH,high(2*r_msg)	
	ldi	ZL,low(2*r_msg)
	rcall	writebyteslcd
r_btns:
	SBIS	inPort,BTN_OK
	rjmp	dir_r
	SBIS	inPort,BTN_UP
	rjmp	usa_menu
	SBIS	inPort,BTN_DOWN
	rjmp	l_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	r_btns

usa_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+8
	rcall	wtt
	ldi	ZH,high(2*usa_msg)	
	ldi	ZL,low(2*usa_msg)
	rcall	writebyteslcd
usa_btns:
	SBIS	inPort,BTN_OK
	rjmp	usa_start
	SBIS	inPort,BTN_UP
	rjmp	usb_menu
	SBIS	inPort,BTN_DOWN
	rjmp	r_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	usa_btns

usb_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+9
	rcall	wtt
	ldi	ZH,high(2*usb_msg)	
	ldi	ZL,low(2*usb_msg)
	rcall	writebyteslcd
usb_btns:
	SBIS	inPort,BTN_OK
	rjmp	usb_start
	SBIS	inPort,BTN_UP
	rjmp	us_menu
	SBIS	inPort,BTN_DOWN
	rjmp	usa_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	usb_btns

us_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+1
	rcall	wtt
	ldi	LCD,Z_X+0
	rcall	wtt
	ldi	ZH,high(2*us_msg)	
	ldi	ZL,low(2*us_msg)
	rcall	writebyteslcd
us_btns:
	SBIS	inPort,BTN_OK
	rjmp	us_start
	SBIS	inPort,BTN_UP
	rjmp	demo_menu
	SBIS	inPort,BTN_DOWN
	rjmp	usb_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	us_btns


demo_menu:
	rcall	btn_wait
	rcall	cls
	ldi	LCD,Z_X+1
	rcall	wtt
	ldi	LCD,Z_X+1
	rcall	wtt
	ldi	ZH,high(2*demo_msg)	
	ldi	ZL,low(2*demo_msg)
	rcall	writebyteslcd
demo_btns:
	SBIS	inPort,BTN_OK
	rjmp	demo_start
	SBIS	inPort,BTN_UP
	rjmp	sa_menu
	SBIS	inPort,BTN_DOWN
	rjmp	us_menu
	SBIS	inPort,BTN_CANCEL
	rjmp	ready
	rjmp	demo_btns

demo_start:
	rcall	btn_wait
	sbr	drive_status,0b00100000
	rcall	cls
	ldi	ZH,high(2*stop_msg)	
	ldi	ZL,low(2*stop_msg)
	rcall	writebyteslcd
forward_us:
	sbi	outPort,Servo
	rcall	osd
	ldi	temp,0b00010101
	out	outPort,temp

start_us:
	SBIS	inPort,BTN_CANCEL
	rjmp	end_us

	rcall	read_usensor1
	rcall	wt
	cpi	temp,9
	brsh	next_us

	rcall	read_usensor1
	rcall	wt
	cpi	temp,9
	brsh	next_us

	rcall	read_usensor1
	rcall	wt
	cpi	temp,9
	brsh	next_us

	rcall	read_usensor1
	rcall	wt
	cpi	temp,9
	brsh	next_us

	rcall	read_usensor1
	rcall	wt
	cpi	temp,9
	brsh	next_us

	ldi	temp,0b00011010
	out	outPort,temp
	rcall	osd
	rcall	osd
	ldi	temp,0b00000000
	out	outPort,temp
	rcall	osd
	ldi	temp,0b00001001
	out	outPort,temp
	rcall	osd
	rcall	osd
	ldi	temp,0b00010000
	out	outPort,temp
	rjmp	forward_us
next_us:
	SBIS	inPort,BTN_CANCEL
	rjmp	end_us

	rcall	read_usensor2
	rcall	wt
	cpi	temp,7
	brsh	start_us

	rcall	read_usensor2
	rcall	wt
	cpi	temp,7
	brsh	start_us

	rcall	read_usensor2
	rcall	wt
	cpi	temp,7
	brsh	start_us

	rcall	read_usensor2
	rcall	wt
	cpi	temp,7
	brsh	start_us

	rcall	read_usensor2
	rcall	wt
	cpi	temp,7
	brsh	start_us

	ldi	temp,0b00011010
	out	outPort,temp
	rcall	osd
	rcall	osd
	ldi	temp,0b00000000
	out	outPort,temp
	rcall	osd
	ldi	temp,0b00000110
	out	outPort,temp
	rcall	osd
	rcall	osd
	ldi	temp,0b00010000
	out	outPort,temp
	rjmp	forward_us
end_us:
	cbr	drive_status,0b00100000
	ldi	temp,0b00000000
	out	outPort,temp
	rcall	cls
	rcall	btn_wait
	rjmp	demo_menu
usa_start:
	sbr	drive_status,0b00100000
	rcall	btn_wait
	rcall	ee_10
	ldi	data,10
	rcall	ee_write
	rcall	inc_ee
	ldi	data,0
	rcall	ee_write
	cbr	drive_status,0b00100000
	rjmp	usa_menu

usb_start:
	sbr	drive_status,0b00100000
	rcall	btn_wait
	rcall	ee_11
	ldi	data,11
	rcall	ee_write
	rcall	inc_ee
	ldi	data,0
	rcall	ee_write
	cbr	drive_status,0b00100000
	rjmp	usb_menu

us_start:
	sbr	drive_status,0b00100000
	rcall	btn_wait
	rcall	ee_12
	ldi	data,12
	rcall	ee_write
	rcall	inc_ee
	ldi	data,0
	rcall	ee_write
	cbr	drive_status,0b00100000
	rjmp	us_menu



missiondataclear:
	rcall	btn_wait
	sbic	EECR,EEWE
	rjmp	missiondataclear
	clr	eehigh
	clr	eelow
 	out 	EEARH,eehigh	
	out 	EEARL,eelow
	ldi	data,0
	out	EEDR,data
	sbi	EECR,EEMWE
	sbi 	EECR,EEWE
	rjmp	ready
dir_f:
	clr	weg
	rcall	btn_wait
	ldi	data,5
	rcall	ee_write
	rcall	inc_ee
	sbi	outPort,Servo
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*stop_msg)	
	ldi	ZL,low(2*stop_msg)
	rcall	writebyteslcd
	sbr	drive_status,0b00010000
	ldi	temp,0b00010101
	out	outPort,temp
	rcall	count
	ldi	temp,0b00000000
	out	outPort,temp
	cbr	drive_status,0b00010000
	rcall	btn_wait
	rjmp	f_menu

dir_z:
	clr	weg
	rcall	btn_wait
	ldi	data,6
	rcall	ee_write
	rcall	inc_ee
	sbi	outPort,Servo
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*stop_msg)	
	ldi	ZL,low(2*stop_msg)
	rcall	writebyteslcd
	sbr	drive_status,0b00010000
	ldi	temp,0b00011010
	out	outPort,temp
	rcall	count
	ldi	temp,0b00000000
	out	outPort,temp
	cbr	drive_status,0b00010000
	rcall	btn_wait
	rjmp	z_menu

dir_l:
	clr	weg
	rcall	btn_wait
	ldi	data,8
	rcall	ee_write
	rcall	inc_ee
	cbi	outPort,Servo
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*stop_msg)	
	ldi	ZL,low(2*stop_msg)
	rcall	writebyteslcd
	sbr	drive_status,0b00010000
	ldi	temp,0b00001001
	out	outPort,temp
	rcall	count
	ldi	temp,0b00000000
	out	outPort,temp
	cbr	drive_status,0b00010000
	rcall	btn_wait
	rjmp	l_menu

dir_r:
	clr	weg
	rcall	btn_wait
	ldi	data,7
	rcall	ee_write
	rcall	inc_ee
	cbi	outPort,Servo
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*stop_msg)	
	ldi	ZL,low(2*stop_msg)
	rcall	writebyteslcd
	sbr	drive_status,0b00010000
	ldi	temp,0b00000110
	out	outPort,temp
	rcall	count
	ldi	temp,0b00000000
	out	outPort,temp
	cbr	drive_status,0b00010000
	rcall	btn_wait
	rjmp	r_menu


	

ab_show:
	rcall	btn_wait
	rcall	cls
	ldi	ZH,high(2*ab_msg1)	
	ldi	ZL,low(2*ab_msg1)
	rcall	writebyteslcd
	rcall	osd
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*ab_msg2)	
	ldi	ZL,low(2*ab_msg2)
	rcall	writebyteslcd
	rcall	osd
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*ab_msg3)	
	ldi	ZL,low(2*ab_msg3)
	rcall	writebyteslcd
	rcall	osd
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*ab_msg4)	
	ldi	ZL,low(2*ab_msg4)
	rcall	writebyteslcd
	rcall	osd
	rcall	osd
	rcall	cls
	ldi	ZH,high(2*ab_msg5)	
	ldi	ZL,low(2*ab_msg5)
	rcall	writebyteslcd
	rcall	osd
	rcall	osd
	rjmp	ready

standalone_on:
	rcall	btn_wait
	rcall	cls
	ldi	ZH,high(2*sa_msg)	
	ldi	ZL,low(2*sa_msg)
	rcall	writebyteslcd
        sbr	drive_status,0b01100000
	clr	eehigh
	clr	eelow
	rcall	osd
	rjmp	mainloop


check_next:
	SBRS	drive_status,6
	ret
	cbr	drive_status,0b01000000
	rcall	ee_read
	rcall	inc_ee
	cpi	data,0	
	breq	ee_end
	cpi	data,5	
	breq	ee_5
	cpi	data,6	
	breq	ee_6
	cpi	data,7	
	breq	ee_7
	cpi	data,8	
	breq	ee_8
	cpi	data,10	
	breq	ee_10
	cpi	data,11	
	breq	ee_11
	cpi	data,12	
	breq	ee_12
	cpi	data,14
	breq	ee_14
	cpi	data,15
	breq	ee_15
	ret
ee_end:
	rcall	endmessage
	cbr	drive_status,0b01100000
	ret
ee_5:
	rcall	ee_read
	rcall	inc_ee
	mov	weg,data
	rcall	d_forward
	sbi	outPort,Servo
	rcall	osd
	sbr	drive_status,0b00000011
	ret
ee_6:
	rcall	ee_read
	rcall	inc_ee
	mov	weg,data
	rcall	d_backward
	sbi	outPort,Servo
	rcall	osd
	sbr	drive_status,0b00001111
	ret
ee_7:
	rcall	ee_read
	rcall	inc_ee
	mov	weg,data
	rcall	rotate
	rcall	r_right
	cbi	outPort,Servo
	rcall	osd
	sbr 	drive_status,0b00000111
	ret
ee_8:
	rcall	ee_read
	rcall	inc_ee
	mov	weg,data
	rcall	rotate
	rcall	r_left
	cbi	outPort,Servo
	rcall	osd
	sbr 	drive_status,0b00001011
	ret
ee_10:
	rjmp	SA_USENSOR1
ee_11:
	rjmp	SA_USENSOR2
ee_12:
	rjmp	SA_UREAD
ee_14:
	rcall	cls
	ldi	ZH,high(2*sa_msg)	
	ldi	ZL,low(2*sa_msg)
	rcall	writebyteslcd
        sbr	drive_status,0b01100000
	clr	eehigh
	clr	eelow
	rcall	osd
	ret
ee_15:
	rjmp	waitseconds
waitseconds:
	rcall	cls
	ldi	ZH,high(2*wait_msg)	
	ldi	ZL,low(2*wait_msg)
	rcall	writebyteslcd
	rcall	ee_read
	rcall	inc_ee
	mov	temp,data
waittime:
	cpi	temp,10
	brlo	write_wait
next_ww:
	rcall	osd
	dec	temp
	brne	waittime
	rcall	cls
	sbr	drive_status,0b01000000
	ret

write_wait:
	rcall	cls
	ldi	ZH,high(2*wait_msg)	
	ldi	ZL,low(2*wait_msg)
	rcall	writebyteslcd
	ldi	lcd,Z_X
	add	lcd,temp
	rcall	wtt
	rjmp	next_ww
SA_UREAD:
	rcall	message_wait
	rcall	wtt
	clr	wait_object
	sbi	outPort,Servo
	rcall	osd
	ldi	temp,0b00010101
	out	outPort,temp
tc:
	clr	wait_object
testu:
	rcall	read_usensor1
	rcall	wt
	cpi	temp,7
	brsh	n_u
	rjmp	u_ok
n_u:
	rcall	read_usensor2
	rcall	wt
	cpi	temp,7
	brsh	tc
u_ok:
	inc	wait_object
	cpi	wait_object,30
	brlo	testu
	ldi	temp,0b00010000
 	out	outPort,temp
	sbr	drive_status,0b01000000
	rcall	cls
	ret

SA_USENSOR1:
	rcall	message_wait
	ldi	LCD,Z_X + 1
	rcall	wtt
	clr	wait_object
	sbi	outPort,Servo
	rcall	osd
	ldi	temp,0b00010101
	out	outPort,temp
tc1:
	clr	wait_object
test1:
	rcall	read_usensor1
	rcall	wt
	cpi	temp,7
	brsh	tc1
	inc	wait_object
	cpi	wait_object,30
	brlo	test1

	ldi	temp,0b00010000
 	out	outPort,temp
	sbr	drive_status,0b01000000
	rcall	cls
	ret

SA_USENSOR2:
	rcall	message_wait
	ldi	LCD,Z_X + 2
	rcall	wtt
	clr	wait_object
	sbi	outPort,Servo
	rcall	osd
	ldi	temp,0b00010101
	out	outPort,temp
tc2:
	clr	wait_object
test2:
	rcall	read_usensor2
	rcall	wt
	cpi	temp,7
	brsh	tc2
	inc	wait_object
	cpi	wait_object,30
	brlo	test2

	ldi	temp,0b00010000
 	out	outPort,temp
	sbr	drive_status,0b01000000
	rcall	cls
	ret

message_wait:
	rcall	cls
	ldi	ZH,high(2*wfo_msg)	
	ldi	ZL,low(2*wfo_msg)
	rcall	writebyteslcd
	ret
d_forward:
	rcall	cls
	ldi	ZH,high(2*fore_msg)	
	ldi	ZL,low(2*fore_msg)
	rcall	writebyteslcd
	ret
d_backward:
	rcall	cls
	ldi	ZH,high(2*back_msg)	
	ldi	ZL,low(2*back_msg)
	rcall	writebyteslcd
	ret
rotate:
	rcall	cls
	ldi	ZH,high(2*rotate_msg)	
	ldi	ZL,low(2*rotate_msg)
	rcall	writebyteslcd
	ret
r_left:
	ldi	ZH,high(2*left_msg)	
	ldi	ZL,low(2*left_msg)
	rcall	writebyteslcd
	ret
r_right:
	ldi	ZH,high(2*right_msg)	
	ldi	ZL,low(2*right_msg)
	rcall	writebyteslcd
	ret
endmessage:
	rcall	cls
	ldi	ZH,high(2*mend_msg)	
	ldi	ZL,low(2*mend_msg)
	rcall	writebyteslcd
	rcall	osd
	rcall	osd
	rcall	osd
	rcall	cls
	ret

DriveA_stop:
	cbi	outPort,DriveA_1
	cbi	outPort,DriveA_2
	ret
DriveB_stop:
	cbi	outPort,DriveB_1
	cbi	outPort,DriveB_2
	ret

Go:
	SBRS	drive_status,DriveA_status
	rjmp Next_Drive
	SBRS	drive_status,DriveA_direction
	sbi	outPort,DriveA_1
	SBRS	drive_status,DriveA_direction
	cbi	outPort,DriveA_2
	SBRC	drive_status,DriveA_direction
	cbi	outPort,DriveA_1
	SBRC	drive_status,DriveA_direction
	sbi	outPort,DriveA_2
Next_Drive:
	SBRS	drive_status,DriveB_status
	ret
	SBRS	drive_status,DriveB_direction
	sbi	outPort,DriveB_1
	SBRS	drive_status,DriveB_direction
	cbi	outPort,DriveB_2
	SBRC	drive_status,DriveB_direction
	cbi	outPort,DriveB_1
	SBRC	drive_status,DriveB_direction
	sbi	outPort,DriveB_2
	ret

RxD:			       
	in	status,SREG	;Saves SREG register 
				;to variable status
	ldi	temp,0b00011000	;Disables serialport-interupts
	out	UCR,temp	;Disables serialport-interupts
	in	temp,UDR
	cpi	temp,0x01
	breq    Bef01
	cpi	temp,0x02
	breq    Bef02
	cpi	temp,0x03
	breq    Bef03
	cpi	temp,0x04
	breq    Bef04
	cpi	temp,0x05
	breq    Bef05
	cpi	temp,0x06
	breq    Bef06
	cpi	temp,0x07
	breq    Bef07
	cpi	temp,0x08
	breq    Bef08
	cpi	temp,0x09
	breq    Bef09
	cpi	temp,0x0A
	breq    Bef0A
	cpi	temp,0x0B
	breq    Bef0B
	cpi	temp,0x0C
	breq    Bef0C
	cpi	temp,0x0D
	breq    Bef0D
	cpi	temp,0x0E
	breq    Bef0E
	cpi	temp,200
	breq    Bef200
	cpi	temp,201
	breq    Bef201
	cpi	temp,202
	breq    Bef202

End_UDR:
	ldi	temp,0b10011000	;Enables serialport-interupts
	out	UCR,temp	;Enables serialport-interupts
	out	SREG,status	;Restores SREG register
	reti	                ;return from interupt
Bef01:
	rjmp	RESET_DEVICE
Bef02:
	rjmp	EEPROM_clear
Bef03:
	rjmp	set_mission
Bef04:
	rjmp	so_offline
Bef05:
	rjmp	forward
Bef06:
	rjmp	backward
Bef07:
	rjmp	left
Bef08:
	rjmp	right
Bef09:
	rjmp	stop
Bef0A:
	rjmp	read_usensor1
Bef0B:
	rjmp    read_usensor2
Bef0C:  
	rjmp 	read_inports
Bef0D:
	rjmp	set_outports
Bef0E:
	rjmp	so_modus
Bef200:
	rjmp	about
Bef201:
	rjmp	version
Bef202:
	rjmp	information

stop:
	clr	drive_status
	rjmp    End_UDR

set_outports:
	sbr	drive_status,0b00010000
	SBIS    USR,7
	rjmp	set_outports
	in 	temp,UDR	;Reads serialbuffer
	out	outPort,temp	;Sets the Outputports
	rjmp    End_UDR

read_inports:
	in	temp,inPort	;Get inputports
	out	UDR,temp	;Writes data on serialport
	rjmp    End_UDR

read_usensor1:
	ldi	temp,0x05
	out	TCCR0,temp
	ldi	temp,0
	out	TCNT0,temp
	cbi	outPort,U_SensorA_out
	rcall	wt
	sbi	outPort,U_SensorA_out
	
chu1:
	in	temp,TCNT0
	cpi	temp,255
	breq	endchu
	SBIS	inPort,U_SensorA_in
	rjmp	chu1
	sbi	outPort,U_SensorA_out
	rjmp	endchu
read_usensor2:
	ldi	temp,0x05
	out	TCCR0,temp
	ldi	temp,0
	out	TCNT0,temp
	cbi	outPort,U_SensorB_out
	rcall	wt
	sbi	outPort,U_SensorB_out
	
chu2:
	in	temp,TCNT0
	cpi	temp,255
	breq	endchu
	SBIS	inPort,U_SensorB_in
	rjmp	chu2
	sbi	outPort,U_SensorB_out

endchu:
	SBRC	drive_status,5
	rjmp	checker
	out	UDR,temp	;Writes data on serialport
	rjmp    End_UDR
checker:
	ret

EEPROM_clear:
	sbic	EECR,EEWE
	rjmp	EEPROM_clear
	clr	eehigh
	clr	eelow
 	out 	EEARH,eehigh	
	out 	EEARL,eelow
	ldi	data,0
	out	EEDR,data
	sbi	EECR,EEMWE
	sbi 	EECR,EEWE
	rjmp    End_UDR

set_mission:
	SBIS    USR,7
	rjmp	set_mission
	in 	data,UDR	
	rcall	ee_write
	rcall	inc_ee	
	rjmp    End_UDR

so_offline:
	sbr	drive_status,0b00100000
	rjmp    End_UDR

forward:
	SBIS    USR,7
	rjmp	forward
	in	weg,UDR
	sbi	outPort,Servo
	rcall	osd
	ldi	drive_status,0b00000011
	rcall	d_forward
	rjmp    End_UDR

backward:
	SBIS    USR,7
	rjmp	backward
	in	weg,UDR
	sbi	outPort,Servo
	rcall	osd
	ldi	drive_status,0b00001111
	rcall	d_backward
	rjmp    End_UDR

left:
	SBIS    USR,7
	rjmp	left
	in	weg,UDR
	cbi	outPort,Servo
	rcall	osd
	ldi	drive_status,0b00000111
	rcall	rotate
	rcall	r_left
	rjmp    End_UDR

right:
	SBIS    USR,7
	rjmp	right
	in	weg,UDR
	cbi	outPort,Servo
	rcall	osd
	ldi	drive_status,0b00001011
	rcall	rotate
	rcall	r_right
	rjmp    End_UDR

Go_Left:
	ldi	drive_status,0b00001011
	ret

Go_Right:
	ldi	drive_status,0b00000111
	ret

Go_Forward:
	ldi	drive_status,0b00001111
	ret

Go_Back:
	ldi	drive_status,0b00000011
	ret

so_modus:
	sbr	drive_status,0b01100000
	clr	eehigh
	clr	eelow
	rjmp    End_UDR

T_Wait: ;******** Waites for Transmit-Ready *********
	;SBIS	USR,5
	;rjmp	T_WAIT
	;SBIS	PIND,3
	;rjmp	T_WAIT
	ret

wtt:
inc 	pos
out	PORTB,LCD
sbi	PORTD,CL
rcall wt
cbi	PORTD,CL
rcall wt
cpi	pos,8
breq	newpos1
cpi	pos,48
breq	newpos2
ret

newpos2:
mov	r27,LCD
ldi	pos,0
mov	LCD,pos
subi	LCD,-0b10000000
rjmp	Befehl
newpos1:
mov	r27,LCD
ldi	pos,40
mov	LCD,pos
subi	LCD,-0b10000000
Befehl:
cbi	PORTD,RS
rcall	wt
out	PORTB,LCD
sbi	PORTD,CL
rcall wt
cbi	PORTD,CL
sbi	PORTD,RS
rcall	wt
mov	LCD,r27
ret

cls:
ldi	pos,0
ldi	LCD,0x01
rcall Befehl
ret

ee_read:
	sbic	EECR,EEWE
	rjmp	ee_read
	out 	EEARL,eelow
 	out 	EEARH,eehigh
	sbi	EECR,EERE	
	in	data,EEDR
	ret

ee_write:
	sbic	EECR,EEWE
	rjmp	ee_write
 	out 	EEARH,eehigh	
	out 	EEARL,eelow
	out	EEDR,data
	sbi	EECR,EEMWE
	sbi 	EECR,EEWE
	ret

inc_ee:
	inc	eelow
	brne	end_inc_ee
	inc	eehigh
end_inc_ee:
	ret

dec_ee:
	dec	eelow
	brne	end_dec_ee
	dec	eehigh
end_dec_ee:
	ret


wt:
	ldi	r20, 1
	ldi	r21, 5
	ldi	r22, 200
delay:	dec	r22
	brne	delay
	dec	r21
	brne	delay
	dec	r20
	brne	delay
	ret
osd:
	ldi	r20, 20
	ldi	r21, 255
	ldi	r22, 255
	rjmp delay
BTN_WAIT:
	ldi	r20,50
btn_wt:
	SBIS	inPort,BTN_OK
	rjmp	BTN_wt
	SBIS	inPort,BTN_UP
	rjmp	BTN_wt
	SBIS	inPort,BTN_DOWN
	rjmp	BTN_wt
	SBIS	inPort,BTN_CANCEL
	rjmp	BTN_wt
	dec	r20
	brne	btn_wt
	ret

Count:
	SBIS	inPort,BTN_CANCEL
	rjmp	e_count
	SBIS	inPort,D_SensorA
	rjmp	count
	inc	weg
w_count:
	SBIS	inPort,BTN_CANCEL
	rjmp	e_count
	SBIC	inPort,D_SensorA
	rjmp	w_count
	cpi	weg,255
	brne	Count
e_count:
	mov	data,weg
	rcall	ee_write
	rcall	inc_ee
	clr	data
	rcall	ee_write
	clr	weg
	ret


;******************************** About-Sendings ******************************

about:
	ldi	ZH,high(2*ONGEL)	; Load high part of byte address into ZH
	ldi	ZL,low(2*ONGEL)	; Load low part of byte address into ZL
	rjmp	writebytes

Version:
	ldi	ZH,high(2*vers)	; Load high part of byte address into ZH
	ldi	ZL,low(2*vers)	; Load low part of byte address into ZL
	rjmp	writebytes

Information:
	ldi	ZH,high(2*info)	; Load high part of byte address into ZH
	ldi	ZL,low(2*info)	; Load low part of byte address into ZL
	rjmp	writebytes

writebytes:
	in	r0,USR
	SBRS	r0,5
	rjmp	writebytes
	lpm				; Load byte from program memory into r0
	tst	r0			; Check if we've reached the end of the message
	breq	endwrite		; If so, quit
	rcall	wt
	rcall	wt
	out	UDR,r0		
	adiw	ZL,1			; Increase Z registers
	rjmp	writebytes
endwrite:
	rjmp    End_UDR

writebyteslcd:
	lpm				; Load byte from program memory into r0
	tst	r0			; Check if we've reached the end of the message
	breq	endwritelcd		; If so, quit
	mov	lcd,r0
	rcall	wtt
	adiw	ZL,1			; Increase Z registers
	rjmp	writebyteslcd
endwritelcd:
	ret


;************************** Informations/ROM *************************

ONGEL:
.db	"ONGEL by Manuel Schreiner, Christian Klose & Florian Mehm - http://www.schreiner-homebase.de/ongel/"
.db	0

VERS:
.db	"ONGEL V1.5"
.db	0

System_load:
.db	" Loading System"
.db	0

Serial_speed:
.db	"Serial: 9600,N,1"
.db	0

IR_online:
.db	"IR online & rdy "
.db	0

IR_offline:
.db	"IR not connected"
.db	0

INFO:
.db	"Processor: AT90S8515"
.db	"Speed    : 4,096 MHz"
.db	"Serial   : 9600,N,1 "
.db     "Voltage  : 12V 1A   "
.db     "                    "
.db	"Internal-Program by "
.db     "Manuel Schreiner    "
.db	0

ROTATE_MSG:
.db "Drehung: "
.db 0

LEFT_MSG:
.db "Links"
.db 0

Right_MSG:
.db "Rechts"
.db 0

Ongel_Ready:
.db " ONGEL bereit..."
.db 0

mend_msg:
.db "  MISSIONSENDE"
.db 0

fore_msg:
.db "   Vorfahren"
.db 0

back_msg:
.db " Zurueckfahren"
.db 0

wfo_msg:
.db "Wait for object"
.db 0

sa_msg:
.db " StandAloneMode"
.db 0

ab_msg:
.db " About"
.db 0

ab_msg1:
.db "ONGEL created by"
.db 0

ab_msg2:
.db "Christian Klose"
.db 0

ab_msg3:
.db "Manuel Schreiner"
.db 0

ab_msg4:
.db "Florian Mehm"
.db 0

ab_msg5:
.db " Vikomade  2000 "
.db 0

f_msg:
.db " Vorfahren"
.db 0

z_msg:
.db " Zurueckfahren"
.db 0

l_msg:
.db " Links drehen"
.db 0

r_msg:
.db " Rechts drehen"
.db 0

stop_msg:
.db "Auf rot druecken"
.db 0

mc_msg:
.db " Missonclear"
.db 0

usa_msg:
.db " USensor 1"
.db 0

usb_msg:
.db " USensor 2"
.db 0

us_msg:
.db " USensor 1/2"
.db 0

demo_msg:
.db " Demomode"
.db 0

buttons:         
.db " C  UP  DOWN  OK"
.db 0

wait_msg:
.db "Please wait... "
.db 0

RESET_DEVICE: