0% found this document useful (0 votes)
3 views

Unit-7 8051 Serial Port and Programming_Technical

The document outlines the syllabus for a course on 8051 Serial Port and Programming, covering topics such as serial communication basics, RS232 standards, and programming in Assembly and C. It includes comparisons between parallel and serial data transfer, types of serial communication, and details about the RS232 standard and its connectors. Key concepts include baud rate, data transmission methods, and the differences between asynchronous and synchronous communication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit-7 8051 Serial Port and Programming_Technical

The document outlines the syllabus for a course on 8051 Serial Port and Programming, covering topics such as serial communication basics, RS232 standards, and programming in Assembly and C. It includes comparisons between parallel and serial data transfer, types of serial communication, and details about the RS232 standard and its connectors. Key concepts include baud rate, data transmission methods, and the differences between asynchronous and synchronous communication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Acc.

No-g092|
Mhan institute o

10 8051 Serial Port


and Programming
Syllabus
Basics of serial communication, RS232 standards. 8051
connection to RS232, Serial data
input/output and associated registers, Various modes of serial data communication, serial data
communication programs in Assembly language/ Embedded C.
Contents
10.1 Data Communication
10.2 Basics of Serial Data Communication
10.3 8051Serial Communication Summer-16, 17, 18, 19,
Winter-17, Marks 7
10.4 Connection to RS 232
10.5 Serial Communication Programming in Assembly
Winter-06, 07, 08, 09, 11, 15, 17, 18,
Summer-09, 11, 17, Marks 7
10.6 Seral Communication Programming in C.... Winter-10, 11, 13, 17,
Summer-10, 15, 19, "Marks 7
10.7 Short Questions and Answers
8051 Serial Port and
Microprocessors and Microcontrollers 10-2 Programming
10.1 Data Communication
microprocessors/microcontrollers are designed for parallel
Most of the
communication number of lines required to transfer dat
Communication. In parallel of data
number of bits to be transferred. For example,to transter a byte
depend on the transferred simultaneously.
required and all 8-bits are
Olmes are
Each bit requires separate n Data lines
line for transfer. One
additional line is required to Transmitter Receiver
transfer parity information. Parity bit
Thus, for byte transfer, total
nine lines are required for
transmission. This is illustrated Fig. 10.1.1 Parallel data transfer
in Fig. 10.1.1.
parallel transfer is faster than
Since more number of bits are transferred at a time,
with the serial transfer.
serial transfer. However, it requires more wires in comparison
for short distance
Due to more wires, cost of cabling is more and hence it is used
communication.
Parallel data transfer technique is used in communication between computer and
peripherals, such as hard disk drive, printer etc.
Parallel communication is also not practical for devices such as cassette tapes or a
CRT terminal. In such situations, serial communication is used. In serial communication
one bit is transferred at a time over a single line.

Transmitter Receiver.

Fig. 10.1.2 Serial data transfer

Sr. No. Parallel data trarnsfer Serial data transfer


1, Group of bits (usually 8-bits) of data Only one bit of data is transferred at
is transferred at a time. a time.
2. Data transfer rate is high. Data transfer rate is low.
3. Preferred for short distance.
Preferred for long distance.
4 Cabling cost is more.
Cabling cost is less.
5 Less reliable.
More reliable
6 Examples : GPIB, PCI, ISA etc.
Bxamples : RS 232C, USB, HDLCetc.
Table 10.1.1 Comparison between
parallel and serial data transfer
TECHNICAL PUBLICATIONs®
Microprocessors and MIC

Review Questions

1. Define serial data transfer and parallel data transfer.


2. Give comparison between serial and parallel data transfer.

10.2 Basics of Serial Data Communication


In serial data communication, during transmission parallel data is converted into
serial bits sing a parallel-in serial-out (PISO) shift register. At the receiver, the serial
bits are connected into parallel data by another shift register called serial-in parallel-out
(SIPO)shift registe.
10.2.1 Classification
Serial data transmission can be classified on the basis of how transmission occurs.
1. Simplex 2. Half duplex 3. Full duplex
10.2.1.1 Simplex
In simplex, the hardware exists such that data transfer takes place only in one
direction. There is no possibility of data transfer in the other direction. A typical
example is transmission from a computer to the printer.
10.2.1.2 Half Duplex
The half duplex transmission allows the data transfer in both directions, but not
simultaneously. A typical example is a walkie-talkie.
10.2.1.3 Full Duplex
The full duplexX transmission allows the data transfer in both direction
simultaneously. The typical example is transmission through telephone lines.
10.2.2 Types of Serial Data Communication
Serial data communication uses two types of communications
Asynchronous serial data communication
Synchronous serial data communication
U.22.1 Asynchronous Serial Data Communication
Fig. 10.2.1 shows the transmission format for asynchronous transmission.
ASynchronous formats are character oriented. In this, the bits of a character or data
Ord are sent at a constant rate, but characters can come at any rate (asynchronously) as
sent, a line stays high at
longas they do not overlap. When no characters are being by
gc 1 called mark. logic 0 is called space. The beginning of acharacter is indicated
TECHNICAL PUBLICATIONS - An up thrust for knowledge
8051 Serial Port and Programming
MicroproCeSsors and Microcontrollers 10-4

always low. This is used to synchronize the transmitter and recejver


astart bit which is least significant bit first, followed by one
data bits are sent with
After the start bit, the indicate the end of character. Differorms
high). The stop bits
or more stop bits (active combination of start bit, character and stop
bite
systems use 1, 1 1/2 or 2 stop bits. The required
frame. The start and stop bits carry no intormation, but are
is known as data brto
asynchronous nature of data. Fig. 10.2.2 ilustrates how the
because of the
the asynchronous serial format.
CAH would look when transmitted in

Marking
Stop
Transmitter D D, D, D,D, D,|Ds| D| bits Receiver

ASCII
characters CLK
CLK
Time

transmission
Fig. 10.2.1 Transmission format for asynchronous

1 Stop bit
Start bit

1 1 0 0

Transmitter Receiver
1Frame

Time

Fig. 10.2.2 Asynchronous format with data byte CAH

The data rate can be expressed as bits/sec or characters/sec. The term bits/ sec 15 asv
called the baud rate. The asynchronous format is generally used in low-speed
transmission (less than 20 kbits/sec).

10.2.2.2 Synchronous Serial Data Communlcatlon


The start and stop bits in Clock

each frame of asynchronous


format represents wasted Sync Sync
Recaiver
overhead bytes that reduce the Transmitter
Data
overall character rate. These
Start
start and stop bits can be Time

eliminated by synchronizing Flg. 10.2.3 Synchronous transmisslon format


receiver and transmitter. They
TECHNICAL PUBLICATIONS -An up thrust for
knowledge
Microprocessors and Microcontrollers
10-5
8051 Serial Port and Programming
can be synchronized by having a common clock signal. Such a
synchronous serial communication. The Fig. 10.2.3 shows the communication is called
Synchronous serial transmission format of
instead of start and communication.
In this
stop bits. transmission synchronous bits are inserted
St. No.
Asynchronous serial comnmunication Synchronous serial commnunication
Transmitters and receivers are not
synchronized by clock.
......
Transmitters and receivers
2 Bits of data are
synchronized by clock.
rate. transmitted at constant Data bits are ransmitted with
3. synchronization of clock
Character may arrive at any rate at
receiver. Character is receied at constant tate.
Data transfer is character oriented.
5
Data transfer takes place in blocks.
Start and stop bits are
establish communication of each required to Startarnd stop bits are not reqtured to
character. establish communication of each character,
however synchronizatioR bits are required
to trangfer the data block
Used in low-speed transmissions at about Used in highspeed
speed less than 20 kbits/sec. transnissions
Table 10.2.1 Comparison between
asynchronous and synchronous serial data transfer
10.2.3 Baud Rate
Baud rate is data transmission speed. The rate at which the bits are
transmnitted
(bits/second) is called baud or transfer rate. The baud rate is the reciprocal of the timne
to send 1-bit. The common baud rates are multiples of 75 bits per second (bit/s) are
ypically : 75, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400 bit/s
10.2.4 RS 232 Standard
In response to the need for signals and handshake standards between DTE and DCE,
the Electronic Industries Association (ELA) introduced ElA standard RS 232 in 1962. It
Was revised and named as RS 232C, in 1969 by ElA. It is widely accepted for single
ed data transnmission over short distances with low data rates.
for serial data
nis standard describes the functions of 25 signal and handshake pins
transfer. It also describes the voltage levels,forimpedance levels, rise and fall times,
signal lines. RS 232C specifies 25
maximum bit rate, and maximum capacitance these
connector should be male, and the DCE
signal pins and it specifies that the DTE
commonly used connector should be a
female.
Connector
The most
should be a female. The most
shown in the Fig. 10.2.4.
used connector, DB-25P is
commonly knowledge
TECHNICAL PUBLICATIONS An up thrust for
MicroproDCessors and Microcontrollers 10-6 8051 Serial Port and Programming

Protective ground
Secondary transmitted data 14 2 Transmitted data (TxD) DCE
Transmission signal element timing (DCE source) 15 3 Received data (RxD) DTE
Secondary received data 16 Request to send (RTS) DCE
Receiver signal element timing (DCE source) 17 Clear to send (CTS) DTE
Unassigned 18 6 Data set ready (DSR) DTE
Secondary request to send 19 Signal ground
Data terminal ready (DTR) 20 Received line signal detector

Fig. 10.2.4
10.2.4.1 DB-25 P Connector
The Table 10.2.2 shows pins and signals description for RS 232C DB-25P connector.

Pin Common RS 2320 Description Signal


direction
number name
on DEC

AA Protective ground
2 TxD BA Transmitted data N
RxD BB OUT
Received data
4 RTS CA N
Request to send
5 CTS CB
OUT
Clear to send
6 CC OUT
Data set ready
7 GND AB
....e..... Signal ground (common retum)
8 8 CF OUT
Received line signal detector
(Reserved for data set testing)
10
(Reserved for data set testing)
11
Unsigned
12 SCF OUT
Secondary recd. line sig. detector

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Microprocessors and MicroOcontrollers 10-7 8051 Serial Port and Programming
13 SCB
Secondary clear to send .......
OUT
14 SBA
Secondary transmitted data IN
15 DB
Transmission signal element timing(DCE source) OUT
16 SBB
Secondary received data OUT
DD
Receiver signal element timing (DCE source) OUT
18

19 SCA
Unassigned
20 DTR
Secondary request to send IN
CD
Data terminal ready IN
Signal quality detector OUT
2 CE
Ring indicator
........ OUT
23 CH/CI Data signal rate selector (DTE/DCE source)
IN/OUT
24 DA
Transmit signal element timing (DTE source) IN
Unassigned
c00o0000ooooooooooow

Table 10.2.2

10.2.4.2 DB-9P Connector


Since not all the pins are used in PC cables,
IBM introduced the DB-9P version of the serial 1 2 3 5
I/O standard, which uses only 9-pins. The
Table 10.2.3 describes the pins for DB-9P and 6 7
Fig. 10.2.5 shows the DB-9P
connector. Fig. 10.2.5 DB-9P connector

Pin n0. Pin Description


DCD Data carrier detect
RxD Received data
TxD Transmitted data
DTR Data ternminal ready
GND Signal ground
6 DSR Data set ready
RTS Request to send
CIS Cear to send

9 RI Ring indicator
Table 10.2.3 Pin description for DB-9P connector

TECHNICAL PUBLICATIONS-An up thrust for knowledge


10-8
8061 Serial Port and Programming
MicroprocesSors and Microcontrollers

232C
10.2.4.3 Data Transmission and Reception using RS
shows the digital data transmission using modems and standard telephone
Fig. 10.2.6 serial data over long distances are
and other eguipment used to send
lines. Modems
(DCE). The terminals and comnputers that are
known as Data Communication Equipment (DTE).
receiving the serial data are referred to as Data Terminal Equipment
sending or

Large
Microcomputer timeshare
controlled Modem computer
terminal Modem
Telephone TXD
TXD line
Rx
RxD
RTS
RTS
CTS
CTS
CD
DTR
DTR
DSR
DSR
DCE DCE
DTE
DTE DTE = Data Terminal Equipment
DCE = Data Communication Equipment

standard telephone lines


Fig. 10.2.6 Digital data transmission using modems and

runs any self-checks, it asserts


After the terminal power is turned on and the terminal
is ready. When modem s
the data terminal ready (DTR) signal to tell the modem it
ready (DTR) signal to the
ready to transmit or receive data, it asserts the data set
asserting KI
terminal. After receiving DTR, the DTE requests use of the data channel by dialed. This
signal to begin transmission. The MODEM at the receiving end is then the modem
When
answer-mode modem responds with a 2225 Hz carrier frequency.
connected to the sending terminal receives this carrier, it asserts carrier detect( (CIS)
signal
the terminal. After proper time interval the modem sends clear-to-send Immediately
indicating modem and communication channels are ready for transmission.
TxD output.
after CTS signal the terminal at the transmitting end sends serial data on its forthe
Asimilar handshake takes place for receiving data. Fig. 10.2.7 shows flowchartwiththe
handshaking process when an RS 232C data port is used to exchange data
modem.
Microprocessors and Microcontrollers
10-9
8051 Serial Port and Programming

Start
Output DTR
low The DTE is ready

Read DSR
from modem Check if the modem is ready

No Low
Wait until it is low
Yes
Prepare for Initialize pointers and counters
transmission for the text to be
transmitted
Output RTS The DTErequest use of the data
low channel to begin transmission

Read CD Make sure modem has made


from modem contact with remote modem

No Low Wait until remote


carrier is detected

Yes
Read CTS When CTS goes low the modem
and communication channels are
from modem ready for transmissions

No Low
Wait for modem to be ready

Yes

Transmission
begins

Fig. 10.2.7 Flowchart showing handshaking process


Review Questions
1. Mention RS232 handshaking signal DB-9 pin configuration.
2. Write a note on Asynchronous serial communication and data framing".
3. List the advantages of serial communication over parallel communication.
4. What is
data serialization ? Explain different types with examples.
knowledge
TECHNICAL PUBLICATIONS- An up thrust for
8051 Serial Port and Programming
Microprocessors and Microcontrollers 10- 10

and simplex serial data transfer.


5. Explain fullduplex, half duplex
communication ?
6. What is asynchronous serial
asynchronous serial communication.
7. Explain the format of
pins of DB-9 connector.
8. Explain the function of RS232C
GTU : Summer-16, 17, 18, 19, Winter- 17
10.3 8051 Serial Communication
rereive
8051 is full duplex, means it can transmit and
The serial port of data. Register SCON controls data
hold
simultarneously. It uses register SBUF to do
register-PCON controls data rates and pin RD (P3.0) and TxD (P3.1)
communication,
the data transfer.
dedicated for serial communication in 8051. Its address is
SBUF is an 8-bit register data to
addressed like any other register in 8051. Writing to SBUF loads
99H. It can be received data. There are two separate and
and reading SBUF accesses
be transmitted the receive read-only register. This
the transmit write-only register and
distinct registers,
is illustrated in Fig. 10.3.1.
RxD SBUF TxD
(P3.0) Shift register CLK Write only)
(P3.1)
CLK
Baud rate clock
(Transmit)
Baud rate clock
(Receive) SBUF
(Read only)

8051 Internal bus

Fig. 10.3.1
reception of the data durng
The way in which SBUF is used for the transmission and
serial communication is explained below.
Transmission : When a byte of data is to be transmitted via the TxD p, ue
written into SBUF, it
SBUF is loaded with this data byte. As soon as a data byte is
is framed with the start and stop bits and transmitted serially via the TxD pin.
detrae
Reception : When 8051 receives data serially via RxD pin of it, the 8051 byteis
This
it. The start and stop bits are separated out from a byte of data.
placed in SBUF register.
Bit pattern of SCON register A Communication.
The 8051 provides four programmable modes for serial data mode
particular mode can be selected by setting the SM0 and SM1 bits in SCON. The
for SCON.
selection also decides the baud rate. The Fig. 10.3.2 shows the bit patterns

TECHNICAL PUBLICATIONS An up thrust for knowledge


Microprocessors and Microcontrollers 10- 11 8051 Serial Port and Programming

(MSB)
(LSB)
7 5 4 3 2 1 0

SMO SM1 SM2 REN TB8 RB8 T RI

Symbol Position Name and significance


SMO SCON.7 Serial port Mode control bit O.
Set/cleared by software
*......
(see note).
SMI SCON.6 Serial port Mode control bit 1.
Set/cleared by software (see note).
SM2 SCON5 Serial port Mode control bit 2.
Set by software to disable reception of frames for which bit 8 is
zero.
REN SCON.4 Receiver Enable control bit.
Set/cleared by software to enable/ disable serial data reception.
TB8 SCON3 Transmit Bit 8.
Set/cleared by hardware to determnine state of ninth data bit transmitted in 9-bit
UART mode.
RB8 SCON.2 Receive Bit 8.

Set/cleared by hardware to indicate state of ninth data bit received.


SCON.1 Transmit Interrupt flag.
Set by hardware when byte transnitted. Cleared by software after servicing
*..****.....

RI SCON.Q Receive nterrupt flag.


Set by hardware when byte received. Cleared by software after servicing.
Note : The state of (SM0, SM1) selects
Mode SMO SM1
00 Shift register , baud = f/12
1 &bit UART variable data rate.
01
2 1 0 9bit UART fixed data rate ; baud f/32 or f/64
1 1-9-bit UART, variable data rate.
Fig. 10.3.2 SCON-serial port control/status register
Bt pattern of
PCON register.
(MSB) (LSB)
4 3 2 1
7 6 5

GF1 GFO PD IDL


SMOD

knowledge
TECHNICAL PUBLICATIONS- An up thrust for
8051 Serial Port
10- 12 and
Microprocessors and Microcontrollers
Programming
Name and significance
Symbol Position
Serial baud rate modify bit. It is 0 at reset. It is set to 1 by program to double the
SMOD PCON.7
baud rate.

PCON.6-4 Not defined


progran.
GFI PCON.3 General purpose user flag bit 1. Set/cleared by
program.
GFO PCON.2 General purpose user flag bit 0. Set/ cleared by
PD PCON.1 1 by program to enter power down
Power down bit. It is set tomierocontrolers.
COnfiguration for CHMOS
DL PCONO Idle mode bit. It is set to 1 by program to enter idle mode conigyra
o CMOS nicrocO

Note : PCON is not bit addressable


Fig. 10.3.3PCON register

10.3.1 Operating Modes for Serial Port


Mode 0

In this mode, serial data enters and exits through RxD. TxD outputs the shift clock.
8 bits are transmitted/received : 8 data bits (LSB first). The baud rate is fixed at 1/12 the
Oscillator frequency.
Mode 1

In this mode, 10 bits are transmitted (through TxD) or received (through RxD) : A
start bit (0), 8 data bits (LSB first) and a stop bit (1). On receive, the stop bit goes into
RB8 in Special Function Register SCON. The baud rate is variable.
Mode 2

In this mode, 11 bits are transmitted (through TxD) or received (through RxD) : A
start bit (0), 8 data bits (LSB first), a programmable 9n data bit, and a stop bit(1). On
Transmit, the 9 data bit (TB8 in SCON) can be assigned the value of 0 Or 1. Or, for
thegth
example, the parity bit (P, in the PSW) could be moved into TB8. On receive,
data bit goes into RB8 in Special Function Register SCON, while the stop bit is ignored.
The baud rate is programmable to either 1 1
32 64 the oscillator frequency.
Mode 3

In this mode, 11 bits are


start bit (0), 8 data bits (LSBtransmitted (through TxD)9n ordata
first), a Programmable received
bit and(throug
a stop bit(1).In
fact, Mode 3is the same as Mode 2 in all respects except the baud rate. Thebaudratein
Mode 3 is variable.

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Microprocessors and Microcontrollers 10- 13 8051 Serial Port and Programming
In all four modes,transmission is initiated by any instruction that uses SBUF as a
destination register. Reception is initiated in Mode 0 by the condition RI = 0
REN =1. Reception is initiated in the other modes by the and
incoming start bit if REN=1.
The Table 10.3.1 summarizes the four
serial port modes provided by 8051.
Mode
Transmission format Baud ate
8data bits
oscillator frequency,
1 10-bit(start bit + 8-data bits + stop bit).
2
Variable.
11-bit (start bit +8-data bits +
g data bit + stop bit). programmable Prograrnmable to either 32
or 05cilator frequency
3 11 bit (start bit 8 data bit +
data bit t stop bit). programmable g Variable.
Table 10.3.1 Summary of serial port
modes
|10.3.2 Generating Baud Rates
Serial Port in Mode 0:
Mode 0 has a fixed baud rate which is 1/12 of the
serial port in this mnode none of the oscillator frequency. To run the
register needs to be defined. Timer/Counters need to be set up. Only the SCON
Baud rate = Oscillator frequency
12
Serial Port in Mode 1
Mode 1 has a variable baud rate. The baud rate can be
or Timer 2 generated by either Timer 1
(8052 only).
USing Timer/Counter 1 to Generate Baud Rates
For this Purpose, Timer 1 is used in mode 2 (Auto-Reload).
Baud rate = Kx Oscillator frequency
32 × 12 x [(256-TH1)]
f
If
SMOD= 0. then
K=1.
SMOD=1, then K = 2. (SMOD is the PCON register)
Most of the time the user knows the baud rate and needs to know the reload value
TH1.
TherTH1efore, 256the equation
= -
to calculate TH1 can be written as :
Kx Oscillator frequency
384 x Baud rate

TECHNICAL PUBLICATIONS - An up thrust for knowledge


10- 14
8051 Serial Port and Programming
Microprocessors and Microcontrollers
not
value. Rounding off THi to the nearest integer may
TH1 must be an integer user may have to choose another
crystal
rate. In this case, the
produce the desired baud
frequency. way to set the bit is logical
ORino
not bit addressable, one
Since the PCON register is 87H.
(i.e. ORL PCON, #80H). The address of PCON is
the PCON register. corresponding
Table 10.3.2 shows the values to be loaded into TH1 to get the
The when SMOD =1.
the baud rates are doubled
baud rate. It also shows that
Baud rate (SMOD 1
THI (HEX Baudrate (SMOD 0)
9600
19,200
4800
9600
FA
2400 4800
2400
1200

Note : XTAL = 11.0592 MHz


Table 10.3.2

Rates
Using Timer/Counter 2 to Generate Baud
rate generating mode. If Timer 2
For this purpose, Timer 2 must be used in the baud
is :
is being clocked through pin T2 (P1.0) the baud rate
Timer 2 overflow rate
Baud rate =
16

And if it is being clocked internally the baud rate is :

Baud rate Oscillator frequency


32 x [65536-(RCAP2H, RCAP2L)]
To obtain the reload value for RCAP2H and RCAP2L the above equation ca
rewritten as :

RCAP2H, RCAP2L = 65536 - Oscillator frequency


32 x Baud rate
Serial Port in Mode 2

The baud rate is fixed in this mode and is 1 1 frequency


32 64
or of the oscillator
depending on the none ofthe
value of the SMOD bit in the PCON register. In this mode
Timers are used and the clock comes from the internal phase 2
clock.
1
SMOD = 1, Baud rate =
32 Oscillator frequency
TECHNICAL PUBLICATIONS - An up thrust for knowledge
MicroprDCOSSors and Microcontrollers 10- 15 8051 Serial Port and Programming

1
SMOD = 0, Baud rate =
64 Oscillator frequency
To set the SMOD bit : ORL PCON, #80H. The address of PCON is 87H.
Note By changing SMOD bít in PCON from 0 to 1 we can double the baud rate in
8051.
Serial Port in Mode 3
The baud rate in mode 3 is variable and sets up exactly the same as in
mode 1
Review Questions

1. Explain the format of SCON register in details.


2. Explain the bit pattern for PCON register.
3. Explain operating modes for serial port in8051.
4. Write a note on generating baud rates.
5. Explain the special function registers SCON and SBUF in 8051 serial
comnunication.
GTU :Summer-16, Marks 7
6. Discuss about mode 2 of serial communication. GTU : Summer-17, Marks 7
7. Write the format of SCON special function register. Also explain how baud rate can be doubled
using PCON register ? GTU : Summer-17, Marks 7.
8. How baud rate can be set in 8051 microcontroller for serial communication?
GTU: Winter-17, Marks 3
9. Explain the role of SCON and SBUF in serial communication. GTU :'Summer-18, Marks 7
10. Erplatn the role of SBUF and SCON registers in serial transfer in 8051.
GTU : Summer-19, Marks 7

10.4 Connection to RS 232


In RS 232Cthe voltage level +3 V to + 15 V is defined as logic 0; from -3 V to
-15Vis defined as logic 1. The control and timing signals are compatible with the TTL
8051 MAX 232 RS 232
TTL
side side

P 3.0
RxD 3
R, OUT R, IN
P3.1
TxD
T IN T, OUT
DB-9P
Rg OUT R, IN
connector
RS 232C
T, IN T, OUT

Fig. 10.4.1 Connection between RS 232C and 8051

TECHNICAL PUBLICATIONS - An up thrust for knowledge


8051 Serial Port and
MicroproceSSors and Microcontrollers
10- 16 Programming
level. Because of the incompatibility of the data lines with the TTL logic, voltage
are required to intertace TTL logic wiL
translators, called line drivers and line receivers, connection between RS 232C and on
10.4.1 shows the
the RS 232C signals. The Fig. driver and line receiver. We know that one.
used as a line
Here, MAX 232 chip is serial dat
pins RxD (P3.0) and TD (P3.1) for reception and transmission of
assigns two require line driver and lino
therefore, they
respectively. These pins are TIL Compatible; The MAX 232 has two sets of line drivos
compatible.
receiver to make them RS 232C ono
receivers for transmitting and receiving data. Only one set is required for
and line
serial communication.

Review Question

specify the purpose of max-232 while interfacing.


1. Explain RS-232 handshaking signals and
10.5 Serial Communication Programming in Assembly
Summer-09, 11, 17
Winter-06, 07, 08, 09, 11, 15, 17, 18,
GTU :

Transfer
10.5.1 Programming 8051 for Serial Data
to perform following sequence of
To program 8051, to transfer data serially we have
actions :
value 20H to use timer 1 in mode 2
1. Load the TMOD register with the
(8-bit auto-reload) to set the baud rate.
data transfer.
2. Load TH1 to set the desire baud rate for serial
3. Load SCON register with the value 50H, to use
serial mode 1, where an &-bit data
is framed with start and stop bits.
4. Set TR1 to 1 to start timer 1.
5. Clear TI with CLR TI instruction.
6. Write a character to be sent in to the SBUF register.
character has
7. Check the TI flag bit with instruction JNB TI, XXXX to see if the
been transferred completely.
8. Go to step 5 to transfer the next character.
Example 10.5.1 8051 uses 11.0592 MHz Crystal. To get 9600 hertz. baud rate how w
you program it for serial transmission?
9600 hertz is
Solution: When 11.0592 MHz crystal is used and a standard baud rate of
required then, the setting of TH1can be found as
THI = 256 -
k xOscillator frequency
384 x Baud rate

TECHNICAL PUBLICATIONS - An up thrust for knowledge


MicroproCeSSors and Microcontrollers 10- 17
8051 Serial Port and Programming

= 256 1x 11.0592 x 106


384 x 9600 = 253 = FDH

Progranm: MOV TMOD, #020 Initialize timer 1 in mode 2


MOV SCON, #4CH Initialize serial mode 1
ORL PCON, #8OH Make SMOD = 1
MOV TH1, #FDH Load count
Evample 10.5.2 Write an 8O51 assembly language program to
trunsfer letter A serially
n 9600 buua rate, continiuously.
GTU: Summer-11, Winter-18
Solution :
MOV TMOD, #20H timer 1, mode 2 (auto reload)
MOV TH1, #FDH 9600 baud rate
MOV SCON, #50H 8-bit, 1 stop REN enabled
SETB TR1 start timer 1
START: MOV SBUF, #"A" Letter "A" to be transferred
HERE: JNB TI, HERE Wait for the last bit to transfer
CLR TI Clear TI for the next character
SJMP START Go to send the character again
Example 13.5.3 Write an 8051 assembly language program to transer the message
HELLO"serially at 9600 baud, 8-bit data, 1 stop bit. GTU: Winter-07, 09, Winter-17
Solution:
MOVTMOD, #20OH timer 1, mode 2
MOV TH1, #FDH 9600 baud rate
MOV S ON,#50H 8-bit, 1 stop bit, REN enabled
SETB TR1 start timer 1
START: MOVA, #"H" transfer "H
ACALL TRANS
MOV A, #"E" transfer "E"
ACALL TRANS
MOV A, #L transfer "L"
ACALL TRANS
MOV A, #"L" transfer ""
ACALL TRANS
MOV A, #"O" transfer "O"

ACALL TRANS Serial data transfer subroutine


TRANS: MOV SBUF, A Load SBUF
HERE: JNB TI, HERE wait for the last bit to transfer
CLR TI Clear TI for the next character
RET

TECHNICAL PUBLICATIONS - An up thrust for knowledge


10- 18 8051 Serial Port
MicroprOcessors and MicrOOontrollers and
Programming
The importance of the TI flag bit.
When a data is to be transmitted via TxD pin, first a data byte is loaded into the
bit are transmitted
start bit, a data byte and then the stop
SBUF register. A
via TxD pin. During the transmission of the stop bit, 8051 sets the TI flag, ie. TI =1. sequential y
This indicates the end of data byte transmission and 8051 is ready for the transmission
and 8051 is ready for the transmission of next data. The programmer has to clear the TI
flag, i.e. TI = 0, with the 'CLR TT instruction to transmit next data. The TI flag bit
should be monitored to make sure that the SBUF register is not Overwritten. If we write
the next byte to be transmitted into the SBUF register before setting the TI flag bit
untransmitted portion of the previous byte will be lost. The programmer can check tha
TI flag bit by JNB TI, XX instruction or by using an interrupt.

10.5.2 Programming 8051 for Receiving Data Serially


To program 8051, to receive data serially we have to perform following sequence of
actions:

1. Load the TMOD register with the value 20H to use timer 1 in mode 2 (8-bit
auto-reload) to set the baud rate.
2. Load THI to set the desire baud rate for serial data transfer.
3. Load SCON register with the value 50H, to use serial mode 1, where an &-bit data
is framed with start and stop bits.
4. Set TR1 to 1 to start timer 1.
5. Clear RI with CLR RI instruction..
6. Check the RI flag bit with instruction JNB RI, XXXX to see if an entire character
has been received yet.
7. If RI is set, SBUF has the byte. Save
this byte.
8. Go to step 5 to receive the next
character.
Example 10.5.4 Write an 8051 Assembly language program to receive bytes serially withbaud
rate 9600, 8-bit data and 1 stop bit.
Solution :
Simultaneously send received bytes to por
MOV TMOD, #20H
timer 1, mode 2 (auto reload)
MOV TH1, #FDH
9600 baud rate
MOV SCON, #5OH
SETB TR1 8-bit, 1stop, REN enabled
HERE: start timer 1
JNB RI, HERE
MOV A, SBUF wait for character receive completely
MOV P2, A save the received character
CLR RI send character to port 2
SJMP HERE Get ready to receive next byte
Go to receive next character

TECHNICAL PUBLICATIONS -An up thrust for knowledge


MicroprOcessors and Microcontrollers 10- 19 8051 Serial Port and Programming

The importance of the RI flag bit.


When a data is to be received via RxD pin, first the start bit, and a data byte with
one bit at time are received sequentially via RxD pin. When the last bit of a data byte is
received, a byte is formed and the SBUF register is loaded with this byte. Then the stop
bit is received. During the reception of the stop bit, 8051 sets the RI flag, ie. RI =
1. This
indicates that the entire data byte has been received. This data byte which is loaded in
the SBUF register, should be placed to a safe place such as any
before it is lost. The programmer has to clear the RI flag, ie. RI = 0,register or memory
with the 'CLR RI
instruction to receive next data and place it in SBUF register. The RI flag bit should be
monitored to make sure that the entire byte has been received. This
flag bit till it becomes one to ensure the reception of monitoring of the RI
comnplete data place it in SBUF
register, copy it to the safe place and then make RI zero with 'CLR RI
the necessary steps to avoid any loss of received data.
instruction are

10.5.3 Doubling the Baud Rate in the 8051


We can double the baud rate in 8051
using two way
By doubling the crystal frequency.
By making SMOD bit in the PCON register from 0 to 1.
Example 10.5.5 Write a program to recetve message from PC to 8051.
Message string is
"Hello After this micro controller sends message to PC "Fine"
Solution : The Fig. 10.5.1 shows the connections between 8051 and PC.
8051

TXD
(P3.1)
To PC
COM Port
RxD
(P3.0)

Fig. 10.5.1
MOVTMOD, #20H Initialize timer 1 in mode 2
MOVTH1, #0FDH Load count to get 9600 baud rate
MOV SCON, #5OH 8-bit, 1 stop, REN enabled
SETB TR1 Start timer 1
MOVDPTR, #2000H Initialize memory pointer to
save received data
MOVR0,#05H Initialize counter to read 5 characters
RECV: JNB RIRECV wait for character

TECHNICAL PUBLICATIONS - An up thrust for knowledge


10- 20 8051 Serial Port and Programming
Microprocessors end Microcontrollers

Read the character


MOVA,SBUF
Save it in memory
MOVX @DPTR,A increment memory pointer
INC DPTR character
CLR RI Get ready for next
DJNZ RO, RECV If not last character repeat
MOV DPTR, #MYDATA; Initialize pointer for message
CLR A characters
Initialize counter to send 4
MOV RO, #4H
Get the character
MOVC A, @A+DPTR
Load the data
MOV SBUF, A
HERE: JNB TI,HERE Wait for complete byte transfer
character
CLR TI get ready for next

10.5.4 Examples
transfer the message GOODLUCK
Example 10.5.6 Write an ssembly language program to GTU :Winter-06, 15
sertally at 9600 baud, 8-bitdata, 1 stop bit.
Solution :
MOV TMOD, #20H ;timer 1, mode 2
MOVTH1, #FDH ;9600 baud rate
MOV SCOH, #50H ;8 bit, 1 stop bit, REN-enabled.
SETB TRI, ;start timer 1
START MOV A, #"G" ;transfer "C
ACALL TRANS
MOVA, #"O" ;transfer "O"
ACALL TRANS
MOV A, #"O" ;transfer "O"
ACALL TRANS
MØV A, #"D" ;transfer "D"
ACALL TRANS
MOV A, #"L" ;transfer "I"
ACALL TRANS
MOV A, #"U" ;transfer "U"
ACALL TRANS
MOV A, #"C" ;transfer "CH
ACALL TRANS
MOV A, #"K" ;transfer "K"
;serial data transfer subroutine
TRANS: MOV SBUF, A;load SBUF
HERE JNB TI, HERE;wait for last bit to transfer
CLR TI ; clear TI for next
RET ;character

TECHNICAL PUBLICATIONSs - An up thrust for krnowledge


Microprocessors and Microcontrollers 10 - 21
8051 Serial Port and Programming
Example 10.5.7 Write an assembly level program for 8051 to
in internal memory serially by
transmit ten bytes of data stored
selecting proper baud rate, data length and stop bit.
Solution :
GTU : Winter-07
MOV TMOD, #20H ;time 1,
MOV TH1, #FDH
mode 2
:9600 baud rate
MOV SCON, #50H
;8-bit 1 stop bit, REN enabled
MOV R1, #10 : Initialize counter
MOV RO, #20H :Initialize pointer
SETB TR1 ;Start timer1
START : MOV A, @RO ;Get the byte
A CALL TRANS
INC RO ;incremnent pointer
DJNZ R1,START ;if R1#0,repeat
TRANS : MOVSBUF,A ;Load SBUF
HERE: JNB TI,HERE :Wait for the last bit to
CLR TI transfer
:Clear TI for the next character
RET

Example 10.5.8 Write 8051 interrupt program to do the


) Receive data serially and sent it to P1 following
i) Make timer-0 to generate a square
continuously.
wave of 5kHz
Assume XTAL 11.059 MHz at a band of 9600. frequency at port PO.1
Solution : GTU: Summer-09
ORG O
LJMP MAIN
ORG 000BH ; ISR for Timer 0
CPL P0.1 ;toggle P0.1
RETI ;Return from ISR
ORG 23H
LJMP SERIAL ;jump to serial interrupt routine
ORG 30H
MAIN: MOV TMOD, #02H ; Timer 0, mode 2, auto-reload
MOV THO, #-92 : for 5 kHz wave
MOV IE, #10010010B enable serial, timer 0 interrupt
SETB TRO Start timer 0
AGAIN: SJMP AGAIN
ORG 10OH
SERIAL: JB TI, TRANS ;jump if TI is high otherwise
MOV A, SBUF :Receive data
MOV P1, A :Send it to port 1
TRANS: CLR RI Clear RI
TECHNICAL PUBLICATIONS - An up thrust for knowledge
10- 22 8051 Serial Port and Programming
Microprocessors and Microcontrolers

;Return from ISR


RETI
END
Count for 5 kHz
1 = 2x 10- s
T =
5 kHz

TON = ToFF =T/2 = 1x 10- s


Crystal frequency x 1x 10-4
Count
12

X1x 10 92
1.085x 10-6
Example 10.5.9Write 8051 progTamto receive bytesof aata serally ana put them tn PI Set
GTU: Summer-17, Marks 7
the baud rate as 4800, 8 bit aata and one stop bit.
Solution :
MOV TMOD, #20H : Timer 1, mode 2 (auto-reload)
MOV TH1, #-6 :4800 baud rate
MOV SCON, #50H ;8-bit, 1 stop bit,REN enabled
SETB TR1 ;start Timer 1
HERE: JNB RI, HERE :Wait for character to come in
MOVA, SBUF ;save incoming byte in A
MOV P1, A :Send received byte to port 1
CLR RI ;Get ready to receive next byte
SJMP HERE :Continue receiving data

Example 10.5.10 Write instructions to initialize serial port in mode 1 with baud rate of 80
and crystal frequency of 11.059, MH,
o*****

Solution :
MOV TMOD, #20H ;timer 1, mode 2(auto reload)
MOV TH1, #FAH ;count for baud rate 4800
MOV SCON, #50H ;8-bit, 1stop, mode 1
SETB TR1 ;start timer 1

TH1 = 256 KxOSCFreq.


384× Baud rate

= 256
1x11.059 MHz
384 x 4800

= 250 = FAH

TECHNICAL PUBLICATIONS An up thrust for knowledge


MicroprDcessors and Microcontrollers
10-23 8051 Serial Port and Programming
Example 10.5.11 Write a program using interrupts to do the following:
) Receive data serially and send it to P,
) Have P, port read and transmitted serially and a
copy given to P,
i) Make timer 0 generates 0square wave of 5 kHz
frequency on P0.1
Assume that XTAL = 11.059 MHz, set the buud rate at
4800. GTU : Winter-08
Solution :
ORG 0
LJMP MAIN
ORG 000BH :ISR for timer 0
CPL PO.1 ;toggle P0.1
RETI ;Retum from ISR
ORG 23H
LJMP SERIAL ;jump to serial int. ISR
ORG 3OH
MAIN: MOV P1, #0FFH ;Configure P1 input port
MOV TMOD, #22H ;timer 0 and 1, mode 2, auto reload
MOV TH1, #0F6H ;4800 Baud rate
MOV SCON, #50H ;8bit, 1 stop, REN enabled
MOVTHO, #-92 :For 5 kHz wave
MOV IE, #10010010B ;enable serial timer 0 int
SETB TR1 ;Start timer 1
SETB TRO ;Start timer 0
UP: MOV A, P1 ;Read data from port 1
MOV SBUF, A ;Send a copy to SBUF
MOV P2, A ;Write to port 2
SJMP UP ;Stay in loop
;Serial port ISR
ORG 10OH
SERIAL: JB TI, TRANS ijump if TI is high
MOV A, SBUF ;otherwise send serial
MOV P0, A ;data to port 0
CLR RI ;Clear RI
RETI ;Returm from ISR
TRANS : CLR TI ;Clear TI
RETI Return from ISR
END
Example 10.5.12 Write an 8051 ALP to send the message CORRECT or FALSE' serially
On the status of aswitch connected to pin P2.2 f it is ON
or OFF, respectively.
dependi
Assumeng the crystal frequency as 11.0592 MHZ. Ise 2400 baud rate, & bit data
and one
GTU : Winter-11
Stop bit format. Show delay calculation.
knowledge
TECHNICAL PUBLICATIONS- An up thrust for
8051 Serial Port and
Microprocessors and Microcontrollers 10-24 Programming

Solution:
SW1 EQU P2.2
;Starting position
ORG OH
MAIN : MOV TMOD, #20H : Timer 1, mode 2
baud rate
MOV TH1, #-12 ;(28800/12) = 2400
MOV SCON, #50H ;8-bit data, 1 stop bit
SETB TR1 ;Start Timer 1
SETB SW1 :Make SW an input
ST: JBP2.2, NEXT ;Check the status of SW1
MOV DPTR, #MESS1 : if SW1 = 0 send FALSE'
BACK: CLR A
MOVC A,@A+DPTR : Read the value
JZLAST
ACALL SENDCOM :Send value to serial port
INC DPTR : Point to the next value
SJMP BACK ;Repeat
NEXT : MOV DPTR, #MESS2:If SW1 = 2 send 'CORRE CT
BACK1: CLR A
MOVC A, @A+DPTR :Read the value
JZLAST
ACALL SENDCOM ;Send value to serial port
INC DPTR :Point to the next value
SJMP BACK1 ;Repeat
LAST: NOP

SENDCOM :MOV SBUF, A ;Place the value in buffer


HERE : JNB TI, HERE :Wait until transmitted
CLR TI ;Clear
RET ;Return

MESS1: DB "FALSE", 0
MESS2: DB "CORRECT" 0
END

Delay Calculations:
With XTAL = 11.0592 MHz we have,
11.0592/12
= 921.6 kHz machine cycle frequency
921.6 kHz/32 = 28800 Hz frequency used by Timer 1 to set the baud rate
Baud rate = 28800/12 = 2400

TECHNICAL PUBLICATIONS - An up thrust for knowledge


MicroprOcessors and Microcontrolers 10- 25 8051 Serial Port and Programming

Review Questions

1. How will you double the baud rate in 8051 ?


2. With the help of example explain programming of serial interrupt.
3. Write an assembly language program for 8051 microcontroller to transfer the message "VTU"
serially with 9600 baud, 8-bit data, 1 stop bit. Give the complete harduare details for the design.
XTAL = 11.0592 MHz.

4. Write an 8051 program to send the message "The Earth is beautiful", to the serial port
continuously. Assume XTAL = 11.0592 MHz, 9600 baud rate, 8-bit data and one stop bit.
5. Write 8051 program to transfer serially the message "VITUBELGALUM" continuously at baud rate
of 9600.
6. Explain the importance of TI and RI flags.
7. Indicate steps of programming 8051 to transmit a character and recetve a character serialy.
10.6 Serial Communication Programming in C
GTU : Winter-10, 11, 13, 17, Summer-10, 15, 19

We have seen that the SFR registers of the 8051 are accessible directly in 8051 C
compilers by inclusion of the reg51.h file. However, to use second serial port we have to
declare the byte addresses of the new SFR registers, ie. SBUF1 and sCONI using SFR
data type. In additiorn, we have to declare bit addresses for TI1 and RIl using bit data
type.
Example 10.6.1 Writen Cprogram for the 8051 to tnansfer the letter "C serialty at
9600 baud continuously.Use 8-bitdataand1 stopbit GTU : Winter-10
Solution:
#include<reg51.h>
void main (void)

TMOD= (x20;
TH1 = XFD;
SCON = x50;
TR1 = 1;
while(1)
{
SBUF='C;
while(TI ==0);
TI=0;

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Microprocessors and Microcontrollers 10- 26 8051 Serial Port and Programming

Example 10.6.2 Write aC program that continuously receives asingie bit of data from P1.0
and sends it to P2.0, while simultaneously creating a square wave of 400 us period on pin
P2.5. Use timer 0to create the sauare wave. Assume that XTAL 11.0592 MHz.
Solution : We will use timer 0 in mode 2 (auto-reload). One half of the period is 200 us.
200/1.085 us = 184 and THO = 256 184 = 72 or 48H.
#include <reg51.h>
sbit IBt = P1^0;
sbit Obit = P2^0:
sbit SWNAVE = P2^5;
void timer0(void) interrupt 1
{
SWAVE = ~ SWAVE; /* toggle pin P2.5 */

void main(void)
{
Obit = 1; /* make P1.0 input */
TMOD = 0x02;
THO = Ox72; * THO = - 184 */
IE = 0x82; /* enable interruptsfor timer 0 */
while(1)

Obit = Ibit; * send received bit to P2.0 */

Example 10.6.3 Nrte a Cprogram that continuously getsa single bit of data from P1.0and
sends it to P2:0 in the main, while simultaneously (a) creating a squarewave of 400 JS
period on pin P2.5, and (b) sendingletter 'A through Z to the serial port. Use timer0
to create the squarewave.Assume thatXTAL = 11.0592 MHz. Use the 9600 baud Tate.
GTU: Winter-17, Marks 7
Solution : We will use timer 0in mode 2 (auto-reload). THO = 200/1.085 us =- 184,
which is 72H.
#include <reg51.h>
sbit Ibit = P1^0;
sbit Obit = P2^0;
sbit SWAVE = P2^5;
unsigned char ch = 'A:
void timerD(void) interrupt 1

SWAVE=~SWAVE; /* toggle pin 2.5 */

vold serial0(void) interrupt 4

TECHNICAL PUBLICATIONS - An up thrust for


knowledge
MicropfoceSSOrs and Microcontrollers
10- 27
8051 Serial Port and
{ Programming
if(TI == 1)
SBUF = ch; * send
TI = 0; character to serial port */
/* clear interrupt */
ch++;
if(ch >= Z)
ch ='A;

else

RI = 0; /* clear interrupt */

void main(void)

Ibit = 1; /* make switch input */


TH1 = -3; /* 9600 baud */
TMOD = Ox22; /* set mode 2 for both timers */
THO = Ox72; /* load 72H as a timer count */
SCON =0x50;
TRO = 1;
TR1 = 1; /* start timer */
IE = 0x92; /* enable interrupt for TO
while(1) /* stay here */
{
Obit = Ibit; /* send received bit to bit P2.0 */

Example 10.6.4 Write a Cprogram usinginterruptsto do thefollowing:


a)Receive data serially and send # to PO
b)RendportPl, transmit data serially, and gibe a copy to Z
O Set tiner 0, generate a square wave of 2.5 kHz frequency on PU.1
Assume that XTAL = 11.0592 MHz. MHz.Set
Set the baud rate at 9600.
Solution:
#include <reg51.h>
sbit SWAVE = P0^1;
vold timer0 () interrupt 1

SWAVE = ~ SWAVE; /* toggle pin "/

void serial0() interrupt 4

if(TI == 1)
TECHNICAL PUBLICATIONS - An up thrust for knowledge
8051 Serial Port and Programming
10- 28
MicroprOcessors end Microcontrollers

TI = 0; /* clear interrupt */

else

*/
PO = SBUF: /* put value on pins
RI = 0; /* clear interrupt */

void main()
{
unsigned char c;
* make P1 an input "/
P1 = 0xFF;
TMOD = 0x22;
TH1 = OxFO; /* 9600 baud rate */
SCON = Ox50;
THO = Ox72; /* 2.5 kHz has T= 400 us */
IE = 0x92; /* enable interrupts "/
TR1 = 1; /* start timer 1 */
TRO = 1; /* start timer 0 */
while(1)

C= P1; * read data byte from port P1 */


SBUF = C; /* put data byte in buffer
P2 = C; /* send data byte to port P2 */

Example 10.6.5 Write u 8051 C program that continuously gets a single bit of data from
PL7 and sends it to Pl.0, which creates a square waoe of 200 us period on pm
XTAL frequency = 1l.0592 MHz. GTU : Summer-10
Solution: We will use timer 0in mode 2 (auto-reload). One half of the period is 100 S.
100/1.085 us = 92 and TH0 = 256 92 = 164 or A4H.
#include <reg51.b>
sbit IBit = P1^7;
sbit Obit = P1^0;
sbit SWAVE = P2^5;

void timer0(void) interrupt 1

SWAVE = ~ SWAVE; /* toggle pin P2.5 "/

TECHNICAL PUBLICATIONS - An up
thrust for
Microprocessors and Microcontrollers 10- 29 8051 Serial Port and Programming

vold main(vold)

Obit = 1; /* make P1.7 input */


TMOD = Ox02;
THO = OxA4; /* THO = -92 */
IE =0x82; /* enable interrupts for timer 0 */
while(1)

Obit = Ibit; /* send received bit to P1.0 *l

Example 10.6.6 Write an 8051 Cprogram to i) Continnously read the status of


switch
Connected to pin PI.2 and send it to pin P2.1 in the main program and ii) Generate 4
square wave of 100 usec period on P2.3 and send character continuously serially usng
timer and serial interrupt routines, respectively. Use XTAL frequency as 11.0592 MHz
and 8 bits data, one stopbit, 4800 baud rate format. GTU: Winter-11
Solution: We will use timer 0 in mode 2 (auto-reload).
For square wave TON = T/2 = 50usec
12
1T-state 1.085 usec
11.0592x106
50 usec
TH0 = =-46
1.085 usec
= 256 - 46 = 210

= D2H

#include <reg51.b>
sbit SW =P1^2;
sbit ST =P2^1;
sbit SWAVE = P2^3;
vold timer 0(void) interrupt 1

Swave = ~ Swave: /* toggle pin "/


}
void
serial 0)interrupt 4

if (TI = = 1)

SBUF = *: /*send ' to serial port*/


TI = 0; /* clear interrupt */
TECHNICAL PUBLICATIONS An up thrust for knowledge
Microprocessors and Microcontrollers 10-30 8051 Serial Port and
Programming

else

RI =0; /* clear interrupt */

void main ()

SW = 1; /* Make switch input */


TH1 =- 6; /* 4800 baud rate */
TMOD = 0X22; * Mode 2 for both timers */
THO = 0XD2; /* Load count D2H for timer 0 */
SCON = 0X50; /* 8-bit data, 1-stop bit */
TRO = 1; /* start timer 0 */
TR1 =1; /* start timer 1 */
IE = 0X92; /* Enable interrupt for T0 */
While (1) /* wait for interupt */
{
ST = SW; /* send status of SW to pin P2.1 */

Example 10.6.7 Write progtam to receive 16 data bytes from computer to


microcontroller
8051 through serial port Store data from memonn location 0400h
onwards.
GTU: Winter-13, Marks 7
Solution : Assume the baud rate = 4800, 8-bit data and 1 stop bit
MOV RO, #10H Initialize iteration counter
MOV TMOD, #20H timer 1, mode 2 (auto reload)
MOV TH1, #- 6 : 4800 baud rate
MOV SCON, #50H ; 8-bit, 1stop, REN enabled
MOV DPTR, #0400H ; Initialize memory
SETB TR1
pointer
;start timer 1
HERE : JNB RI, HERE : wait for
character to come in
MØV A, SBUF save the incoming character
MOV@DPTR, A :save character in memory
INC DPTR :increment
CLR RI
memory pointer
:get ready to receive next
DJNZ R0, HERE :If iteration counter #0,
character
repeat
Microprocessors and Microcontrollers 10- 31 8051 Serial Port and Programming

Example 10.6.8 Write a program to send the message "Happy New Year to serial port.
Assume aswitch SW is connected to pin P1.2. Monitor its status and set the baud ate as
follows : If SW 0, 4800 baud rate otherwise SW = 1 9600 baud rate. Assume
XTAL = 11.0592 MHz, 8-bit data and 1stop bit GTU : Summer-15, Marks 7
Solution :
SW BIT P1.2
ORG 0 ;starting position
MAIN: MOV TMOD, #20H
MOV TH1, # -6 :4800 baud rate (default)
MOV SCON, #50H
SETB TR1 ;start Timer 1
SETB SW ; make SW an input
CH: JNB SW, BR48 ;check SW status
MOV A, PCON Read PCON
SETB ACC.7 ;set SMOD High for 9600 baud rate
MOV PCON, A ;write PCON
SJMP OVER : send message

Example 10.6.9 Write a C program to continuously transter the


message "YES serially at
9600 baud, 8 bit data and I stop bit
GTU: Summer-19. Marks 7
Solution:
#include <reg51.h>
void SerialTx(unsigned char);
void main(void)

TMOD=0x20; /* use Timer 1, mode 2 */


TH1=0xFD; /* 9600 baud rate */
SCON=0x50;
TR1=1; /* start timer */
while (1)

SerialTx(Y);
SerialTx(E');
SerialTx(S);
}
void SerialTx(unsigned char x)

SBUF =x, /* place value in buffer */


while (TI==0); /* wait until transmitted */
TI=0;
TECHNICAL PUBLICATIONS- An up thrust for knowledge
MicroprDCessors and Microcontrollers 10- 32 8051 Serial Port and Programming

Review Questions
1. Write an 8051 Cprogram to transfer the message "Good morning" serially at 9600 baud, 8-bit
data, 1stop bit.
2. Write 8051 'C program to receive bytes of data serially and put themn in P1, Set the baud rate as
4800, 8 bit data and one stop bit.
3. Write a 8051 Cprogram to transmit the character * continuously serially in the &bit, 1start bit,
1stop bit, 2400 baud rate format. Assume the crystal frequency as 11.0592 MHz.

10.7 Short Questions and Answers


Fill in the Blanks
Q.1 In 8051, SCON is located at [Ans. :98H]
Q.2 In 8051, serial port uses register to hold data. [Ans. :SBUF]
Q.3 In 8051, register is used by serial port to control data rates. [Ans. : PCON]
@.4 In mode 0 of 8051's serial port, the baud rate is fixed at
1
[Ans.: the oscillator frequency]
12
Q.5 In mode 1 of 8051 serial port, bits are transmitted or received. [Ans. : 10]
Q.6 The baud rate of mode 1of 8051 serial port is [Ans. : variable]
Q.7 Define SBUF register in 8051 and mention its use.
Ans. : SBUF is an 8-bit register dedicated for serial communication in 8051. Its address
is 99H. It can be addressed like any other register in 8051. Writing to SBUF loads data
to be transmitted and reading SBUF accesses received data.
Q.8
Mention the registers used for serial communication in 8051 microcontroller.
(Refer section 10.3)
Multiple Choice Questions
Q.1 The serial port of 8051 is
half duplex b full duplex
C simplex d none of these [Ans. : b]
.....

Q.2 Serial port of 8051 uses register SCON to


;*******

a control data communication bË control data rates


C hold data idË all of the abOve
........

..... [Ans. :a]


Q.3 The baud rate of mode 2 of 8051 serial port is
1
programmable to the oscillator
....

32 frequency
b 1
programmable to the oscillator frequency
64
C fixed dË either (a) or (b) [Ans. : d]

TECHNICAL PUBLICATIONS - An up thrust for knowledge

You might also like