8051 Serial
Communication
Compiled by
Savita C. Raut
8051 Microcontroller SCR_KJSCE 1
Basics of Serial Communication
• Computers transfer data in two ways:
• Parallel: Often 8 or more lines (wire conductors) are used to
transfer data to a device that is only a few feet away.
• Serial: To transfer to a device located many meters away, the
serial method is used. The data is sent one bit at a time.
8051 Microcontroller SCR_KJSCE 2
Basics of Serial Communication
• Serial data communication uses two methods
• Synchronous method transfers a block of data at a time
• Asynchronous method transfers a single byte at a time
• There are special IC’s made by many manufacturers for
serial communications.
• UART (universal asynchronous Receiver transmitter)
• USART (universal synchronous-asynchronous Receiver-
transmitter)
8051 Microcontroller SCR_KJSCE 3
Asynchronous – Start & Stop Bit
• Asynchronous serial data communication is widely used
for character-oriented transmissions
• Each character is placed in between start and stop bits, this is
called framing.
• Block-oriented data transfers use the synchronous method.
• The start bit is always one bit, but the stop bit can be
one or two bits
• The start bit is always a 0 (low) and the stop bit(s) is 1
(high)
8051 Microcontroller SCR_KJSCE 4
Asynchronous – Start & Stop Bit
8051 Microcontroller SCR_KJSCE 5
Data Transfer Rate
• The rate of data transfer in serial data communication is stated in
bps (bits per second).
• Another widely used terminology for bps is baud rate.(Baud rate is
a MODEM terminology .baud rate and bps are not necessarily
equal)
• It is defined as time required to send one bit on transmission
line in other words it is the number of signal changes per
second.
• In modems, there are occasions when a single change of signal
transfers several bits of data
• As far as the conductor wire is concerned, the baud rate and bps
are the same.
8051 Microcontroller SCR_KJSCE 6
Data Transfer Rate..cntd
• The data transfer rate of a given computer system depends on the
communication ports incorporated into that system. For ex, the
early IBM PC/XT used data transfer rate 100 to 9600 bps.
• However Pentium based PCs transfers the data at high rates upto
56K bps
• In Asynchronous serial data communication , the Baud rate is
limited to 100,000 bps.
8051 Microcontroller SCR_KJSCE 7
8051 Serial Port
• Synchronous and Asynchronous methods
• SCON Register is used to Control
• Data Transfer through TXd (P3.1)& RXd (P3.0)pins
• Some time - Clock through TXd Pin
• Four Modes of Operation:
Mode 0 :Synchronous Serial Communication
Mode 1 :8-Bit UART with Timer Data Rate
Mode 2 :9-Bit UART with Set Data Rate
Mode 3 :9-Bit UART with Timer Data Rate
8051 Microcontroller SCR_KJSCE 8
Registers related to
Serial Communication
1. SBUF Register
2. SCON Register
3. PCON Register
8051 Microcontroller SCR_KJSCE 9
SBUF Register
• SBUF is an 8-bit register used solely for serial communication.
• For a byte data to be transferred via the TxD line, it must be
placed in the SBUF register.
• The moment a byte is written into SBUF, it is framed with the
start and stop bits and transferred serially via the TxD line.
• SBUF holds the byte of data when it is received by 8051 RxD line.
• When the bits are received serially via RxD, the 8051 deframes it
by eliminating the stop and start bits, making a byte out of the
data received, and then placing it in SBUF.
8051 Microcontroller SCR_KJSCE 10
SBUF Register
8051 Microcontroller SCR_KJSCE 11
SBUF Register …cntd
8051 Microcontroller SCR_KJSCE 12
SBUF Register
• Sample Program:
8051 Microcontroller SCR_KJSCE 13
SCON Register
SM0 SM1 SM2 REN TB8 RB8 TI RI
Set when a Cha-
ractor received
Set to Enable
Serial Data
reception Set when Stop bit Txed
Enable Multiprocessor 9th Data Bit 9th Data Bit
Communication Mode Sent in Mode 2,3 Received in Mode 2,3
8051 Microcontroller SCR_KJSCE 14
8051 Serial Port – Mode 0
The Serial Port in Mode-0 has the following features:
1. Serial data enters and exits through RXD
2. TXD outputs the clock
3. 8 bits are transmitted / received
4. The baud rate is fixed at (1/12) of the oscillator frequency
8051 Microcontroller SCR_KJSCE 15
8051 Serial Port – Mode 1
The Serial Port in Mode-1 has the following features:
1. Serial data enters through RXD
2. Serial data exits through TXD
3. On receive, the stop bit goes into RB8 in SCON
4. 10 bits are transmitted / received
1. Start bit (0)
2. Data bits (8)
3. Stop Bit (1)
5. Baud rate is determined by the Timer 1 over flow rate.
•CNT =(-(2SMOD *FCLK)/(32*BR*12))
8051 Microcontroller SCR_KJSCE 16
8051 Serial Port – Mode 2
The Serial Port in Mode-2 has the following features:
1. Serial data enters through RXD
2. Serial data exits through TXD
3. 9th data bit (TB8) can be assign value 0 or 1
4. On receive, the 9th data bit goes into RB8 in SCON
5. 11 bits are transmitted / received
[Link] bit (0)
[Link] bits (9)
[Link] Bit (1)
6. Baud rate is programmable
8051 Microcontroller SCR_KJSCE 17
8051 Serial Port – Mode 3
The Serial Port in Mode-3 has the following features:
1. Serial data enters through RXD
2. Serial data exits through TXD
3. 9th data bit (TB8) can be assign value 0 or 1
4. On receive, the 9th data bit goes into RB8 in SCON
5. 11 bits are transmitted / received
[Link] bit (0)
[Link] bits (9)
[Link] Bit (1)
6. Baud rate is determined by Timer 1 overflow rate.
8051 Microcontroller SCR_KJSCE 18
Serial mode 1 is widely used
• Other 3 modes are rarely used today.
• When serial mode1 is chosen , the data framing is 8-
bits,1 stop bit and 1 start bit,which makes it
compatible with the COM port of PCs.
• More important, serial mode 1 allows the baud rate to
be variable and it is set by Timer1 of 8051.
8051 Microcontroller SCR_KJSCE 19
Programming Serial Data Transmission
1. TMOD register is loaded with the value 20H, indicating the use of timer
1 in mode 2 (8-bit auto-reload) to set baud rate.
2. The TH1 is loaded with one of the values to set baud rate for serial data
transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode
1, where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF
register.
7. The TI flag bit is monitored with the use of instruction JNB TI, xx to see
if the character has been transferred completely.
8. To transfer the next byte, go to step 5
8051 Microcontroller SCR_KJSCE 20
Steps involved in Serial Data
Transmission
8051 Microcontroller SCR_KJSCE 21
8051 Microcontroller SCR_KJSCE 22
Programming Serial Data Reception
1. TMOD register is loaded with the value 20H, indicating the use of timer 1
in mode 2 (8-bit auto-reload) to set baud rate.
2. TH1 is loaded to set baud rate
3. The SCON register is loaded with the value 50H, indicating serial mode 1,
where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. RI is cleared by CLR RI instruction
6. The RI flag bit is monitored with the use of instruction JNB RI, xx to see if
an entire character has been received yet
7. When RI is raised, SBUF has the byte, its contents are moved into a safe
place.
8. To receive the next character, go to step 5.
8051 Microcontroller SCR_KJSCE 23
Steps involved in Serial Data Reception
8051 Microcontroller SCR_KJSCE 24
8051 Microcontroller SCR_KJSCE 25
8051 Microcontroller SCR_KJSCE 26
8051 Microcontroller SCR_KJSCE 27
TH1 register values for various Baud
rate
8051 Microcontroller SCR_KJSCE 28
Doubling Baud Rate
• There are two ways to increase the baud rate of data
transfer
1. By using a higher frequency crystal
2. By changing a bit in the PCON register
• PCON register is an 8-bit register.
• When 8051 is powered up, SMOD is zero
• We can set it to high by software and thereby double the baud rate.
8051 Microcontroller SCR_KJSCE 29
Doubling Baud Rate (cont…)
8051 Microcontroller SCR_KJSCE 30
Programming-Serial Communication
8051 Microcontroller SCR_KJSCE 31
Programming- serial communication
8051 Microcontroller SCR_KJSCE 32
Programming -serial communication
8051 Microcontroller SCR_KJSCE 33
Program to transfer serially at the baud rate of 4800
8051 Microcontroller SCR_KJSCE 34
Program to receive serially at the baud rate of 4800
8051 Microcontroller SCR_KJSCE 35
Programming- serial communication
8051 Microcontroller SCR_KJSCE 36
8051 Microcontroller SCR_KJSCE 37
DB-9 pin Connector
8051 Microcontroller SCR_KJSCE 38
Data Communication Classification
8051 Microcontroller SCR_KJSCE 39
Handshaking signals in RS232
• To ensure fast and reliable data transmission between
two devices, the data transfer must be coordinated.
• Just as in the case of printer,since the receiving device
in serial communication may not have a room for the
data ,there must be some way to inform the sender to
stop sending the data.
8051 Microcontroller SCR_KJSCE 40
Handshaking signals in
RS232..cntd
8051 Microcontroller SCR_KJSCE 41
Handshaking signals in
RS232..cntd
8051 Microcontroller SCR_KJSCE 42
8051 connection to RS232
• The RS232 standard is not TTL compatible; therefore, it
requires a line driver such as the MAX232 chip to
convert RS232 voltage levels to TTL levels, and vice
versa. The interfacing of 8051 with RS232 connectors
via the MAX232 chip is done to connect RS 232 to 8051.
• RxD and TxD pins in the 8051
The 8051 has two pins that are used specifically for
transferring and receiving data serially. These two pins
are called TxD and RxD and are part of the port 3 group
(P3.0 and P3.1). Pin 11 of the 8051 (P3.1) is assigned to
TxD and pin 10 (P3.0) is designated as RxD. These pins
are TTL compatible; therefore, they require a line driver
to make them RS232 compatible. One such line driver is
the MAX232 chip.
8051 Microcontroller SCR_KJSCE 43
8051 connection to RS232…cntd
• MAX232
Since the RS232 is not compatible with today’s
microprocessors and microcontrollers, we need a line
driver (voltage converter) to convert the RS232′s signals
to TTL voltage levels that will be acceptable to the 8051
‘s TxD and RxD pins. One example of such a converter is
MAX232 from Maxim Corp. ([Link]). The
MAX232 converts from RS232 voltage levels to TTL
voltage levels, and vice versa. One advantage of the
MAX232 chip is that it uses a +5 V power source which,
is the same as the source voltage for the 8051. In other
words, with a single +5 V power supply we can power
both the 8051 and MAX232, with no need for the dual
power supplies that are common in many older systems.
8051 Microcontroller SCR_KJSCE 44
8051 connection to RS232…cntd
• The MAX232 has two sets of line drivers for transferring and receiving data, as shown
in Figure below:
• The line drivers used for TxD are called T1 and T2,
• Figure :(a) Inside MAX232 and (b) its Connection to the 8051 (Null Modem)
8051 Microcontroller SCR_KJSCE 45
8051 connection to RS232…cntd
• The line drivers used for TxD are called T1 and T2, while the line drivers for
RxD are designated as Rl and R2. In many applications only one of each is
used. For example, Tl and Rl are used together for TxD and RxD of the 8051,
and the second set is left unused. Notice in MAX232 that the Tl line driver has
a designation of Tlin and Tlout on pin numbers 11 and 14, respectively. The
Tlin pin is the TTL side and is connected to TxD of the microcontroller, while
Tlout is the RS232 side that is connected to the RxD pin of the RS232 DB
connector. The Rl line driver has a designation of Rlin and Rlout on pin
numbers 13 and 12, respectively. The Rlin (pin 13) is the RS232 side that is
connected to the TxD pin of the RS232 DB connector, and Rlout (pin 12) is the
TTL side that is connected to the RxD pin of the microcontroller.
• Figure :(a) Inside MAX232 and (b) its Connection to the 8051 (Null Modem)
8051 Microcontroller SCR_KJSCE 46
8051 connection to RS232..cntd
• To save board space, some designers use the MAX233 chip
from Maxim. The MAX233 performs the same job as the
MAX232 but eliminates the need for capacitors.
• However, the MAX233 chip is much more expensive than the
MAX232. Notice that MAX233 and MAX232 are not pin
compatible. You cannot take a MAX232 out of a board and
replace it with a MAX233. See Figure 10-8 for MAX233 with no
capacitor used.
8051 Microcontroller SCR_KJSCE 47