*
* Ex5.asm ----  Example program 5 for FOX11 board
*               with Wytec Phantom Monitor, (c)2003, EVBplus.com
*		Written By Wayne Chu
*
*
*     Function:	Adjust the trimmer pot VR1 to vary the voltage on the PE7
*		of the ADC and to change brightness of the PB0-PB7 LEDs
*
MULTI_MODE	equ	$10
SINGLE_MODE	equ	0
SCAN_MODE	equ	$20
NO_SCAN_MODE	equ	0
CHANNEL_NUM:	equ	7	; reading input from PE7
*	
DB6		equ	$40
NOTDB6		equ	$BF
portb:		equ	$1404


* 68HC11 control register address is the offset address from the
* register base address $1000, so index addressing mode can also be used.
* 

portd:		equ	8
ddrd:		equ	9
toc2:		equ	$18	
tctl1:		equ	$20
tmsk1:		equ	$22
tflg1:		equ	$23	
tmsk2:		equ	$24	
adctl:		equ	$30	
adr1:		equ	$31	
adr2:		equ	$32	
adr3:		equ	$33	
adr4:		equ	$34	
option:		equ	$39	

REGBLK:		equ	$1000

TB5MS:		equ	10000	; time base of 10,000 instruction cycles
*				; = 10,000 x 0.5us = 5ms at 8MHz

		org	$40
*
select:		rmb	1
d5ms_flag:	rmb	1
disp_data:	rmb	4
disptn:		rmb	4
adctl_image:	rmb	1
brtness:	rmb	1
turn_led_on:	rmb	1

STACK:	equ	$FF
*
              	org     $0800	; EVBplus board I/O routines
rs485_recv:     rmb     3	; enables rs485 recv mode
rs485_xmit:     rmb     3	; enables rs485 xmit mode
get_date:       rmb     3	; gets date info. from host PC
get_time:       rmb     3	; gets time info. from host PC
outstrg00:	rmb	3	; print a string ended by 0
lcd_ini:	rmb	3	; initializes the 16x2 LCD module
lcd_line1:	rmb	3	; displays 16 char on the first line
lcd_line2:	rmb	3	; displays 16 char on the second line
sel_inst:	rmb	3	; selects instruction before writing LCD module 
sel_data:	rmb	3	; selects data before writing the LCD module
wrt_pulse:	rmb	3	; generates a write pulse to the LCD module
seven_segment:  rmb	3	; convert Accu A to segment pattern, bit 7= DP
*
*
                org     $0FA0	; Buffalo I/O routines are duplicated here

upcase:         rmb     3
wchek:          rmb     3
dchek:          rmb     3
init_sci:       rmb     3
input:          rmb     3
output:         rmb     3
outlhlf:        rmb     3
outrhlf:        rmb     3
outa:           rmb     3
out1byt:        rmb     3
out1bsp:        rmb     3
out2bsp:        rmb     3
outcrlf:        rmb     3
outstrg:        rmb     3
outstrg0:	rmb	3
inchar:         rmb     3
*
	org	$F000
	jmp	start

*
* this routine will read adc input on pin pe7 and store 4 consecutive
* readings at adr1-adr4. (single channel)
*
*
adc_conv:
	adda	#SINGLE_MODE+NO_SCAN_MODE
*
* if you want to read multi-channel input, change above statement to
*	adda	#MULTI_MODE+NO_SCAN_MODE
*
	staa	adctl_image
	ldx	#REGBLK
	jsr	conv
	rts
*	
conv:	ldaa	adctl_image	; clear ccf flag before testing it
	staa	adctl,x
not_ready:
	brclr	adctl,x $80 not_ready
	rts
start:
	lds	#STACK
	ldx	#REGBLK
*	ldaa	#11111111b
	ldaa	#$ff
	staa	portd,x
*	ldaa 	#11111100b	; 0 =input, 1 = output
	ldaa 	#$fc
	staa 	ddrd,x		
*
	ldx	#REGBLK
	ldaa	#DB6	 
	staa	tmsk1,x
	bset 	option,x $80	; enable adc operation
	cli
*
begin:	ldaa	#CHANNEL_NUM	; set channel number before calling 
	jsr	adc_conv	
	ldaa	adr1+REGBLK
	staa	brtness
*	
	ldab	#$FF
	stab	portb

 	ldaa	brtness		; was read from adc
	beq	turn_off	; if =0, turn off display 
*				; otherwise keep on until a=0
back:	iny			; make each loop=31 cycles = 15.5us
	dey			; 15.5 us x256 = 4ms
	iny			; leaves 1 ms for other task
	dey
	iny
	dey
	nop
	deca
	bne	back		
turn_off:
	clr	portb		; turn off 8 LEDs
*
wait:	tst	d5ms_flag
	beq	wait
	clr	d5ms_flag
	jmp	begin
*
* in an interrupt servicing routine the x register will be saved automatically
* the rti instruction will pop the x register off stack.
*
tmr6oc2:
	ldx	#REGBLK	
	inc	d5ms_flag
	ldd	#TB5MS		; reload the count for 5 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

