PIC Experimenter's Board Manual
Using the TRIS & Port Registers
The TRIS (TRI-State Enable) Register is a one byte (8-bit) programmable register on the PIC 16F84 that controls whether a particular I/O pin is configured as an input or output pin. There is a TRIS Register for each port. TRISA controls the I/O status for the pins on Port-A and TRISB controls the I/O status for the pins on Port-B.
If one places a binary 0 at a bit location in TRISB for Port-B the corresponding pin location on Port B will become an output pin. If one places a binary 1 at a bit location in the TRISB, the corresponding pin on Port B becomes an input pin. The TRISB data memory address for Port-B is 134 (or 86h in hex).
After Port-B has been configured using TRISB register, the user can read or write to the port using Port-B address (decimal number 6)
Here is an Example. Suppose we want to make all port B lines output lines. To do so we need to put a binary "0" in each bit position in the TRISB register. So the number we would write into the register is decimal 0. Now all our I/O lines are configured as output lines.
If we connect an LED (light emitting diode) to each output line we can see a visual indication of any number we write to the Port-B. If we want to turn on the LED's connected to RB2 and RB6 we need to place a binary "1" at each bit position on Port-B register. To accomplish this we look at the bit weights associated with each line. RB2 has a bit weight of 4, and RB6 has a bit weight if 64. We add these number together (4 + 64 = 68) and write that number into the Port-B register. When we write the number 68 into the Port-B register, the LED's connected to RB2 and RB6 will light.
To configure Port-A we use the TRISA register, decimal address 133, see figure 1 (above). On Port-A however only the first 5 bits of the TRISA and the corresponding I/O lines (RA0-RA4) are available for use. Examine the I/O pin out on the 16F84 and you will find there are only 5 I/O pins (RA0-RA4) corresponding to Port-A. These pins are configure using the TRISA register and used using the PORT-A address.
On power up and reset, all the I/O pins of Port-B and Port-A are initialized (configured) as input pins. We can change this configuration with our program.
Here's another example. Let's configure port-B so that bit 0 (RB0) is an input pin and all other pins are output lines. To place binary 0's and 1 in the proper bit location we use the bit weights shown in binary number table. For instance to turn bit 0 on ("1") and all other bits off ("0"), we would write the decimal number 1 into TRISB for Port B. In basic the command to write to a register is the Poke command. The program line to write the decimal value 1, into the TRISB register will look like
Poke 134,1 (PICBasic Command)
The number after the Poke command is the memory address the command will write to, in this case 134 which is the data memory address of the TRISB for Port B. The next number separated by a "," is the value we want to write in that memory address. In this case it's number 1.
The PICBasic Pro Compiler has the microcontroller registers memory location stored, so we can access the register locations using their proper name. For instance The above PICBasic command may be written as:
TRISB = 1 (PICBasic Pro Command))
using the PICBasic Pro compiler.
Look at the binary equivalent of the decimal number 1
0 0 0 0 0 0 0 1
If you mentally place each "1" and "0" into the TRISB register locations shown below. See how the "1" fits into the bit 0 place making that corresponding line an input line. While all other bit locations have a "0" written in them making them output lines.
So by poking or writing into this location with a decimal number that represents a binary number that contain the proper sequence of bits (0's and 1's) we can configure any pin in the port to be either an output or input in any combination we might require. In addition we can change the configuration of the port "on the fly" as the program is running.
To summarize, by poking a binary 1 into the TRIS register turns that corresponding bit/pin on the Port to an input pin. Likewise, poking a binary 0 will turn the bit into an output.
Accessing the Ports for Output
Once the port(s) lines have been configured (input or output) using the TRIS Register we can start using it. To output a binary number at the port simply write the number to the port using the Poke command. The binary equivalent of the decimal number will be outputted as shown in our first example. To output a high signal on RB3 we could use this either;
PicBasic command. Poke 6, 8
PICBasic Pro command. PortB = 8
Where 6 is the memory address in the Poke command for Port B and 8 is the decimal equivalent of the binary number (00001000) we want to output. In the PICBasic Pro command we can access PortB simply by using the Port's name; PortB .