The PICmicro designers want the PICmicro to be as flexible as possible. To this end they allow the PICmicro user to set exactly which of the 13 PIO pins are inputs and which are outputs. This means that we could have anything from 13 inputs to 13 outputs, depending on whether we are flashing lights or reading lots of switches.

Each PICmicro input/output port has a matching register which determines the mode of each of the pins. If the bit in the "data direction register" is set this means that the port is configured as an input. If the port is an input we can read the state of a voltage on that pin by reading from the port. We will see how to do this later in the course.

If the bit in the register is clear this means that the port is an output. If a bit is an output we can change its state by loading values into the input/output register. Note that this technique effectively uses software (our program) to configure how the hardware behaves.

There is nothing wrong with having some pins as inputs and others as outputs in a given port. If I want to make bits 0, 1, and 2 outputs and bits 3, 4, 5, 6 and 7 inputs I simply have to calculate the right number to place in the data direction register:

Remember that if the bit is set high this means we are an input. This means that I need have a "high" value for bits 3, 4, 5, 6 and 7. The number I should put into the data direction register for the port should be:

    128+64+32+16+8 = 248 or 0xf8

Note that I have given the values in HEX as well as decimal. HEX is easier to convert to patterns of bits, and is often used in embedded programs for this reason.

On the right you can see a CPIC which manipulates TRISB to set a variety of input/output combinations. If you step through the program you can see how the input state of the pins on PORTB change as each setting is made. Note how you can only change the state of an input bit if the corresponding bit in TRISB is set high.

Remember that if we wanted to actually read from PORTB in a program we would need to set the bank selection bit to allow PORTB to be accessed rather than TRISB, since the live at the same address in different banks.