I/O on the 8051
The 8051 has 4 GPIO 8-bit ports.
Output
To write an output to a port, just move some data to that port's register.
mov P1, A ; Output the contents of the accumulator on port 1.
Individual bits of ports can also be set or cleared.
clr P1.0 ; Output high on the 0th pin of port 1. setb P1.0 ; Output low on the 0th pin of port 1.
Input
Before reading from an input pin, the pin MUST be set to high in order to prevent a short circuit.
After setting the pins to high, the inputs can be read by reading the port's register.
mov P1, #0FFh ; Set all pins of port 1 to high. mov A, P1 ; Move the input data on port 1 to the accumulator.