Login

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

Configuring UART Module of PIC18F4550

The UART (Universal Asynchronous Receiver Transmitter) module in the PIC18F4550 microcontroller is one of the most essential peripherals for embedded engineers. It enables serial communication between your microcontroller and external devices like PCs, GSM modules, GPS receivers, Bluetooth modules, and more.

In this blog, we’ll take you through the practical steps to configure and use the UART module on the PIC18F4550 using the MPLAB X IDE and XC8 compiler – the same way engineers do it in real projects.

Goal: By the end of this post, you’ll be able to send and receive data via UART and interface your PIC18F4550 with a PC using a USB-to-Serial converter.

UART provides full-duplex serial communication using two pins:

SignalPinDescription
TX (Transmit)RC6Sends data serially
RX (Receive)RC7Receives data serially

You can set the communication speed (baud rate) by configuring the SPBRG and SPBRGH registers. Common baud rates are 9600 bps, 19200 bps, etc.

Important Registers
RegisterPurpose
TXSTATransmit Status and Control
RCSTAReceive Status and Control
SPBRG / SPBRGHBaud Rate Generator
PIR1, PIE1Interrupt and flag bits for TX/RX
1️⃣ Configure UART Pins

Let’s go step-by-step:

Make RC6 (TX) as output and RC7 (RX) as input.

TRISC6 = 0;  // TX as output
TRISC7 = 1;  // RX as input
C

2️⃣ Set the Baud Rate

For example, 9600 bps at 8 MHz:

SPBRG = 51;       // Baud rate value for 9600 bps
TXSTAbits.BRGH = 1;  // High speed
C

3️⃣ Enable UART Transmit and Receive
RCSTAbits.SPEN = 1;   // Enable Serial Port
TXSTAbits.TXEN = 1;   // Enable Transmission
RCSTAbits.CREN = 1;   // Enable Continuous Reception
C

4️⃣ Create Transmit and Receive Functions

void UART_TxChar(char ch) {
    while(!TXSTAbits.TRMT);  // Wait until buffer is empty
    TXREG = ch;
}

char UART_RxChar(void) {
    while(!PIR1bits.RCIF);   // Wait until data is received
    return RCREG;
}
C

5️⃣ Transmit a String

void UART_TxString(const char *str) {
    while(*str) UART_TxChar(*str++);
}
C

6️⃣ Example Main Program

#include <xc.h>
#define _XTAL_FREQ 8000000

void UART_Init(void);
void UART_TxChar(char);
char UART_RxChar(void);
void UART_TxString(const char *);

void main(void) {
    UART_Init();
    UART_TxString("UART Initialized!\r\n");

    while(1) {
        char data = UART_RxChar();
        UART_TxChar(data);  // Echo back received data
    }
}

void UART_Init(void) {
    TRISC6 = 0; TRISC7 = 1;
    SPBRG = 51; TXSTAbits.BRGH = 1;
    RCSTAbits.SPEN = 1;
    TXSTAbits.TXEN = 1;
    RCSTAbits.CREN = 1;
}
C

  • PIC18F4550 connected to a USB-to-Serial (FT232 / CH340) converter.
  • RC6 (TX) → RX of converter
  • RC7 (RX) → TX of converter
  • Common GND between PIC and converter
  • Connect converter’s USB to PC and open any serial terminal (like PuTTY or Tera Term).
  1. Program your PIC18F4550 with the above code.
  2. Open your serial terminal on PC at 9600 bps.
  3. Type any character — you’ll see it echoed back!
  • Sending debug messages to PC
  • Interfacing GSM/GPS/Bluetooth modules
  • Data logging and serial monitoring

Want to go beyond this simple example?
Learn interrupt-driven UART, multi-device serial networks, and real-time project integration in our Full Practical Course on PIC18F4550 Microcontroller.

If you enjoyed this tutorial and want to learn the complete practicals of PIC18F4550, including:

  • Timers, Interrupts, and UART Communication
  • PWM for Motor Control
  • USB Interface & Sensors Integration
  • Real-world Project Building

👉 Then don’t miss our Premium Course: “Mastering PIC18F4550 Microcontroller”
🎓 A complete hands-on training designed for students, hobbyists, and engineers.

Learn, Code, and Build Real Embedded Systems Projects!

Full Practical Course on PIC18F4550 Microcontroller


🔟 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)

11️⃣ 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

12️⃣ 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 » Home › Blog › Configuring ADC Module of PIC18F4550

https://bitziga.com

Leave a Comment

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

*
*