I have confusion in assigning ports as input or outputs in 8051 microcontroller.

As I have done programming with atmel atmega16 and variants we clearly have DDR registers to assign ports as inputs or outputs but not both in same program(or it might be both since I dont know if it can).

If it can be both(i/p and o/p) how would the microcontroller does this?

In a program while interfacing a 16x2 LCD with 8051 the P1.0 is used as input to find the value as busy flag and is used as an output pin when sending commands and data.

Please provide explanations.


  • 6
    \$\begingroup\$ OOOh I think I have found the answer by seeing the outer pins driver and latch arrangement of 8051 from the mazidi book at the appendix and the explanation from the electronics stackexchange answer electronics.stackexchange.com/questions/155854/… . Though the link doesnt have the diagram but when you see the diagram from the book(or any other source) and then read the answer from the link it will provide you a clear beautiful explanation! \$\endgroup\$
    – Jasser
    Feb 23, 2016 at 16:49

1 Answer 1


Microcontroller pins are typically input or output. When configured for output, there is a SFR (special function register) for that port that controls what levels the pins on that port are set to. When configured for input, the pin values show up on that same SFR.

In the example you gave (interfacing with a 16x2 LCD), P1.0 would spend most of its time as an input, and the microcontroller code would wait for the busy flag to clear. When the microcontroller detects the flag has cleared, it would switch the direct of P1.0 to an output, send the commands and data, and then switch back to an input.


Not the answer you're looking for? Browse other questions tagged or ask your own question.