Difference between PORT and LATCH on PIC 18F

2019-01-13 16:24发布

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:

TRISCbits.TRISC6 = 1;

Then I read the value with PORT and LATCH and I have different value!

v1 = LATCbits.LATC6;

v2 = PORTCbits.RC6;

v1 gives me 0 where v2 gives 1.

Is it normal? In which case we have to use PORT and in which case LATCH?

标签: port pic pic18
7条回答
Lonely孤独者°
2楼-- · 2019-01-13 16:34

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-13 16:46

The latch is the output latch onto which values are written. The port is the voltage at the actual pin.

There are a few situations where they can be different. The one that I've encountered most frequently is if you have a pin (accidentally) shorted to ground. If you set the latch high, the latch will read high, but the port will read low because the voltage on the pin is still approximately ground.

Another situation leading to what you've described is when the port pin hasn't been configured correctly. I (and everyone I work with) have spent many hours trying to figure out why our PIC isn't working to expectations, to eventually find out that we glossed over turning off the analog modules, for instance. Make sure you go over the section I/O Ports -> PORT?, TRIS?, and LAT? registers in the datasheet. You can get more info in the Microchip wiki page which explains about reading the wrong value immediately after you write an output on a pin connected to a capacitive load.

That wiki page also explains:

A read of the port latch register returns the settings of the output drivers, whilst a read of the port register returns the logic levels seen on the pins.

Also, here's a snippet from the I/O Ports section on the 18F14K50 (which ought to be the same as the rest of the 18F series):

Each port has three registers for its operation. These registers are:

  • TRIS register (data direction register)
  • PORT register (reads the levels on the pins of the device)
  • LAT register (output latch)

So in most situations, you will write to the latch and read from the port.

查看更多
再贱就再见
4楼-- · 2019-01-13 16:46

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.

查看更多
放我归山
5楼-- · 2019-01-13 16:49

My recommendation is to regard the PORT values as read-only. The LAT values may be read or written, but the value read will be the last value written, not the input value of the pin.

On older PICs, the LATx values didn't exist; the only way to write to a port was via the PORTx registers. Curiously, some of the really old PICs, back from the General Instruments (pre-Microchip) days, supported LATx, but Microchip didn't add that feature until the PIC18x line.

查看更多
你好瞎i
6楼-- · 2019-01-13 16:55

I recently experienced that writing on PORTx Ri (e.g. PORTC RC1) of PIC18F14K50 is ineffective when another PORTx Rj (e.g. PORTC RC0) was already set. I observed a peek in the oscilloscope on PORTx Ri but I was unable to sustain the output. This issue has vanished as soon as I was writing on LATx.

LATx writing looks mandatory on PIC18 and PORTx writing prohibited.

查看更多
神经病院院长
7楼-- · 2019-01-13 16:56

Yes, it's normal to read PORTx and LATx and occasionally find they have different values.

When you want to read whether some external hardware is driving a pin high or low, you must set the pin to input mode (with TRIS or the DIR register), and you must read PORTx. That read tells you if the actual voltage at the pin is high or low.

When you want to drive a pin high or low, you must set the pin to output (with TRIS or the DIR register); you should write the bit to the LATx register.

(Writing that bit to the PORTx register may seem to do the right thing: that pin will -- eventually -- go high or low as commanded. But there are many cases -- such as when some other pin on that port is connected to an open-collector bus -- that writing to one bit of the the PORTx register will mess up the state of the other pins on that port, leading to difficult-to-debug problems).

Open Circuits: read before write

查看更多
登录 后发表回答