Light Detection using LDR with 16F877

Status
Not open for further replies.

antoni1

New Member
Hi i wrote the foloowing program for 16F877 to eneable my robot to search for light

read_LDR()
{
char LDR;
LDR=0;

STATUS=0x3f;
TRISA=0b001111; //SET RA4 output//
STATUS=0x1f;
nop();
nop();
nop();
STATUS=0x3f;
TRISA=0b011111; //SET RA4 input//
STATUS=0x1f;
RA4=1;
while(RA4!=1)
{
LDR++;
}
}
Im trying to set RA4 to +5V output and wait a short time for the capacitor to discharge (both plates at +5V). Then, change the pin to act as an input. As soon as this is done the capacitor will be free to charge up again through the resistor (Ie it brings the capacitor plate back down to 0 Volts). As soon as the pin is changed to an input, it suppose that i have to wait for a certain amount of time and if it hasn't charged by then assume that the resistance is high and therefore the light levels are low.
(I use 10K parallel with capacitor of 100nF)
The problem is that i can't get 5V out from RA4 of 16F877!!!.. so i can't charge and discharge the capacitor
Any ideas how to fix the problem??
thanks
 

RA4 is an open-collector output, so it can only sink current, not source it!.

Either use a different I/O pin or invert the action, so the pin can work pulled low, instead of pulled high.

Or you could add a pullup resistor, but it won't charge/discharge as fast as an I/O pin.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…