PIC microcontroller input-output ports are named PORTA, PORTB, PORTC, etc. depending on the type of microcontroller you are using. Port pins can be in analog or digital mode. In analog mode, ports are input only and the in-built analog-to-digital converter and multiplexer circuits are used. In digital mode, a port pin can be configured as either input or output. The TRIS registers control the port directions. We have TRIS registers for each port, named as TRISA, TRISB, TRISC, and so forth. Clearing a TRIS register bit to 0 sets the corresponding port bit to output mode. In the same way, setting a TRIS register bit to 1, sets the corresponding port bit to input mode.
Ports can be accessed as a single 8-bit register, or individual bits of a port can be accessed. The following example, demonstrates how PORTB is configured as an output port and all its bits set to a 1:
TRISB = 0; // Set PORTB as output
PORTB = 0xFF; // Set PORTB bits to 1
Note that, the hexadecimal number 0xFF in binary is 11111111; therefore all the 8 bits are set to 1.
In the same way, the example below, illustrates how the 4 upper bits of PORTC can be set as input and the 4 lower bits of PORTC can be set as output.
TRISC = 0xF0;
Hexadecimal number 0xF0 in binary is 11110000; and this explains why the upper bits are set as input with the 1s and the lower bits set as output with the 0s.
We can access the bits of an input-output port by specifying the required bit number. For example, variable P2 is loaded with bit 2 of PORTB:
P2 = PORTB.2;
We can complement all the bits of a port by using the following statement:
PORTB = ~PORTB;
More Examples
Example 1:
Write a program to set the odd-numbered PORTB pins, that is, bits 1, 3, 5, and 7 to logic 1:
Solution:
Odd-numbered port pins can be set to logic 1 by sending the bit pattern 10101010 to the port. This bit pattern is the hexadecimal number 0xAA and the required program is:
void main() {
TRISB = 0; // Configure PORTB as output
PORTB = 0xAA; //Turn on odd numbered port pins
}
Example 2:
Write a program to set all the eight port pins of PORTB to logic 1:
Solution:
PORTB is configured as an output port and then all port pins are set to logic 1 by sending the hexadecimal number 0xFF. (0xFF in binary is 11111111).
void main() {
TRISB = 0; // Configure PORTB as output
PORTB = 0xFF; // Set all the port pins to logic 1
}
Example 3:
Write a program to set all bits of PORTB to logic 1, and then logic 0, and to repeat this process 10 times.
Solution:
A for statement can be used to create a loop to repeat the required operation 10 times, note 0xFF in binary is 11111111:
void main() {
unsigned char j;
for (j = 0; j < 10; j++) { // repeat 10 times
PORTB = 0xFF; // set PORTB pins to 1
PORTB = 0; // clear PORTB pins
}
You may also read:
The world of electronics is constantly evolving, allowing designers and manufacturers to push the boundaries…
PIC microcontrollers are fabricated by Microchip Technology. PIC16C84 and PIC16F84 are the two microcontrollers in…
The ADD instruction tells the microcontroller’s CPU to add the source byte to register A…
8-bit microprocessors are limited in their speed (the number of instructions that can be executed…
The 68HC11 (also abbreviated as 6811 or HC11) is an 8-bit microcontroller that was introduced…
Mutual exclusion typically imposes some conditions on access to a given resource by two or…
View Comments