0
\$\begingroup\$

As far as I understand, the 8051 microcontroller is able to read a pin

(1) by reading the actual pin, assuming the nFET is in cutoff (so a logic 1 is written before reading the pin)

(2) by reading the corresponding latch

Now is it true to assume that reading the latch only makes sense when using the pin as an output? And what are the advantages of reading the latch (which is used in e.g. read-write-modify instructions) instead of the actual pin?

Port 1 pin on a 8051

\$\endgroup\$
0
\$\begingroup\$

Reading the buffered output has an advantage when you want to do something like read-modify-write. E.g. you want to take the current value of the pin and logically AND it with some other value:

PIN = PIN & Value;

If the PIN is not buffered you won't be able to read it's value, as it is output, not input.

\$\endgroup\$
1
\$\begingroup\$

To change the state of a single I/O output latch while leaving the others unaffected, it's necessary for the 8051 to read eight latches, change one of the bits, and write the result back. If that read operation were to report the state of the pin rather than the latch, then changing the state of one pin while another pin was being used as an input but pulled low would cause that other pin to start outputting low. Not fun.

Normally the need to use read-modify-write operations on the latch registers would be accommodated by using separate read addresses for the latches and the pin states. Because bit-addressable addressing space is at a premium (only 16 I/O registers can be bit addressable), the designers of the 8051 made it so that reading the ports associated with the general-purpose I/O (i.e. 0x80, 0x90, 0xA0, 0xB0) will sometimes yield the contents of the port latches (when using most read-modify-write instructions, including BSET, CLR, etc.) or the state of the pins (when using most input instructions, including JB, JNB, etc.). While it would sometimes be helpful to have a way of simply reading the state of the latch registers, the 8051 does not provide one; the only instructions that read the latch state are those which use the read value purely as part of a read-modify-write sequence.

\$\endgroup\$
  • \$\begingroup\$ +1. related: "The Read-Modify-Write problem" \$\endgroup\$ – davidcary Jun 17 '16 at 21:17
  • \$\begingroup\$ @davidcary: I find it interesting that some of GI's PICs from the 1970s had separate read addresses for latches and pins, but it took Microchip a long time to add such features to their own PIC parts. \$\endgroup\$ – supercat Jun 17 '16 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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