Serial communication of microcontroller 8051

nbahadure 883 views 73 slides Jul 26, 2021
Slide 1
Slide 1 of 73
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73

About This Presentation

Total slides: 73
Universal Asynchronous Receiver Transmitter (UART)
Introduction to Serial Communication
Types of Transmission
Simplex Communication
Duplex Communication
Half Duplex Communication
Full Duplex Communication
Methods of Serial data Transmission
Synchronous serial data transfer
Asynchron...


Slide Content

Serial Communication of 8051 Microcontroller
Dr. Nilesh Bhaskarrao Bahadure
https://www.sites.google.com/site/nileshbbahadure/home
July 25, 2021
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 1 / 73

Overview
1
Universal Asynchronous Receiver Transmitter (UART)
2
Introduction to Serial Communication
3
Types of Transmission
Simplex Communication
Duplex Communication
Half Duplex Communication
Full Duplex Communication
4
Methods of Serial data Transmission
Synchronous serial data transfer
Asynchronous serial data transfer
Dierences Synchronous Asynchronous
5
Data Transfer Rate
6
Calculation of Baud Rate
7
SCON Register
Importance of TI Flag Bit
Importance of RI Flag Bit
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 2 / 73

Overview
8
SBUF Register
9
Writing to the Serial port
10
Reading the Serial port
11
PCON Register
12
Programming of transmission byte serially
13
Programming of reception of byte serially
14
Examples
Example -1: Transmission of N
Example -2: Transmission of N
Example -3: Receive byte serially
Example -4: Transmission of multiple byte serially
Example -5: Transmission & Reception byte serially
Example - 6: Switch base Yes - No
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 3 / 73

Universal Asynchronous Receiver Transmitter (UART)
UARTs are used for serial communication between systems; they can be
either half duplex (send or receive but at dierent intervals) or full duplex
(send and receive at the same time). Also known as an RS232 connection
the microcontroller UART can provide the connection with a PC or another
Microcontroller-based system. Figure 1 illustrates possible connection ar-
rangements. In a minimum connection there could be only two transmission
lines, transmit (Tx) and receive (Rx) as shown in Figure 2. The data is con-
veyed as a bit stream, either transmit or receive, and the speed is dened
by the baud rate i.e. the bits per second.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 4 / 73

Universal Asynchronous Receiver Transmitter (UART)...
Figure :
microcontrollers
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 5 / 73

Universal Asynchronous Receiver Transmitter (UART)...
Figure :
Microcontroller
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 6 / 73

Universal Asynchronous Receiver Transmitter (UART)...
The UART has four modes of operation, 0 to 3. Modes 0 and 2 have xed
baud rates, mode 0 is one-sixth of the oscillator frequency, and mode 2 is
1/16 or 1/32 of the oscillator frequency. For modes 1 and 3 the baud rate
can be selected, a typical range is:
75; 150; 300; 600; 1200; 2400; 4800; 9600; 19200; 38400Modes 0 and 1 are used for connection between two devices. Modes 2 and 3
are used for master slave multiprocessor systems, in principle there could be
one master microcontroller and up to 255 slave microcontrollers. In mode
1 ten bits are used to specify an RS232 frame consisting of 1 start bit (logic
0), 8 data bits and 1 stop bit (logic 1). For example the ASCII bit pattern
0100 0001 (hex 41) represents the character 'A' and is transmitted as shown
in Figure 3; least signicant bit (LSB) rst.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 7 / 73

Universal Asynchronous Receiver Transmitter (UART)...
Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 8 / 73

Introduction to Serial Communication
One of the 8051s most powerful features is its integrated UART, otherwise
known as a serial port. The fact that the 8051 has an integrated serial port
means that you may very easily read and write values to the serial port.
If it were not for the integrated serial port, writing a byte to a serial line
would be a rather complicated process requiring turning ON and OFF one
of the I/O lines in rapid succession to properly \clock out" each individual
bit, including start bits, stop bits, and parity bits.
Actually, if the serial communication port is not integrated within the chip
then proper synchronization between the transmitter and the receiver is
very complicated process. However, we do not have to do this. Instead,
we simply need to congure the serial ports operation mode and baud rate.
This conguration can be done by writing very simple code, may requires
only 5 to 8 lines of code. Once congured, all we have to do is write to
an SFR to write a value to the serial port or read the same SFR to read a
value from the serial port.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 9 / 73

Types of Transmission
Serial communication is capable of processing or transmitting of information
by 2 types they are Simplex communication and Duplex communication.
Further the duplex communication is also of 2 types, they are, half duplex
communication and full duplex communication.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 10 / 73

Simplex Communication
In the simplex communication, the serial data is transmitted only in one
direction, i.e. from transmitter to receiver.
The simplex is a one way transmission, data is transferred only in one di-
rection over a single communication channel. For example Microprocessor
transmit data to the Printer, Microprocessor transmit data to the CRT dis-
play unit.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 11 / 73

Simplex Communication...
Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 12 / 73

Duplex Communication
The Duplex is a two way transmission; data may be transferred between
two transceivers in both directions simultaneously. So it will contain one
way transmission or two way transmission at a time. For example Telephone
line.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 13 / 73

Half Duplex Communication
The Half Duplex is a two way transmission but in such a way that data may
travel in only one direction at a time. For example Wacky Talky (Wireless
System).
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 14 / 73

Full Duplex Communication
The full duplex is a two way transmission (communication) but in such a
way that the data may travel in both the direction at the same time. For
example Telephone line, mobile communication etc.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 15 / 73

Duplex Communication...
Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 16 / 73

Methods of Serial data Transmission / Format of serial
data transmission
In modern data communication system for the data transfer two types of
format are used Synchronous and Asynchronous.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 17 / 73

Synchronous serial data transfer
In this method block of data bytes are transferred serially at a time. The
numbers of data bytes are not limited. The data bytes are transferred one
after the other and so on up to the end. In this method at the starting of
data transfer, rst transfer synchronous characters one synchronous char-
acter is compulsory for the data transfer and 2nd is optional. Synchronous
characters are inform to the receiver will start the transferring of data i.e.
its provide synchronization between transmitter and receiver. In one syn-
chronous character 256 receivers are connected i.e. from 00 to FFh. As
the synchronization is done only once and before the start of data transfer,
the synchronous communication is faster compared to asynchronous com-
munication. So it is generally used for fast data transfer at high rate. The
synchronous data format with two synchronous characters is shown in the
gure 6
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 18 / 73

Synchronous serial data transfer
Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 19 / 73

Asynchronous serial data transfer
In this method block of data bytes are transferred serially at a time. The
numbers of data bytes are not limited. The data bytes are transferred one
after the other and so on up to the end. In this method at the starting of
data transfer, rst transfer synchronous characters one synchronous char-
acter is compulsory for the data transfer and 2nd is optional. Synchronous
characters are inform to the receiver will start the transferring of data i.e.
its provide synchronization between transmitter and receiver. In one syn-
chronous character 256 receivers are connected i.e. from 00 to FFh. As
the synchronization is done only once and before the start of data transfer,
the synchronous communication is faster compared to asynchronous com-
munication. So it is generally used for fast data transfer at high rate. The
synchronous data format with two synchronous characters is shown in the
gure 7
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 20 / 73

Asynchronous serial data transfer
Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 21 / 73

Dierences Between Synchronous & Asynchronous Data
Transfer
Synchronous data transfer Asynchronous data transfer
It is used to transfer a group of
Characters at a time.
It is used to transfer one char-
acter at a time
Used for high data transfer
rates 20 K bits / second
Used for data transfer rates 20
K bits / second
Synchronous character are
transmitted along with the
group of characters
Synchronous characters are not
transmitted along with the
characters
No start and stop bits for each
character is used
Start and stop bit for each char-
acter is present which forms a
frame
Table :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 22 / 73

Dierences Between Synchronous & Asynchronous Data
Transfer...
Synchronous data transfer Asynchronous data transfer
One clock is used for both
transmitter and receiver
Two separate clock inputs can
be used for transmitter and re-
ceiver
Since synchronization is in-
volved, this can be imple-
mented by using hardware only
No synchronization is required
hence hardware and software
implementation is possible.
Table :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 23 / 73

Data Transfer Rate / Baud Rate
Data transfer rate is dened as the number of bits are transmitted or re-
ceived in one second. Usually it is referred as bits per second (BPS) and
sometimes it is also called baud rate (BR). Transmission of the serial data
requires conguring baud rate, actually at the baud rate it was denes that
how many bits to be transferred in one second. It is not possible to con-
gure data transfer serially without conguring baud rate. It is important
to note that, it is not necessary that bits per second and baud rate are
same. Because the baud rate is used in reference to MODEM (Modulator
- Demodulator), where number of signal changes indicate bits transmission
and in single signal change may indicate more data bits are transmitted,
whereas if conductor based transmission is concerned then bits per second
and baud rate are same.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 24 / 73

Data Transfer Rate / Baud Rate...
Baud rate in 8051 Microcontroller is programmable, means that dierent
baud rates can be easily achieved without any hardware conguration or
modications; this is simply done by writing a set of instructions. The
8051 Microcontroller divides crystal frequency by 12 to get the machine
cycle frequency, in other words we say that internal frequency is the 1/12th
of crystal frequency (External frequency). Serial communication UART
circuitry once again divides this frequency i.e. machine cycle frequency by
32 or 16, depending on the status of SMOD bit in the PCON register to use
by timer 1 to generate baud rate. in other words we may say that on chip
UART circuitry uses the frequency after diving the machine cycle frequency
by 32 or 16 for calculation of baud rate.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 25 / 73

Data Transfer Rate / Baud Rate...
Figure :Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 26 / 73

Data Transfer Rate / Baud Rate...
To set baud rate compatible with IBM PCs timer 1 must be operated in
auto reload mode. The reason behind to use the auto reload mode is that,
it is possible to get maximum overows (ticks) at very high rate only in
auto reload mode.
The values to be loaded in TH1 register and their corresponding baud rate
compatible with IBM PCs are shown in the table 3
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 27 / 73

Data Transfer Rate / Baud Rate...
TH1 (Decimal) TH1 (Hex) Baud Rate
-3FDh9600-6FAh4800-12F4h2400-24E8h1200-48D0h600
Table :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 28 / 73

Calculation of Baud Rate
In mode 1 and 3, the baud rate is determined by how frequently timer 1
overows. The more frequently timer 1 overows, the higher the baud rate
i.e. how fast the timer ag bit overows that decides baud rate. In other
words we can say that baud rate is directly related with timer ag rollover.
There are many ways one can cause timer 1 to overow at a rate that
determines a baud rate but the most common method is to put timer 1 in 8
- bit auto - reload mode (timer mode 2) and set a reload value (TH1) that
causes timer 1 to overow at a frequency appropriate to generate a baud
rate.
To determine the value that must be loaded in TH1 register to generate a
given baud rate, we may use the following equation:
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 29 / 73

Calculation of Baud Rate...
TH1 = 256
2
SMOD
Oscillator Frequency
1232Baud Rate
TH1 = 256
2
SMOD
Oscillator Frequency
384Baud Rate
ifSMOD = 0
TH1 = 256
Oscillator Frequency
384Baud Rate
ifSMOD = 1
TH1 = 256
Oscillator Frequency
192Baud Rate
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 30 / 73

SCON Register
D7 D6 D5 D4 D3 D2 D1 D0
SM0 SM1 SM2 REN TB8 RB8 TI RI
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 31 / 73

SCON Register...
Bit Name Bit address Function
7SM09Fh
Serial port mode bit 0
6SM19Eh
Serial port mode bit 1
5SM29Dh
Multiprocessor communication
enable bit
4REN9Ch
Receiver enables. This bit must
be set in order to receive char-
acters serially.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 32 / 73

SCON Register...
Bit Name Bit address Function
3TB89Bh
Transmit bit 8. The 9th bit to
transmit in mode 2 and mode 3
2RB89Ah
Receive bit 8. The 9th bit re-
ceived in mode 2 and mode 3
1TI99h
Transmit ag. Set when a
byte has been transmitted com-
pletely.
0RI98h
Receive ag. Set when a byte
has been received completely.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 33 / 73

SCON Register...
SM0 SM1 Serial mode Explanation Baud rate
00 0 8 - bit shift register Oscillator/1201 1 8 - bit UART Set by timer 110 2 9 - bit UART Oscillator/3211 3 9 - bit UART Set by timer 1Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 34 / 73

Importance of TI Flag Bit
The following steps are observed when the character byte transferred serially
via TxD line
1. The start bit is transmitted; indicate that data transmitted is going to
be start.
2. The 8 - bit main value of the character byte start transmission with LSB
(bit D0) rst.
3. The stop bit is transmitted. The stop bit is the last bit to be transmitted
if the parity bit is not considered. It is during the transmission of stop bit,
TI ag bit is raised to one (logic high) by CPU, indicating that transmission
is completed. So 8051 is ready to transfer a new character byte.
4. By monitoring TI ag bit we ensure that the previous byte is transmitted
completely or not. Suppose TI ag bit doesn't raises and we are trying to
send new character, so there may be chances that some bits of the previous
byte are lost and new character gets transmitted. In other words TI ag bit
is raised by CPU in order to ensure proper transmission of data bits.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 35 / 73

Importance of TI Flag Bit..
5. When TI rises it must be forced to zero by software program in order to
transmit next character or byte.
From the above points it is clear that when 8051 Microcontroller nishes
transmission, it will raise TI ag bit automatically, gives the information
that transmission is completed in all respects.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 36 / 73

Importance of RI Flag Bit
In receiving bits via its RxD line, the 8051 Microcontroller goes through the
following steps
1. It receives the start bit, indicating that the next bit is the rst bit of the
character byte it is about to receive.
2. The 8 - bit character is received one bit at a time, The LSB i.e. D0 bit
receives rst. When the last bit (bit D7) is received, a byte is formed and
placed in the SBUF register.
3. The stop bit is received, when receiving the stop bit the 8051 makes RI
= 1, indicating that an entire character byte has been received and must
be picked up before it gets over written by an incoming character.
4. By checking the RI ag bit when it is raised, we know that the character
has been received and it is available in the SBUF register. We must copy the
SBUF register contents to a safe place in some other register or in memory
before it is lost.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 37 / 73

Importance of RI Flag Bit...
5. After the SBUF contents are copied into a safe place, the RI ag bit
must be forced to 0 by the software instruction CLR RI in order to allow the
next received character byte to be placed in SBUF, failure to do this causes
loss of the received character.
From the above, we conclude that by checking the RI ag bit, we know
whether or not the 8051 has received a character byte. If we fail to copy
SBUF into a safe place, we risk the loss of the received byte. More impor-
tantly, it must be noted that RI ag bit is raised by the 8051, but it must
be cleared by the programmer with an instruction such as CLR RI. It must
also be noted that if we copy SBUF into a safe place before the RI ag bit
is raised, we risk copying the garbage.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 38 / 73

SBUF Register
The SBUF register is used to load the data to be transmitted serially. The
data received serially by 8051 Microcontroller is also copied to the SBUF
register. The ON chip address of SBUF register is 99h. The SBUF register
is also access like any other register of 8051 Microcontroller for example, to
load data 34h to the SBUF register then, it may written as
MOV SBUF, #34h orMOV 99h, #34hSimilarly the data from SBUF register is copied to any other register, for
example
MOV A, SBUFMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 39 / 73

Writing to the serial port
Once the Serial Port has been properly congured as explained above, the
serial port is ready to be used to send data and receive data. If you thought
that conguring the serial port was simple, using the serial port will be a
breeze. To write a byte to the serial port one must simply write the value
to the SBUF (99h) SFR.
For Example, if you wanted to send the letter \A" to the serial port, it could
be accomplished as easily as:
MOV SBUF, #'A'Upon execution of the above instruction the 8051 will begin transmitting the
character via the serial port. Obviously transmission is not instantaneous{it
takes a measureable amount of time to transmit
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 40 / 73

Writing to the serial port
The 8051 lets us know when it is done transmitting a character by setting
the TI bit in SCON. When this bit is set we know that the last character has
been transmitted and that we may send the next character, if any. Consider
the following code segment:
CLR TI; be sure the bit is initially clearMOV SBUF, #'A'; Send the letter A to the serial portJNB TI, $; Pause until the RI bit is set.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 41 / 73

Reading the serial port
Reading data received by the serial port is equally easy. To read a byte
from the serial port one just needs to read the value stored in theSBUF
(99h)SFR after the 8051 has automatically set the RI ag in SCON. For
example, if your program wants to wait for a character to be received and
subsequently read it into the Accumulator, the following code segment may
be used:
JNB RI, $; Wait for the 8051 to set the RI agMOV A, SBUF; Read the character from the serial port
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 42 / 73

PCON Register
Question Explain how to double the baud rate without changing crystal frequency
D7 D6 D5 D4 D3 D2 D1 D0
SMOD { { { GF1 GF0 PD IDL
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 43 / 73

PCON Register...
There are two ways to increase the baud rate of data transfer in the 8051
Microcontroller
1. Use a higher frequency crystal2. Change a bit in the PCON register, i.e. bit D7 (SMOD) bit of PCON
register
When the 8051 is powered up, D7 bit of the PCON register is zero, we can
set this bit to HIGH by software and thereby double the baud rate. The
following sequence of the instructions must be used to set bit D7 HIGH of
the PCON register, since PCON is not a bit addressable register. There
may be more methods to set SMOD = 1, without aecting the other bits of
PCON register, so one may use any sequence of the program with ensuring
that the other bits of the PCON register is unaected and only SMOD gets
high.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 44 / 73

PCON Register...
MOV A, PCON; get original or old value of PCON register in Accumulator
SETB ACC.7; make D7 (SMOD) = 1MOV PCON, A; now SMOD = 1 without modifying other bits of the PCON
register
Alternatively,MOV A, PCON; get original or old value of PCON register in AccumulatorOR A, #0FFh; make D7 (SMOD) = 1MOV PCON, A; now SMOD = 1 without modifying other bits of the PCON
register
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 45 / 73

Calculation of Baud Rate...
TH1 = 256
2
SMOD
Oscillator Frequency
1232Baud Rate
TH1 = 256
2
SMOD
Oscillator Frequency
384Baud Rate
ifSMOD = 0
TH1 = 256
Oscillator Frequency
384Baud Rate
ifSMOD = 1
TH1 = 256
Oscillator Frequency
192Baud Rate
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 46 / 73

Baud rate comparision for SMOD = 0 & SMOD = 1
Baud rate comparisons for SMOD = 0 and SMOD =1 is shown in the table
below. It shows that the for the same value of TH1, the baud rate are
doubled when SMOD = 1
TH1 (Decimal) TH1 (Hex) Baud Rate
SMOD = 0 SMOD = 1
-3FDh 9600 19200-6FAh 4800 9600-12F4h 2400 4800-24E8h 1200 2400-48D0h 600 1200Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 47 / 73

Programming of transmission byte serially
The following steps are taken to transmit a character or byte serially through
the TxD line of 8051 Microcontroller.
1. The TMOD register must be initialized with 20h indicating that timer
1 in auto - reload mode (mode 2), to set the baud rate. The baud rate
is congured only using auto - reload mode, because only in auto - reload
mode it is possible to get maximum overow at very high rate.
2. Load TH1 register with a value corresponding to the specied baud rate.3. Initialize SCON register with value 50h indicating serial mode 1 i.e. 8 -
bit data, 1 start bit and 1 stop bit. In most of the system use only serial
mode 1, because this mode is IBM compatible.
4. Set TR1 = 1 to start the timer 1, to get the baud rate.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 48 / 73

Programming of transmission byte serially...
5. The character byte to be transmitted serially must be put in the SBUF
register.
6. Keep monitoring TI (transmitter interrupt) ag bit using the instruction
\CHECK: JNB TI, CHECK", in order to ensure that transmission is com-
pleted or not. When TI rises, indicates completion of the transmission of
character them go out of the loop.
7. Clear TI ag bit using instruction CLR TI8. In order to repeat the process, i.e. to transmit another character or byte
go to step 5. No need to start the timer again, as once the baud rate is
congured; it is available throughout the completion of the program. The
baud rate is change again by loading another value in TH1 register and start
the timer or it may gets laps by resetting the system.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 49 / 73

Programming of reception of byte serially
The following steps are followed to receive character or byte serially through
the RxD line of 8051 Microcontroller.
1. The TMOD register must be initialized with 20h indicating that timer
1 in auto - reload mode (mode 2), to set the baud rate. The baud rate
is congured only using auto - reload mode, because only in auto - reload
mode it is possible to get maximum overow at very high rate.
2. Load TH1 register with a value corresponding to the specied baud rate.3. Initialize SCON register with value 50h indicating serial mode 1 i.e. 8 -
bit data, 1 start bit and 1 stop bit. In most of the system use only serial
mode 1, because this mode is IBM compatible.
4. Set TR1 = 1 to start the timer 1, to get the baud rate.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 50 / 73

Programming of reception of byte serially...
5. Keep monitoring RI (receiver interrupt) ag bit using the instruction
\CHECK: JNB RI, CHECK", in order to ensure that reception is completed
or not. When RI rises, indicates completion of the reception of character
then go out of the loop.
6. When RI ag bit is rises then there is a byte i.e. received byte in SBUF
register, so it must be picked up before a new incoming character is received.
In other words when RI is rises then store the SBUF contents in safe place.
7. Clear RI ag bit using instruction CLR RI8. In order to repeat the process, i.e. to receive another character or byte
go to step 5. No need to start the timer again, as once the baud rate is
congured; it is available throughout the completion of the program. The
baud rate is change again by loading another value in TH1 register and start
the timer or it may gets laps by resetting the system.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 51 / 73

Example -1: Transmission of character N serially
Example - 1 8051 Microcontroller has an oscillator frequency of 11.0592MHz. Using
timer 1 and conguring the UART in mode 1 write an assembly language
program that transmits the ASCII character \N" at a baud rate of 9600.
Solution
TH1 = 256
2
SMOD
Oscillator Frequency
1232Baud Rate
(Assume) SMOD = 0By putting baud rate 9600, we getTH1=FDhCalculate TMOD and SCONTMOD = 20hSCON = 50h
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 52 / 73

Example -1: Transmission of character N serially
Assembly Language Program
ORG 0 ; reset addressSJMP START; jump over reserved areaORG 40H ; program start addressSTART:MOV SCON, #50H ; serial mode 1MOV TMOD, #20H; timer 1 mode 2MOV TH1, #0FDH; baud rate 9600SETB TR1; turn timer 1 onAGAIN:MOV SBUF, #'N' ; ASCII of N into SBUFHERE:JNB TI, HERE ; stay here till TI is setCLR TI ; clear TISJMP AGAIN; repeatEND ; end of assembly language
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 53 / 73

Example -2: Transmission of character N serially
Example - 2 8051 Microcontroller has an oscillator frequency of 11.0592MHz. Using
timer 1 and conguring the UART in mode 1 write an assembly language
program that transmits the ASCII character \N" at a baud rate of 19200.
Solution
TH1 = 256
2
SMOD
Oscillator Frequency
1232Baud Rate
(Assume) SMOD = 0By putting baud rate 19200, we getTH1=FEhCalculate TMOD and SCONTMOD = 20hSCON = 50h
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 54 / 73

Example -2: Transmission of character N serially
Assembly Language Program
ORG 0 ; reset addressSJMP START; jump over reserved areaORG 40H ; program start addressSTART:MOV SCON, #50H ; serial mode 1MOV TMOD, #20H; timer 1 mode 2MOV TH1, #0FEH; baud rate 9600SETB TR1; turn timer 1 onAGAIN:MOV SBUF, #'N' ; ASCII of N into SBUFHERE:JNB TI, HERE ; stay here till TI is setCLR TI ; clear TISJMP AGAIN; repeatEND ; end of assembly language
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 55 / 73

Example -3: Receive byte serially and send to port P1
Example - 3 8051 Microcontroller has an oscillator frequency of 11.0592 MHz. Write an
assembly language program that receives a character into the serial buer
(SBUF) of the UART and writes the hex value of the character onto port
1. The UART should be congured as mode 1. Use 9600 baud rate.
Solution
TH1 = 256
2
SMOD
Oscillator Frequency
1232Baud Rate
(Assume) SMOD = 0By putting baud rate 9600, we getTH1=FDhCalculate TMOD and SCONTMOD = 20hSCON = 50h
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 56 / 73

Example -3: Receive byte serially
Assembly Language Program
ORG 0 ; reset addressSJMP START; jump over reserved areaORG 40H ; program start addressSTART:MOV SCON, #50H ; mode 1, REN enabledMOV TH1, #0FDH; 9600 baudMOV TMOD, #20H; timer 1 mode 2SETB TR1; turn timer 1 onSTAY:JNB RI, STAY ; stay here, until RI = 1CLR RI ; clear RIMOV A, SBUF; move character from buer to AMOV P1, A; hex value onto port 1RETI ; return from interruptEND ; no more assembly language
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 57 / 73

Example -4: Transmission of message BHEL serially
Example - 4 8051 Microcontroller has an oscillator frequency of 11.0592 MHz. write
an assembly language program to transfer the message \BHEL" serially at
4800 baud rate, 8 - bit data, 1 start bit and 1 stop bit. Send the message
continuously.
Solution
TH1 = 256
2
SMOD
Oscillator Frequency
1232Baud Rate
(Assume) SMOD = 0By putting baud rate 4800, we getTH1=FAhCalculate TMOD and SCONTMOD = 20hSCON = 50h
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 58 / 73

Example -4: Trans. of byte serially (Method - I)
Assembly Language Program
ORG 0SJMP STARTORG 0040HSTART:MOV TMOD, #20HMOV TH1, #-6 / MOV TH1, # 0FAHMOV SCON, #50HSETB TR1AGAIN:MOV A, #'B'ACALL SENDMOV A, #'H'ACALL SENDMOV A, #'E'ACALL SENDMOV A, #'L'ACALL SENDSJMP AGAIN
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 59 / 73

Example -4: Trans. of byte serially (Method - I)...
Assembly Language Program
SEND:MOV SBUF, AHERE:JNB TI, HERECLR TIRET
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 60 / 73

Example - 4: Trans. of byte serially (Method - II)
Assembly Language Program
ORG 0SJMP STARTORG 0040HSTART:MOV TMOD, #20HMOV TH1, #-6 / MOV TH1, #0FAHMOV SCON, #50HSETB TR1LAST:MOV DPTR, #MSGAGAIN:CLR AMOVC A, @A+DPTRJZ LAST ; If last character get out and
repeat again
ACALL SENDINC DPTRSJMP AGAIN
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 61 / 73

Example - 4: Trans. of byte serially (Method - II)...
Assembly Language Program
SEND:MOV SBUF, AHERE:JNB TI, HERECLR TIRETMSG:DB 'BHEL', 0END
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 62 / 73

Example -5: Transmission & Reception byte serially
Example - 5 Assume that the 8051 microcontroller's serial port is connected to the COM
port of the IBM PC, and on the PC we are using the Hyper Terminal program
to send and receive data serially. Port P1 and P2 of the 8051 are connected
to the LED's and switches respectively. Write an assembly language program
to perform the following
1. Send to the PC the message \Microcontroller is ready"
2. Receive any data sent by the PC and put it on LEDs connected to the
port P1.
3. Get data on switches connected to port P2 and send it to the PC
serially. The program should perform in part (1) once but part (2) and (3)
continuously. Assume the baud rate 4800 with oscillator frequency 11.0592
MHz.
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 63 / 73

Example -5: Transmission & Reception byte serially
Solution
TH1 = 256
2
SMOD
Oscillator Frequency
1232Baud Rate
(Assume) SMOD = 0By putting baud rate 4800, we getTH1=FAhCalculate TMOD and SCONTMOD = 20hSCON = 50h
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 64 / 73

Example -5: Transmission & Reception byte serially
Assembly Language Program
ORG 0 ; reset addressSJMP START; jump over reserved areaORG 40H ; program start addressSTART:MOV P2, #0FFH ; Port P2 congured as an input portMOV SCON, #50H; serial mode 1MOV TMOD, #20H; timer 1 mode 2MOV TH1, #0FAH; baud rate 19200SETB TR1; turn timer 1 onMOV DPTR, #MSG; load pointer for messageTRMSG:CLR AMOVC A, @A+DPTR; get the characterJZ READSW; check for last character, get out if lastACALL SEND; otherwise transfer message seriallyINC DPTR; next characterSJMP TRMSG; stay in loop until last character is sent
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 65 / 73

Example -5: Transmission & Reception byte serially...
Assembly Language Program
READSW:MOV A, P2 ; read switches value from port P2ACALL SEND; transfer it seriallyACALL RECEV; get the serial dataMOV P1, A; display it on LED'sSJMP READSW; repeat the processSEND:MOV SBUF, A ; load the data into SBUFHERE:JNB TI, HERE ; stay here till TI setCLR TI ; clear TIRET
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 66 / 73

Example -5: Transmission & Reception byte serially...
Assembly Language Program
RECEV:JNB RI, RECEVMOV A, SBUFCLR RIRETMSG:DB 'Microcontroller is ready', 0END ; end of assembly language
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 67 / 73

Example - 6: Switch base Yes - No
Example - 6 Assume a switch is connected to pin P0.3, write an assembly language
program to monitor its status and send two messages to the serial port
continuously as follows:
SW = 0 send \NO"
SW = 1 send \YES"
Assume that crystal frequency of the 8051 is 11.0592 MHz, use 19200 baud
rate and 8 - bit UART. Use SMOD =1
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 68 / 73

Example - 6: Transmission & Reception byte serially
Solution
TH1 = 256
2
SMOD
Oscillator Frequency
1232Baud Rate
(Given) SMOD = 1By putting baud rate 19200, we getTH1=FDhCalculate TMOD and SCONTMOD = 20hSCON = 50h
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 69 / 73

Example - 6: Switch base Yes - No
Assembly Language Program
ORG 0 ; reset addressSJMP START; jump over reserved areaORG 40H ; program start addressSTART:MOV A, PCONSETB ACC.7MOV PCON, AMOV SCON, #50H; serial mode 1MOV TMOD, #20H; timer 1 mode 2MOV TH1, #0FDH; baud rate 19200SETB TR1; turn timer 1 onSETB P0.3; make port pin P0.3 as an input portCHECK:JB P0.3, NEXT
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 70 / 73

Example - 6: Switch base Yes - No...
Assembly Language Program
MOV DPTR, #MSG1; If SW = 0 then send message NOREP1:CLR AMOVC A, @A+DPTR; read the messageJZ CHECK; check for last characterACALL SEND; send message seriallyINC DPTR; go for next byteSJMP REP1NEXT:MOV DPTR, #MSG2 ; if SW = 1 then send message YESREP2:CLR AMOVC A, @A+DPTR; read the messageJZ CHECK; check for last characterACALL SEND; send message seriallyINC DPTR; go for next byteSJMP REP2
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 71 / 73

Example - 6: Switch base Yes - No...
Assembly Language Program
SEND:MOV SBUF, A ; copy A into SBUF for serial transmissionHERE:JNB TI, HERE ; stay here till TI setCLR TI; clear TIRETMSG1:DB 'NO', 0MSG2:DB 'YES', 0END ; end of assembly language
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 72 / 73

Thank you
Please send your feedback at [email protected]
For more details and updates kindly visit
https://sites.google.com/site/nileshbbahadure/home
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - III (Part I) July 25, 2021 73 / 73