;-----------------------------------------------------------------
; Name:			16 MHz#1.asm
; Description:	Test Output Compare PWM Mode 8 Functionality.
;				With external interrupts
;				new clock speed at 16 MHz

; Author:		Max Koessick
; Class:		EEL5666C, Intelligent Machine Design Lab
; Date:			June 6, 2003
; Revision		1.a
; Changes to Date:
;				6/6/03 First Revision
;				7/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 16 MHz/256(prescaler), the 
;	new clock is equal to 16us.  A TOP value must be defined in order 
;	to create the proper PWM.  The value for TOP is (20ms/2)/16us.  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)/16us=625=0x271=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/16us=47 clocks.  TOP - 47 = 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			; General Purpose Delay Register
.def		delay3	=r20			; General Purpose Delay Register
.def		OCRH	=r21			; Output Compare Register High Byte Mirror
.def		OCRL	=r22			; Output Compare Register Low Byte Mirror

; Equate statements

.equ		L90H 	= 	$4a			; Left 90 degrees upper byte
.equ		L90L	=	$38
.equ		NH		= 	$48			; Neutral Position High Byte
.equ		NL		=	$44
.equ		R90H 	= 	$46			; Right 90 deg. High Byte
.equ		R90L	=	$50
.equ		TOPH	=	$4e			; Top High Byte
.equ		TOPL	=	$20
.equ		ZERO	= 	$00
.equ		ONEs	=	$FF


;-----------------------------------------------------------------
; 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		mpr2,(1<<PB5)|(1<<PB6)|(1<<PB7)
	out		PortB,mpr2

	out 	DDRC,mpr
	out		PORTC,mpr	

	clr 	mpr						; clear all bits
	out 	DDRD,mpr				; Set 'D' as input

;-----Disable A/D-------------------------------------------------
	ldi		mpr,$80					; Disable ADC
	out		ACSR,mpr
;
;-----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,1,2,3 set
	out		EIMSK,mpr

;-----Enable Output Compare 1A-C----------------------------------------

	ldi		mpr,0b11111100			; Set Timer1 to PWM mode 8
	out		TCCR1A,mpr

	ldi		mpr,0b00010100			; Prescaler = 256
	out		TCCR1B,mpr

	ldi		mpr,TOPH
	ldi		mpr2,TOPL
	out		ICR1H,mpr
	out		ICR1L,mpr2				; Set Top Value to 625 = $271
									; Period = 20ms

	ldi		mpr,ZERO					; Clear the TCNT register
	ldi		mpr2,ZERO
	out		TCNT1H,mpr
	out		TCNT1L,mpr2	


	ldi		r30,OCR1CH
	ldi		mpr,L90L
	st		Z,mpr

	ldi		r30,OCR1CL
	ldi		mpr,L90H
	st		Z,mpr

;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		r30,OCR1CH
	ldi		mpr,L90L
	st		Z,mpr

	ldi		r30,OCR1CL
	ldi		mpr,L90H
	st		Z,mpr

	ldi		mpr,$55
	out 	portc,mpr

	reti

;-----------------------------------------------------------------
;
; Int1 vector - set equate equal to desired servo direction
;				INT0 = 0 degrees or ServoDir = 2

IntV1:

	ldi		mpr,NL
	out		OCR1AH,mpr
	ldi		mpr,NH
	out		OCR1AL,mpr

		
	ldi		mpr,$aa
	out 	portc,mpr

	reti
;-----------------------------------------------------------------
;
; Int1 vector - set equate equal to desired servo direction
;				INT0 = +90 degrees or ServoDir = 3

IntV2:

	ldi		mpr,R90L
	out		OCR1AH,mpr
	ldi		mpr,R90H
	out		OCR1AL,mpr

		
	ldi		mpr,$00
	out 	portc,mpr
	reti



