; TITLE 'Infra Red Proximity Detector - uses Sharp GP1U581Y' ; ; Copyright (c) 1998 Jeff Koenig ; Licensed under the GNU GPL Version 2 or later ; LIST P = 12C509, F = INHX8M ; P12C509.INC Standard Header File, Version 1.02 Microchip Technology, Inc. NOLIST ;========================================================================== ; Verify Processor ;========================================================================== IFNDEF __12C509 MESSG "Processor-header file mismatch. Verify selected processor." ENDIF ;========================================================================== ; Register Definitions ;========================================================================== W EQU H'0000' F EQU H'0001' ;----- Register Files ----------------------------------------------------- INDF EQU H'0000' ; Uses FSR to address data mem. TMR0 EQU H'0001' ; 8 bit real time clock/counter PCL EQU H'0002' ; Low order 8 bits of PC STATUS EQU H'0003' ; STATUS FSR EQU H'0004' ; Indirect data memory addr pointer OSCCAL EQU H'0005' ; Calibration data for osc. GPIO EQU H'0006' ; General Purpose I/O ;----- STATUS Bits -----------Page 14-------------------------------------- GPWUF EQU H'0007' ; GPIO reset bit PA0 EQU H'0005' ; Program Page preselect NOT_TO EQU H'0004' ; Time Out bit NOT_PD EQU H'0003' ; Power Down bit ZERO EQU H'0002' ; Zero bit DC EQU H'0001' ; Digit carry/*borrow bit CARRY EQU H'0000' ; carry/*borrow bit ;----- OPTION Bits -----------Page 15-------------------------------------- NOT_GPWU EQU H'0007' ; Enable wake-up on pin change NOT_GPPU EQU H'0006' ; Enable weak pull-ups T0CS EQU H'0005' ; Timer0 clock source select T0SE EQU H'0004' ; Timer0 sources edge select PSA EQU H'0003' ; Prescalar assignment bit PS2 EQU H'0002' ;\ PS1 EQU H'0001' ; > Prescalar rate select bits PS0 EQU H'0000' ;/ ;========================================================================== ; RAM Definition ;========================================================================== __MAXRAM H'3F' ;========================================================================== ; Configuration Bits ;========================================================================== _MCLRE_ON EQU H'0FFF' _MCLRE_OFF EQU H'0FEF' _CP_ON EQU H'0FF7' _CP_OFF EQU H'0FFF' _WDT_ON EQU H'0FFF' _WDT_OFF EQU H'0FFB' _LP_OSC EQU H'0FFC' _XT_OSC EQU H'0FFD' _IntRC_OSC EQU H'0FFE' _ExtRC_OSC EQU H'0FFF' __CONFIG ( _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC ) ;========================================================================== ; Program Variables ;========================================================================== CONSTANT DELAYTIME=D'150' ; Timer value #define LEFTDETECT GPIO,5 ; Pin 2 = GP5 = Bit 5 #define RIGHTDETECT GPIO,4 ; Pin 3 = GP4 = Bit 4 #define INHIBIT GPIO,3 ; Pin 4 = GP3 = Bit 3 NOTE: INPUT ONLY! #define RIGHTLED GPIO,2 ; Pin 5 = GP2 = Bit 2 #define IRDETECT GPIO,1 ; Pin 6 = GP1 = Bit 1 #define LEFTLED GPIO,0 ; Pin 7 = GP0 = Bit 0 LIST WAIT EQU 9 ; Location for counter for time delay loop ;========================================================================== ; Code Begins ;========================================================================== start ORG H'00' ; MOVLW H'7F' ; -- USED FOR OFFSET IN ERASABLE PARTS --- MOVWF OSCCAL ; Store the factory osc. calibration value MOVLW B'00001010' ; Set pins 4 as inputs, 2,3,5,6,7 as outputs TRIS GPIO ; Configure pins as either I or O MOVLW B'00000000' ; Set OPTION bits OPTION ; Implement OPTION bits BCF LEFTLED BCF LEFTDETECT BCF RIGHTLED BCF RIGHTDETECT main noflash BTFSC INHIBIT ; Don't flash the IR Leds if the INHIBIT pin is low GOTO do_left ; BTFSC IRDETECT ; Check the IR detector GOTO NF1 GOTO NF2 NF1 BCF LEFTDETECT ; No 38 KHz IR detected BCF RIGHTDETECT ; So turn on off right and left LED NF2 BSF LEFTDETECT ; 38 KHz IR detected BSF RIGHTDETECT ; So turn on right and left LED GOTO noflash ; do_left CALL pulseleft ; Pulse the left IR LED at 38 KHz for 600 microseconds BTFSC IRDETECT ; Read the Sharp Module. Skip next inst. if detection (0=detect) GOTO no_left ; IR detector didn't sense reflection with Left LED on CALL delayloop ; Wait 600 microseconds BTFSS IRDETECT ; Read the Sharp Module. Skip next inst. if no detection (0=detect) GOTO no_left ; Now check for an obstacle on the right side CALL pulseleft ; Pulse the left IR LED at 38 KHz for 600 microseconds BTFSC IRDETECT ; Read the Sharp Module. Skip next inst. if detection (0=detect) GOTO no_left ; IR detector didn't sense reflection with Left LED on CALL delayloop ; Wait 600 microseconds BTFSS IRDETECT ; Read the Sharp Module. Skip next inst. if detection (0=detect) GOTO no_left ; Now check for an obstacle on the right side CALL pulseleft ; Pulse the left IR LED at 38 KHz for 600 microseconds BTFSC IRDETECT ; Read the Sharp Module. Skip next inst. if detection (0=detect) GOTO no_left ; IR detector didn't sense reflection with Left LED on BSF LEFTDETECT ; Turn on the left Visible LED GOTO do_right ; no_left ; No obstacle to the left, so BCF LEFTDETECT ; Turn off the left visible LED do_right CALL pulseright ; Pulse the right IR LED at 38 KHz for 600 microseconds BTFSC IRDETECT ; Read the Sharp Module. Skip next inst. if detection (0=detect) GOTO no_right ; IR detector didn't sense reflection with Right LED on CALL delayloop ; Wait 600 microseconds BTFSS IRDETECT ; Read the Sharp Module. Skip next inst. if no detection (0=detect) GOTO no_right ; Now check for an obstacle on the left side CALL pulseright ; Pulse the right IR LED at 38 KHz for 600 microseconds BTFSC IRDETECT ; Read the Sharp Module. Skip next inst. if detection (0=detect) GOTO no_right ; IR detector didn't sense reflection with Right LED on CALL delayloop ; Wait 600 microseconds BTFSS IRDETECT ; Read the Sharp Module. Skip next inst. if detection (0=detect) GOTO no_right ; Now check for an obstacle on the left side CALL pulseright ; Pulse the right IR LED at 38 KHz for 600 microseconds BTFSC IRDETECT ; Read the Sharp Module. Skip next inst. if detection (0=detect) GOTO no_right ; IR detector didn't sense reflection with Right LED on BSF RIGHTDETECT ; Turn on the left Visible LED GOTO main no_right ; No obstacle to the right, so BCF RIGHTDETECT ; Turn off the right visible LED GOTO main delayloop ;------------------------ MOVLW D'195' ; MOVWF WAIT ; This creates a 600 microsecond delay loop ; NOP ; DECFSZ WAIT,F ; goto loop ; RETLW 0 ;------------------------ pulseleft ;------------------------ MOVLW D'24' ; MOVWF WAIT ; Pulses the left IR led at 38 KHz leloop BSF LEFTLED ; for 600 microseconds NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; BCF LEFTLED ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; DECFSZ WAIT,F ; GOTO leloop ; RETLW 0 ;------------------------ pulseright ;------------------------ MOVLW D'24' ; MOVWF WAIT ; Pulses the right IR led riloop BSF RIGHTLED ; for 600 microseconds NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; BCF RIGHTLED ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; NOP ; DECFSZ WAIT,F ; GOTO riloop ; RETLW 0 ;------------------------ end