Serial communication–PIC18

By Sami Mughal

Alternative Titles: Serial communication – PIC18F67J60
Alternative Titles: Serial communication – Olimex PIC-P67J60

One of the greatest features about the PIC18 (or most other PICs and Microcontrollers) is that you communicate with them via a Serial interface. That means that not only can it talk to your devices and computer and perform or issue commands, it can also work as a very good debugging tool as you can put serial feedback messages in your code. That means that you can debug your software without having to use a debugger, and do it much quickly too. It is also a personal preference, but I find for simpler programs this is an easier approach.

As I said, most PICs have the ability to communicate over a serial/RS232 protocol. However, if your PIC works on a 3.3V you need to convert the voltages into RS232 level, which works on a 5V level. Since this is most PICs or microcontrollers, there is a lot of transceivers that are designed for this purpose. Since this post is designed around the Olimex PIC-P67J60 I will use the transceiver they have used as an example. The Olimex uses the ST3232 chip as an RS232 transceiver.

On the PIC18F67J60 pins 31 and 32 act as pins TXD1 and RXD1 respectively. These pins are also available on port C as RC6 and RC7 respectively, which means they need to be set as TXD1 (transmitter) as an output in your code, and RXD1 as a receiver or input in your code.

The following diagram shows how the ST3232 is then connected from those wires to the RS232 9 way D-Sub.


(This diagram has been taken from the Olimex PIC-P67J60 schematic, and it can be found at the following address: and the datasheet for the ST3232 can be found at

Once this has wired in, you can connect this via a serial cable to your computer, or if your computer is like most computers these days, you can get a USB to Serial convertor and then connect this to your computer. Once you have connected either, make sure you make a note of what COM port it appears at on your computer, because you will need to use that information to communicate between the PIC and your computer later.

Now we look at the code. Much like other codes, you have to import the PIC’s header file, as well as a couple of libraries that allow the user to communicate with a serial interface. Since we use the ‘UART’ interface for serial, we import the USART library, as well as the STDIO and STDLIB as well so we can manipulate strings and other such operations.



You have to set up the configuration for your PIC as well. This is done by setting the following flags. This has been explained in the last post about the PIC. (

#pragma config XINST = OFF       // Extended Instruction Set (Disabled)
#pragma config FOSC = HS
#pragma config WDT = OFF

Now we come to the actual code part. You can start your main () method with any variables you need to declare and other such.

As mentioned earlier, one of the first things you have to do is set up pins RC6 and RC7 as input and output to make them work as Receiver and Transmitter:

TRISCbits.RC7 = 1;
TRISCbits.RC6 = 0;

Next you initialize the USART process. This is normally the tricky bit, and changes here can mean your port may or may not work. We will configure the unit to run on 9600 bps.


So as can be seen, the configuration command is Open1USART( xxx , yy). This operates TX1 and RX1. This is found on this particular chip, but in other chips, the OpenUSART command as well as Open2USART command can be used as well. The datasheet needs to be looked at for this value.

The first set of numbers is setting different flags. The various options can be found in the C18 literature, but the ones used are explained:

USART_TX_INT_OFF: Turn off transmitter interrupts
USART_RX_INT_ON: Turn on receiver interrupts
USART_ASYNCH_MODE: Ignore Synchronous Mode
USART_EIGHT_BIT: Run in 8 bit mode
USART_CONT_RX: Run continuous receive mode

The next two parameters are explained in the PIC18F67J60 datasheet ( and in the current version can be found under section “21.1 Baud Rate Generator”. We will use the low Baud Rate value, by using the term:


And the following charts, once again taken from the datasheet help us choose the SPRG value:


As we are using a 25MHz clock on the Olimex, and want to use a 9600 bps Baud Rate, we will be using the value 40 from the chart above.

Once this command is issued, your system should be able to read and write commands from serial port.

Here is a list of various commands available on the USART library:


A good tutorial on the topic can be found at the following address:

To communicate with your computer, you need a program like HyperTerminal or Putty, where you configure it to run at 9600 bps on the right COM port.

While all the above commands are useful, I found the easiest way to communicate was to use the printf command:

printf(“Hello, world!\n \r”);

This just uses the STDIO library to push the string out on the serial port. Once this works correctly, you should be able to see the words Hello world! appear in whatever serial communication computer you are using.

However, to read the the data in, we need to use the gets1USART command.


Where x is a character array, while 5 is number of characters to read from the serial port.

Once data is input to the array, it is in a string format. Once it is input, it can be turned into an integer by using the atoi command, such as:


where a is an integer.

Another way to write to the output is by using the following set of commands:


Which means you wait for USART to get free, and then push data out byte by byte.

So this post should cover most of the basics for serial communication using a PIC. In case of any problems go through the following list:

Is the wiring correct?
Is the transceiver worked as required?
Are the RX and TX ports configured as input and output in the code?
Are the appropriate libraries imported in your code?
Is the USART port opened with the correct configurations?
Is the right value of BRGH and SPRG set?
Is Putty (or your HyperTerminal) program configured to the right COM port and the right Baud Rate?
Is the code correct?

In case of any questions, look through the datasheet, or drop me a question below.

Related Posts

2 thoughts on “Serial communication–PIC18

  1. I am using PIC18F46K22 micro controller and in my code I have used the function Open2USART(USART_TX_INT_OFF &
    BAUD_16_BIT_RATE &
    USART_ADDEN_OFF,USART2spbrg);. here USART2spbrg=416, so i have configured as 9600 baud rate and 16 bit baud rate generator. with this code I am sending data from a terminal utility but i am not receiving the same data on microcontroller.

Leave a Reply

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