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.
Answer
Latch is a kind of memory of one bit.
Let's use the picture in manual:
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:
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:
And when you read from PORTx, you're reading the actual I/O pin value. See below in blue:
PIC uses read-modify-write to write operations and this can be a problem, so they use this shadow register to avoid it.
No comments:
Post a Comment