tags:

views:

233

answers:

2

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);
        }
    }       
}
+1  A: 

You typically use the sbit data type for P2_0 to define a bit within a special function register (SFR).

From C51: READING FROM AN INPUT PORT (modified)

sfr P2 = 0xA0;
sbit P2_0 = P2^0;
...
P2_0 = 1;    /* set port for input */
var = P2_0;  /* read P2_0 into var */

It is important to note that sbit variables may not be declared inside a function. They must be declared outside of the function body.


Another option may be to read all 8 pins of P2 and then mask off the unwanted bits.

char var;     /* define 8 bit variable */
P2 = 0xFF;    /* set P2 for input */
var = P2;     /* read P2 into var */
var &= 0x01;  /* mask off unwanted bits */

Rather than read P2 or the P2_0 pin into an unsigned int (16 bits), you could use a char (8 bits) or single bit to save on memory.

char var;
...
var = P2;

or

bit var;
...
var = P2_0;

Another option may be to make the char bit-addressable.

char bdata var;      /* bit-addressable char */
sbit var_0 = var^0;  /* bit 0 of var */
...
var = P2;       /* read P2 into var */
if(var_0 == 0)  /* test var_0 (bit 0 of var char) */
{
    ...
}

You can find additional useful information in the Keil Cx51 Compiler User's Guide and related links.

Note: Most of my 8051 experience is in assembly. The C examples above may not be 100% correct.

jschmier
+1  A: 

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.

Jim Tshr
thnx ill try this but ive included delay to notify led turning on or off
Aabid Ali
You might try increasing your delay times to make results more obvious. Since it seems you are using the UART also, you might monitor the data transmitted.
jschmier
as a matter of fact i was monitoring it using terminal on com port 1 kept reading 9 ,bit addressable sounds good thnx for tht could u elaborate on the char part tho
Aabid Ali
Remember that the 8051 is an 8-bit microcontroller. An 8051 typically has four I/O ports of 8 bits and a `char` is stored in a single byte (8 bits) of memory. Reading P2 into an `int` will waste 1 byte as an `int` is stored in 2 bytes of memory. Reading a single pin of P2 into an `int` will waste 15 bits.
jschmier
Jim Tshr has a good point. You might try replacing the circuit connected to P2_0 with a simple switch that you can manually control to verify the functionality of your code.
jschmier
char worked beautifully thank u jschmier
Aabid Ali