How to configure 8051 pins as input/output?

Muhammad Nouman picture Muhammad Nouman · Dec 15, 2014 · Viewed 8.5k times · Source

In pic microcontroller TRIS register determines whether port would act as input or output and PORT register would determine the content, what should be written (in case of output) or read (in case of input).

But in 8051 there isn't any such kind of TRIS register. To configure the register as input/output you have to write to the same register and to write/read the data you have to use the same register again. Am I right about this? If yes, then please explain how is it possible? If not, then please explain the correct theory about this.

Answer

norekhov picture norekhov · Dec 15, 2014

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/various/80C51_FAM_HARDWARE_1.pdf

Start reading at p2, "Port Structure and Operation" and pay particular attention to Figures 4 & 5.