;-----------------------------------------------------------------
; Name:			INT.asm
; Description:	Tests external interrup functionality.
; 				When a respective switch is pressed, the program will
; 				increment or decrement LEDs attached to PortB via ISR
; Author:		Max Koessick
; Class:		EEL5666C, Intelligent Machine Design Lab
; Date:			June 4, 2003
; Revision		FINAL (with EICRA!!!!)
; Changes to Date:
;				6/1/03 Working with EICRB only, 2 Ints doing dec/inc of LEDs
;				6/5/03 Fixed EICRA bug and added one more external interrupt to 
;					   prepare for servo control
;				6/14/03 Added INT4
;						Added Pullups to PORTD input pins to work with
;							bump switches.
			
;-----------------------------------------------------------------

.include "m128def.inc"

; Interrupt service vectors

.org $0000
	rjmp Reset						; Reset vector
.org INT0addr
	rjmp IntV0						; INT0 vector (ext. interrupt from pin D0)--Front Left	
.org INT1addr	
	rjmp IntV1						; INT1 vector (ext. interrupt from pin D1)--Front Right
.org INT2addr		
	rjmp IntV2						; INT2 vector (ext. interrupt from pin D2)--Left Rear
.org INT3addr			
	rjmp IntV3						; INT3 vector (ext. interrupt from pin D3)--Right Rear

; Bump Switch Map
; PD0->Front Left
; PD1->Front Right
; PD2->Left Rear
; PD3->Right Rear

	
;-----------------------------------------------------------------
; Register defines for main loop
;-----------------------------------------------------------------


.def		mpr=r16					; defines multipurpose register
.def		mpr2=r17				; multipurpose register 2
.def		mpr3=r18				; multipurpose register 3


;-----------------------------------------------------------------
; 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-----------------------------------------
	ser		mpr						; Set TEMP to $FF to...
	out		DDRB,mpr				; ...set data direction to "out"
	out		DDRC,mpr

	clr		mpr
	out		PORTC,mpr				; ...all lights off!

	clr 	mpr						; clear all bits
	ldi		mpr2,(1<<PD0)+(1<<PD1)+(1<<PD2)+(1<<PD3)
									; Define pull ups on the input pins of PORTD
	out 	DDRD,mpr				; Set 'D' as input
	out		PORTD,mpr2


;-----Enabling Interrupts-----------------------------------------

;Clear INT0 and INT1 in EIMSK register
	in		mpr,EIMSK
	cbr		mpr,(0<<INT0)+(0<<INT1)+(0<<INT2)+(0<<INT3) ; Disable int 0,1,2,3 to set level control
	out		EIMSK,mpr
;Set level sense control
	ldi		r30,EICRA
	ld		mpr,Z
	sbr 	mpr,(1<<ISC00)+(1<<ISC01)+(1<<ISC10)+(1<<ISC11)+(1<<ISC20)+(1<<ISC21)+(1<<ISC30)+(1<<ISC31)				
									; Set four pairs of interrupt control bits high
	st		Z,mpr
;Clear any interrupts pending in EIFR
	in		mpr,EIFR
	sbr     mpr, (1<<INTF0)+(1<<INTF1)+(1<<INTF2)+(1<<INTF3);Set interrupt flag in EIFR
	out		EIFR,mpr
;Set INT0 and INT1 in EIMSK register
	in		mpr,EIMSK
	sbr		mpr,(1<<INT0)+(1<<INT1)+(1<<INT2)+(1<<INT3) ; int masks 0,1,2,3 set
	out		EIMSK,mpr

;Clear MPR2 for use as counter
	clr		mpr2

;Enable global interrupts

	sei

mainloop:

	rjmp mainloop					;loop here forever.

;-----------------------------------------------------------------
;
; Int0 vector - decrease count

IntV0:
	ldi		mpr2,$FF
	rjmp	Int01					;jump to common code to display new count


;-----------------------------------------------------------------
;
; Int1 vector - increase count

IntV1:
	ldi 	mpr2,$01				;drop to common code to display new count
	rjmp	Int01

IntV2:
	ldi		mpr2,$02
	rjmp	int01

IntV3:
	ldi		mpr2,$03
	
Int01:
	mov		mpr3,mpr2				;display on LEDs
	com		mpr3
	out		PORTC,mpr3
	out		Portb,mpr3
	reti






