How to set the '51 port pin into input (output) mode?
Submitted By: Steve M. Taylor FAQ Last Modified: 08/09/06
- {Altered slightly from the original pen of Joseph Hebert, this is an excellent overview of the port structure of a standard 8052
- Steve, Moderator}
Don't think of the port of the µ-controller as if it were the port of a digital i/o card. Stop thinking of it like that. Instead, think of the port as a register associated with a data bus. Each bit in the register is tied to one line of the bus.
Now, if anything tied to the bus pulls one of the lines low, that line will cause anything that "reads" the bus to read that bit as a 0. And if the µ-controller reads the port register tied to that bus, it too will read that bit as a 0. Of course, if you put a 0 in any of the bits in that register, then you will still read that bit as a 0.
This is why you want to load 0xFF into port 0 anytime you aren't actively writing something to the data bus. This is not the same as pulling all of the lines high. Instead you are allowing them to go high, as long as nothing else on the data bus is pulling any of them low. If you're familiar with tri-state devices, think of writing a 1 to one of the port bits as putting that bit in the Hi-Z state.
So, it really isn't like working with a digital i/o card. You don't have to program the µ-controller ports as input or output. If you MOV A, P0, then you read the data bus and used P0 as input. If you MOV P0, A, then you wrote something to the data bus and used P0 as an output.
Some might tell you to think of loading 0xFF to P0 as "putting P0 in the input mode." Don't think of it this way. If you do, it will only be a matter of time before you're trying to figure out why something else on the bus isn't working when the only problem is that you never put the port back into "INPUT" mode since you had no plans to read anything. Instead, think of putting the bus back into HI-Z mode after your finished writing to it. And just stop thinking about putting it in either INPUT or OUTPUT modes at all.
another explanation ?
Submitted By: Jan Waclawek FAQ Last Modified: 08/09/06
- This one is by Andy Neil:
Actually, there is nothing to set a standard 8051 pin as an input; all pins are always bidirectional*.
However, if you write a '0' to a pin, it turns the open-drain driver ON and there is no way that anything external can ever force it to anything other than '0'
Therefore the input will just read '0' - irrespective of the external signal you might wish to read.
But If you write a '1' to a pin, it turns the open-drain driver OFF and so an external signal can take the pin either high or low - and the input can read the state of the external signal.
The is all explained in Chapter 3 of the "bible" for the 8051:
Chapter 3 - 80C51 Family Hardware Description:
http://www.semiconductors.philips.com/acrobat/v...WARE_1.pdf
Start reading at p2, "Port Structure and Operation" and pay particular attention to Figures 4 & 5.
------
* actually, this arrangement is called "quasi-bidirectional". See also this explanation of bidirectional ports.
also check
Submitted By: Erik Malund FAQ Last Modified: 08/09/06
- How do Quasi-Bidirectional I/O ports work? in same FAQ group
Add Information to this FAQ: If you have additional information or alternative solutions to this question, you may add to this FAQ by clicking here.