Search code examples
c8051keil

need help configuring port to input in 8051


The connection is as follows An infrared sensor circuit which yields 0 or 5v depending on closed or open circuit output line to port 2_0 pin of microcontroller 8051 philips.Problem is when i do this the circuit value are overridden by the current value on port 2_0 led always goes on.Here is my code(in keil c) i guess i have not configured P 2_0 as input properly

void MSDelay(unsigned int);

sbit led=P1^0;

void main()
{
    unsigned int var;
    P2=0xFF;
    TMOD=0x20;
    TH1=0xFD;
    SCON =0x50;
    TR1=1;

    while(1)
    {
        var=P2^0;
        if(var==0)
        {  
             led=1;
             SBUF='0';
             while(TI==0);
             TI=0;
             MSDelay(250);
        } 
        else
        {   
            led=0;
            SBUF='9';
            while(TI==0);
            TI=0;
            MSDelay(100);
        }
    }       
}

EDIT : I was facing a problem since the 8086 processor i was using had a fault in it. Would recommend anyone trying this to get a few spares when programming.


Solution

  • jschmier has a good point. Also the port may not be configured correctly, or is there something in the circuit that is causing the led to toggle off and on very quickly so it looks like it is on all the time.