0% found this document useful (0 votes)
52 views36 pages

Microprocessor & Microcontrollers: Serial Communication

The document provides an overview of serial communication basics including parallel vs serial data transmission, synchronous vs asynchronous transmission methods, start and stop bits, baud rates, RS-232 standards, and programming serial communication on the 8051 microcontroller. It discusses how serial data is converted to and from parallel data using shift registers. Key concepts covered include half and full duplex transmission, framing of asynchronous data, and the SBUF and SCON registers used for serial data transfer on the 8051.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views36 pages

Microprocessor & Microcontrollers: Serial Communication

The document provides an overview of serial communication basics including parallel vs serial data transmission, synchronous vs asynchronous transmission methods, start and stop bits, baud rates, RS-232 standards, and programming serial communication on the 8051 microcontroller. It discusses how serial data is converted to and from parallel data using shift registers. Key concepts covered include half and full duplex transmission, framing of asynchronous data, and the SBUF and SCON registers used for serial data transfer on the 8051.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

MICROPROCESSOR &

MICROCONTROLLERS
Chapter 10
Serial Communication

Engr. Rashid Farid Chishti


Faculty of Engineering & Technology
International Islamic University Islamabad
Mobile: +92 321 5300 497
e-mail: [email protected]
11/2/20 www.iiu.edu.pk 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

11/2/20 www.iiu.edu.pk 2
Basics of Serial Communication
 At the transmitting end, the byte of data must be
converted to serial bits using parallel-in-serial-out
shift register
 At the receiving end, there is a serial-in-parallel-out
shift register to receive the serial data and pack them
into byte
 When the distance is short, the digital signal can be
transferred as it is on a simple wire and requires no
modulation
 If data is to be transferred on the telephone line, it
must be converted from 0s and 1s to audio tones
 This conversion is performed by a device called a
modem, “Modulator/demodulator”
11/2/20 www.iiu.edu.pk 3
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
 It is possible to write software to use either of these
methods, but the programs can be tedious and long
 There are special IC chips made by many
manufacturers for serial communications
 UART (universal asynchronous Receiver transmitter)
 USART (universal synchronous-asynchronous Receiver-
transmitter)

11/2/20 www.iiu.edu.pk 4
Half- and Full-Duplex Transmission
 If data can be transmitted and received, it is a duplex
transmission
 If data transmitted one way a time, it is referred to as half
duplex
 If data can go both ways at a time, it is full duplex
 This is contrast to simplex transmission

11/2/20 www.iiu.edu.pk 5
Start and Stop Bits
 A protocol is a set of rules agreed by both the sender and
receiver on
 How the data is packed
 How many bits constitute a character
 When the data begins and ends
 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
11/2/20 www.iiu.edu.pk 6
Start and Stop Bits
 The start bit is always a 0 (low) and the stop bit(s) is 1 (high)

11/2/20 www.iiu.edu.pk 7
Start and Stop Bits
 Due to the extended ASCII characters, 8-bit ASCII
data is common
 In older systems, ASCII characters were 7-bit
 In modern PCs the use of one stop bit is standard
 In older systems, due to the slowness of the
receiving mechanical device, two stop bits were
used to give the device sufficient time to organize
itself before transmission of the next byte
 Assuming that we are transferring a text file of ASCII
characters using 1 stop bit, we have a total of 10 bits
for each character
 This gives 25% overhead, i.e. each 8-bit character
with an extra 2 bits.
11/2/20 www.iiu.edu.pk 8
Data Transfer Rate
 In some systems in order to maintain data integrity,
the parity bit of the character byte is included in the
data frame
 UART chips allow programming of the parity bit
for odd-, even-, and no-parity options.
 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
 It is modem terminology and is defined as the
number of signal changes per second
 In modems, there are occasions when a single
change of signal transfers several bits of data.
11/2/20 www.iiu.edu.pk 9
Data Transfer Rate
 As far as the conductor wire is concerned, the baud
rate and bps are the same, and we use the terms
interchangeably.
 The data transfer rate of given computer system
depends on communication ports incorporated into
that system
 IBM PC/XT could transfer data at the rate of 100 to
9600 bps
 Pentium-based PCs transfer data at rates as high as
56K bps
 In asynchronous serial data communication, the
baud rate is limited to 100K bps

11/2/20 www.iiu.edu.pk 10
RS232 Standard
 An interfacing standard RS232 was set by the
Electronics Industries Association (EIA) in 1960
 The standard was set long before the advent of the
TTL logic family, its input and output voltage levels
are not TTL compatible.
 In RS232, a 1 is represented by -3 ~ -25 V, while a 0
bit is +3 ~ +25 V, making -3 to +3 undefined.
 IBM has introduced the DB-9 version of the serial
I/O standard.

11/2/20 www.iiu.edu.pk 11
RS232 Standard

11/2/20 www.iiu.edu.pk 12
RS232 Standard
 The simplest connection between a PC and
microcontroller/PC requires a minimum of three
pins, TxD, RxD, and ground.
Connecting two PCs together using RS232, without handshaking

11/2/20 www.iiu.edu.pk 13
8051 Connection to RS232
 A line driver such as the MAX232 chip is required to
convert RS232 voltage levels to TTL levels, and vice
versa
 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)
 These pins are TTL compatible; We need a line
driver (voltage converter) to convert the R232’s
signals to TTL voltage levels that will be acceptable
to 8051’s TxD and RxD pins

11/2/20 www.iiu.edu.pk 14
MAX 232

11/2/20 www.iiu.edu.pk 15
MAX 232

11/2/20 www.iiu.edu.pk 16
Serial Communication Programming
 To allow data transfer between the PC and an 8051
system without any error, we must make sure that
the baud rate of 8051 system matches the baud rate
of the PC’s COM port
 Hyper terminal function supports baud rates much
higher than listed below.
110
300
1200
2400
4800
9600
11/2/20 www.iiu.edu.pk 17
Example 10-1: With XTAL = 11.0592 MHz, find the TH1 value needed to
have the following baud rates. (a) 9600 (b) 2400 (c) 1200
Solution:
The machine cycle frequency of 8051 = 11.0592 / 12 = 921.6 kHz,
UART Frequency = 921.6 kHz / 32 = 28,800 Hz

(a) 28,800 / 3 = 9600 where -3 = FD (hex) is loaded into TH1


(b) 28,800 / 12 = 2400 where -12 = F4 (hex) is loaded into TH1
(c) 28,800 / 24 = 1200 where -24 = E8 (hex) is loaded into TH1

Notice that dividing 1/12 of the crystal frequency by 32 is the default


value upon activation of the 8051 RESET pin.

Alternatively for Crystal = 11.0592 MHz, Baud Rate = 1200


Crystal
TH 1  256 
384  BaudRate
11059200
TH 1  256   256  24  232  E 8  24
384  1200
11/2/20 www.iiu.edu.pk 18
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
11/2/20 www.iiu.edu.pk 19
SCON Register
 SCON is an 8-bit register used to program the start bit, stop
bit, and data bits of data framing, among other Things.

11/2/20 www.iiu.edu.pk 20
SCON Register
 SM0, SM1: They determine the framing of data by specifying
the number of bits per character, and the start and stop bits

 SM2: This enables the multiprocessing capability of the 8051


 REN (receive enable): When it is high, it allows 8051 to receive
data on RxD pin, If low, the receiver is disable

11/2/20 www.iiu.edu.pk 21
SCON Register
 TI (transmit interrupt): When 8051 finishes the transfer of 8-bit
character,
 It raises TI flag to indicate that it is ready to transfer another
byte
 TI bit is raised at the beginning of the stop bit
 RI (receive interrupt): When 8051 receives data serially via
RxD, it gets rid of the start and stop bits and places the byte in
SBUF register.
 It raises the RI flag bit to indicate that a byte has been
received and should be picked up before it is lost.
 RI is raised halfway through the stop bit

11/2/20 www.iiu.edu.pk 22
Programming Serial Data Transmission
1. TMOD register is loaded with the value 20H, indicating the
use of timer 1 in modev2 (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 TI=0
6. The character byte to be transferred serially is written into
SBUF register
7. The TI flag bit is monitored with the use of while(!TI) to see if
the character has been transferred completely
8. To transfer the next byte, go to step 5
11/2/20 www.iiu.edu.pk 23
Importance of TI Flag
The steps that 8051 goes through in transmitting a character via TxD
1. The byte character to be transmitted is written into the SBUF
register
2. The start bit is transferred, The 8-bit character is transferred
on bit at a time
3. The stop bit is transferred
 It is during the transfer of the stop bit that 8051 raises the TI
flag, indicating that the last character was transmitted
4. By monitoring the TI flag, we make sure that we are not
overloading the SBUF
 If we write another byte into the SBUF before TI is raised,
the un-transmitted portion of the previous byte will be lost
5. After SBUF is loaded with a new byte, the TI flag bit must be
forced to 0 by CLR TI in order for this new byte to be
transferred
11/2/20 www.iiu.edu.pk 24
Programming Serial Data Receiving
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 using RI=0
6. The RI flag bit is monitored with the use of while(!RI) 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

11/2/20 www.iiu.edu.pk 25
Importance of RI Flag
In receiving bit via its RxD pin, 8051 goes through the
following steps
1. It receives the start bit
 Indicating that the next bit is the first bit of the
character byte it is about to receive
2. The 8-bit character is received one bit at time

3. The stop bit is received


 When receiving the stop bit 8051 makes RI = 1,
indicating that an entire character byte has been
received and must be picked up before it gets
overwritten by an incoming character

11/2/20 www.iiu.edu.pk 26
Importance of RI Flag
4. By checking the RI flag bit when it is raised, we
know that a character has been received and is
sitting in the SBUF register
 We copy the SBUF contents to a safe place in some
other register or memory before it is lost
5. After the SBUF contents are copied into a safe place,
the RI flag bit must be forced to 0 using RI =0 in
order to allow the next received character byte to be
placed in SBUF
 Failure to do this causes loss of the received
character

11/2/20 www.iiu.edu.pk 27
Serial Port Programming in C
Example 10-15: Write a C program for 8051 to transfer the letter “A” serially
at 4800 baud continuously. Use 8-bit data and 1 stop bit.
#include <reg51.h>

void main(void)
{
TMOD=0x20; // use Timer 1, mode 2
TH1=0xFA; // 4800 baud rate
SCON=0x50;
TR1=1;
while (1) {
SBUF='A'; // place value in buffer
while (TI==0);
TI=0;
}
}

11/2/20 www.iiu.edu.pk 28
Serial Port Programming in C
Example 10-16: Write an 8051 C program to transfer the message “YES”
serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.
#include <reg51.h>
void SerTx(unsigned char);
void main(void){
TMOD=0x20; // use Timer 1, mode 2
TH1=0xFD; // 9600 baud rate
SCON=0x50; // Serial mode 1, Receive enable
TR1=1; // start timer
while (1) {
SerTx('Y');
SerTx('E');
SerTx('S');
}
}
void SerTx(unsigned char x){
SBUF=x; // place value in buffer
while (TI==0); // wait until transmitted
TI=0;
}
11/2/20 www.iiu.edu.pk 29
Serial Port Programming in C
Example 10-16: Write an 8051 C program to transfer the message "YES"
serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.
#include <reg51.h>
void send(unsigned char ch []){
unsigned char i;
for(i=0;ch[i]; i++){
SBUF = ch[i]; // place value in buffer
while (TI == 0); // wait until transmitted
TI = 0; // Clear TI Flag
}
}
void main(void){
code unsigned char message[] = "www.iiu.edu.pk ";
TMOD = 0x20; // use Timer 1, mode 2
TH1 = 0xFD; // 9600 baud rate
SCON = 0x50; // Serial Mode 1, Receive enable
TR1 = 1; // start timer
while (1)
send(message);
}
11/2/20 www.iiu.edu.pk 30
Serial Port Programming in C
Example 10-17: Program the 8051 in C to receive bytes of data serially and
put them in P1. Set the baud rate at 4800, 8-bit data, and 1 stop bit.
#include <reg51.h>
void main(void){
unsigned char mybyte;
TMOD=0x20; // use Timer 1, mode 2
TH1=0xFA; // 4800 baud rate
SCON=0x50;
TR1=1; // start timer
while (1){ // repeat forever
while ( RI==0 ); // wait to receive
mybyte = SBUF; // save value
RI = 0;
P1 = mybyte; // write value to port
}
}

11/2/20 www.iiu.edu.pk 31
Doubling Baud Rate
 There are two ways to increase the baud rate of data
transfer
 To use a higher frequency crystal
 To change 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.

11/2/20 www.iiu.edu.pk 32
Doubling Baud Rate

11/2/20 www.iiu.edu.pk 33
Example 10-19Write an 8051 C Program to send the two messages “Normal
Speed”and “High Speed” to the serial port. Assuming that SW is connected
to pin P2.0, monitor its status and set the baud rate as follows:
SW = 0, 28,800 baud rate
SW = 1, 56K baud rate Assume that XTAL = 11.0592 MHz for both cases.
#include <reg51.h>
sbit MYSW=P2^0; // input switch
void main(void){
unsigned char z;
code unsigned char msg1[]= "Normal Speed";
code unsigned char msg2[]= "High Speed";
MYSW = 1; // make as an input
TMOD=0x20; // use Timer 1, mode 2
TH1=0xFF; // 28800 for normal
SCON = 0x50;
TR1=1; // start timer
while(1){
if(MYSW==0){
PCON=PCON & 0x7F;
for (z=0;msg1[z];z++) {
11/2/20 SBUF=msg1[z]; // place value in buffer
www.iiu.edu.pk 34

while(TI==0); // wait for transmit


TI=0;
Serial Port Programming in C
}
}
else{
PCON=PCON|0x80; // for high speed of 56K
for (z=0;msg2[z];z++) {
SBUF = msg2[z]; // place value in buffer
while(TI==0); // wait for transmit
TI=0;
}
}
}
}

11/2/20 www.iiu.edu.pk 35
THE END OF
CHAPTER 10

11/2/20 www.iiu.edu.pk 36

You might also like