Cool Cap Engineer

Engineering by an anime nerd

Tutorials: USART Communication For PIC18 Microcontroller


In my last post, I mentioned two awesome devices I’ve been messing around with lately. The cool thing about those devices is that it only needed one actual pin from the PIC microcontroller to properly work. This pin is one of the pins needed to achieve proper USART communication. Today’s tutorial will serve as a guide in order to achieve proper USART communication. Although I mentioned that USART is used to communicate with the devices I mentioned in the last post, today’s focus will be on achieving communication between PIC18F2550 and the computer using USART.

USART (Universal Synchronous Asynchronous Receiver Transmitter)/ UART (Universal Asynchronous Receiver Transmitter) requires only two pins from any microcontroller to send and receive serial data: TX and RX. You can properly guess that TX is the pin which the microcontroller data is transmitted through, and RX is responsible for data received by the microcontroller. The figure below shows the TX and RX pins on the PIC18F2550.
Keep in mind; we need one more device to have the PIC18F2550 communicate with the computer. If you have an old computer, then you could use a MAX232 and connect it directly to the serial port of that computer. For this tutorial, we’re going to use a laptop to talk to the PIC. In order to do this, we’re going to use a FTDI to USB board from Sparkfun. We’re just going to use 4 pins of the FTDI to USB board: VCC, GND, TX and RX. Look at the figure below to learn how to set up the circuit. Please note that JP2 is connected to the PICKIT3.
Now, create a new project in MPLab X, create a new C file, and code and paste the following code. If you forgot how to create a project in MPlab X, visit my tutorial on starting with PIC18 microcontrollers.

#include <p18f4553.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <stdio.h>
#include <stdlib.h>

/*	PIC Configuratings */
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG= OFF

/* UART Global variables */
char message;						//	Used for receiving commands from the computer

void main(void)
	//	Set all of PORTC as outputs. TX and RX must be set as outputs first
	//	Configure UART

So open a Serial Terminal program. For this tutorial, I will use the Arduino IDE. Open the serial monitor in the Arduino IDE and set the baudrate to 115200. Finally, type a single character and hit send. You should see the same character you entered appear in the window.
Important! Please Read!

Make sure you enter the right baudrate. If you do not enter the correct baudrate, you will lose data. The figure below shows what happens if we set the serial program to 9600 baud rate even though the PIC is set to a baud rate of 115200.

But how do you know if you set the USART to the right baud rate? Let’s look at the following piece of code.


As you can tell by the name, this is responsible for setting up the USART on the PIC. We’re going to look at the last two parameters of this command. The last two parameters correspond to the baudrate of the USART. USART_BRGH_HIGH sets the USART to a high baudrate, but we can set it to a low baudrate by replacing USART_BRGH_HIGH with USART_BRGH_LOW. The final parameter is a spbrg value. Although there are a set of equations you can use to find this value, as long as you’re using a 1Mhz, 2Mhz, 4Mhz, 10Mhz, 20Mhz, or 40Mhz crystal, you can find the SPBRG value in the datasheet. The figure below shows where we can find the spbrg value.
Well, I hope this helped you guys! If you have any questions, or comments, please feel free to post a comment!

3 thoughts on “Tutorials: USART Communication For PIC18 Microcontroller

  1. Hello my loved one! I want to say that this post is awesome, nice written and include almost all important infos. I would like to peer extra posts like this .

    • I will be writing more PIC 18 tutorials in the future as well as a worklog for various projects as well. Don’t worry, I got you covered 🙂

  2. Firstly Thanks a TON !! Looking for a UART receiver code and tried writing a code from scratch without usart.h but never worked.
    I am working on the PIC18F97J60 (FOR MY Under-GRAD school project) , this is by far the best thing I’ve come across in terms of PIC support.

    Looking forward to more posts in PIC18 !!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s