9
\$\begingroup\$

This is what I found in PIC16F1947 data sheet:

Reading the PORTB register reads the status of the pins, whereas writing to it will write to the PORT latch. All write operations are read-modify-write operations. Therefore, a write to a port implies that the port pins are read, this value is modified and then written to the PORT data latch (LATB).

I'm a firmware developer and my background is Computer Science. I still struggle to understand electronics and logic in hardware level. I only have the basic knowledge.

So, I want to understand what happens when data is written to latch in hardware level.

Thank you.

\$\endgroup\$

2 Answers 2

21
\$\begingroup\$

Latch is a kind of memory of one bit.

Let's use the picture in manual:

Generic I/O Port Operation

When you write a bit in a I/O pin, you're storing this bit from Data Bus to the Data Register (D-FlipFlop). If TRISx of this bit is 0, so data from Q of the Data Register will be in the I/O pin. Write in LATx or PORTx is the same. See below in red:

Generic I/O Port Operation Write

On the other hand, read from LATx is different of read from PORTx.

When you're reading from LATx, you're reading what is in the Data Register (D-FlipFlop). See picture below in green:

Generic I/O Port Operation Read LATx

And when you read from PORTx, you're reading the actual I/O pin value. See below in blue:

Generic I/O Port Operation Read PORTx

PIC uses read-modify-write to write operations and this can be a problem, so they use this shadow register to avoid it.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ +1 for linking to a place the clearly describes the (read-modify-write)[techref.massmind.org/techref/readmodwrite.htm] problem (and solution). \$\endgroup\$ Commented Apr 5, 2011 at 11:55
  • 1
    \$\begingroup\$ Wow, great explanation. \$\endgroup\$ Commented Jun 13, 2012 at 7:28
  • \$\begingroup\$ The two links to the read-mod-write problem are broken. \$\endgroup\$ Commented Nov 2, 2012 at 15:31
  • \$\begingroup\$ @Randomblue,I've put another link. The problem with the other link is the ']' character at the end. Just delete it in your browser address. \$\endgroup\$ Commented Sep 4, 2013 at 3:21
  • 1
    \$\begingroup\$ The link contributed by davidcary A Couple more links referencing the problem: 1, 2, 3 \$\endgroup\$ Commented May 25, 2023 at 16:26
6
\$\begingroup\$

To avoid read-modify-write problems you should write to the port as a whole, rather than setting or resetting individual bits in the port. An R-M-W problem might result in a bit not being set, or another output going high, especially if output pins are sourcing or sinking a lot of current.

A "shadow register" is typically used. Set or reset bits in that, and output it to the port, to avoid R-M-W problems.

The problem is avoided with 18F PICs by the use of a separate latch, individual bits in that can be set and reset with impunity.

\$\endgroup\$
4
  • \$\begingroup\$ but i guess i don't need to write to the latch register, since writing to original port register will write to latch, right? \$\endgroup\$ Commented Apr 4, 2011 at 18:01
  • \$\begingroup\$ @Donotalo, You're right. You can write in the port register too. It does not matter. \$\endgroup\$ Commented Apr 4, 2011 at 18:13
  • \$\begingroup\$ @Donotalo: It's possible to write to the port register, but I'd recommend as a matter of habit writing to the LATx registers on those processors that have them, and regarding the PORTx registers as read-only. A "blind" store to a PORTx register (e.g. PORTB = 0x42;) will behave no differently from one to LATBx, and a read-modify-write to a PORTx register (e.g. PORTB |= 0x02;) will have an effect which will either be the same as LATx or else differ in a most-likely-undesirable way. BTW, some of the later pre-Microchip PICs offered LATx; I don't know why Microchip took years (decades?) to do so. \$\endgroup\$ Commented Apr 4, 2011 at 20:21
  • \$\begingroup\$ +1 for mentioning that PIC18F chips (aka "16-bit instruction PICs) have the LAT register, while PIC16F chips (aka "14-bit instruction PICs") require simulating the LAT register in software ("shadow register"). \$\endgroup\$ Commented Apr 5, 2011 at 11:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.