;-----------------------------------------------------------------
; Name:			ServoNUM3.asm
; Description:	Test Output Compare PWM Mode 8 Functionality.

; Author:		Max Koessick
; Class:		EEL5666C, Intelligent Machine Design Lab
; Date:			June 6, 2003
; Revision		1.a
; Changes to Date:
;				6/6/03 First Revision
;				8/11/03 Successful integration of External Interrupts 
;						servo control.

; General Info:
; Servo period is 20ms; i.e. the servo is sent a 1.0-2.0ms pulse 
; 	every 20 ms.  Using a clock speed of 3.69 MHz/64(prescaler), the 
;	new clock is equal to 17us.  A TOP value must be defined in order 
;	to create the proper PWM.  The value for TOP is 20ms/2.  The TCNT1 
;	register counts to this value and then back down again.  A compare 
;	match is made on both the upcount and downcount.  These 2 compare
;	matches generate the PWM.

;	Formulas:
;		(20ms/2)/17us=588=0x24C=TOP
;			*TCNT counts up to TOP and back down again, so desired 
;			 period=TOP*2
;			*NEUTRAL (1.5ms)--Compare match made twice while upcounting and
;			 downcounting.  Needed period = .75ms.
;			 .75ms/17us=44 clocks.  TOP - 44 = OCR1A
;			 Experiment with this number to get the servo in *exact* neutral
;			 postion
;			*+90 (2.0ms) Similiar to above
;			*-90 (1.0ms) Ditto
			
;-----------------------------------------------------------------

.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)


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

.def		mpr=r16					; defines multipurpose register
.def		mpr2=r17				; multipurpose register 2
.def		delay=r18				; servo direction register (values 1-3)
.def		delay2=r19
.def		delay3=r20

;-----------------------------------------------------------------
; 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"
	ldi		mpr,(1<<PORTB1)|(1<<PORTB0)|(1<<PORTB2)|(1<<PORTB3)|(1<<PORTB4)
	out		PORTB,mpr				; ...all lights off!

	out		ddre,mpr
	out 	PORTE,mpr

	clr 	mpr						; clear all bits
	out 	DDRD,mpr				; Set 'D' as input


;-----Enabling Interrupts-----------------------------------------

;Clear INT0 and INT1 in EIMSK register
	in		mpr,EIMSK
	cbr		mpr,(0<<INT0)+(0<<INT1)+(0<<INT2) ; Disable int 0,1,2 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)				
									; Set three 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);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) ; int masks 0 and 1 set
	out		EIMSK,mpr

;-----Enable Output Compare 1A-C----------------------------------------

	ldi		mpr,0b11111100			; Set Timer1 to PWM mode 8
	out		TCCR1A,mpr

	ldi		mpr,0b00010011			; Prescaler = 64
	out		TCCR1B,mpr

	ldi		mpr,$02
	ldi		mpr2,$4c
	out		ICR1H,mpr
	out		ICR1L,mpr2				; Set Top Value to 1177 = $499
									; Period = 20ms

	ldi		mpr,$00
	ldi		mpr2,$00
	out		TCNT1H,mpr
	out		TCNT1L,mpr2	


	ldi		mpr,$02					
	ldi		mpr2,$22
load:
	out		OCR1AH,mpr
	out		OCR1AL,mpr2				; Set DIRservo = 1.5ms Neutral

;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		mpr,$02
	out		OCR1AH,mpr
	ldi		mpr,$45
	out		OCR1AL,mpr

	reti

;-----------------------------------------------------------------
;
; Int1 vector - set equate equal to desired servo direction
;				INT0 = 0 degrees or ServoDir = 2

IntV1:

	ldi		mpr,$02
	out		OCR1AH,mpr
	ldi		mpr,$27
	out		OCR1AL,mpr

	reti
;-----------------------------------------------------------------
;
; Int1 vector - set equate equal to desired servo direction
;				INT0 = +90 degrees or ServoDir = 3

IntV2:

	ldi		mpr,$02
	out		OCR1AH,mpr
	ldi		mpr,$0a
	out		OCR1AL,mpr
	reti


