;-----------------------------------------------------------------
; Name:			OCR0ServoTest.asm
; Description:	Tests external interrupt functionality.
; 				When a respective switch is pressed, generate an output compart
;				to control a servo in the -90,0,90 directions
; Author:		Max Koessick
; Class:		EEL5666C, Intelligent Machine Design Lab
; Date:			June 6, 2003
; Revision		1.a
; Changes to Date:
;				6/5/03 First Revision
			
;-----------------------------------------------------------------

.include "m128def.inc"

; Interrupt service vectors

.org $0000
	rjmp Reset						; Reset vector
.org INT0addr
	rjmp IntV0						; INT0 vector (ext. interrupt from pin D0)	
.org INT1addr	
	rjmp IntV1						; INT1 vector (ext. interrupt from pin D1)
.org INT2addr		
	rjmp IntV2						; INT2 vector (ext. interrupt from pin D2)
.org OC0addr						
	rjmp INTOC0						; OC0 Vector

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

.def		mpr=r16					; defines multipurpose register
.def		mpr2=r17				; multipurpose register 2
.def		ServoDir=r18				; servo direction register (values 1-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-----------------------------------------
	ldi		mpr,$FF					; Set multipurpose register to $EF to...
	out		DDRB,mpr				; sets all as output except
	out		PORTB,mpr				; bit4 OCR0

	clr 	mpr						; clear all bits
	out 	DDRD,mpr				; Set 'D' as input

	ldi 	r20,$ff



;-----Enabling Interrupts-----------------------------------------

;-----Enable Output Compare----------------------------------------

	in		mpr,TIMSK
	sbr		mpr,(1<<OCIE0)
	out		TIMSK,mpr				;Enable Output Compare 0

	in		mpr,TCCR0
	sbr		mpr,(1<<COM00)+(1<<WGM01)+(1<<CS02)+(1<<CS01)
	out		TCCR0,mpr
;Sets Output Compare to:
;	CTC (Clear Timer on Compare) mode WGM01=1,WGM00=0
;   Prescaler = /256	CS02:0 = 0b110
;	Compare Output Mode = Toggle on Compare COM1:0 = 0b01

;	Move servo to Neutral position to start
							; load address of output compare register 0 into Z
	ldi 	mpr,$0a

				; Load OCR0 (3.69MHz)/(256 Prescaler)=14.41 kHz; f=1/14.41 = 69.38us
	out		OCR0,mpr			; Want 0 degrees or OCO0 = 1.5ms; 1.5ms/69.38us = 14.4 or 
							; 22 clocks.  So load OCR0 with 22 or $16


;Enable global interrupts

	sei

mainloop:
	

	rjmp mainloop					;loop here forever.

;-----------------------------------------------------------------
;
; Int0 vector - set equate equal to desired servo direction
;				INT0 = -90 degrees or ServoDir = 1

IntV0:
	ldi		ServoDir,$01
	ldi		mpr,$01
	out		portb,mpr
	reti

;-----------------------------------------------------------------
;
; Int1 vector - set equate equal to desired servo direction
;				INT0 = 0 degrees or ServoDir = 2

IntV1:
	ldi		ServoDir,$02
	ldi		mpr,$02
	out		portb,mpr
	reti
;-----------------------------------------------------------------
;
; Int1 vector - set equate equal to desired servo direction
;				INT0 = +90 degrees or ServoDir = 3

IntV2:
	ldi		ServoDir,$03
	ldi		mpr,$03
	out		portb,mpr

	reti


;------------------------------------------------------------------
;Output Compare Interrupt

INTOC0:

	reti





