PIC Projects

Handling two IR sensors

Working with two sensors is similar to working with just one. One should alternate the code for the first and the second sensor. It is important to make sure that only one LED emits in a time. This is necessary to avoid the IR receiver modules to catch signals from a wrong LED. In this experiments we practically double the schematic for the previous experiment and the corresponding code: use 2 IR receiver moduls, 2 IR emitting LEDs, and 2 (visible) red light LEDs for indication (one of them is not shown on the schematic and must be attached via a 470 Ohm current-limiting resistor to the hanging pin RC3 of PIC).

Schematic

The code is as follows:

 TITLE  "ir2.asm"       	    	; working with two IR sensors  
 List P=PIC16F684, R=DEC
 INCLUDE "p16f684.inc"

; data segment
 CBLOCK 0x20                   
 del  					; variable used for delay
 pulse, pulses2Send, pulsesReceived	
 ENDC

; code segment
 PAGE
 __CONFIG _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _CPD_OFF & 
  _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

  org 0                		      	; start program at the beginning of mem
	bcf	STATUS, RP0		; activate BANK 0
	clrf	PORTC			; initialize PORT C
	movlw	0x07
	movwf	CMCON0			; comparators OFF
	bsf    	STATUS, RP0      	; change to BANK 1

	clrf	ANSEL ^ 0x80		; make all inputs digital
	movlw	b'101101'
	movwf	TRISA ^ 0x80		; enable RA1 and RA4 for output
	bcf	TRISC ^ 0x80, 2		; enable RC2 for output
	bcf	TRISC ^ 0x80, 3		; enable RC3 for output
	bcf    	STATUS, RP0        	; back to BANK 0

	clrf	pulse			; start with IR pulse value 0
loop
	movlw	10
	movwf	del
	call	delay			; 10 msec delay

	; process pulses from the right sensor
	clrf	pulsesReceived	
	movlw	20
	movwf	pulses2Send

send_pulses1
	goto	$+1
	goto 	$+1
	btfss	PORTA, 0		; is there IR reflected waveform?
	incf	pulsesReceived, f

	movlw	b'10'
	xorwf	pulse, w		; compose a new pulse value
	movwf	pulse
	movwf	PORTA			; output the new pulse value
	decfsz	pulses2Send, f
	goto	send_pulses1		; end of a 26 cycle (38KHz=26.3mksec)

	movlw	4
	subwf	pulsesReceived, w	; C=1 if more than 2 pulses received
	movf	PORTC, w
	andlw	b'11111011'	
	btfsc	STATUS, C
	iorlw	b'100'			; turn on LED
	movwf	PORTC

	; process pulses from the left sensor
	clrf	pulsesReceived	
	movlw	20
	movwf	pulses2Send

send_pulses2
	goto	$+1
	goto 	$+1
	btfss	PORTA, 5		; is there IR reflected waveform?
	incf	pulsesReceived, f

	movlw	b'10000'
	xorwf	pulse, w		; compose a new pulse value
	movwf	pulse
	movwf	PORTA			; output the new pulse value
	decfsz	pulses2Send, f
	goto	send_pulses2		; end of a 26 cycle (38KHz=26.3mksec)

	movlw	4
	subwf	pulsesReceived, w	; C=1 if more than 2 pulses received
	movf	PORTC, w
	andlw	b'11110111'	
	btfsc	STATUS, C
	iorlw	b'1000'			; turn on LED
	movwf	PORTC

  	goto 	loop			; endless loop


; procedures
delay					; a delay for del milliseconds
	movlw 	200
	
	sublw	1			; this loop takes 5us*200 = 1ms
	sublw	0			; for PIC16F684 @ 4 Mhz
	btfss	STATUS, Z
	goto 	$-3

	decfsz	del, f
	goto 	delay
	return

 end

Download ir2.asm


Last modified:Mon, Jan 23, 2023.

03955