Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8051 Input

Status
Not open for further replies.
Hi, im using atmel 89C52 and Keil C , and i want to check an input from a sensor , i've been told that i should write 1 to the pin where im gonna read from , i wrote that but it doesnt work , i wanna check for a high from the sensor and check a low from the sensor too .
pseudo code
void main ()
{
P1_3=1;
while (1)
{
if (P1_3==1) // if we got 1 from the sensor
{
// Do Something
}
if (P1_3==0) // if we got 0 from the sensor
{

// Do Something
}
}
}

that expression P1_3==1 is itself true , cuz it's been intialiazed as 1 before ... dont know what to do

Thanks
 
The ports on an 8051 class machine are called quasi-bi-directional. In the data sheet you will see a schematic of PORT 1. You will notice that ports do not work like RAM. The data that you wrote goes to a flip-flop. The output of the flip-flop controls a pair of FETs. The lower FET, when on, will sink about 3.2 ma. The upper one wil source 60 uA when it is on. It is a VERY weak pullup. Just about any output you connect to a pin with a one in the data latch will be able to pull it low.

Now look at the port schematic again starting at the pin you should see a little buffer symbol and be able to find a READ PORT signal. Any time you read the port you read the pin.

The only dangerous situation with this setup is to write a zero to the data latch and have it connected to an output that can source a bunch of current, like a 74ACT244

As to why your program does not work as you expect you need to confirm that the pin has a valid logic level on it. These values can be found in the datasheet. They are called Vih, for "Voltage, Input High", and Vil, for "Voltage, Input Low". The voltage should never spend any appreciable time betwee the maximum Vil and the minimum Vih.
 
thanks for ur help

i wrote that today and i think it works on the simulator ...

if ( Pin_3 == 0 )
{
// do nothing

if ( Pin_3==1)
{

// Blink an LED
}
}
what's ur opinion about that ?
 
its really worked .... without removing }
but just in the simulator ....

so should i check for 0 and then check for 1 if i wanted to check for a high input to the pin ?
 
ahmedragia21 said:
thanks for ur help

i wrote that today and i think it works on the simulator ...

if ( Pin_3 == 0 )
{
// do nothing

if ( Pin_3==1)
{

// Blink an LED
}
}
what's ur opinion about that ?

There's nothing wrong with "}" in his machine. He just didn't copy the entire code to this forum. Instead I doubt the Pin_3 expression is something.

ahmedragia21, how exactly did your 89C52 work? Did it appear that P1.3 was always logical "1" or always "0"? what did you connect to the pin? I never had any problem like this with my 89x5x. Check your hardware, the pin might have been clamped by something. Disconnect P1.3 from all its external components, use a switch between it and GND as its input. Simply modify your code in this way:

Code:
void main () 
{ 
  P1_3=1; 
  while (1) 
  { 
    if (P1_3==1) // if we got 1 from the sensor 
    { 
       // Do Something
       //P1_7=1 or any other pin =1
    } 
    if (P1_3==0) // if we got 0 from the sensor 
    { 
       // Do Something
       //Same pin as above =0
    } 
  } 
}

Use a multimeter to measure the level on the output pin, or connect it to an LED in serial with a 470ohm resistor. If the 89C52 works with such simple circuit, then check your sensor.
 
ahmedragia21 said:
its really worked .... without removing }
but just in the simulator ....

so should i check for 0 and then check for 1 if i wanted to check for a high input to the pin ?
as i understood from ur post u need to check 1s and 0s in the input port
but this code
Code:
if ( Pin_3 == 0 )
{
// do nothing

    if ( Pin_3==1)
      {
        // Blink an LED
       }
}
checks if the pin_3 is zero then immediatly checks the pin for 1 :roll: , that's not u want right?
i think u want to check the 0->1 transition , for that use another variable.
pseudocode
Code:
bool var;
//init var as zero 
if pin_3 == 0 
  var = 1 
if pin_3==1 and var ==1 
 //blink led
 var = 0;
is this u want?
 
still i've problems in that code :(

well to clear it , i wish to monitor if there's a high ( 5v) on that pin ...

but the problem is this pin is initialy been set to high = 1 to allow to for input
so any if ( expression == true) , will be true whatever happend on the pin .


thanks :-
 
ahmedragia21 said:
still i've problems in that code :(

well to clear it , i wish to monitor if there's a high ( 5v) on that pin ...

but the problem is this pin is initialy been set to high = 1 to allow to for input
so any if ( expression == true) , will be true whatever happend on the pin .


thanks :-

The pin is set to high but, it is very weakly set to high. Try connecting a 1K resistor from the pin to GND and it will read low even though you set it high. Disconnecting the resistor will return it to reading high. If whatever you have connected to the pin is outputting a high then it will read high. If it outputs a low then it will read low.

Mike.
 
yea, the problem is in the Keil C simulator :
when i write
if ( pin == 1)
{
P2=0x00;
}

it outputs P2=000000 .. whatever i put 1 on it ...
i want to count if 5 ones has been input on that pin and then Zeros P2 ..
 
ahmedragia21 said:
but the problem is this pin is initialy been set to high = 1 to allow to for input
so any if ( expression == true) , will be true whatever happend on the pin .
I think you don't understand "You have to leave a "1" on a port before using it as an input."
Leaving a "1" on a port doesn't mean that you write directly to the port pin, you write the "1" in a latch somewhere in the ports hardware, but not on the physical port pin. The read instruction reads the port pin not the latch you leave a "1" in.
First try to understand the hardware of each port (3 different types on a 8051 from Intell) then start writing code :)
 
Hardware

Another question: What is the sensor's output like, TTL, open collector, ... ...? Do you have the correct interface between your sensor and your Atmel?
First try your code with a pull-up resistor and a switch to ground. When you get that running, connect the sensor and go on...
 
Hi, thanks for ur reply :)
the problem is i can work with checking if the pin has been grounded .. but couldnt check if there's a 5v on it for a number of times or not :S .

e.g
if ( pin_3==0)
{
// blink led
}
the above code is worked but when checking if a 1 is on the pin , it doesnt work :S .
 
Hi, thanks for ur reply :)
the problem is i can work with checking if the pin has been grounded .. but couldnt check if there's a 5v on it for a number of times or not :S .

e.g
if ( pin_3==0)
{
// blink led
}
the above code is worked but when checking if a 1 is on the pin , it doesnt work :S .
and the interface from the sensor to the micro is good , it ouputs 1,0 , i wanna check if a 1 from sensor been on the pin .
 
Do you mean that with the code
Code:
if (pin_3==0)
{
// blink led 
}
your led is blinking, and with
Code:
if (pin_3==1)
{
// blink led 
}
your led is always off or on?

What happens with
Code:
if (pin_3==0)
{
// blink led 
}
else
{
// blink another led 
}
 
Last edited:
Hardware ok? Are u sure

ahmedragia21 said:
and the interface from the sensor to the micro is good , it ouputs 1,0 , i wanna check if a 1 from sensor been on the pin .
How do you test your software?
How do you "make" a "1" on pin P1.3? With a "1" on pin P1.3 did you measure the voltage on that pin?
Can you describe your hardware a little bit more or even better post a layout of your hardware?
 
hi, thanks alot for ur reply
the hardware is ok and it works when checking if a zero is on the pin .

init code
pin_3=1;

main loop

if ( pin_3==0)
{
// move forward
}
the above code is worked
but when i write
if ( pin_3==1)
{
// the motor moves without putting a signal on it ...
}

when checking the voltage on the pin, i saw that there's a 5v on without any input on it .
 
Lost

Now you lost me...

What is it that you are trying to do?
Motor forward when pin_3=0 and motor reversre when pin_3=1?
Please post a drawing of your hardware. Even if it's a sketch it will help much more than 1000 words...

Please also try
Code:
if (pin_3==0)
{
// blink led 
}
else
{
// blink another led
}

Why testing for 1 AND 0 on an input pin??
If it's not a 1 it has to be 0 or what else do you expect?? 0.5 :( :(

when checking the voltage on the pin, i saw that there's a 5v on without any input on it .
That's good news. Did you reed the datasheet of the 89C52? What is it saying about Port 3? If you still has doubts about the status of that port try a resistor to GND and measure if it source current.

Are you sure you µP passes the
Code:
if (pin_3==1)
{
// do whatever
}
I bet it's not because it's stuck somewhere in your "move forward" part.
Try a little program that generate pulses on a output pin when pin_3==0.
At the start of the endless loop allways drive the testpin low.
Replace the "move forward" code with just one command do drive the testpin high.
Now connect a scoop to the testpin and you should see pulses when pin_3 is low and a continous low signal when pin_3 is high.
If that's ok replace the pin_3==0 part with pin_3==1 and it still should produce pulses but now with pin_3 high and a continous low signal when pin_3 is low.
If that's also ok try the If/else code above on two different testpins. Don't forget do drive both pins low at the start of the endless loop.
NOT TWO if statements; ONE if/else statement!!!!


the motor moves without putting a signal on it ...
That's even better news. Ladies and Gentlemen, the invention of this brand-new century: The self-running-motor ;) :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top