; Copyright(c) 1999 Noriaki Mitsunaga
; Sample for front sensor

MR0	equ	0	; PORTA bit 0: right 0
MR1	equ	1	; PORTA bit 1: right 1
ML1	equ	2	; PORTA bit 2: left 1
ML0	equ	3	; PORTA bit 3: left 0
SPDL	equ	0	; PORTB bit 0: SPD Front left
SPDR	equ	1	; PORTB bit 1: SPD Front right
LEDL	equ	2	; PORTB bit 2: IR LED left
LEDR	equ	3	; PORTB bit 3: IR LED right
FL	equ	4	; PORTB bit 4: front left(0:white, 1:black)
RL	equ	5	; PORTB bit 5: rear left
RR	equ	6	; PORTB bit 6: rear right
FR	equ	7	; PORTB bit 7: front right

	list	p=16f84
	#include p16f84.inc

TMP	equ	0x4e
DELAYTMP equ	0x4f

TIMER	equ	1	; 256 - 10MHz/4/256/255

	org	0
	goto	START

	org 4
interrupt
	movlw	TIMER
	movwf	TMR0

	movlw	0x0c		; LEDR+LEDL on
	movwf	PORTB
	call	delay10us
	call	delay10us
	call	delay10us
	call	delay10us
	call	delay10us
	call	delay10us
	call	delay10us
	call	delay10us
	movfw	PORTB
	movwf	TMP
	bsf	PORTA,ML0
	btfss	TMP,SPDR
	bcf	PORTA,ML0
	bsf	PORTA,MR0
	btfss	TMP,SPDL
	bcf	PORTA,MR0
	clrf	PORTB		; LED off

	bcf	INTCON,T0IF	; clear intterrupt
	retfie			; return from intterrupt

START
	clrf	STATUS
 	BSF	STATUS,RP0	; register bank 1
	MOVLW	0xe0		;11100000B
	MOVWF	TRISA
	MOVLW	0xf3		;11110011B
	MOVWF	TRISB
	movlw	0x7		; PORTB pull up
				; PSA = TMR0
				; PS=1/256
	movwf	OPTION_REG
	BCF	STATUS, RP0	; register bank 0
	
	MOVLW	0x1f	;00011111B	Motor off
	MOVWF	PORTA
	MOVLW	0xf3	;11110011B	LED off
	MOVWF	PORTB

	movlw	TIMER
	movwf	TMR0
	bsf	INTCON,T0IE	; timer 0 intterupt enable
	bsf	INTCON,GIE	; global intterupt enable

	goto	$

delay4cy			; 10MHz/16
	return

delay2us			; 10MHz/20 = 0.5MHz
	nop			; call(2)+nop(1)+ret(2)
	return

delay10us			; 10MHz/25*4
				; 2(call)+2(ret)+2(movlw,movwf)+4*(3+1)+2(goto)+1(nop)
	movlw	.253		; 1
	movwf	DELAYTMP	; 1
loop10	incf	DELAYTMP,F	; 1
	btfss	STATUS,Z	; 1
	goto	loop10		; 2
	nop			; 1	compensate for goto
	goto	$+1		; 2
	nop			; 1
	return

	end
