PIC24F digital lines I/O...need help

Thread Starter

puneetnepsam

Joined May 12, 2009
5
hi i am a beginner in the embedded systems.....please help

in the I/O ports section i have read.....
TRIS register means that it will define the direction of the pin which is connected to the particular port
exmple:

TRISGbits.TRISG1 = 1 will make that line in the input mode...

but what does
PORTGbits.PORTG1 = 1;
and
LATGbits.LAT1 = 1;

means??

does it mean that it would write 1 to the corresponding line??

thanks
Puneet Gupta
 

AlexR

Joined Jan 16, 2008
732
As you have surmised PORTGbits.PORTG1 = 1; writes a 1 to port G bit/pin 1 and
LATGbits.LAT1 = 1; writes a 1 to latch G bit 1.

In the case of writing, the two instructions are interchangeable but if you were doing a read then there can be significant differences between reading a port (pin) and reading the contents of the port data latch.
 

houta69

Joined Mar 8, 2009
4
The rule of thumb is to write to the LAT register and read from the PORT register to avoid read-modify-write problems of writing directly to the PORT. Check out section 12.2.2 of the PIC24F family reference manual for the details.
 

AlexR

Joined Jan 16, 2008
732
houta69;

I think you have it the wrong way around.

To quote from the ref manual section 12.2.3

The differences between the PORTx and LATx 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.
• A read of the LATx register reads the data value held in the port latch
To prevent the read, modify, write problem you read from the latch and write where-ever you like since when you write to the port you are actually writing to the latch.
 
Top