Login

Lost your password?
Don't have an account? Sign Up

IO port in PIC Microcontroller (PIC18F4550)

Before diving into advanced modules like ADC, UART, or PWM, every embedded engineer must first understand IO port (General Purpose Input/Output port) — the building blocks of microcontroller programming.

The PIC18F4550 microcontroller comes with powerful, flexible I/O ports that let you read input signals (like switches or sensors) and control output devices (like LEDs, relays, or buzzers).

In this blog, we’ll explore how to configure and use GPIO ports in PIC18F4550 using MPLAB X IDE and the XC8 compiler, through simple and practical examples.

🎯 Goal: After reading this post, you’ll be able to set up I/O pins, blink LEDs, and read switch states — the essential first step to mastering embedded systems.

PIC18F4550 has five ports: PORTA, PORTB, PORTC, PORTD, and PORTE.
Each port has 8 pins (some with fewer active pins depending on configuration).
Every pin can act as input or output, configured through TRISx registers.

RegisterFunction
TRISxData Direction Register (1 = Input, 0 = Output)
PORTxReads the current logic level on the port
LATxLatch register used for writing output data

Output: Set TRIS bit = 0 → write data to LATx register.

Input: Set TRIS bit = 1 → read data from PORTx register.

Example:

TRISBbits.TRISB0 = 0; // Configure RB0 as output
LATBbits.LATB0 = 1;   // Set RB0 HIGH (LED ON)
C

đź”§ Hardware Setup

  • Connect an LED (with 330Ω resistor) to RB0.
  • Connect VSS (GND) to common ground.
#include <xc.h>
#define _XTAL_FREQ 8000000

void main(void) {
    TRISBbits.TRISB0 = 0; // RB0 as output

    while(1) {
        LATBbits.LATB0 = 1; // LED ON
        __delay_ms(500);
        LATBbits.LATB0 = 0; // LED OFF
        __delay_ms(500);
    }
}
C

âś… Result: The LED connected to RB0 will blink every 500 ms.

đź”§ Hardware Setup

LED → connected to RB0 (output).

Switch → connected to RB1 (input) with pull-down resistor.

#include <xc.h>
#define _XTAL_FREQ 8000000

void main(void) {
    TRISBbits.TRISB1 = 1; // RB1 as input (switch)
    TRISBbits.TRISB0 = 0; // RB0 as output (LED)

    while(1) {
        if(PORTBbits.RB1 == 1)
            LATBbits.LATB0 = 1; // LED ON when switch pressed
        else
            LATBbits.LATB0 = 0; // LED OFF
    }
}
C

âś… Result: When you press the switch, the LED lights up!

Important Tips

  • Always configure TRIS registers before using any pin.
  • Use LATx for writing output and PORTx for reading inputs.
  • Use digital input mode (disable ADC on analog pins if needed).

Example:

ADCON1 = 0x0F; // Configure all pins as digital
C

  • LED indicators and displays
  • Push-button or sensor inputs
  • Relay and transistor control
  • Interfacing digital modules (IR sensor, keypad, etc.)

Once you master GPIOs, you can easily move on to ADC, Timers, UART, PWM, and Interrupts – all of which depend on correct pin configuration.

👉 Learn how to design real embedded projects step-by-step in our
Complete Practical Video Course on PIC18F4550 Microcontroller

Tag: IO port, GPIO port, Input Output port


Q1. What is the difference between LATx and PORTx?

LATx is used for writing output data, while PORTx is used to read the logic level on the pins.

Q2. Why do I need to set ADCON1 = 0x0F?

Some pins are analog by default; setting ADCON1 = 0x0F makes them digital for GPIO use.

Q3. Can I use GPIO pins for both input and output simultaneously?

No – each pin can only be in one direction at a time. You can change its direction dynamically if needed.

Q4. How many GPIO pins are available in PIC18F4550?

Up to 35 I/O pins, depending on configuration and function assignments.

Q5. What tools do I need to try this experiment?

You can start with:
PIC18F4550 Development Board (available at Bitziga Store)
10kΩ Potentiometer or LM35 Sensor
MPLAB X IDE + XC8 Compiler
Proteus Simulation (optional)

Q6. Where can I learn all modules of PIC18F4550 in depth?

You can join our premium hands-on course:
🎓 “Mastering PIC18F4550 Microcontroller” – A complete practical training covering ADC, Timers, Interrupts, UART, PWM, and real-world projects.
👉Mastering PIC18F4550 Microcontroller

Q7. Will this blog series cover other PIC modules too?

Yes! This is part of our “Design to Learn” campaign.
Upcoming posts will include:
Timer Configuration and Delay Generation
UART Communication and Serial Data Display
PWM Generation and Motor Control Projects
Stay tuned and subscribe for updates 🚀

Home » Recent posts » Learn › Configuring GPIO Module of PIC18F4550

https://bitziga.com

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*