*
* 5s_delay.asm  5 second delay timer for FOX11 board
*               with Wytec Phantom Monitor (c)2003, EVBplus.com
*               Written By Wayne Chu
*
*     Function:	5 second delay routine using output comparator OC2
*		The PB5 LED will be turned on immediately after running
*		the program. It will be turned off after 5 second delay. 
*		Change the DELAY_TIME to 18000 will delay 3 minutes
*
DB6:		equ	$40
PB5:		equ	$20
NOTDB6		equ	$BF
portb:  	equ     $1404 		; 74HC273 latch
*
* 68HC11 control register address is the offset address from the
* register base address $1000, so index addressing mode can also be used.
*
toc2:   	equ     $0018
tmsk1:		equ	$0022
tflg1:		equ	$0023	
REGBLK:		equ	$1000
WYTEC_MON:	equ	$0880		; WYTEC monitor re-entry point	
*
*DELAY_TIME:	equ	18000		; 18000 X 10 ms= 180 sec = 3 min
DELAY_TIME:	equ	500		; 500 X 10 ms= 5 sec
TB10MS:		equ	20000		; time base of 20,000 instruction cycles
*					; = 20,000 x 0.5us = 10ms at 8MHz

		org	$40
*
flag_5s:	rmb	1
cnt_5s:		rmb	2
d10ms_flag:	rmb	1
*
STACK:		equ	$FF
*
		org	$F000
start:
		lds	#STACK
		ldx	#REGBLK
		ldaa	#DB6	
		staa	tmsk1,x
		cli
		ldaa	#PB5
		staa	portb		; turn on LED PB5
		jsr	delay_5s
		ldaa	#1
		staa	portb		; turn off LED PB5, but keep LED PB0 on
*
		jmp	WYTEC_MON	; re-enter Wytec monitor
*
delay_5s: 	clr	cnt_5s
		clr	cnt_5s+1
		clr	flag_5s
delay:		ldaa	flag_5s
		beq	delay
		rts
*
tmr6oc2: 	ldx	cnt_5s
		inx
		stx	cnt_5s
		cpx	#DELAY_TIME
		bne	tmr6
		clr	cnt_5s
		clr	cnt_5s+1
		ldaa	#1
		staa	flag_5s

* in an interrupt servicing routine the x register will be saved automatically
* the rti instruction will pop the x register off stack.
*
tmr6:	        ldx	#REGBLK	
		ldd	#TB10MS			; reload the count for 10 ms time base
		addd	toc2,x
		std	toc2,x
		bclr	tflg1,x NOTDB6		; clear flag
		rti
*
		org	$FFE6
		fdb	tmr6oc2
		org	$FFFE
		fdb	start
		end



