Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PORT and LATCH on PIC 18F

Tags:

port

pic

pic18

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?

like image 335
acemtp Avatar asked Apr 12 '10 16:04

acemtp


People also ask

What is the difference between LAT and port?

The differences between the PORT and LAT registers can be summarized as follows: A write to the PORTx register writes the data value to the port latch. A write to the LATx register writes the data value to the port latch. A read of the PORTx register reads the data value on the I/O pin.

How many latches are there in a port?

There are basically four main types of latches and flip-flops: SR, D, JK, and T. The major differences in these flip-flop types are the number of inputs they have and how they change state. For each type, there are also different variations that enhance their operations.

What are the differences between tris registers and port registers?

PORT E pins are multiplexed with analog inputs. When selected for analog input, these pins will read as '0's. TRIS E controls the direction of the RE pins, even when they are being used as analog inputs. The user must make sure to keep the pins configured as inputs when using them as analog inputs.

Which instruction read the status of Port latch?

Reading "LAT" reads the value of the output latch irrespective of the electrical state of the pin.


1 Answers

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.

like image 126
Mark Rushakoff Avatar answered Sep 18 '22 19:09

Mark Rushakoff