Need help configuring port to input to 8051
The connection looks like this. An infrared sensor circuit that gives 0 or 5 volts depending on open or closed circuit output to pin 2_0 of the philips 8051 microcontroller. Problem: When I do this, the schema value is overridden by the current value on port 2_0 is always on. Here is my code (in keil c) I think I have not configured P 2_0 as correct
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);
}
}
}
a source to share
Typically you use the sbit datatype for P2_0 to define a bit in a special function register (SFR).
From C51: READING FROM INPUT PORT (changed)
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 cannot be declared inside a function. They must be declared outside the function body.
Another option could be reading all 8 P2 pins and then masking 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 */
Instead of reading P2 or P2_0 pin into unsigned int (16 bits), you can use a char (8 bits) or single bit to store in memory.
char var;
...
var = P2;
or
bit var;
...
var = P2_0;
Another option might be to make 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 more useful information in the Keil Cx51 Compiler User Guide and related links.
Note. Most of my 8051 experience is in assembly. The C examples above may not be 100% correct.
a source to share
Thank you so much ... my coding works
And I will find out how to determine the input port and read the data
#include<reg51.h>
#define opp P1
#define ipp P0
sbit op =P1^0;
sbit ip =P0^0;
main()
{
unsigned int value;
P0=0xFF;
value=P0;
value &=0x01;
if(value==0)
{
P1=0x01;
}
else
{
P1=0x00;
}
}
a source to share