LDR TEST PROGRAM

LDR (Light Dependant Resistor) Test Program for Mainboard (PIC16F876)

The main part of the code at "Start" performs the following function:

Select channel 0 (LDRL)
Wait for channel selection (10mS)
Start AtoD conversion
Wait for conversion
Turn off Left LED
Test bit7 of AtoD conversion
if it's set do nothing (leave the LED off)
if it's clear turn on Left LED

Select channel 1 (LDRR)
Wait for channel selection (10mS)
Start AtoD conversion
Wait for conversion
Turn off Right LED
Test bit7 of AtoD conversion
if it's set do nothing (leave the LED off)
if it's clear turn on Right LED
Goto Start

The program loops forever and has the following effect:
If the light level is low it will turn on the appropriate LED.
Note if the LED's are on shine a bright light on the sensors (a torch) and the LED's will go out.

 

The Code:

	list p=16f876
#include "P16F876.inc"
;Define ports so we can use them by name
#define LDRL PORTA,0	;INPUT
#define LDRR PORTA,1	;INPUT
#define	AntL PORTA,2	;OUTPUT
#define AntR PORTA,3	;OUTPUT
	cblock	20h
	VLDelay, LDelay, MDelay, SDelay		;variables for delay routines
	endc
	org 0
	goto init	;jump over interrupt vectors
	
	org 10
init
	bsf	STATUS, RP0
	bcf	STATUS, RP1	;select bank1
	movlw	b'11110011'
	movwf	TRISA
	movlw	b'00000000'	;set porta i's as Analogue
	movwf	ADCON1
	bcf	STATUS, RP0 	;reselect bank0
	bsf	ADCON0, ADON	;turn on ADC	
Start
	bcf	ADCON0, CHS0	;set to read RA0 LDRL
	movlw	.1		;1*10mS=10mS 
	call	LongDelay	;wait for 10 mSeconds
	bsf	ADCON0, GO	;start conversion
WaitADC
	btfsc	ADCON0, NOT_DONE	;test if conversion done yet
	goto	WaitADC			;if no wait until done	
	bcf	AntL			;turn off left LED
	btfss	ADRESH, 7		;test if bit7 set
	bsf	AntL			;if not set turn on left LED
	bsf	ADCON0, CHS0		;set to read RA1 LDRR
	movlw	.1			;1*10mS=10mS 
	call	LongDelay		;wait for 10 mSeconds
	bsf	ADCON0, GO		;start conversion
WaitADC1
	btfsc	ADCON0, NOT_DONE	;test if conversion done yet
	goto	WaitADC1		;if no wait until done	
	bcf	AntR			;turn off right LED
	btfss	ADRESH, 7		;test if bit7 set
	bsf	AntR			;if not set turn on right LED
	goto	Start			;loop forever
	 

;***** Delay Routines from 12 uS to 255 Sec
;***** Very Long Delay 1 Sec to 255 Sec, value in w on entry, 1 tick = 1 Sec
VLongDelay	movwf	VLDelay
waitvldelay	movlw	.100
	call	LongDelay
	decfsz	VLDelay,F
	goto	waitvldelay
	return
;***** Long Delay 10 mS to 2.55 Sec, value in w on entry, 1 tick = 10 mS
LongDelay	movwf	LDelay
waitldelay	movlw	.10
	call	MedDelay
	decfsz	LDelay,F
	goto	waitldelay
	return
;***** Medium Delay 1 mS to 255 mSec, value in w on entry, 1 tick = 1 mS
MedDelay	movwf	MDelay
waitmdelay	movlw	.100
	call	ShortDelay
	decfsz	MDelay,F
	goto	waitmdelay
	return
;***** Short Delay 10 uS to 2.55 mSec, value in w on entry, 1 tick = 10 uS
ShortDelay
	movwf	SDelay
WaitSDF	
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	decfsz	SDelay,F
	goto	WaitSDF
	Return
;**************************
;***    Config word     ***
;**************************
	__CONFIG h'3FFF' & _LVP_OFF & _BODEN_OFF & _XT_OSC & _PWRTE_ON & _WDT_OFF 
	end