Home ยป PIC Programming ยป PIC Microcontroller Input-Output Ports Programming in C

PIC Microcontroller Input-Output Ports Programming in C

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.

PIC Microcontroller Input-Output Port Programming in C

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
}
Please follow us and share:

Comments

One response to “PIC Microcontroller Input-Output Ports Programming in C”

  1. […] PIC Microcontroller Input-Output Ports Programming in C […]

Leave a Reply

Currently trending: