Help in using LDR and ADC in 8051

Thread Solved

Join Date: Nov 2009
Posts: 4
Reputation: jazz_vill is an unknown quantity at this point 
Solved Threads: 0
jazz_vill jazz_vill is offline Offline
Newbie Poster

Help in using LDR and ADC in 8051

 
0
  #1
20 Days Ago
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

  1. DB0 EQU P1.0
  2.  
  3. DB1 EQU P1.1
  4.  
  5. DB2 EQU P1.2
  6.  
  7. DB3 EQU P1.3
  8.  
  9. DB4 EQU P1.4
  10.  
  11. DB5 EQU P1.5
  12.  
  13. DB6 EQU P1.6
  14.  
  15. DB7 EQU P1.7
  16.  
  17.  
  18.  
  19. EN EQU P3.7
  20.  
  21. RS EQU P3.6
  22.  
  23. RW EQU P3.5
  24.  
  25. DATA EQU P1
  26.  
  27.  
  28.  
  29. ;7-Segment Display
  30.  
  31. enableDIGIT3 EQU P3.2
  32.  
  33. enableDIGIT4 EQU P3.3
  34.  
  35.  
  36.  
  37. ZERO EQU 10001000b
  38.  
  39. ONE EQU 10111110b
  40.  
  41. TWO EQU 11000100b
  42.  
  43. THREE EQU 10010100b
  44.  
  45. FOUR EQU 10110010b
  46.  
  47. FIVE EQU 10010001b
  48.  
  49. SIX EQU 10000001b
  50.  
  51. SEVEN EQU 10111100b
  52.  
  53. EIGHT EQU 10000000b
  54.  
  55. NINE EQU 10010000b
  56.  
  57. DOT EQU 01111111b
  58.  
  59.  
  60.  
  61. MOV 20h,#ZERO
  62.  
  63. MOV 21h,#ONE
  64.  
  65. MOV 22h,#TWO
  66.  
  67. MOV 23h,#THREE
  68.  
  69. MOV 24h,#FOUR
  70.  
  71. MOV 25h,#FIVE
  72.  
  73. MOV 26h,#SIX
  74.  
  75. MOV 27h,#SEVEN
  76.  
  77. MOV 28h,#EIGHT
  78.  
  79. MOV 29h,#NINE
  80.  
  81.  
  82.  
  83. MOV R0, #20h
  84.  
  85. MOV R1, #22h
  86.  
  87.  
  88.  
  89. PDATA EQU P2 ; Connect 8 pins here
  90.  
  91.  
  92.  
  93. CLR 51h
  94.  
  95. LCALL LCD_WELCOME
  96.  
  97.  
  98.  
  99. Start:
  100.  
  101. JB P3.1, check1
  102.  
  103. SETB 50h
  104.  
  105. check1:
  106.  
  107. JB P3.0, check2
  108.  
  109. SETB 52h
  110.  
  111. check2:
  112.  
  113. JNB P3.1, cont1
  114.  
  115. JNB 50h, cont1
  116.  
  117. CLR 50h
  118.  
  119. LCALL INC_7SEGMENT
  120.  
  121. cont1:
  122.  
  123. JNB P3.0, cont2
  124.  
  125. JNB 52h, cont2
  126.  
  127. CLR 52h
  128.  
  129. LCALL DEC_7SEGMENT
  130.  
  131. cont2:
  132.  
  133. LCALL SHOW_7SEGMENT
  134.  
  135. LCALL SHOW_LCD
  136.  
  137. SJMP Start
  138.  
  139.  
  140.  
  141. SHOW_LCD:
  142.  
  143. CJNE @R0, #ZERO, welcome
  144.  
  145. CJNE @R1, #ZERO, welcome
  146.  
  147. JB 51h, done_showlcd
  148.  
  149. LCALL LCD_FULL
  150.  
  151. sjmp done_showlcd
  152.  
  153. welcome:
  154.  
  155. JNB 51h, done_showlcd
  156.  
  157. LCALL LCD_WELCOME
  158.  
  159. done_showlcd:
  160.  
  161. RET
  162.  
  163. LCD_FULL:
  164.  
  165. LCALL CLEAR_LCD
  166.  
  167. LCALL INIT_LCD
  168.  
  169. CLR RS
  170.  
  171. MOV DATA,#086H
  172.  
  173. SETB EN
  174.  
  175. CLR EN
  176.  
  177. LCALL WAIT_LCD
  178.  
  179. MOV A,#'F'
  180.  
  181. LCALL WRITE_TEXT
  182.  
  183. MOV A,#'U'
  184.  
  185. LCALL WRITE_TEXT
  186.  
  187. MOV A,#'L'
  188.  
  189. LCALL WRITE_TEXT
  190.  
  191. MOV A,#'L'
  192.  
  193. LCALL WRITE_TEXT
  194.  
  195. MOV A,#'!'
  196.  
  197. LCALL WRITE_TEXT
  198.  
  199. MOV A,#'!'
  200.  
  201. LCALL WRITE_TEXT
  202.  
  203.  
  204.  
  205. SETB 51h
  206.  
  207. RET
  208.  
  209.  
  210.  
  211. LCD_WELCOME:
  212.  
  213. LCALL CLEAR_LCD
  214.  
  215. LCALL INIT_LCD
  216.  
  217. CLR RS
  218.  
  219. MOV DATA,#081h
  220.  
  221. SETB EN
  222.  
  223. CLR EN
  224.  
  225. LCALL WAIT_LCD
  226.  
  227.  
  228.  
  229. MOV A,#'P'
  230.  
  231. LCALL WRITE_TEXT
  232.  
  233. MOV A,#'A'
  234.  
  235. LCALL WRITE_TEXT
  236.  
  237. MOV A,#'R'
  238.  
  239. LCALL WRITE_TEXT
  240.  
  241. MOV A,#'K'
  242.  
  243. LCALL WRITE_TEXT
  244.  
  245. MOV A,#'I'
  246.  
  247. LCALL WRITE_TEXT
  248.  
  249. MOV A,#'N'
  250.  
  251. LCALL WRITE_TEXT
  252.  
  253. MOV A,#'G'
  254.  
  255. LCALL WRITE_TEXT
  256.  
  257. MOV A,#' '
  258.  
  259. LCALL WRITE_TEXT
  260.  
  261. MOV A,#'S'
  262.  
  263. LCALL WRITE_TEXT
  264.  
  265. MOV A,#'P'
  266.  
  267. LCALL WRITE_TEXT
  268.  
  269. MOV A,#'A'
  270.  
  271. LCALL WRITE_TEXT
  272.  
  273. MOV A,#'C'
  274.  
  275. LCALL WRITE_TEXT
  276.  
  277. MOV A,#'E'
  278.  
  279. LCALL WRITE_TEXT
  280.  
  281.  
  282.  
  283. CLR RS
  284.  
  285. MOV DATA,#0C3h
  286.  
  287. SETB EN
  288.  
  289. CLR EN
  290.  
  291. LCALL WAIT_LCD
  292.  
  293.  
  294.  
  295. MOV A,#'A'
  296.  
  297. LCALL WRITE_TEXT
  298.  
  299. MOV A,#'V'
  300.  
  301. LCALL WRITE_TEXT
  302.  
  303. MOV A,#'A'
  304.  
  305. LCALL WRITE_TEXT
  306.  
  307. MOV A,#'I'
  308.  
  309. LCALL WRITE_TEXT
  310.  
  311. MOV A,#'L'
  312.  
  313. LCALL WRITE_TEXT
  314.  
  315. MOV A,#'A'
  316.  
  317. LCALL WRITE_TEXT
  318.  
  319. MOV A,#'B'
  320.  
  321. LCALL WRITE_TEXT
  322.  
  323. MOV A,#'L'
  324.  
  325. LCALL WRITE_TEXT
  326.  
  327. MOV A,#'E'
  328.  
  329. LCALL WRITE_TEXT
  330.  
  331. CLR 51h
  332.  
  333. RET
  334.  
  335.  
  336.  
  337. INC_7SEGMENT:
  338.  
  339. CJNE @R1, #TWO, increment
  340.  
  341. sjmp doneinc
  342.  
  343. increment:
  344.  
  345. CJNE @R0, #NINE, incr0
  346.  
  347. MOV R0, #20h
  348.  
  349. INC R1
  350.  
  351. SJMP doneinc
  352.  
  353. incr0:
  354.  
  355. INC R0
  356.  
  357. doneinc:
  358.  
  359. RET
  360.  
  361.  
  362.  
  363. DEC_7SEGMENT:
  364.  
  365. CJNE @R0, #ZERO, dec0
  366.  
  367. CJNE @R1, #ZERO, dec1
  368.  
  369. sjmp donedec
  370.  
  371. dec1:
  372.  
  373. MOV R0, #29h
  374.  
  375. DEC R1
  376.  
  377. SJMP donedec
  378.  
  379. dec0:
  380.  
  381. DEC R0
  382.  
  383. donedec:
  384.  
  385. RET
  386.  
  387.  
  388.  
  389. SHOW_7SEGMENT:
  390.  
  391. MOV PDATA,#00h
  392.  
  393.  
  394.  
  395. setb enableDIGIT3
  396.  
  397. MOV A,@R1
  398.  
  399. MOV PDATA,A
  400.  
  401. LCALL smallDelay
  402.  
  403. clr enableDIGIT3
  404.  
  405.  
  406.  
  407. setb enableDIGIT4
  408.  
  409. MOV A,@R0
  410.  
  411. MOV PDATA,A
  412.  
  413. LCALL smallDelay
  414.  
  415. clr enableDIGIT4
  416.  
  417. RET
  418.  
  419.  
  420.  
  421. INIT_LCD:
  422.  
  423. CLR RS
  424.  
  425. MOV DATA,#38h
  426.  
  427. SETB EN
  428.  
  429. CLR EN
  430.  
  431. LCALL WAIT_LCD
  432.  
  433.  
  434.  
  435. CLR RS
  436.  
  437. MOV DATA,#0Ch
  438.  
  439. SETB EN
  440.  
  441. CLR EN
  442.  
  443. LCALL WAIT_LCD
  444.  
  445.  
  446.  
  447. RET
  448.  
  449.  
  450.  
  451. CLEAR_LCD:
  452.  
  453. CLR RS
  454.  
  455. MOV DATA,#01h
  456.  
  457. SETB EN
  458.  
  459. CLR EN
  460.  
  461. LCALL WAIT_LCD
  462.  
  463. RET
  464.  
  465.  
  466.  
  467. WRITE_TEXT:
  468.  
  469. SETB RS
  470.  
  471. MOV DATA,A
  472.  
  473. SETB EN
  474.  
  475. CLR EN
  476.  
  477. LCALL WAIT_LCD
  478.  
  479. RET
  480.  
  481.  
  482.  
  483. WAIT_LCD:
  484.  
  485. CLR EN ;Start LCD command
  486.  
  487. CLR RS ;It's a command
  488.  
  489. SETB RW ;It's a read command
  490.  
  491. MOV DATA,#0FFh ;Set all pins to FF initially
  492.  
  493. SETB EN ;Clock out command to LCD
  494.  
  495. LCALL smallDelay
  496.  
  497. MOV A,DATA ;Read the return value
  498.  
  499. JB ACC.7,WAIT_LCD ;If bit 7 high, LCD still busy
  500.  
  501. CLR EN ;Finish the command
  502.  
  503. CLR RW ;Turn off RW for future commands
  504.  
  505. RET
  506.  
  507.  
  508.  
  509. smallDelay:
  510.  
  511. CLR TF1
  512.  
  513. CLR TR1
  514.  
  515. MOV TH1,#76
  516.  
  517. MOV TL1,#01
  518.  
  519. MOV TMOD,#01
  520.  
  521. SETB TR1
  522.  
  523. JNB TF1,$
  524.  
  525. RET
Last edited by jazz_vill; 20 Days Ago at 11:54 am.
Attached Thumbnails
design.jpeg  
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz
 
0
  #2
20 Days Ago
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.
Last edited by Colin Mac; 20 Days Ago at 7:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: jazz_vill is an unknown quantity at this point 
Solved Threads: 0
jazz_vill jazz_vill is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz
 
0
  #4
19 Days Ago
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
Last edited by Colin Mac; 19 Days Ago at 3:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: jazz_vill is an unknown quantity at this point 
Solved Threads: 0
jazz_vill jazz_vill is offline Offline
Newbie Poster
 
0
  #5
19 Days Ago
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).
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz
 
0
  #6
19 Days Ago
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.
Last edited by Colin Mac; 19 Days Ago at 5:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: jazz_vill is an unknown quantity at this point 
Solved Threads: 0
jazz_vill jazz_vill is offline Offline
Newbie Poster
 
0
  #7
19 Days Ago
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC