PIC16F877A – USART Tutorial

In this tutorial we are going to learn PIC16F877A Serial Communication (USART). USART (Universal Synchronous Asynchronous Receiver Transmitter) are one of the basic interfaces which provide a cost effective simple and reliable communication between one controller to another controller or between a controller and PC.

Prerequisites

If you are new to UART please go through our previous article about UART.

PIC16F877A Serial Communication Tutorial

PIC16F877A comes with inbuilt USART which can be used for Synchronous/Asynchronous communication. USART is a two wire communication system in which the data flow serially. USART is also a full-duplex communication, means you can send and receive data at the same time which can be used to communicate with peripheral devices, such as CRT terminals and personal computers.

The USART can be configured in the following modes:

  • Asynchronous (full-duplex)
  • Synchronous – Master (half-duplex)
  • Synchronous – Slave (half-duplex)

 But here We will be discussing only the UART (Asynchronous).

Registers used for Serial Communication

  • TXSTA (Transmit Status And Control Register)
  • RCSTA (Receive Status And Control Register)
  • SPBRG (USART Baud Rate Generator)
  • TXREG (USART Transmit Register)
  • RCREG (USART Receiver Register)

TXSTA (Transmit Status And Control Register)

This register is used to configure the Serial communication for TX.

CSRC: Clock Source Select bit (Asynchronous mode:Don’t care).

TX9: 9-bit Transmit Enable bit

1 = Selects 9-bit transmission
0 = Selects 8-bit transmission

TXEN: Transmit Enable bit

1 = Transmit enabled
0 = Transmit disabled

SYNC: USART Mode Select bit

1 = Synchronous mode
0 = Asynchronous mode

BRGH: High Baud Rate Select bit

1 = High speed
0 = Low speed

TRMT: Transmit Shift Register Status bit

1 = TSR empty
0 = TSR full

TX9D: 9th bit of Transmit Data, can be Parity bit

RCSTA (Receive Status And Control Register)

This register is used to configure the Serial communication for RX.

SPEN: Serial Port Enable bit

1 = Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port pins)
0 = Serial port disabled

RX9: 9-bit Receive Enable bit

1 = Selects 9-bit reception
0 = Selects 8-bit reception

SREN: Single Receive Enable bit (Asynchronous mode:Don’t care)

CREN: Continuous Receive Enable bit

Asynchronous mode:
1 = Enables continuous receive
0 = Disables continuous receive

ADDEN: Address Detect Enable bit

Asynchronous mode 9-bit (RX9 = 1):
1 = Enables address detection, enables interrupt and load of the receive buffer when RSR is set
0 = Disables address detection, all bytes are received and ninth bit can be used as parity bit

FERR: Framing Error bit

1 = Framing error (can be updated by reading RCREG register and receive next valid byte)
0 = No framing error

OERR: Overrun Error bit

1 = Overrun error (can be cleared by clearing bit CREN)
0 = No overrun error

RX9D: 9th bit of Received Data (can be parity bit but must be calculated by user firmware)

SPBRG (USART Baud Rate Generator)

The main criteria for UART communication is its baud rate. Both the devices Rx/Tx should be set to same baud rate for successful communication.This can be achieved by SPBRG register. SPBRG is a 8-bit register which controls the baud rate generation.  The SPBRG register controls the period of a free running 8-bit timer. In Asynchronous mode, bit BRGH (TXSTA<2>) also controls the baud rate. In Synchronous mode, bit BRGH is ignored.

Given the desired baud rate and FOSC, the nearest integer value for the SPBRG register can be calculated using the below formula.

Calculation:

My Fosc = 11.0592MHz (You can put your board Fosc)

Baud Rate = 9600

9600 = 11059200 / ( 64X + 64)

64X+64 = 1152

X = 17.

If we want to generate 9600 Baudrate (Fosc = 11.0592MHz) you have to set 17 to SPBRG Register.

TXREG (USART Transmit Register)

This is like a transmit buffer. But it is only 8 bit register. So we have to place the character whatever we want to transmit via USART.

RCREG (USART Receiver Register)

This is like a receiver buffer. But it is only 8 bit register. So we can take the character whatever we microcontroller received via USART.

Programming

Initializing USART

  1. Configure the TXSTA Register for Transmit
  2. Configure the RCSTA Register for Receiver
  3. Feed the value for baudrate that you have calculated using above formula to SPBRG Register

Transmit

  1. Load the new char to be transmitted into THR.
  2. Wait till the char is transmitted. TXIF will be set when the TXREG is empty.
  3. Clear the TXIF for next cycle.

Receive

  1. Wait till the Data is received. RCIF will be set once the data is received in RCREG register.
  2. Clear the receiver flag(RCIF) for next cycle.
  3. Copy/Read the received data from RCREG register.

Full code

This program, first transmit the some string (EmbeTronicX :  Enter the letters in keyboard). Then it will act as a echo. Whatever we pressed in keyboard, it will print that in serial terminal.

Output

pic16f877a serial communication tutorial

0 0 vote
Article Rating
Subscribe
Notify of
guest

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

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Maya
Maya
June 14, 2020 3:57 PM

14th line is TRSC6+TRISC7=1; how it can be both =1? C6 is transmitter pin so it shoud be set as output.,

EmbeTronicX
EmbeTronicX
Reply to  Maya
June 15, 2020 12:13 AM

Hi, It doesn’t matter as we are configuring those pins for USART. But for safer side, we can make this TRISC7 to input. Please find the attached source code. Thanks for commenting 😊.

2
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: