The “tri-state” register, or “TRIS” register, is used to specify whether a pin is used as an output or an input. I/O pins are individually assigned as inputs or outputs by assigning a zero or one to the corresponding bit position within the TRIS register. Writing a zero to the bit position causes the pin to operate as an output. Writing a '1' to the bit position causes the pin to operate as an input. An example of a C statement to simultaneously set and clear all 16 bits of the TRIS register is:
TRISA = 0x0030;
This instruction sets PORT A pins 4 and 5 as input pins and all others as output pins.
In general, it is not a good practice to set the “direction” of all 16 bits of a port when the goal is really only to set the direction of a subset of the port pins. For example, if only pins 12 to 15 are to be set as output and pins 0 to 3 are to be set as inputs, while the direction of all other pins is left unspecified, then one should refrain from setting the direction of the unspecified pins. To accomplish this, you would use two instructions:
TRISASET = 0x000F; // Set RA0, RA1, RA2, and RA3 to be input pins without changing // the direction of other PORT A pins TRISACLR = 0xF000; // Set RA12, RA13, RA14, and RA14 to be output pins without // changing the direction of other PORT A pins
Seemingly, the same configuration could be achieved with the single statement:
TRISA = 0X000F;
However, not only does this statement set pins 12 to 15 to output, it also sets pins 4 to 11 to output. It may have been that a separate part of a more complex program had previously configured these pins for input. Thus, changing the direction of these “unspecified” pins may break the application.
One of the main goals of this redesign is to make our site, both the code and the content, semantically and syntactically correct and in line with HTML standards. This helps with search engine indexing and text to speech programs. This also enables our content to stand on its own without extra formatting or context.
Being able to both read and write to the LAT register is an important feature of many modern microprocessors. Sometimes it is necessary to implement a read-modify-write sequence of instructions to change a subset of port pins without modifying the state of other pins sharing the same port register. As discussed in the text above, due to electrical loading on the I/O pin by an external device or components, the processor may read the state of the pin at a different logic level using the PORT register than is actually set into the LAT register. The solution to this conflict is to determine the output settings be reading the LAT register. Hence, a proper read-modify-write sequence is shown in the following code example where the constant, MASK, defines the bits locations that will be modified and the variable “new_data” contains the values the bits at the locations that are to be modified.
A more concise sequence of code to accomplish the same result can be written as:
temp = LATG & ~MASK; // Read all 16 port G bits and clear MASK bits LATG = temp | (new_data & MASK;) // Write the modified bits to port G
For a more detailed explanation of the read-modify-write issue, refer to http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/rmw.htm.
Open Drain Control (ODC) causes the I/O pin high state to be high impedance. The most common use of open drain output is to accommodate an interface with a device that operates on different voltage levels and to tie multiple outputs together in a wired AND configuration. All PIC™32 I/O pins have individual open drain control. An example of the use of the ODC is:
ODCGSET = BIT_13;
To read from a specific port you use the PORT command. The PORT for each register stores the state of the register at a given time. You can assign this value to a variable to check conditions such as:
unsigned int x; x = PORTG;
This instruction assigns the current value of Port G to the variable 'x'.
To write to a port, use the PORT value to assign a value to the port using the instruction:
PORTG = 0x1234;
This instruction will result in the same output as the following instruction:
LATG = 0x1234;
In reality, as shown in Fig. 1, the WR PORT and WR LAT control both set the LAT requester, while the RD PORT control reads the state of the processor pin. Thus, assignments to LATG and PORTG are equivalent.
The documentation for the chipKIT™ Pro MX7 processor board refers to combinations of connectors as JA, JB, JC, etc. These letters do not correspond with the port names and pins of the PIC32MX7 processor, nor are consecutive I/O pins assigned to the Pmod connectors. To learn more about the difference, refer to the chipKIT Pro MX7 reference manual. For example, the pin assignment for Pmod connector JA is shown in Table 1.
Table 1. PIC32 port assignments for chipKIT Pro MX7 Pmod connector JA.