;-----------------------------------------------------------------
; Name:		rev2.asm
; This program is a portion of the stapleit project.  Thanks to Max 
; for helping with the ADC
;-----------------------------------------------------------------

.include "m128def.inc"

; Interrupt service vectors

.org $0000
	rjmp Reset	; Reset vector
	
;-----------------------------------------------------------------
; Register defines for main loop
;-----------------------------------------------------------------

.def Direction = R16          ;Gives "Defines" Register R16 the name 
.def	TEMP=r17
.def	MUX_SEL=r21
.def	TEMP2=r22
.def	UPADDRMIR=r23
.def Pulse = R18
.def Delay = R19
.def Delay2 = R24
.def DisplayE = R15
.def DisplayA = R14
.def Sense1 = R13
.def OffSense1 = R10
.def Sense2 = R12
.def OffSense2 = R09
.def Sense3 = R11
.def OffSense3 = R08

;-----------------------------------------------------------------
; Reset vector
;-----------------------------------------------------------------

Reset:

	ldi		TEMP,$FF 			; Set PORTB (digital counter input) data direction
	out		DDRB,TEMP
	out		DDRC,TEMP
	ldi		TEMP,$00
	out 	DDRE, TEMP
	ldi 	TEMP, $07
	out 	DDRA, TEMP

rjmp initLCD
doneLCD:
	ldi		TEMP,$F8
	sts		$61,TEMP			; (DDRF) Set A/D channel 0 for input, else output

	; -- ADC initialization --

	; ADMUX = 01100000, result left adjusted,
	; channel 0 selected 
	ldi		TEMP,$20
	out		ADMUX,TEMP
	; ADCSRA = 10000101, ADEN set, conversion not yet started, Single conversion mode
	; ADC interrupt not activated, XTAL (11.0592MHz) div by 32 for max resolution
	; NOTE: a 1 will be written to bit 6 of ADCSR to start each conversion
	; NOTE: ADIF (bit 4) will be checked for completion of each conversion
	ldi		TEMP,$85
	out		ADCSR,TEMP

	ldi Temp, $00
	mov Sense1, Temp
	mov Sense2, Temp
	mov Sense3, Temp
	mov OffSense1, TEMP
	mov OffSense2, TEMP
	mov OffSense3, TEMP

;1 second Delay loop to allow ATD to stabilize

;change between 1f for real and 01 for debug
	ldi		Delay,$1F
loop_a1:
    ldi		TEMP,$FF
outer_a1:	
	ldi		TEMP2,$FF
inner_a1:
	dec		TEMP2
	brne	inner_a1

	dec		TEMP
	brne	outer_a1

	dec		Delay
	brne	loop_a1

;end delay loop

;-----------------------------------------------------------------
; read one analog sensor
;-----------------------------------------------------------------

LOOP:
again_AD:

	sbis PIND, 0x00
	rjmp Go

	sbis PIND, 0x01
	rjmp Back

	sbis PIND, 0x02
	rjmp GoM2

	sbis PIND, 0x03
	rjmp BackM2

	sbis PIND, 0x07
	rjmp LCD

	in		TEMP,ADCSR		; set bit 6 in ADCSR to 1 to initialize conversion
	sbr		TEMP,$40
	out		ADCSR,TEMP

poll_AD:
	ldi		TEMP2,$10		; poll for ADIF (AD interrupt flag-- bit 4)
	in		TEMP,ADCSR		; once flag is set grab value from AD result register
	and		TEMP,TEMP2
	breq	poll_AD

	in		TEMP,ADCH		; load 8 bit conversion result
	com		TEMP
	out		PORTB, TEMP	
	com		TEMP

	sbi		ADCSR,4		; reset ADIF flag

;if 1x we have sense3
;if 01 we have sense2
;if 00 we have sense1 in TEMP
sbic ADMUX, 0x01
	rjmp Set3
sbic ADMUX, 0x00
	rjmp Set2

	rjmp Set1

backSet:

end:
;1 second Delay loop to allow ATD to stabilize

	ldi		Delay,$03
loop_a1a:
    ldi		TEMP,$FF
outer_a1a:	
	ldi		TEMP2,$FF
inner_a1a:
	dec		TEMP2
	brne	inner_a1a

	dec		TEMP
	brne	outer_a1a

	dec		Delay
	brne	loop_a1a

;end delay loop

sbis PIND, 0x04
	rjmp	chan1
sbis PIND, 0x05
	rjmp 	chan2
sbis PIND, 0x06
	rjmp	chan3

rjmp again_AD

Set3:
	ldi Pulse, 0x00
	mov Sense3, TEMP
	cp OffSense3, Pulse
	breq first

	sub Sense3, OffSense3
;	brcs away3
	rjmp compare
;	rjmp backSet

away3:
;	ldi Sense3, 0x01
	rjmp compare

Set2:
	mov Sense2, TEMP
	sub Sense2, OffSense2
;	brcs away2
	rjmp chan3
;	rjmp backSet

away2:
;	ldi Sense2, 0x01
	rjmp chan3

Set1:
	Mov Sense1, TEMP
	sub Sense1, OffSense1
;	brcs away1
	rjmp chan2
;	rjmp backSet

away1:
;	ldi Sense1, 0x01
	rjmp chan2

first:
	mov OffSense3, TEMP
	mov OffSense2, Sense2
	mov OffSense1, Sense1
	rjmp Chan1

compare:

	cp Sense1, Sense2
	brlo others

	cp Sense1, Sense3
	brlo others

	rjmp direction1

others:
	cp Sense2, Sense3
	brlo direction3

	rjmp direction2

direction1:
	rjmp BackM2

direction2:
	rjmp Back

direction3:
	rjmp Go

chan1:
	ldi		TEMP,$20
	out		ADMUX,TEMP
	ldi		TEMP,$FF
	out		PORTB, TEMP
	rjmp wait_then_AD

chan2:
	ldi		TEMP,$21
	out		ADMUX,TEMP
	ldi		TEMP,$FE
	out		PORTB, TEMP
	rjmp wait_then_AD

chan3:
	ldi		TEMP,$22
	out		ADMUX,TEMP
	ldi		TEMP,$FD
	out		PORTB, TEMP
	rjmp wait_then_AD

wait_then_AD:
	ldi		Delay,$04
loop_a1b:
    ldi		TEMP,$FF
outer_a1b:	
	ldi		TEMP2,$FF
inner_a1b:
	dec		TEMP2
	brne	inner_a1b

	dec		TEMP
	brne	outer_a1b

	dec		Delay
	brne	loop_a1b
rjmp again_AD

;-------------init LCD-----------------
initLCD:

;	E0	E1	E2	E3	E4	E5	E6	E7
;	DB0	DB1	DB2	DB3	DB4	DB5	DB6	DB7
;
;	A0	A1	A2
;	R/W	D/I	E
;		(RS)

	ldi TEMP, 0x38
	out PORTE, TEMP

	nop
	nop

	ldi TEMP, 0x0E
	out PORTE, TEMP

	nop
	nop

	ldi TEMP, 0x06
	out PORTE, TEMP

	nop
	nop

	ldi TEMP, 0x02
	out PORTA, TEMP
	ldi TEMP, 0x53
	out PORTE, TEMP

	nop
	nop

;	nop
;	nop

;	ldi TEMP, 0x01  ;DisplayA
;	out PORTA, TEMP

;	ldi TEMP, 0x38	;DisplayE
;	out PORTE, TEMP

;nop
;nop
;nop

;	ldi TEMP, 0x01	;DisplayA
;	out PORTA, TEMP

;	ldi TEMP, 0x0F	;DisplayE
;	out PORTE, TEMP

	rjmp doneLCD

LCD:

	ldi TEMP, 0x02	;DisplayA
	out PORTA, TEMP
nop
nop
nop

	ldi TEMP, 0x03	;DisplayA
	out PORTA, TEMP

nop
nop
nop

	ldi TEMP, 0x52	;DisplayE
	out PORTE, TEMP

	rjmp LOOP

Go:
	ldi TEMP, 0x02
Gob:
	ldi Delay2, 0x08
DLY:
	dec Delay
	brne DLY
	dec Delay2
	brne DLY

	ldi Pulse, 0x2A
	out PORTC, Pulse

	ldi Delay2, 0x08
DLY2:
	dec Delay
	brne DLY2
	dec Delay2
	brne DLY2

	ldi Pulse, 0x22
	out PORTC, Pulse
	dec TEMP
	brne Gob
	rjmp chan1

Back:
	ldi TEMP, 0x02

Backb:
	ldi Delay2, 0x08
DLYB:
	dec Delay
	brne DLYB
	dec Delay2
	brne DLYB

	ldi Pulse, 0x28
	out PORTC, Pulse

	ldi Delay2, 0x08
DLY2B:
	dec Delay
	brne DLY2B
	dec Delay2
	brne DLY2B

	ldi Pulse, 0x20
	out PORTC, Pulse
	dec TEMP
	brne Backb

	rjmp chan1

GoM2:
	ldi TEMP, 0x02

GoM2b:
	ldi Delay2, 0x08
DLYM2:
	dec Delay
	brne DLYM2
	dec Delay2
	brne DLYM2

	ldi Pulse, 0x15
	out PORTC, Pulse

	ldi Delay2, 0x08
DLYM22:
	dec Delay
	brne DLYM22
	dec Delay2
	brne DLYM22

	ldi Pulse, 0x11
	out PORTC, Pulse
	dec TEMP
	brne GoM2b	
	rjmp chan1

BackM2:
	ldi TEMP, 0x02

BackM2b:

	ldi Delay2, 0x08
DLYBM2:
	dec Delay
	brne DLYBM2
	dec Delay2
	brne DLYBM2

	ldi Pulse, 0x14
	out PORTC, Pulse

	ldi Delay2, 0x08
DLY2BM2:
	dec Delay
	brne DLY2BM2
	dec Delay2
	brne DLY2BM2

	ldi Pulse, 0x10
	out PORTC, Pulse
	dec TEMP
	brne BackM2b
	rjmp chan1