UART Example with PIC microcontroller | MPLAB Projects

This post shows how to use PIC16F887 microcontroller USART module in order to receive and send data from and to PC serial monitor software (hyper terminal, Arduino IDE serial monitor tool, mikroElektronika USART Terminal …) or any device that uses UART communication.
USART: Universal Synchronous/Asynchronous Receiver/Transmitter.
UART: Universal Asynchronous Receiver/Transmitter
The compiler used in this project is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).

The PIC16F887 microcontroller has one USART module, this module can be used as USART or UART depending on the configuration of some registers related with the module.
In this example I’m going to use the module in UART mode.

Hardware Required:

  • PIC16F887 microcontroller   —->  datasheet
  • USB-to-serial converter (such as FT232RL)
  • 5V Power source
  • Protoboard
  • Jumper wires

UART Example with PIC microcontroller circuit:
the connection between PIC16F887 microcontroller and laptop (or PC) is shown below. The FT232RL module is directly connected to laptop USB port with mini USB cable.

PIC16F887 UART withe FT232RL USB to serial converter - MPLAB XC8 UART

(All grounded terminals are connected together)

To be able to send/receive data from/to the microcontroller to/from the laptop we need a USB-to-Serial converter module. In this example I used the FT232RL module (the main component is FT232RL chip from FTDI), other modules can be used including the Arduino boards.

The GND pin of the USB-to-Serial converter is connected to circuit ground, pins RX and TX are respectively connected to TX (#25) and RX (#26) pins of the PIC16F887 microcontroller.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

UART Example with PIC microcontroller C code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05.

Functions used in the C code:
First, UART registers used are:
TXSTA: RANSMIT STATUS AND CONTROL REGISTER
RCSTA: RECEIVE STATUS AND CONTROL REGISTER
SPBRG: SERIAL PORT BAUD RATE GENERATOR
RCREG: EUSART RECEIVE DATA REGISTER
TXREG: EUSART TRANSMIT DATA REGISTER   —-> EUSART: Enhanced USART

void UART_Init(const uint32_t baud_rate): initializes hardware UART module with a baud rate of baud_rate.

__bit UART_Data_Ready(): this function returns 1 if there is data (in the receiver buffer) ready for reading, returns 0 if there is no data.

uint8_t UART_GetC(): returns the data which is available in the receiver buffer.

void UART_PutC(const char data): this function transmits a character (byte) data via the UART module.

void UART_Print(const char *data): transmits a text *data via the UART module. This function is based on the previous function UART_PutC.

The microcontroller used in this example is PIC16F887, configuration words are:

Where:

  • In-Circuit Debugger disabled
  • Low voltage programming disabled
  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • Brown-out Reset (BOR) disabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
  • Flash Program Memory Self Write disabled
  • Brown-out Reset set to 4.0V

Full MPLAB XC8 code:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.