; mini_v02.asm
; June 06, 2006, PTB. (06/06/06 eh?)
; Update: 06/15/2006 PTB
;
; PIC16F84A-20 with a 16.0MHz crystal.
;	Actually an 16F84A/04 part runs with a 16.0MHz xtal, weird...
;
; For: Mini-Sumo 'Bot with really-really wide tires. (hp printer rollers)
;   Now with   48:1 gear-ratio - Motor = 15_teeth,
;   instead of 80:1 gear-ratio - Motor = 20_teeth.
;   May have to shorten "turn _counts" values.
;
; Yes, I know "right" is spelled "rite".
;
	LIST    p=16F84A
	include "p16f84ab.inc"
	include "ints_f84.inc"
	include "pins_f84.inc"
	__config _cp_off & _wdt_off & _pwrte_on & _hs_osc
	__idlocs 0x016B
	errorlevel -302, -206
;
; RAM RAM RAM RAM RAM RAM RAM RAM RAM
;
	CBLOCK	0x0C	; to 0x4F inclusive
	 _status, _wtemp
	 _sec5, _loops, _counts, _dir, _h2h
	 _eeadr, _eedata
	ENDC
;
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;     @@@@
;    @   .@
;    @  . @
;    @ .  @
;    @.   @
;     @@@@
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;
	org	0

	b	Cold
	b	Cold
	b	Cold
	b	Cold
;
;44444444444444444444444444444444
;         4
;    4    4
;    4    4
;    444444
;         4
;         4
;44444444444444444444444444444444
;
	org	4

	btfsc	_rbif		; did an edge occur?
	b	doedges
	btfsc	_intf		; push_me = sumo?
;	b	H2Head
	b	dosumo
	retfie			; false hit, ignore it
;
doedges	bcf	_rbif		; clear reason
	btfss	rt_edge		; looking for any edge active =0
	b	dorite
	btfss	lt_edge		; 0= edge
	b	doleft
	btfss	r_front		; 0= sumo
	b	riteturn
	btfss	l_front		; 0= sumo
	b	leftturn
	retfie
;
leftturn
	movlw	rotccw		; turn left, top view
	movwf	motors
	movlw	.20		; change the length of run-time
	movwf	_counts		; ../
	retfie
;
riteturn
	movlw	rotcw		; turn right, top view
	movwf	motors
	movlw	.20		; change the length of run-time
	movwf	_counts		; ../
	retfie
;
doleft	movlw	m1ccw		; rotate cw w/ rite motor
	movwf	motors
	movlw	.140		; change the length of run-time
	movwf	_counts		; ../
	retfie
;
dorite	movlw	m2ccw		; rotate ccw w/ left motor
	movwf	motors
	movlw	.140		; change the length of run-time
	movwf	_counts		; ../
	retfie
;
dosumo	bcf	_intf		; clear reason
	movlw	allcw		; go forward
	movwf	motors
	movlw	.100		; change the length of run-time
	movwf	_counts		; ../
	retfie
;
; ###############################
;
; Head 2 Head silliness:
;
; Change the head-to-head motion
;  if stuck pushing with nothing else going on...
;
H2Head	incfsz	_h2h,1		; update head2head monotony
	b	h3head		; not zero yet
	movlw	.25
	movwf	_counts		; ../
	movlw	m2ccw		; turn a bit...
	movwf	motors
	b	RT3		; 'til done...

h3head	movlw	allcw		; else - go forward
	movwf	motors
	movlw	.50		; change the length of run-time
	movwf	_counts		; ../
	b	RT3		; 'til done...
;
;--------------------------------
;
Cold	clrf	intcon		; no interrupts used at all
	clrf	tmr0		; start timer after 2 cycles
	clrf	_h2h		; can't be - unless really fast!
	movlw	a_pins		; motors off
	movwf	porta
	movlw	b_pins
	movwf	portb
	bsf	bank1		; yup, continue
	movlw	_opt64		; setup prescaler amount
	movwf	option_reg
	movlw	a_tris		; setup external pin directions
	movwf	trisa
	movlw	b_tris
	movwf	trisb
	bcf	bank0
	movfw	portb		; read/flush portb
	nop
	nop
;
; ###############################
;
; Hold push_me in while powering up,
; Then release to start 5 seconds timer.
; Else it skips over this stuff...
;   This works at 16.0MHz
;
wait5secs
	btfsc	push_me		; is wait-5-seconds enabled?
	b	human		; no, skip for now
	call	pauze		; yes, debounce timer
;
; skip over this if push_me not activated...
;
hu1	btfss	push_me		; hang until released...
	b	hu1		; 
;
; part of the 5 seconds timer, DON'T TOUCH!
;
	movlw	.40		; outer loop, was .70 for 14.3mhz
	movwf	_sec5		; 06/08/2006 PTB, close...

hu2	call	pauze		; this long
	decf	_sec5,1
	btfss	z_flag		; quit when zero=1
	b	hu2
	b	human		; done with setup
;-------
pauz4	movlw	.100		; better in backing up with slow motors...
	b	pauz2
;
; part of the 5 seconds timer, DON'T TOUCH!
;
pauze	movlw	morelps
pauz2	movwf	_loops
	bcf	_t0if

pz3	movlw	.0
	movwf	tmr0

pz2	btfss	_t0if
	b	pz2
	bcf	_t0if

	decf	_loops,1
	btfss	z_flag
	b	pz3
	return
;
; ###############################
;
; These two routines
;   can be eliminated later...
;
human	movlw	m1cw		; rite motor moves forward
	movwf	motors
	call	pauz4

	movlw	m2cw		; left motor moves forward
	movwf	motors
	call	pauz4
;
; ###############################
;
; Now enable interrupts
; For _rbif & _intf - only...
;
IntsOn
	movlw	_inton		; gie, inte, rbie, no t0ie!
	movwf	intcon		; go!
;
; ###############################
;
; Run Time:
;
; How this works...
;	Run forward for .100 times.
;	Expect interrupts to change direction.
;	Run for that time then change back to going forward.
;		when _counts runs out
;		then direction changes from interrupt time.
;	So, if rite-edge goes active,
;	_rbif goes active and interrupt
;	changes the motor directions and _counts amount.
;	This moves the sumo in a different direction
;	Until another interrupt or _counts runs out.
;	_counts and motor direction gets reset.
;
GoNow	movlw	.100		; good guess...
	movwf	_counts		; update direction counter

	movlw	allcw		; now go forward
	movwf	motors		; turn motors on

LoopM	bcf	_t0if
	movlw	.0		; use current motor settings
	movwf	tmr0
;
; DO NOT POLL PIN CHANGE(B)
; IF USING PIN CHANGE(B) INTERRUPTS!
;
RT2
	btfss	bk_edge		; if (portA.4) rear sensor is calling
	b	GoFwd		; then go here

	btfss	_t0if		; if TMR0 not ready
	b	RT2		; then continue
;
; ###############################
;
; Now check for going forward again...
;
RT3	decf	_counts,1
	btfsc	z_flag
	b	GoNow		; yes reset direction
	b	LoopM		; no change in direction
;
; ###############################
;
; Found one edge active
;  but not INTERRUPTED
;  so still at/over border...
;
GoFwd	movlw	.100		; change the length of run-time
	movwf	_counts		; ../
	movlw	allcw		; go forward
	movwf	motors
	b	RT3		; 'til done...

;RSpin	movlw	.190		; change the length of run-time
;	movwf	_counts		; ../
;	movlw	m2ccw		; turn a bit...
;	movwf	motors
;	b	RT3		; 'til done...
;
;LSpin	movlw	.190		; change the length of run-time
;	movwf	_counts		; ../
;	movlw	m1ccw		; turn a bit...
;	movwf	motors
;	b	RT3		; 'til done...
;
;SpinR	movlw	.20		; change the length of run-time
;	movwf	_counts		; ../
;	movlw	rotcw		; rotate a bit...
;	movwf	motors
;	b	RT3		; 'til done...
;
;SpinL	movlw	.20		; change the length of run-time
;	movwf	_counts		; ../
;	movlw	rotccw		; rotate a bit...
;	movwf	motors
;	b	RT3		; 'til done...
;
; ###############################
;
;EErbyte
;	movfw	_eeadr	; get preset address
;	movwf	eeadr	; write to eeprom address register
;	bsf	bank1	; bank 1
;	bsf	_rd	; enable read byte
;	bcf	bank0	; bank 0
;	movfw	eedata	; read data from eedata register
;	return
;
;--------------------------------
;
;EEwbyte
;	movfw	_eeadr	; get pre-set address
;	movwf	eeadr	; write to eeprom address register
;	movfw	_eedata	; get pre-set data byte
;	movwf	eedata	; write data to ee data register
;-------
;EEwrite
;	bsf	bank1	; bank 1
;	bsf	_wren	; enable write byte
;	movlw	0x55	; do this sequence for every byte
;	movwf	eecon2	;____/
;	movlw	0xaa	;___/
;	movwf	eecon2	;__/
;	bsf	_wr	;_/
;	return
;
;--------------------------------
;
; CODEPAGE   NAME=eedata   START=0x2100   END=0x213F
; 64 bytes of onboard EEPROM
;
;	org	0x2100
;
;	de	.25
;
;--------------------------------
;
	END
