PIC16F877A GPIO Tutorial

This article is a continuation of the series of tutorials on the PIC16F877A Microcontroller. The aim of this series is to provide easy and practical examples that anyone can understand. Now we are going to see PIC16F877A LED Interfacing Tutorial (PIC16F877A GPIO Tutorial). If you want to interface with LED, you should know the Registers used for GPIO. At the end of this tutorial, you will be familiar with the PIC GPIO’s and the associated registers for configuring and accessing the GPIO’s.

Prerequisites

PIC16F877A GPIO Tutorial

Introduction

 PIC16F877A has 33-GPIO‘s grouped into five ports namely PORTA to PORTE. They are used for the input/output interfacing with other devices/circuits. Most of these port pins are multiplexed for handling alternate functions for peripheral features on the devices. All ports in a PIC chip are bi-directional. When the peripheral action is enabled in a pin, it may not be used as its general input/output functions.

  • PORTA
  • PORTB
  • PORTC
  • PORTD
  • PORTE

Now we will get into GPIO registers.

GPIO Registers

RegisterDescription
TRISXThis Register is used for Select that respected IO port as a input or output.
PORTXThis is the IO Port

Note: Here ‘x’ could be A,B,C,D,E so on depending on the number of ports supported by the controller.

TRISX Register

Before accessing the PORTX register, we should declare that port whether input or output. So this register is used to select that direction. If you set 0 that IO port will act as the output port. If you set 1 that IO port will act as an input port. Just see the snippet below. Then you will understand.

TRISB = 0xff;  // Configure PORTB as Input.
 
TRISC = 0x00; // Configure PORTC as Output.
 
TRISD = 0x0F; // Configure lower nibble of PORTD as Input and higher nibble as Output
 
TRISD = (1<<0) | (1<3) | (1<<6); // Configure PD0,PD3,PD6 as Input and others as Output

PORTX Register

This register is used to read/write the data from/to port pins. Writing 1’s to PORTx will make the corresponding PORTx pins as HIGH. Similarly writing 0’s to PORTx will make the corresponding PORTx pins as LOW. PORTX registers and their alternate functions are shown in the below table.

PORT Number of Pins Alternative Function
PORTA6 (PA0-PA5) ADC
PORTB8 (PB0-PB7) Interrupts
PORTC8 (PC0-PC7) UART,I2C,PWM
PORTD8 (PD0-PD7) Parallel Slave Port
PORTE3 (PE0-PB2) ADC

I think this is enough to make code. Let’s move into the coding part.

LED Interfacing

Code

In this code, I’ve connected LEDs to Port D.

#include<htc.h>

void delay()
{
    unsigned int a;
    for(a=0;a<10000;a++);
}

void main()
{
    TRISD=0;              //Port D is act as Output
    while(1) {
        PORTD=0xFF;   //Port D ON
        delay();
        PORTD=0x00;   //Port D OFF
        delay();
    }
}

Output

PIC16F877A GPIO Tutorial

[ Please find the output image Here ]

Switch Interfacing

Code

In this code, Switch is connected to Port D. LEDs are connected to Port B.

#include<htc.h>

#define sw RD0                  //Switch is connected at PORTD.0

void delay()
{
    unsigned int a;
    for(a=0;a<10000;a++);
}

void main()
{
    TRISB=0;               //Port B act as Output
    TRISD=0xff;            //Port D act as Input
    while(1) {
        if(!sw) {
            PORTB=0xff;       //LED ON
        } else 
            PORTB=0;          //LED OFF
    }
}

Output

[ Please find the output image Here ]

In our next tutorial, we will see how to use the Timer in PIC16F877A. And also you can see LCD interfacing with the PIC16F877A.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents