;-----------------------------------------------------------------
; Name:			Starting Wait Loop.asm
; Description:	Implements Starting Loop for Robot Demo.  
;				Wait until either PinE6 or PinE7 is pressed before
;					program sequence starts

; Author:		Max Koessick
; Class:		EEL5666C, Intelligent Machine Design Lab
; Date:			July 8, 2003
; Revision		1.a (completed and 100% Functional)

; PE6 and PE7 are connected to normally closed switches.
; Internal Pullups are enabled and a high true signal is wanted
; Program stays in wait loop until PE6 or PE7 goes high 
; Signaling that a bump switch has been tapped
;-----------------------------------------------------------------

.nolist
.include "m323def.inc"
.list

; Interrupt service vectors

.org $0000
	rjmp Reset						; Reset vector

;-----------------------------------------------------------------
; Register defines for main loop
;-----------------------------------------------------------------

.def		mpr		=r16			; defines multipurpose register

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

Reset:
;-----Setting Stackpointer----------------------------------------
	ldi		MPR,low(RAMEND)			; Set stackptr to ram end
	out		SPL,MPR
	ldi 	MPR, high(RAMEND)
	out 	SPH, MPR

;-----Set Port Directions-----------------------------------------
	ldi		mpr,0b11110011			; Set PE6 and PE7 to input
	out		DDRD,mpr
	ldi		mpr,(1<<PD2)|(1<<PD3)
	out		PortD,mpr				; Set Pullups on Input

	ser		mpr
	out		DDRA,mpr				; for testing
	out		PortA,mpr				; lights off
;-----------------------------------------------------------------


WaitToStart:
	in		mpr,PIND				; read Port E
	andi	mpr,$80					; mask lower bits
	sbrc	mpr,7					; skip if bit in register set
	rjmp	Start					; ...if not, break out
	in 		mpr,PIND				; read Port E
	andi	mpr,$40					; mask bit 6
	sbrc 	mpr,6					; skip if bit in register set

	rjmp	Start					; ...if not, break out
	rjmp	WaitToStart				; keep waiting
	
Start:
	clr		mpr
	out		PortA,mpr				; Turn LEDs on

Mainloop:
	rjmp 	mainloop


	

						