Difference between PORT and LATCH on PIC 18F

前端 未结 8 803
南旧
南旧 2020-12-04 22:02

I already read the datasheet and google but I still don\'t understand something.

In my case, I set PIN RC6 of a PIC18F26K20 in INPUT mode:

TRI

相关标签:
8条回答
  • 2020-12-04 22:45

    Use LATx: to write to an output pin

    Use PORTx: to read an input pin

    For all PICs with LATx registers, all INPUT must be from PORTx and all OUTPUT should be to LATx, which totally avoids the problem of flipping bits when you write to a single bit of the port.

    0 讨论(0)
  • 2020-12-04 22:58

    I'll adapt my answer from Electrical Engineering.

    Let's use the picture from 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.

    0 讨论(0)
提交回复
热议问题