Input/Output ports in PIC16C73/73A/74/74A PIC Microcontroller

I/O Ports

One of the most important feature of the microcontroller is a number of input/output pins used for connection with peripherals. In the case of PIC 167CX, there are in total of thirty-five general purpose I/O pins available, which is quite enough for the most applications.
In order pins’ operation can match internal 8-bit organization, all of them are, similar to registers, grouped into five so called ports denoted by A, B, C, D and E. They all have several features in common:
üFor practical reasons, many I/O pins have two or three functions. If a pin is used as any other function, it may not be used as a general purpose input/output pin; and
üEvery port has its “satellite”, i.e. the corresponding TRIS register: TRISA, TRISB, TRISC etc. which determines performance, but not the contents of the port bits.
By clearing some bit of the TRIS register (bit=0), the corresponding port pin is configured as output. Similarly, by setting some bit of the TRIS register (bit=1), the corresponding port pin is configured as input. This rule is easy to remember 0 = Output, 1 = Input.

Port A and TRISA Register

Port A is an 8-bit wide, bidirectional port. Bits of the TRISA and ANSEL control the PORTA pins. All Port A pins act as digital inputs/outputs. Five of them can also be analog inputs (denoted as AN):

Similar to bits of the TRISA register which determine which of the pins will be configured as input and which as output, the appropriate bits of the ANSEL register determine whether the pins will act as analog inputs or digital inputs/outputs.
RA0 = AN0 (determined by bit ANS0 of the ANSEL register);
RA1 = AN1 (determined by bit ANS1 of the ANSEL register);
RA2 = AN2 (determined by bit ANS2 of the ANSEL register);
RA3 = AN3 (determined by bit ANS3 of the ANSEL register); and
RA5 = AN4 (determined by bit ANS4 of the ANSEL register).
Each bit of this port has an additional function related to some of built-in peripheral units.


The microcontroller is commonly used in devices which have to operate periodically and, completely independently using a battery power supply. In such cases, minimal power consumption is one of the priorities. Typical examples of such applications are: thermometers, sensors for fire detection and similar. It is known that a reduction in clock frequency reduces the power consumption, so one of the most convenient solutions to this problem is to slow the clock down (use 32KHz quartz crystal instead of 20MHz).

Setting the microcontroller to sleep mode is another step in the same direction. However, even when both measures are applied, another problem arises. How to wake the microcontroller and set it to normal mode. It is obviously necessary to have an external signal to change logic state on some of the pins. Thus, the problem still exists. This signal must be generated by additional electronics, which causes higher power consumption of the entire device.

The ideal solution would be the microcontroller wakes up periodically by itself, which is not impossible at all. The circuit which enables that is shown in figure.

The principle of operation is simple:

A pin is configured as output and logic one (1) is brought to it. That causes the capacitor to be charged. Immediately after this, the same pin is configured as an input. The change of logic state enables an interrupt and the microcontroller is set to Sleep mode. Afterwards, there is nothing else to be done except wait for the capacitor to discharge by the leakage current flowing out through the input pin. When it occurs, an interrupt takes place and the microcontroller continues with the program execution in normal mode

Theoretically, this is a perfect solution. The problem is that all pins able to cause an interrupt in this way are digital and have relatively large leakage current when their voltage is not close to the limit values Vdd (1) or Vss (0). In this case, the capacitor is discharged for a short time since the current amounts to several hundreds of microamperes. This is why the ULPWU circuit able to register slow voltage drops with ultra low power consumption was designed. Its output generates an interrupt, while its input is connected to one of the microcontroller pins. It is the RA0 pin. Referring to Fig. (R=200 ohms, C=1nF), discharge time is approximately 30mS, while the total consumption of the microcontroller is 1000 times lower (several hundreds of nano amperes).

Port B and TRISB Register

Port B is an 8-bit wide, bidirectional port. Bits of the TRISB register determine the function of its pins.

Similar to Port A, a logic one (1) in the TRISB register configures the appropriate port pin as input and vice versa. Six pins on this port can act as analog inputs (AN). The bits of the ANSELH register determine whether these pins act as analog inputs or digital inputs/outputs:
RB0 = AN12 (determined by bit ANS12 of the ANSELH register);
RB1 = AN10 (determined by bit ANS10 of the ANSELH register);
RB2 = AN8 (determined by bit ANS8 of the ANSELH register);
RB3 = AN9 (determined by bit ANS9 of the ANSELH register);
RB4 = AN11 (determined by bit ANS11 of the ANSELH register); and
RB5 = AN13 (determined by bit ANS13 of the ANSELH register).

All the port pins have built in pull-up resistor, which make them ideal for connection to push-buttons, switches and opto couplers. In order to connect these resistors to the microcontroller ports, the appropriate bit of the WPUB register should be set.*

Having a high level of resistance (several tens of kilo ohms), these “virtual” resistors do not affect pins configured as outputs, but serves as a useful complement to inputs. As such, they are connected to CMOS logic circuit inputs. Otherwise, they would act as if they are floating because of their high input resistance.

If enabled, each Port B bit configured as an input may cause an interrupt by changing its logic state. In order to enable pins to cause an interrupt, the appropriate bit of the IOCB register should be set.

Because of these features, the port B pins are commonly used for checking push-buttons on the keyboard because they unerringly register any button press. Therefore, there is no need to “scan” these inputs all the time

When the X, Y and Z pins are configured as outputs set to logic one (1), it is only necessary to wait for an interrupt request which arrives upon any button press. By combining zeros and units on these outputs it is checked which push-button is pressed.

Port C and TRISC Register

Port C is an 8-bit wide, bidirectional port. Bits of the TRISC Register determine the function of its pins. Similar to other ports, a logic one (1) in the TRISC Register configures the appropriate port pin as an input.

Port D and TRISD Register

Port E and TRISE Register

Port E is a 4-bit wide, bidirectional port. The TRISE register’s bits determine the function of its pins. Similar to other ports, a logic one (1) in the TRISE register configures the appropriate port pin as input. The exception is RE3 which is input only and its TRIS bit is always read as ‘1’.

Similar to Ports A and B, three pins can be configured as analog inputs in this case. The ANSELH register bits determine whether a pin will act as analog input (AN) or digital input/output:
RE0 = AN5 (determined by bit ANS5 of the ANSEL register);
RE1 = AN6 (determined by bit ANS6 of the ANSEL register); and
RE2 = AN7 (determined by bit ANS7 of the ANSEL register).

ANSEL and ANSELH Registers
The ANSEL and ANSELH registers are used to configure the input mode of an I/O pin to analog or digital.

To configure a pin as an analog input, the appropriate bit of the ANSEL or ANSELH registers must be set (1). To configure pin as digital input/output, the appropriate bit must be cleared (0).
The state of the ANSEL bits has no affect on digital output functions. The result of any attempt to read some port pin configured as analog input will be 0.

In Short:
When designing a device, select a port through which the microcontroller will communicate to the peripheral environment. If you intend using only digital inputs/outputs, select any port you want. If you intend using some of the analog inputs, select the appropriate ports supporting such pins configuration (AN0-AN13);
Each port pin may be configured as either input or output. Bits of the TRISA, TRISB, TRISC, TRISD and TRISE registers determine how the appropriate ports pins- PORTA, PORTB, PORTC, PORTD and PORTE will act;
If you use some of the analog inputs, set the appropriate bits of the ANSEL and ANSELH registers at the beginning of the program;
If you use switches and push-buttons as input signal source, connect them to Port B pins because they have pull-up resistors. The use of these resistors is enabled by the RBPU bit of the OPTION_REG register, whereas the installation of individual resistors is enabled by bits of the WPUB register; and
It is usually necessary to react as soon as input pins change their logic state. How ever, it is not necessary to write a program for changing pins' logic state. It is far simpler to connect such inputs to the PORTB pins and enable the interrupt on every voltage change. Bits of the registers IOCOB and INTCON are in charge of that.

-> Let Us Embed <-