Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
262 views49 pages

8051 Serialcommunication

Serial communication uses asynchronous or synchronous methods to transmit data one bit or block of bits at a time. Asynchronous serial communication transmits each byte with a start bit and stop bit, while synchronous transmits blocks of bytes continuously. The 8051 chip contains a built-in UART for asynchronous serial communication through a MAX232 line driver to convert voltage levels between TTL logic and RS232 standards. Common RS232 connectors use a 9-pin D-sub connector with various pins assigned for transmitted data, received data, handshaking signals and more.

Uploaded by

Santhanamari G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
262 views49 pages

8051 Serialcommunication

Serial communication uses asynchronous or synchronous methods to transmit data one bit or block of bits at a time. Asynchronous serial communication transmits each byte with a start bit and stop bit, while synchronous transmits blocks of bytes continuously. The 8051 chip contains a built-in UART for asynchronous serial communication through a MAX232 line driver to convert voltage levels between TTL logic and RS232 standards. Common RS232 connectors use a 9-pin D-sub connector with various pins assigned for transmitted data, received data, handshaking signals and more.

Uploaded by

Santhanamari G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 49

8051 Serial Communications

1
Parallel vs. Serial
• Parallel Communication
(Printer) One byte at a time or more
– Expensive
– Fast, but distance cannot be great.
– Example: printer, hard disks
Sender Receiver

• Serial Communication (Telephone


line)
– Long distance using communication lines
such as a telephone One bit/data line
Sender Receiver
– cheaper
City City B
A

2
Serial Communications
• Serial Communication (Telephone line)
• P/S: Parallel-in-Serial-out Shift
Register
• S/P: Serial-in-Parallel-out Shift
Register
• D/A: Digital-Analog Converter
• A/D: Analog-Digital Converter

Sender Receiver
P/S D/A A/D S/P
D/A D/A City B
City
A RS232

PC 8051

UART 3
COM 1 port MAX232
Duplex vs. Simplex
• Duplex
PC Tx and Rx PC

• Simplex (only transmit)

Only Tx
PC Printer

ECE473/573 4
Microprocessor System Design, Dr.
Shiue
Duplex
• Full Duplex: Data is transmitted both way at the
same time.

Tx Need 2 lines Rx

Rx Tx

• Half Duplex: Data is transmitted one way at a


time.
Tx Need 1 lines Rx

Rx Tx

5
Asynchronous vs.

Synchronous
Two methods of Serial Communication
– Synchronous: Transfer block of data (characters) at a time.
– Asynchronous: Transfer a single byte at a time.
• You could write S/W to use either of these methods, but
the programs can be tedious and long. So, H/W is
developed instead.
• This H/W is called UART or USART
• UART: Universal Asynchronous Receiver-Transmitter
• USART: Universal Synchronous-Asynchronous Receiver-
Transmitter
• The 8051 chip has a built-in UART.

6
Asynchronous vs. Synchronous

Serial communication uses two methods:


In synchronous communication, data is sent in blocks of
bytes.
time

10101010 10101111 byte byte byte byte


preamble
receiver sender

In asynchronous communication, data is sent without


continuity.

byte byte byte

receiver sender
start bit stop bit
7
Asynchronous
Communications

Framing
ASCII Character

STOP D7 D6 D5 D4 D3 D2 D1 D0 START mark


space bit bit

One or two bits ‘1’


Only one bit ‘0’
Go out last!
Go out first!

• How to detect that a character is sent via the line in the 8

asynchronous mode? -Each character is placed in between start and


stop bits. This is called framing.
Asynchronous
• New System: ASCII character A (0100 0001B) € 8-bit
– When there is no transfer, mark is ‘1’ and space is ‘0’.

Space 1 0 1 0 0 0 0 0 1 0 Mark
=0 =1

• Older System: ASCII is 7-bit


– Due to the slowness of receiving mechanical devices, 2 stop bits
were used to give the device sufficient time to organize itself.

2 7-bit ASCII Character 1

Stop bits Start bit

9
Parity Bit
• Even-Parity bit system
– 01001011 € # of ‘1’=4 € P=0
• Odd-Parity bit system
– 01001011 € # of ‘1’=4 € P=1
• UART chips allow programming of the parity bit for
odd-, even-, and no-parity options.

10
Data Transfer Rate
How fast is the data transferred?
Three methods to describe the speed:
Baud rate is defined as the number of signal changes per second. The rate of data
transfer is stated in Hz (used in modem).
Data rate is defined as the number of bits transferred per second.
Each signal has several voltage levels.The rate of data transfer is stated in bps
(bits per second).
Effective data rate is defined as the number of actual data bits transferred per second.
Redundant bits must be removed
• Data Transfer Rate
– Bits per second (bps) is also called Baud rate (the number of signals
changes per second)
– IBM PC/XT € 100 ~ 9600 bps
– Pentium PC € 56k bps (recent MODEM)
• Asynchronous data communication, the baud rate is limited to 100,000 bps.
• Interfacing standard: RS232
11
RS232
• PC: COM1 (Mouse), COM2: (MODEM & RS232)
• RS232 is set by Electronics Industries Association (EIA)
in 1960.
• 1963: RS232A
• 1965: RS232B
• 1969: RS232C
• RS232 is a serial I/O interfacing standard; however, since
the standard was set long before the advent of the TTL
logic family, its input and output voltage levels are not
TTL compatible.
• TTL is Transistor-Transistor Logic family (Bipolar
transistor) in 1968

12
Line Driver: MAX232
• RS232 is not compatible to TTL. For this reason, to connect any
RS232 to a microcontroller system, we must use “Voltage
Converters” such as MAX232 to convert the TTL logic levels to
the RS232 voltage levels, and vice versa. RS 232 Voltage
• MAX232 IC chips is referred to as “Line Driver”.
25V
MAX232, TSC232, ICL232
logic 0

3V
undefined
-3V

logic 1

-25V

13
RS232 Connector: DB-9
• DB-9 Connector

Pin function description


1 !DCD Data Carrier Detect
1 5
2 RxD Receive
3 TxD Transmit
4 DTR Data Terminal Ready
5 GND Ground
6 !DSR Data Set Ready
7 !RTS Request To Send
8 !CTS Clear To Send
9 RI Ring Indicator

14
8051 Connection to
RS232
• Since RS232 standard is not TTL compatible, so we
need MAX232 (line driver)

8051
11 (P3.1) 11 T1 in T1 14 2
out
MAX232 DB-9 (RS232 Connector)
10 (P3.0) 12 13 3
R1 out R1 in RxD
TxD RxD

TxD

5
GN
D

15
DB-9 9-Pin Connector

1 5

6 9

16
IBM PC DB-9 Signals for DTE

Pin Description
1 Data carrier detect (DCD)
2 Received data (RxD)
3 Transmitted data (TxD)
4 Data terminal ready
(DTR)
5 Signal ground (GND)
6 Data set ready (DSR)
7 Request to send (RTS)
8 Clear to send (CTS)
9 Ring indicator (RI)
17
RS232 Handshaking Signals

Many of the pins of the RS232 connector are used for


handshaking signals.
DTR (data terminal ready)
DSR (data set ready)
RTS (request to send)
CTS (clear to send)
DCD (carrier detect, or data carrier detect)
RI (ring indicator)

18
Communication Flow
1.While signals DTR and DSR are used by the PC and modem, respectively, to
indicate that they are alive and well.
2.RTS and CTS control the flow of data.
3.When the PC wants to send data, it asserts RTS.
4.If the modem is ready (has room) to accept the data, it sends back CTS.
5.If, for lack of room, the modem does not activate CTS, and PC will de-assert DTR
and try again.

19
DB9 - DB25 conversion

DB9 DB25 Function


1 8 Data carrier detect
2 3 Receive data
3 2 Transmit data
4 20 Data terminal ready
5 7 Signal ground
6 6 Data set ready
7 4 Request to send
8 5 Clear to send
9 22 Ring indicator
20
Baud Rates in the 8051

The 8051 transfers and receives data serially at many different baud
rates by using UART.
UART divides the machine cycle frequency by 32 and sends it to
Timer 1 to set the baud rate.
Signal change for each roll over of timer 1
11.0592 MHz
Machine cycle
28800 Hz
frequency ÷ 32
XTAL
÷ 12 Timer 1
oscillator By UART
921.6 kHz To timer 1
To set the
Baud rate

21
Baud Rates in the 8051

Timer 1, mode 2 (8-bit, auto-reload)


Define TH1 to set the baud rate.
XTAL = 11.0592 MHz
The system frequency = 11.0592 MHz / 12 = 921.6 kHz
Timer 1 has 921.6 kHz/ 32 = 28,800 Hz as source.
TH1=FDH means that UART sends a bit every 3 timer
source.
Baud rate = 28,800/3= 9,600 Hz

22
Example
With XTAL = 11.0592 MHz, find the TH1 value needed to have the
following baud rates. (a) 9600 (b) 2400 (c) 1200
Solution:
With XTAL = 11.0592 MHz, we have:
The frequency of system clock = 11.0592 MHz / 12 = 921.6 kHz
The frequency sent to timer 1 = 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/12th of the crystal frequency by 32 is the


default value upon activation of the 8051 RESET pin.

23
Timer 1 TH1 Register Values for Various Baud Rates

24
SBUF Register

Serial data register: SBUF


An 8-bit register to store the serial data
For a byte of data to be transferred via the TxD line, it must be placed in
the SBUF.
MOV SBUF,A ;send data from A
SBUF holds the byte of data when it is received by the 8051’s RxD line.
MOV A,SBUF ;receive and copy to A
Not bit-addressable

25
SCON Register

Serial control register: SCON


SM0, SM1 Serial port mode specifier
REN (Receive enable) set/cleared by software
to enable/disable reception.
TI Transmit interrupt flag.
RI Receive interrupt flag.
SM2 = TB8 = RB8 =0 (not widely used)

(MSB) (LSB)

SM0 SM1 SM2 REN TB8 RB8 TI RI


* SCON is bit-addressable. 26
SM0, SM1

SM0 and SM1 determine the framing of data.


SCON.6 (SM1) a1d SCON.7 (SM0)
Only mode 1 is compatible with COM port of IBM PC.

SM0 SM1 Mode Operating Mode Baud Rate

0 0 0 Shift register Fosc./12


0 1 1 8-bit UART Variable by timer1
1 0 2 9-bit UART Fosc./64 or Fosc./32

1 1 3 9-bit UART Variable

27
Mode 0: Shift Register mode
Transmission and Reception via RxD and TxD connected internal shift
frequency
Mode 1: standard UART
10 bit full duplex UART
Timer 1 in mode 2 Otherwise

for standard 9600hz


for standard 9600hz

Mode 2;Multiprocessor mode[11 bits -1+9 (8data+TB8/RB8 in SCON)+1]

Mode 3 : 9 bit UART - similar to mode 2 and baud rate is determined as


in mode
REN (Receive Enable)

SCON.4
Set/cleared by software to enable/disable reception.
REN=1
It enable the 8051 to receive data on the RxD pin of
the 8051.
If we want the 8051 to both transfer and receive data,
REN must be set to 1.
SETB SCON.4
REN=0
The receiver is disabled.
The 8051 can not receive data.
CLR SCON.4

29
TI (Transmit Interrupt Flag) & RI (Receive Interrupt)

SCON.1
When the 8051 finishes the transfer of the 8-bit character, it raises
the TI flag.
TI is raised by hardware at the beginning of the stop bit in mode 1.
Must be cleared by software.

SCON.0
Receive interrupt flag
When the 8051 receives data serially via RxD, it gets rid of the start
and stop bits and place the byte in the SBUF register.
Set by hardware halfway through the stop bit time in mode 1.
Must be cleared by software.

30
SM2

SCON.5
SM2 enables the multiprocessor communication for mode 2 & 3.
SM2=0 : to receive data  two processors talks
SM2=1 : to receive address  multiprocessor talks

1 2 3 4 5

TxD RxD

0 data 0 data 1 address=5


time 31
TB8(the 9th 8 bits
TB8 (Transfer Bit 8) RB8 (Receive Bit 8)
SCON.3
For multiprocessor communication
The 9th bit will be transmitted in mode 2 & 3.
TB8 = 1: address
TB8 = 0: data
Set/Cleared by software.
SCON.2
For multiprocessor communication
In serial mode 1, RB8 gets a copy of the stop bit when an 8-
bit data is received.
In serial mode 2 & 3, RB8 gets a copy of the 9th bit. In fact, it
is the TB8 in transmitter.

32
SCON Serial Port Control Register (Bit Addressable)

SM0 SM1 SM2 REN TB8 RB8 TI RI


SM0 SCON.7 Serial port mode specifier
SM1 SCON.6 Serial port mode specifier
SM2 SCON.5 Used for multiprocessor communication. (Make it 0)
REN SCON.4 Set/cleared by software to enable/disable reception.
TB8 SCON.3 Not widely used.
RB8 SCON.2 Not widely used.
TI SCON.1 Transmit interrupt flag. Set by hardware at the beginning
of the stop bit in mode 1. Must be cleared by software.
RI SCON.0 Receive interrupt flag. Set by hardware halfway through
the stop bit time in mode 1. Must be cleared by software.
Note: Make SM2, TB8, and RB8 = 0.
33
Transfer Data with the TI flag
The following sequence is the steps that the 8051 goes
through in transmitting a character via TxD:
1. The byte character to be transmitted is written into
the SBUF register.
2. It transfers the start bit.
3. The 8-bit character is transferred one bit at a time.
4. The stop bit is transferred.
8-bit char bit by bit
8-bit
SBUF TxD
A
zero detect shift add start and
TI UART stop bits

34
Transfer Data with the TI flag

Sequence continuous:
5. During the transfer of the stop bit, the 8051 raises the TI flag,
indicating that the last character was transmitted and it is
ready to transfer the next character.
6. By monitoring the TI flag, we know whether or not the 8051
is ready to transfer another byte.
– We will not overloading the SBUF register.
– If we write another byte into the SBUF before TI is raised,
the un transmitted portion of the previous byte will be lost.
– We can use interrupt to transfer data in Chapter 11.
7. After SBUF is loaded with a new byte, the TI flag bit must be
cleared by the programmer.

35
SBUF Register
• SBUF Register: For a byte of data to be transferred via
the TxD line, it must be placed in the SBUF.
• SBUF holds the byte of data when it is received by the
8051’s RxD line.

8051

Framed MAX232 DB-9 (RS232 Connector)

11 (P3.1) 11 T1 in 2 RxD
TxD T1 out 14
SBUF
RxD 10 (P3.0) 12 R1 out R1 in 13 3 TxD

Deframed 5
GN
D
36
Programming (Transfer)
1. MOV TMOD, #20H ; Timer 1, mode 2
2. TH1 is loaded to set the baud rate. MOV TH1,#0FDH;Baud rate = 9600Hz
3. MOV SCON, #50H

SM0 SM1 SM2 REN TB8 RB8 TI RI SCON


0 1 0 1 0 0 0 0

REN=1: allows TxD and RxD


4. SETB TR1 ; Run Timer 1
5. REPEAT: MOV SBUF, #’D’
6. Loop: JNB TI, Loop ; Monitor TI
7. CLR TI
8. JMP REPEAT: Repeat step 5 for next character.

37
Examples
• Q2: Write a program for the 8051 to transfer letter ‘A’
serially at 4800 baud rate, continuously.
• Q3: Write a program to transfer the message “Yes”
serially at 9600 baud, do this continuously.

38
Receive Data with the RI flag
The following sequence is the steps that the 8051 goes
through in receiving a character via RxD:
1. 8051 receives the start bit indicating that the next bit is the
first bit of the character to be received.
2. The 8-bit character is received one bit at a time. When the
last bit is received, a byte is formed and placed in SBUF.
remove/check the
start and stop bits
bit by bit 8-bit 8-bit
RxD shift regiter SBUF A
shift load
REN=1 UART RI 39
Receive Data with the TI flag (2/2)

Sequence continuous:
3. The stop bit is received. During receiving the stop bit, the
8051 make RI=1, indicating that an entire character was been
received and must be picked up before it gets overwritten by
an incoming character.
4. By monitoring the RI flag, 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.
– We can use interrupt to transfer data in Chapter 11.
5. After SBUF is copied into a safe place, the RI flag bit must be
cleared by the programmer.

40
Programming (Receive)
1. MOV TMOD, #20H ; Timer 1, mode 2
2. TH1 is loaded to set the baud rate.
3. MOV SCON, #50H

SM0 SM1 SM2 REN TB8 RB8 TI RI SCON


0 1 0 1 0 0 0 0

REN=1: allows TxD and RxD

4. SETB TR1 ; Run Timer 1


5. Loop: JNB RI, Loop ; Monitor RI
6. MOV A, SBUF
7. CLR RI
8. Repeat step 5 for next character.

41
Examples
• Q4: Program the 8051 to receive bytes of data serially,
and put them in P1. Set the baud rate at 4800.
• Q5: Write a program to (a) send to the PC the message
“We Are Ready”, (b) receive any data sent by the PC and
put it ob LEDs connected to P1, and (c) get data on
switches and sent it to PC. The program should perform
part (a) once, but parts (b) and (c) continuously. Use the
4800 baud rate.

42
PCON Register

SMOD Double baud rate. There are two methods. One is to use a
high XTAL. Another one is to use bit in PCON. If Timer
1 is used to generate baud and SMOD=1, the baud rate is
doubled when the Serial Port is used in modes 1,2,3.
GF1,GF0 General purpose flag bit.
PD Power down bit. Setting this bit activates “Power Down”
operation in the 80C51BH. (precedence)
IDL Idle Mode bit. Setting this bit activates “Idle Mode”
operation in the 80C51BH.
(MSB) (LSB)

SMOD -- -- -- GF1 GF2 PD IDL


* PCON is not bit-addressable.

43
SMOD Flag of the PCON Register

Power control register: PCON


MOV A, PCON
SETB ACC.7
MOV PCON,A ;we don’t want to modify other bits
An 8-bit register
Not bit-addressable
SMOD=0: default
SMOD=1: double the baud rate

44
Baud Rate Comparison for SMOD = 0 and SMOD =1

TH1 (Decimal) (Hex) SMOD = 0 SMOD = 1


-3 FD 9,600 19,200
-6 FA 4,800 9,600
-12 F4 2,400 4,800
-24 E8 1,200 2,400
Note: XTAL = 11.0592 MHz.
SMOD = 1
11.0592 MHz Machine cycle To timer 1
freq. 57600 Hz to set baud
÷ 16 rate
XTAL
÷ 12 28800 Hz
oscillator 921.6 kHz
÷ 32
SMOD = 0 45
Example.1

Find the TH1 value (in both decimal and hex) to set the baud
rate to each of the following: (a) 9600 Hz (b) 4800 Hz if
SMOD =1
Assume that XTAL = 11.0592 MHz.
Solution:
With XTAL = 11.0592 and SMOD = 1,
11.0592 / 12 = 921.6 kHz machine cycle frequency.
921.6 / 16 = 57,600 Hz frequency used by the timer 1
(a) 57,600 / 9600 = 6  TH1 = -6 or TH1 = FAH.
(b) 57,600 / 4800 = 12  TH1 = -12 or TH1 = F4H.

46
Example.2

Find the baud rate if TH1 = -2, SMOD = 1, and XTAL = 11.0592
MHz. Is this baud rate supported by IBM/compatible PCs?
Solution:
With XTAL = 11.0592 and SMOD = 1, we have timer 1
frequency == 57,600 Hz.
The baud rate is 57,600 / 2 = 28,800.
This baud rate is not supported by the BIOS of the PC; however,
the PC can be programmed to do data transfer at such a speed.
The software of many modems can do this.

47
Example.3

Assuming that XTAL = 11.0592 MHz for the following program,


state (a) what this program does, (b) compute the frequency used by
timer 1 to set the baud rate, and (c) find the baud rate of the data
transfer.
Solution:
(a) This program transfers ASCII letter B (01000010 binary)
continuously.
(b) and (c) With XTAL = 11.0592 MHz and SMOD = 1
11.0592 / 12 = 921.6 kHz machine cycle frequency.
921.6 /16 = 57,600 Hz frequency used by timer 1 to set the baud
rate.
57,600 / 3 = 19,200, the baud rate.

48
Example.3

MOV A,PCON
SETB ACC.7
MOV PCON,A ;SMOD=1, double baud rate
MOV TMOD,#20H ;Timer 1, mode 2, auto reload
MOV TH1,#-3 ;19200 baud rate
MOV SCON,#50H ;8-bit data,1 stop bit, RI enabled
SETB TR1 ;start Timer 1
MOV A,#”B” ;transfer letter B
A_1:CLR TI ;make sure TI=0
MOV SBUF,A ;transfer it
H_1:JNB TI H_1 ;check TI
SJMP A_1 ;do again

49

You might also like