;***************************************************************
;Dbt_sw2led
;Moves state of front microswitches to diagnostic leds
;If both switches on then buzzer goes on.
;TJW 24.3.05					Tested & working 20.8.05
;***************************************************************
;Clock is 4MHz
;
;Complete Port Bit Allocation
;    Port A         Port B      	Port C
;    ------         ------          ------
;0  LDR Front R		Ext Int			Rt Opto input (Timer 1)
;1  LDR Front L  	Piezo sounder	L motor PWM
;2  Rt Motor Enable	Opto Enable     R motor PWM
;3  LDR Rear		Servo Drive     SCL
;4  Lt Opto ip 		R uswitch   	SDA
;5  Lt Motor Enable	L uswitch       Diag led/US Pulse
;6  -				ICD2 PGC 		Diag led/US Echo
;7  -				ICD2 PGD		Mode switch

	list 	p=16F873a
	include p16f873a.inc

;Set Configuration Word: crystal oscillator HS, WDT off, 
;     power-up timer on, code protect off, LV Program off.
	__CONFIG _HS_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF

;Specify RAM labels
delcntr1 equ	20	;used in delay SR
delcntr2 equ	21
;
	org 00
;Initialise
start bsf	status,5 	;select memory bank 1
	movlw	B'00000011'	;all port A bits op
	movwf	trisa		
	movlw	B'11111000'
	movwf	trisb		;port B bits
	movlw	B'10000000'
	movwf	trisc		;port C bits
	movlw	B'00000110'
	movwf	adcon1		;set port A for digital function
	bcf		status,5 	;select bank 0
;
;The “main” program starts here
;Switch all outputs off
	clrf 	porta
	clrf 	portb
	clrf 	portc
;diagnostic, switch leds on for half a second
	bsf 	portc,6
	bsf 	portc,5
	call 	delay500
	bcf 	portc,6
	bcf 	portc,5
	call 	delay500
;move microswitch states to diag leds
loop bcf 	portc,6		;preclear port C, bit 6 (led off)
	btfsc 	portb,4		;jump if right switch pressed
	bsf		portc,6		;led on if switch clear
;
	bcf		portc,5		;preclear port C, bit 5 (led off)
	btfsc 	portb,5		;jump if left switch pressed
	bsf		portc,5		;led on if switch clear
;
	btfsc	portb,4
	goto	loop1
	btfsc	portb,5
	goto	loop1
	bsf		portb,1		;switch on sounder if both pressed
	goto	loop
loop1 bcf	portb,1		;switch off sounder
	goto 	loop
;**********************************************
;SUBROUTINES
;**********************************************
;introduces delay of 1ms approx
delay1  movlw 	D'250'	;250 cycles called, 
						   ;each taking 4us
        movwf  	delcntr1
del1    nop		;4 inst cycles in this loop, ie 4us
        decfsz 	delcntr1,1
        goto 	del1
        return
;
;500ms delay (approx)	;500 calls to delay1
delay500  movlw D'250'
        movwf  	delcntr2
del5    call 	delay1
		call 	delay1
        decfsz  delcntr2,1
        goto 	del5
        return  
	end
