Configure GPIO in PIC16F877A example with PROTEUS simulation

Before going to the GPIO configuration we strongly recommend to see how to create a project in MPLAB X ide from the link below

GPIO in PIC16F87XA

GPIO in PIC16F877A is divided into 5 ports that is PORTA, PORTB, PORTC, PORTD, PORTE. We can configure a GPIO pin as a input or output. In all the microcontroller that can be set with the direction register.
Incase of PIC16F877A we can use a direction register TRIS. For every port there will be one TRIS direction register port. Consider the PORTA, which has TRISA. The address is given below.

What is the use of TRIS ❓

As given above TRISA, TRISB, TRISC, TRISC, TRISD, TRISE is used as direction register. So that writing into this register will configure the particular GPIO port as a input or output.

How to configure input and output with TRIS ❓

For this we need to refer the datasheet of the PIC16F877A. Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input (i.e., put the corresponding output driver in a High-Impedance mode). Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output (i.e., put the contents of the output latch on the selected pin). This is common for other ports also.

Reading the PORTA register reads the status of the pins, whereas writing to it will write to the port latch. All write operations are read-modify-write operations. Therefore, a write to a port implies that the port pins are read, the value is modified and then written to the port data latch.

Example program to  cofigure the RA0 in PORTA bit 0 pin as output

#include <xc.h>
void main(void) {
    TRISA0 = 0;
    int i = 0;
    while(1)
    {
       RA0 = 1;
       for(i = 1; i< 6000; i++);
       RA0 = 0;
       for(i = 1; i< 6000; i++);      
    }
    return;
}

Proteus circuit for the above program


Proteus Simulation output


Find the video tutorial which explains how to configure the GPIOs in PIC16F877A with Proteus simulation


Recommended to see the usage of GPIO configuration blinking LED


No comments:

Post a comment