;
; 500Hz.asm --- Square wave generator for the MiniDRAGON (c)2002, EVBplus.com
;		Written by Wayne Chu
;
;     Function:	500 Hz square wave routine using output comparator 6
;		The PH0 LED (segment a) is repeatedly turned on for 1 ms
;               and then turned off for 1 ms.
;		User can use a scope to observe the 500Hz square wave
;		on the PH0 (pin 52 of the MCU).
;		The PH3 LED (segment d) is repeatedly turned on for 250 ms
;               and then turned off for 250 ms
;		User can use a scope to observe the 2Hz square wave
;		on the PH3 (pin 49 of the MCU).
;

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

REGBLK:		equ	$0
#include        reg9s12.h	; include register equates


STACK:		equ	$2000

		org	$1000
count250:	rmb	1


	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

back:	ldaa	count250
	cmpa	#250
	bne	back
	clr	count250
	ldaa	pth,x
	eora	#8		; toggle the PH3 (segment d) every 250ms
	staa	pth,x
	jmp	back
	
timer6:
	ldx	#REGBLK
	inc	count250
	ldd	#TB1MS		; reload the count for 1 ms time base
	addd	tc6,x		
	std	tc6,x

	ldaa	#DB6
	staa	tflg1,x		; clear flag
	ldaa	pth,x
	eora	#1		; toggle the PH0 (segment a) every 1ms
	staa	pth,x
	rti

	org	$3E62
	fdb	timer6
	end


