Hi I need help on how to use LDR and ADC0804 in 8051 microcontroller.
So far I have this in my project but I want to replace the switch with LDR (dark activated).

What happens here (attached file)is that I use the switch to decrement/increment the number of cars and when it reaches to 0(no available space) the LCD will prompt that it is full.

What I want is using a sensor(ldr) and a laser light so that when a car passes (the light will be broken) the ldr will send signal and decrement the number of spaces and vice versa( i'll be using two ldr one for exit the other for entrance).

attached is my design. Im using Isis Proteus 7.4 and the code below

DB0 EQU P1.0

DB1 EQU P1.1

DB2 EQU P1.2

DB3 EQU P1.3

DB4 EQU P1.4

DB5 EQU P1.5

DB6 EQU P1.6

DB7 EQU P1.7



EN EQU P3.7

RS EQU P3.6

RW EQU P3.5

DATA EQU P1



;7-Segment Display

enableDIGIT3 EQU P3.2

enableDIGIT4 EQU P3.3



ZERO    EQU   10001000b

ONE     EQU   10111110b

TWO     EQU   11000100b

THREE   EQU   10010100b

FOUR    EQU   10110010b

FIVE    EQU   10010001b

SIX     EQU   10000001b

SEVEN   EQU   10111100b

EIGHT   EQU   10000000b

NINE    EQU   10010000b

DOT     EQU   01111111b



MOV 20h,#ZERO

MOV 21h,#ONE

MOV 22h,#TWO

MOV 23h,#THREE

MOV 24h,#FOUR

MOV 25h,#FIVE

MOV 26h,#SIX

MOV 27h,#SEVEN

MOV 28h,#EIGHT

MOV 29h,#NINE



MOV R0, #20h

MOV R1, #22h



PDATA EQU P2 ; Connect 8 pins here



CLR 51h	

LCALL LCD_WELCOME

	

Start:

	JB P3.1, check1

		SETB 50h

	check1:

	JB P3.0, check2

		SETB 52h

	check2:

		JNB P3.1, cont1

			JNB 50h, cont1

				CLR 50h

				LCALL INC_7SEGMENT	

	cont1:

		JNB P3.0, cont2

			JNB 52h, cont2

				CLR 52h

				LCALL DEC_7SEGMENT

	cont2:

		LCALL SHOW_7SEGMENT

		LCALL SHOW_LCD

SJMP Start  



SHOW_LCD:

	CJNE @R0, #ZERO, welcome

	CJNE @R1, #ZERO, welcome

		JB 51h, done_showlcd

		LCALL LCD_FULL

	sjmp done_showlcd

	welcome: 

		JNB 51h, done_showlcd

		LCALL LCD_WELCOME

	done_showlcd:

RET

LCD_FULL:	

	LCALL CLEAR_LCD	

	LCALL INIT_LCD

	CLR RS

	MOV DATA,#086H

	SETB EN

	CLR EN

	LCALL WAIT_LCD

	MOV A,#'F'

	LCALL WRITE_TEXT

	MOV A,#'U'

	LCALL WRITE_TEXT

	MOV A,#'L'

	LCALL WRITE_TEXT

	MOV A,#'L'

	LCALL WRITE_TEXT

	MOV A,#'!'

	LCALL WRITE_TEXT

	MOV A,#'!'

	LCALL WRITE_TEXT



	SETB 51h

RET



LCD_WELCOME:

	LCALL CLEAR_LCD	

	LCALL INIT_LCD

	CLR RS

	MOV DATA,#081h

	SETB EN

	CLR EN

	LCALL WAIT_LCD

	

	MOV A,#'P'

	LCALL WRITE_TEXT

	MOV A,#'A'

	LCALL WRITE_TEXT

	MOV A,#'R'

	LCALL WRITE_TEXT

	MOV A,#'K'

	LCALL WRITE_TEXT

	MOV A,#'I'

	LCALL WRITE_TEXT

	MOV A,#'N'

	LCALL WRITE_TEXT

	MOV A,#'G'

	LCALL WRITE_TEXT

	MOV A,#' '

	LCALL WRITE_TEXT

	MOV A,#'S'

	LCALL WRITE_TEXT

	MOV A,#'P'

	LCALL WRITE_TEXT

	MOV A,#'A'

	LCALL WRITE_TEXT

	MOV A,#'C'

	LCALL WRITE_TEXT

	MOV A,#'E'

	LCALL WRITE_TEXT

	

	CLR RS

	MOV DATA,#0C3h

	SETB EN

	CLR EN

	LCALL WAIT_LCD

	

	MOV A,#'A'

	LCALL WRITE_TEXT

	MOV A,#'V'

	LCALL WRITE_TEXT

	MOV A,#'A'

	LCALL WRITE_TEXT

	MOV A,#'I'

	LCALL WRITE_TEXT

	MOV A,#'L'

	LCALL WRITE_TEXT

	MOV A,#'A'

	LCALL WRITE_TEXT

	MOV A,#'B'

	LCALL WRITE_TEXT

	MOV A,#'L'

	LCALL WRITE_TEXT

	MOV A,#'E'

	LCALL WRITE_TEXT

	CLR 51h

RET



INC_7SEGMENT:

	CJNE @R1, #TWO, increment

	sjmp doneinc

	increment:

		CJNE @R0, #NINE, incr0

		MOV R0, #20h

		INC R1

		SJMP doneinc

		incr0:

			INC R0

	doneinc:

RET



DEC_7SEGMENT:

	CJNE @R0, #ZERO, dec0

	CJNE @R1, #ZERO, dec1

	sjmp donedec

	dec1:

		MOV R0, #29h

		DEC R1

		SJMP donedec

	dec0:

		DEC R0

	donedec:

RET



SHOW_7SEGMENT:

	MOV PDATA,#00h



   	setb enableDIGIT3

   	MOV     A,@R1

   	MOV PDATA,A

   	LCALL smallDelay

	clr enableDIGIT3

	

   	setb enableDIGIT4

   	MOV     A,@R0

   	MOV PDATA,A

   	LCALL smallDelay   	

	clr enableDIGIT4

RET	



INIT_LCD:

	CLR RS

	MOV DATA,#38h

	SETB EN

	CLR EN

	LCALL WAIT_LCD

	

	CLR RS

	MOV DATA,#0Ch

	SETB EN

	CLR EN

	LCALL WAIT_LCD

	

RET



CLEAR_LCD:

	CLR RS

	MOV DATA,#01h

	SETB EN

	CLR EN

	LCALL WAIT_LCD

RET

	

WRITE_TEXT:

	SETB RS

	MOV DATA,A

	SETB EN

	CLR EN

	LCALL WAIT_LCD

RET

	

WAIT_LCD:

	CLR EN ;Start LCD command

	CLR RS ;It's a command

	SETB RW ;It's a read command

	MOV DATA,#0FFh ;Set all pins to FF initially

	SETB EN ;Clock out command to LCD

	LCALL smallDelay

	MOV A,DATA ;Read the return value

	JB ACC.7,WAIT_LCD ;If bit 7 high, LCD still busy

	CLR EN ;Finish the command

	CLR RW ;Turn off RW for future commands

RET  



smallDelay:

		CLR TF1

		CLR TR1

		MOV TH1,#76

		MOV TL1,#01

		MOV TMOD,#01

		SETB TR1

		JNB TF1,$

RET

Recommended Answers

All 6 Replies

You've explained what you need to do, but you haven't asked a question.
An LDR in a voltage divider would replace the switch, if your code works for a switch, it will work with a LDR. But it's not going to be very practical, having to line the laser with the LDR.
There's no ADC shown the diagram, and you haven't explained why you need one.
There's no current limiting resistors on the 7-segment display.
It wouldn't work even if there were, the 8051 only sources about 2mA of current from the port pin.

All I want is to make an LDR to function as a switch a dark activated LDR.

The output of the LDR is in analog so you cant directly connect the output to the microcontroller since it only accept digital inputs that is why it is suggested to use an ADC or a comparator(i've read it on other forums)

The thing is I dont really know how to use an ADC (0804 in particular coz it is the onboard ADC of the EEDT I've been using).Or is there any other solutions?

You don't need a ADC. Put the LDR in series with a resistor of about 10K and connect the output of the LDR to a port pin. When the Laser shines on the LDR, its resistance will be much lower than 10K making the input LOW. When the Laser is not shining on it, its resistance will be much higher than 10K making the input HIGH.
http://www.doctronics.co.uk/voltage.htm

I tried to simulate it in ISIS and the microcontroller cannot recieve the signal when we turn on or off the light.

But when I dont connect the Voltage divider(VD) to the microcontroller and put a logic probe in the output of the VD it works just fine I can get a logic 0 when its in the light and 1 when its in the shade (dark activated LDR).

But does your simulation work when you apply a logic high or low to the port pin, by means of, say a switch?

When you connected a voltmeter to the output of the LDR, what readings did you get when you switched the light on and off?
Use 5V to power the voltage divider.

Remember Isis is just a simulator. If everything does work with a switch, then work away as if it were an LDR in a voltage divider.
Get the LDR circuit working in real life so that you're happy it does work.

yes when i use a switch it works...

when i insert a voltmeter to the VD
10k resistor
5V

when no light Vout =4.95V
when theres a light Vout = 0.24V

I'll try to load it later in the EEDT

I think I figure out my mistake. I connected the output of VD in one of Port3 pins but when I use P0 i can get signal from the VD.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.