A universal asynchronous receiver-transmitter is a hardware module for asynchronous serial communication in which the data format and transmission speeds are configurable. Serial communication using a Microcontroller is one of the easiest operations to learn on a microcontroller and it comes into use in almost every application. In this tutorial, we learn to Enable the inbuilt hardware module for UART in PIC16F877A Microcontroller and how to communicate with your Computer using the UART protocol.
For this, we will use an IC called MAX232. which is a level translator that helps the PIC Microcontroller to connect with a PC using RS-232 telecommunications standard.
This can be achieved using a DB9 connector. If your PC doesn’t have a DB9 port, you might want to use an RS232 to USB converter. Also, you may directly use a TTL to USB converter.
TTL – RS232 or TTL USB converter modules can be easily purchased. But if you want to build your own, use the below circuits.
This tutorial will focus on receiving a character typed on PC and then transmitting that back to PC to display on the screen. we will use the serial terminal, ‘RealTerm’ for transmitting and receiving data from the computer side.
Learn more about RealTerm here.
Make sure you chose the right port and Baud rate in Realterm. We will be configuring our UART to work on a Baud rate of 9600. So the same value should be set in RealTerm.
Before moving on to program lets understand the registers associated with PIC16F877A UART.
See the table below.
Here, TXSTA and RCSTA are the control registers. So now, let’s see how these registers can be configured.
The main criterion for UART communication is its baud rate. Both the devices Rx/Tx should be set to the same baud rate for successful communication.
This can be achieved by SPBRG register. SPBRG is an 8-bit register that controls the baud rate generation.
Given the desired baud rate and FOSC, the nearest integer value for the SPBRG register can be calculated using the below formula.
First, the xc.h header should be used to access compiler and device-specific features. I have also included the file containing the configuration bits; ‘Config.h’. You can get detailed instructions on setting up configuration bits here.
#include <xc.h> #include "Config.h"
In the main function, we will do all the required configuration for the UART module.
void main() { char a; TRISC = 0X80; TXSTA = 0X24; RCSTA = 0X90; SPBRG = (20000000UL / (long)(16UL * 9600)) - 1; while(1) { a = rec(a[i]); trans(a[i]); } }
To transmit the character, first, the character should be loaded into the TXREG register. After this, we will wait till the TXIF bit is set to ensure the transaction is complete.
Before leaving the function, TXIF is made 0 again.
void trans(char a) { TXREG=a; while(TXIF==0); TXIF=0; }
To ensure the data is completely received, we will wait till RCIF bit is set. After that, the data is taken from the RCREG register.
After complete reception, RCIF should also be made to 0.
void rec(char a) { while(RCIF==0) RCIF=0; return RCREG; }
Now let’s see an example to send a complete string to the PC using UART.