;
; 5s_delay.asm  5 second delay timer for the MiniDRAGON (c)2002, EVBplus.com
;		Written by Wayne Chu
;
;     Function:	5 second delay routine using output comparator 6
;		The 7-segment LED will be turned on immediately after running
;		the program. It will be turned off after 5 second delay. 
;		Change the DELAY_TIME to 60000 will delay 1 minute

 
DB6:	equ	$40
;
TB1MS:		equ	24000	; 1ms time base of 24,000 instruction cycles
;				; 24,000 x 1/24MHz = 1ms at 24 MHz bus speed

;DELAY_TIME:	equ	60000	; 60000 X 1 ms= 60 sec = 1 min
DELAY_TIME:	equ	5000	; 5000 X 1 ms= 5 sec

REGBLK:		equ	$0
#include        reg9s12.h	; include register equates

	org	$1000
;
flag_5s:	rmb	1
cnt_5s:		rmb	2

STACK:	equ	$2000

	org	$2000
start:
	lds	#STACK
	ldx	#timer6
	stx	$3E62		; initialize the int vetctor

	ldx	#REGBLK
	clr	pth,x
	ldaa	#$ff
	staa	ddrh,x		; make port H an output port
	
	ldaa	#$80
	staa	tscr,x		; enable timer
	ldaa	#DB6
	staa	tios,x		; select t6 as an output compare	 
	staa	tmsk1,x

	cli

	ldaa	#$FF
	staa	pth,x		; turn on 7-seg LED
	jsr	delay_5s
	clr	pth,x		; turn off 7-seg LED

self:	jmp	self


delay_5s:
	clr	cnt_5s
	clr	cnt_5s+1
	clr	flag_5s
delay:	ldaa	flag_5s
	beq	delay
	rts

timer6:
	ldx	cnt_5s
	inx
	stx	cnt_5s
	cpx	#DELAY_TIME
	bne	tmr6
	clr	cnt_5s
	clr	cnt_5s+1
	ldaa	#1
	staa	flag_5s
	rti

tmr6:	ldx	#REGBLK		
	ldd	#TB1MS		; reload the count for 1 ms time base
	addd	tc6,x	
	std	tc6,x
	ldaa	#DB6
	staa	tflg1,x		; clear flag
        rti

	org	$3E62
	fdb	timer6
	end

