**************************************************************************
* RTI Interupt Example    EEL-4744      EMS & AAA *
**************************************************************************
********************************************************
* Run a Single Process using the Real-Time Interrupt
* Port B
* $01-->$02-->$04-->$08-->$10-->$20-->$40-->$80-->$01...
* Rotate the value in PORTB every 32.77 ms
* USE REAL TIME INTERRUPT AS TIME BASE
* USING an 8.0 MHZ CRYSTAL FOR THE 68HCll CLOCK
********************************************************
* SYMBOL DEFINITIONS
********************************************************
* 68HCll REGISTERS
RTIV		EQU	$FFF0		;RTI Interrupt Vector Location
RAM		EQU	$0000		;Start of RAM
RAMEND		EQU	$01FF		;End of RAM
EEPROM		EQU	$B600		;Start of EEPROM
BASE		EQU	$1000		;Base Address of Internal register
PORTB		EQU	$04		;Output-only PORT B REGISTER
TMSK2		EQU	$24		;TIMER MASK REGISTER
TFLG2		EQU	$25		;TIMER FLAG REGISTER
PACTL		EQU	$26		;PULSE ACCUMULATOR CONTROL REG
OUTPAT1		EQU	$01		;OUTPUT Pattern for PORTB
********************************************************
* MASKS
********************************************************
BIT6		EQU	%01000000
BIT10		EQU	%00000011
********************************************************
* DATA SECTION
********************************************************
		ORG 	RTIV		;RTI INTERRUPT VECTOR
		FDB	RTI_ISR
********************************************************
* MAIN PROGRAM
********************************************************
		ORG 	EEPROM
*-----------------------------------------------------
* INITIALIZATION
*-----------------------------------------------------
		LDS	#RAMEND
		LDX	#BASE

* INITIALIZE PORT B
		LDAA	#OUTPAT1	;SET UP PORTB
		STAA	PORTB,X		;ALL OUTPUTS AFFECTED

* INITIALIZE REAL TIME INTERRUPT
		BSET	PACTL,X BIT10	;INTERRUPT at 32.7ms

*   CLEAR RTIF FLAG JUST IN CASE
		LDAA	#BIT6		;Set RTIF; ZEROS IN OTHER BITS
		STAA	TFLG2,X		;..CAUSE NO PROBLEMS

*   ENABLE RTIF INTERRUPT
		LDAA	#BIT6		;set RTII to enable
		STAA	TMSK2,X		;..RTI interrupt; other 0's OK

* TURN ON INTERRUPT SYSTEM
		CLI
		WAI

********************************************************
* Main Program does its work here
******************************************************
HERE		BRA	HERE

********************************************************
* INTERRUPT SERVICE ROUTINE
********************************************************
		ORG	$B700
*------------------------------------------------------
* INTERRUPT POLLING CHAIN
*------------------------------------------------------
* INTERRUPT FROM REAL TIME INTERRUPT DEVICE?
RTI_ISR		LDX	#BASE
     		BRCLR	TFLG2,X BIT6 QUIT	;IGNORE ILLEGAL INTERRUPT
*------------------------------------------------------
* Clear the RTIF Flag
*------------------------------------------------------
		LDAA	#BIT6		;STORE 1 in Bit#6 TO CLEAR RTIF FLAG!
		STAA	TFLG2,X		;..0's Do not affect other Flags
*------------------------------------------------------
* SWITCH PORT B OUTPUTS
*------------------------------------------------------
		LDAA	PORTB,X		;Get current pattern
		ROLA			;Rotate it
		BNE	OK		;If zero, re-initialize
		LDAA	#OUTPAT1	;..
OK		STAA	PORTB,X		;Put it Back
QUIT		RTI
