Professional Documents
Culture Documents
PICmicrocontrollerserialcommunicationtutorial
Search
PIC Microcontroller
Development tools
Home
PIC microcontroller kits
USB PIC microcontroller kits
PIC programming books
Programming PIC in C
Contact us
Blog
PICserialcommunicationtutorial
Introduction to Serial communication with PIC16F877 microcontroller
CLICK here for a quick PIC serial communication tutorial
In this tutorial we will study the communication component USART (Universal Synchronous
Asynchronous Receiver Transmitter) located within the PIC. It is a universal communication
component (Synchronous/Asynchronous), which can be used as transmitter or as receiver. We will
look at:
serial and parallel communications
synchronous and asynchronous communications
how to enable serial communication - TXSTA and RCSTA registers
PIC Microcontroller
Tutorials
Max323 Driver/Receiver
HI-TECH compiler
Microcontroller Board
We will show how to set USART in order to allow communication between PIC to PIC or between PIC
to a personal computer. We will start with the definition of media concepts. There are two options
PIC Debuger
PIC Timer Modules
In order to understand what serial communication is, and emphasize the difference between serial
We have a multi-bit word, and we want to transmit it from one computer to the second computer.
Serial communication /
USART
PIC Interrupts
communication and parallel communication, lets take a look at the following example:
PIC Microcontroller
Projects
Creating new project
PIC Fire Detector project
General
Liquid Crystal Display - LCD
Contact Us
http://www.microcontrollerboard.com/pic_serial_communication.html#SerandParComm
1/8
9/19/2015
PICmicrocontrollerserialcommunicationtutorial
In addition to the serial and parallel communications, there are 2 types of communication we will
explore:
Synchronous communication
Asynchronous communication
Synchronous communication
When using the synchronous communication the information is transmitted from the transmitter
to the receiver:
in sequence
bit after bit
with fixed baud rate
and the clock frequency is transmitted along with the bits
That means that the transmitter and the receiver are synchronized between them by the same
clock frequency. The clock frequency can be transmitted along with the information, while it is
encoded in the information itself, or in many cases there is an additional wire for the clock.
This type of communication is faster compare to the asynchronous communication since it is
"constantly transmitting the information, with no stops.
Asynchronous communication
When using the asynchronous communication - the transmitter and the receiver refraining to
transmit long sequences of bits because there isn't a full synchronization between the transmitter,
that sends the data, and the receiver, that receives the data.
In this case, the information is divided into frames, in the size of byte. Each one of the frame has:
Start bit marks the beginning of a new frame.
Stop bit marks the end of the frame.
Frames of information must not necessarily be transmitted at equal time space, since they are
independent of the clock.
http://www.microcontrollerboard.com/pic_serial_communication.html#SerandParComm
2/8
9/19/2015
PICmicrocontrollerserialcommunicationtutorial
Now, lets define the receiver side to receive 8 bit information. To do so, the register RX9 will be
zero (RX9=0). The received information will be stored in the RSR register, which acts as a
temporary buffer storage.
The received information will look like this:
http://www.microcontrollerboard.com/pic_serial_communication.html#SerandParComm
3/8
9/19/2015
PICmicrocontrollerserialcommunicationtutorial
Now, lets define the receiver side to receive 9 bit information. To do so, the register RX9 will be
set (RX9=1). The received, lower 8-bit information, will be stored in the RSR register, which acts as
a temporary buffer storage. The higher bit information (MSB) will be stored in RX9D.
The received information will look like this:
Now lets continue the explanation. Each transmission is transmitted in the particular rate (BAUD).
The baud rate is measured in units of bps (bit per second) or kbps (kilo bit per second ).
The following outlines how the value which is placed in the SPBRG register is being computed, in
the case of a high baud rate and low baud rate.
For example:
We want to calculate the hex value that will be placed the register SPBRG, to get the baud rate of
1.2kbps with low speed. The formula SPBRG = (Fosc / (64 x Baud rate)) - 1 was chosen since, its
describing the calculation needed for transmission in Low Speed:
SPBRG = (4MHz / (64x1200)) -1 = 51.08
Because it is not possible to write a number with a decimal point to the register, we take only the
whole part of the number and place inside the register SPBRG = 51.
The following tables are the BAUD RATES FOR ASYNCHRONOUS MODE BRGH=0 and BRGH=1.
BRGH=0
BRGH=1
http://www.microcontrollerboard.com/pic_serial_communication.html#SerandParComm
4/8
9/19/2015
PICmicrocontrollerserialcommunicationtutorial
The information we want to transmit is loaded into the 8-bit register - TXREG. If you want
to transmit a 9-bit data, the 9th bit is loaded into TX9D. At the same time, the information
above is being loaded into the register TSR, which is used as a temporary buffer before that
information is transmitted.
Of course, using 2 registers allows faster the transmission of the data. Once the TXREG register
transfers the data to the TSR register, the TXREG register is empty and flag bit, TXIF is set.
As mentioned earlier:
the register SPBRG sets the baud rate in the desired transmission
TXIE allows interrupts when TXREG is empty and TXIF is set
TXEN - Enabling SPBRG
http://www.microcontrollerboard.com/pic_serial_communication.html#SerandParComm
5/8
9/19/2015
PICmicrocontrollerserialcommunicationtutorial
The information is received in the register RSR. If there is a 9-bit transmission, the 9th bit goes
into RX9D. After receiving the data in the register RSR, the information is loaded at the same time
into the register RCREG. Obviously, using 2 registers allows faster receiving of the data. While the
information that was received being transferred into RCREG, the new information has already been
received into the register RSR. Of course, the CREN bit needs to be set.
According to the USART TRANSMIT / RECEIVE BLOCK DIAGRAM, that the information that was
transmitted via pin RC6 in Port C, is received through the pin RC7 in Port C
http://www.microcontrollerboard.com/pic_serial_communication.html#SerandParComm
6/8
9/19/2015
PICmicrocontrollerserialcommunicationtutorial
Basic connections
http://www.microcontrollerboard.com/pic_serial_communication.html#SerandParComm
7/8
9/19/2015
PICmicrocontrollerserialcommunicationtutorial
Sharethispage:
What'sThis?
Buzz
StumbleUpon
Technorati
del.icio.us
Myspace
Contact Page
http://www.microcontrollerboard.com/pic_serial_communication.html#SerandParComm
8/8