9

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.

Donotalo
  • 347
  • 1
  • 3
  • 7

2 Answers2

21

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.

Daniel Grillo
  • 7,659
  • 18
  • 51
  • 69
  • 1
    +1 for linking to a place the clearly describes the (read-modify-write)[http://techref.massmind.org/techref/readmodwrite.htm] problem (and solution). – davidcary Apr 05 '11 at 11:55
  • 1
    Wow, great explanation. – abdullah kahraman Jun 13 '12 at 07:28
  • The two links to the read-mod-write problem are broken. – Randomblue Nov 02 '12 at 15:31
  • @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. – Daniel Grillo Sep 04 '13 at 03:21
  • 1
    The [link contributed by davidcary](http://techref.massmind.org/techref/readmodwrite.htm) A Couple more links referencing the problem: [1](https://electrosome.com/read-modify-write-problem-pic/), [2](http://www.knutselaar.eu/pdf/ReadModifyWrite.pdf), [3](http://www.cornerstonerobotics.org/curriculum/lessons_year2/erii_rmw_problem.pdf) – Pau Coma Ramirez May 25 '23 at 16:26
6

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.

Leon Heller
  • 38,774
  • 2
  • 60
  • 96
  • but i guess i don't need to write to the latch register, since writing to original port register will write to latch, right? – Donotalo Apr 04 '11 at 18:01
  • @Donotalo, You're right. You can write in the port register too. It does not matter. – Daniel Grillo Apr 04 '11 at 18:13
  • @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. – supercat Apr 04 '11 at 20:21
  • +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"). – davidcary Apr 05 '11 at 11:59