	org $2000

	jmp main

	include hc11.inc       ;NEEDED TO DEFINE REGISTERS W/O MANUALLY DEFINING REG.

out1bsp equ $ffbe	       ;SET MEM LOCATION HEX FFBE TO OUT1BSP 
outcrlf equ $ffc4	       ;SET MEM LOCATION HEX FFC4 TO OUTCRLF

Light 	equ $E0

;ambient light $BF - $CE
;Full brightness $F6
;Flashlight at a distance is $E5

;ADR3 Left sensor
;ADR2 Right Sensor 
;from back of sensor (turn towards sensor)


;Timer Overflow Counter
TOFIV		EQU $00D0
Sec_Temp	rmb 1		;counter 32.77mSec [0-15]
Sec_count	rmb 1		;counter 0.5Sec [0-255]

outa	equ $ffb8

main:   jsr tof_init

	ldaa #40
	jsr pause

	ldx #OPTION	       ;POINT X TO OPTION REGISTER	

	BSET 0,X #%10000000    ;SET OPTION REG TO HEX 80, HIGH BIT WILL BE SET AT %1	

	BCLR 0,X #%01000000    ;EVERYWHERE THERE IS BINARY 1, BINARY 0 REPLACES

	LDAA #%00110000	       ;LOAD PIN DEPENDING ON CONFIGURATION OF HARDWARE
	STAA ADCTL             ;STORE VALUE IN REGISTER ADCTL 
	
	
L1:	;ldx #ADR2
	;jsr out1bsp
	;jsr out1bsp
	;jsr outcrlf
 
	ldaa ADR2
	cmpa #Light
	ble L2

	jsr Right 
	JMP L1

L2:	ldaa ADR3
	cmpa #light
	BLE L3

	JSR LEFT
	JMP L1	

L3:	jsr Forward
	jmp L1

	
	
;*************

Forward:
	pshx
	psha

	ldx #porta

	BSET 0,x #%00110000  ; go Forward

;	ldaa #10
;	jsr pause	

	pula
	pulx
	rts

	
RIGHT:
	pshx
	psha
	
	ldx #porta

	BSET 0,x #%00100000  ;go Right
	BCLR 0,x #%00010000
;	ldaa #10
;	jsr pause
	
	pula
	pulx
	rts


LEFT:
	pshx
	psha

	ldx #porta

	BSET 0,x #%00010000  ;go Left
	BCLR 0,x #%00100000
	pula
	pulx
	rts


Brake:
	pshx
	psha

	ldx #porta

	BCLR 0,x #%00110000  ;Stop
	
	pula
	pulx
	rts

	


;**************************************************************************
;Name:		Pause
;Author:	Dan Kohn
;Date:		Dec 19, 2005
;Functions:	Pause for multiple of 0.5 sec 
;		(setup for a 0.5 sec counter)
;
;On call:	Reg A Contains the number of 0.5 sec to pause
;
;On Return;	Sec_Count and Sec_Temp were changed (cleared at start of 
;               pause.
;**************************************************************************

Pause:		psha
		pshb
		ldab #$00
		stab SEC_TEMP
		stab SEC_COUNT
		
PauseL1:	ldab sec_count
		cba
		bne PauseL1
		
		pulb
		pula
		rts
		


;**************************************************************************
;Name:		TOF_init
;Author:	Dan Kohn
;Date:		Dec 14, 2005
;Functions:	Initializes the TOF Function 
;		(setup for a 0.5 sec counter)
;**************************************************************************
TOF_init	psha
		pshx

		
		SEI

		LDAA #$7E		;setting the interrupt vector table
		STAA TOFIV

		LDD #TOF_CNT
		STD TOFIV+1

		ldaa #$80		;clear TOF flag
		staa TFLG2
	
		LDX #TMSK2		;turn on TOF interrupt
		bset 0,x #$80

		cli 

		pulx
		pula

		rts

;**************************************************************************
;Name:		TOF_CNT
;Author:	Dan Kohn
;Date:		Dec 14, 2005
;Functions:	count every 0.5 sec.  
;
;On Return:	Count of 0.5 sec intervals is in SEC_count
;               Count of 32.77mSec intervals is in Sec_Temp
;**************************************************************************

TOF_CNT		ldaa #$80
		staa TFLG2	
		
		ldaa Sec_temp		;count # of times overflow occures (32.77ms)
		inca 
		staa Sec_temp

		cmpa #15		;if ~0.5sec has NOT gone by -> exit
		blo TOF_exit		;      [15*32.77mSec = 0.49155 Sec]


		ldaa #$00		;if ~0.5sec has gone by -> clear Sec_temp
		staa Sec_temp		;and add one to the Sec_count

		ldaa Sec_count
		inca 
		staa Sec_count
		
TOF_exit	RTI