One of the most important feature of the microcontroller is a number of input/output pins used for connection with peripherals. In this case, 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:
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.
Fig. 3-1 I/O Ports
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):
Fig. 3-2 Port A and TRISA Register
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.
Each bit of this port has an additional function related to some of built-in peripheral units. These additional functions will be described in later chapters. This chapter covers only the RA0 pin’s additional function since it is related to Port A only.
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 on the right.
Fig. 3-3 ULPWU Unit
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. The whole sequence is repeated...
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. 3-4 (R=200 ohms, C=1nF), discharge time is approximately 30mS, while the total consumption of the microcontroller is 1000 times lower (several hundreds of nanoamperes).
Fig. 3-4 Sleep Mode
Port B is an 8-bit wide, bidirectional port. Bits of the TRISB register determine the function of its pins.
Fig. 3-5 Port B and TRISB register
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:
Each Port B pin has an additional function related to some of the built-in peripheral units, which will be explained in later chapters.
Fig. 3-6 WPUB register
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.
Fig. 3-7 Pull-up resistors
* Apart from the bits of the WPUB register, there is another bit affecting pull-up resistors installation. It is RBPU bit of the OPTION_REG. It is a general-purpose bit because it affects installation of all Port resistors.
Fig. 3-8 IOCB register
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.
Fig. 3-9 Keyboard Example
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.
The RB0/INT pin is a single “true” external interrupt source. It can be configured to react to signal raising edge (zero-to-one transition) or signal falling edge (one-to-zero transition). The INTEDG bit of the OPTION_REG register selects the signal.
You have probably noticed that the PIC16F887 microcontroller does not have any special pins for programming (writing the program to ROM). The Ports pins available as general purpose I/O pins during normal operation are used for this purpose (Port B pins clock (RB6) and data transfer (RB7) during program loading). In addition, it is necessary to apply a power supply voltage Vdd (5V) and Vss (0V), as well as voltage for FLASH memory programming Vpp (12-14V). During programming, Vpp voltage is applied to the MCLR pin. All details concerning this process, as well as which one of these voltages is applied first, are beside the point, the programmers electronics are in charge of that. The point is that the program can be loaded to the microcontroller even when it is soldered onto the target device. Normally, the loaded program can also be changed in the same way. This function is called ICSP (In-Circuit Serial Programming). It is necessary to plan ahead when using it.
It is not complicated at all! It is only necessary to install a 4-pin connector onto the target device so that the necessary programmer voltages may be applied to the microcontroller. In order that these voltages don't interfere with other device electronics, design some sort of circuit breaking into this connection (using resistors or jumpers).
Fig. 3-10 ICSP Connection
These voltages are applied to socket pins in which the microcontroller is to be placed.
Fig. 3-11 Programmer On-Board Connections
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.
Fig. 3-12 Port C and TRISC Register
All additional functions of this port's bits will be explained later.
Port D is an 8-bit wide, bidirectional port. Bits of the TRISD register determine the function of its pins. A logic one (1) in the TRISD register configures the appropriate port pin as input.
Fig. 3-13 Port D and TRISD 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’.
Fig. 3-14 Port E and TRISE Register
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:
The ANSEL and ANSELH registers are used to configure the input mode of an I/O pin to analog or digital.
Fig. 3-15 ANSEL and ANSELH Registers
The rule is:
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.
Fig. 3-16 ANSEL and ANSELH Configuration
You will probably never write a program which fully utilises all the Ports in an efficient manner to justify learning all there is to know about these Ports. However, they are probably the simplest modules within the microcontroller. This is how they are used: