;**************************************************************************
;Name:		Robot Library
;Version:	0.1b
;Author:	Dan Kohn
;Date of Mod):	Dec 8, 2005
;
;IO Table:
;
;PortA		PA7	Output / Sonar Init
;		PA6	OC2 / RIGHT Motor
;		PA5	OC3 / LEFT Motor (PWM for servo)
;		PA4
;		PA3	OC5 / USED FOR TRACE - BUFFALO
;		PA2
;		PA1	IC2 / Sonar Echo 
;		PA0	IC3/Clock for barcode
;
;PortE		PE7	Data for barcode
;		PE6
;		PE5
;		PE4
;		PE3	Line Track 3
;		PE2	Line Track 2
;		PE1	Line Track 1
;		PE0
;
;PortD		PD5	
;		PD4	
;		PD3	
;		PD2	
;**************************************************************************

		ORG $2000
		
		include Hc11.inc	;definds all HC11 Registers 

	JMP MAIN

;Barcode Equates
IC3IV	 EQU	$00E2
Barcode  RMB	5
BC_count RMB	1
Boxcode  RMB    1
out1bsp  equ $ffbe
Outcrlf  equ $ffc4
outa	 equ $ffb8
;multiplexer values
On_cnts_A	RMB 2
Off_cnts_A	RMB 2
;Gipper Motor Variables and Equates			;mike code
TOC4IV		EQU $00D6
On_cnts_G	RMB 2
Off_cnts_G	RMB 2

Main:	jsr IC3_init
	

	;jsr go_Up
	;jsr go_Astop
	;jsr go_down	
loop:   


	jsr scanner
	ldx #Boxcode  

        JSR Out1bsP
        JSR Outcrlf
	
	jmp loop

	



;**************************************************************************************************
;
;Name:		TOC3_init
;Author:	Michael McNeill, Jr.
;Date:		??
;Functions:	Initalize the scanner
;
;On Call:	Sc_1 reading #$26 to #$03
;		Sc_2 reading #$1e to #$02
;		Sc_3 reading #$16 to #$01
;
;
;
;
;*******************************************************************************************
	

Scanner:
	psha
	jsr BC_clear


sc_0:	lDAA BC_count
	cmpa #33
	BLO sc_0

	Ldaa Barcode+3		;Barcode to read #$26 to plane 3
	cmpa #$26
	Bne  sc_1
	ldaa #$03
	staa Boxcode

	jmp sc_exit

Sc_1	
	Ldaa Barcode+3		;Barcode to read #$1e to plane 2
	cmpa #$1e
	Bne  sc_2
	ldaa #$02
	staa Boxcode

	jmp sc_exit

Sc_2	
	Ldaa Barcode+3		;Barcode to read #$16 to plane 1
	cmpa #$16
	Bne  sc_3
	ldaa #$01
	staa Boxcode

	jmp sc_exit
	
Sc_3	
	

	ldaa #$ff		;flag for bad scan
	staa Boxcode

sc_exit

	pula
	rts



;*******************************************************************************
;Name:		TOC3_init
;Author:	Michael McNeill, Jr.
;Date:		??
;Functions:	Initalize the Multiplexed Servo (OC4) 
;
;On Call:	go_Gstop MUST be called before initalizing 
;When BC_cout= 33
;scancode is barcode +3
;
;
;
;********************************************************************************
IC3_init:	psha
		pshx
					
		sei

		ldaa #$7E
		staa IC3IV

		LDD #IC3RT
		STD IC3IV+1

		LDX #TCTL2

		Bset 0,X #$02	;set for falling edge
		Bclr 0,X #$01
       
		LDAA #$01
		STAA TFLG1	;clr flag

		LDX #TMSK1	;turn on INT
		Bset 0,X #$01

		cli

		pulx
		pula
		rts
;********************************************************************************
IC3RT:		ldaa #$01		;resets the IC3 bit
		staa TFLG1

		Ldaa portE		;check IC3 pin for on or off state
		anda #$80
		BEQ  BC_High
		sec
		BRA BC_skip

BC_High		clc

BC_Skip		ROR Barcode
		ROR Barcode+1
		ROR Barcode+2
		ROR Barcode+3
		ROR Barcode+4
		INC BC_count
		RTI			;return from the interrupt
;********************************************************************************
BC_clear	psha
		LDAA #$FF
		STAA Barcode
		STAA Barcode+1
		STAA Barcode+2
		STAA Barcode+3
		STAA Barcode+4
		LDAA #$0
		STAA BC_count

		Pula
		RTS
;*****************************************************
; gripper stuff
;*****************************************************
go_Gstop	pshx
		ldx #Gstop
		bra robot_go

go_Open		pshx
		ldx #Open
		bra robot_go

go_Close	pshx
		ldx #Close
		bra robot_go

robot_go	ldd 0,x
		std On_cnts_G
		ldd 2,x
		std Off_cnts_G
		
		pulx
		rts
;******************************************
; code for movement of the robot arm
;*************************************
go_Astop	pshx
		ldx #Astop
		bra robot_arm

go_Up		pshx
		ldx #UP
		bra robot_arm

go_Down		pshx
		ldx #DOWN
		bra robot_arm




robot_arm	ldd 0,x
		std On_cnts_A
		ldd 2,x
		std Off_cnts_A
		
		pulx
		rts

;Table for arm motor Functions
;			On_cnts_A	Off_cnts_A
UP		fdb	2000,		18000		
DOWN		fdb	4500,		14800
Astop		fdb	3000,		17000
;			On_cnts_G	Off_cnts_G
CLOSE		fdb	4000,		36000				
Open		fdb	2000,		38000
Gstop		fdb	3000,		37000
	