**************************************************************************
* Timer Overflow Example (for NO BUFFALO)  EEL-4744   EMS & AAA *
**************************************************************************
*********************************************************
* REVERSE ALL PORTC BITS EVERY 262.1 MILLISECONDS
* USE TIMER OVERFLOW AS TIME BASE.
* Record the number of overflows in TcntExt.
* USE 8.0 MHZ CRYSTAL FOR 68HCll CLOCK
*********************************************************
*
*********************************************************
** SYMBOL DEFINITIONS
*********************************************************
* 68HCll REGISTERS
STACK	EQU	$01FF
BASE	EQU	$1000
PORTC	EQU	$03	;I/O PORT C REGISTER
DDRC	EQU	$07	;DATA DIRECTION REGISTER C
TMSK2	EQU	$24	;TIMER MASK REGISTER
TFLG2	EQU	$25	;TIMER FLAG REGISTER
IOPAT	EQU	$FF	;I/O PATTERN, 0=IN 1=OUT
* MASKS
BIT7	EQU	%10000000
INV7	EQU	%01111111
BIT1	EQU	%00000010
BIT0	EQU	%00000001
*********************************************************
** DATA SECTION
*********************************************************
	ORG	$0010
TCNTEXT	RMB	2	;16 bit count of overflows

	ORG	$FFDE	;TOI INTERRUPT VECTOR
     	FDB	TOISR
*
*********************************************************
** MAIN PROGRAM
*********************************************************
	ORG	$B600
*-----------------------------------------------------
* INITIALIZATION
*-----------------------------------------------------
* INITIALIZE STACK & ALL REGISTERS (so SIM11 does not BURP)
	LDS	#STACK
	LDX	#BASE
        CLRB
        LDY     #0	; Initialize counter
	STY	TCNTEXT
*
* INITIALIZE PORT C
* SET UP PORTC INS AND OUTS
	LDAA	#IOPAT
	STAA	DDRC,X	;ALL OUTPUTS
*
* INITIALIZE OUTPUTS TO ZEROS
	CLR	PORTC,X	;0=LOW
*
* INITIALIZE TIMER	;OVERFLOW INTERRUPT
* (Note: This MUST occur within 64 E-clocks of reset)
	BSET	TMSK2,X BIT1	;PR1:PR0=10 FOR 262.1 msec; (11 for 524.3 ms)
	BCLR	TMSK2,X BIT0	;  (01 for 131.1 msec; 00 for 32.77 ms)
	BCLR	TFLG2,X INV7	;Clear flag (TOF)
	BSET	TMSK2,X BIT7	;Enable TOI interrupt
*
* TURN ON INTERRUPT SYSTEM
	CLI
*-----------------------------------------------------
*WAIT FOR INTERRUPTS
*-----------------------------------------------------
HERE	BRA	HERE	;DO NOTHING!

*********************************************************
** Timer Overflow INTERRUPT SERVICE ROUTINE
*********************************************************
	ORG	$B700
*-----------------------------------------------------
*INTERRUPT POLLING CHAIN
*-----------------------------------------------------
* INTERRUPT FROM TIMER OVERFLOW?
TOISR	LDX	#BASE
	BRCLR	TFLG2,X BIT7 ENDTOI	;IGNORE ILLEGAL INTERRUPT

*-----------------------------------------------------
* CONTROL PORT C OUTPUTS
*-----------------------------------------------------
* CLEAR TIMER OVERFLOW FLAG
	BCLR	TFLG2,X INV7	;Clear TOI Flag
*
* COMPLEMENT PORT C OUTPUTS
	COM	PORTC,X		;OUTPUT TO PORT C
	LDX	TCNTEXT		;Update the TCNT Extension
	INX
	STX	TCNTEXT		;Save the TCNT Extension
*
* RETURN FROM INTERRUPT
ENDTOI	RTI
