PIC16F877A GPIO Tutorial

Now we are going to see PIC16F877A LED Interfacing Tutorial (PIC16F877A GPIO Tutorial). If you want to interface LED, you should know 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 function 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 weather input or output. So this register is used to select that direction. If you set 0 that IO port will act as output port. If you set 1 that IO port will act as input port. Just see the snippet below. Then you will understand.

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 its alternate functions are shown in 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 for make code. Let’s move into coding part.

LED Interfacing

Code

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

Output

PIC16F877A GPIO Tutorial

[ Please find the output image Here ]

Switch Interfacing

Code

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

Output

[ Please find the output image Here ]

0 0 vote
Article Rating
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
Sindhuja Pateel
Sindhuja Pateel
March 11, 2019 1:39 AM

hello i am sindhuja i have a quick question on timers in pic controller how to execute in mp lab for 1 sec blinking timer in mplab ide

EmbeTronicx India
EmbeTronicx India
Reply to  Sindhuja Pateel
March 13, 2019 1:35 AM

Hi Sindhu,

Please look at this Timer tutorial. (https://embetronicx.com/tutorials/microcontrollers/pic16f877a/pic16f877a-timer-tutorial/)
Thanks.

2
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: