Vous êtes sur la page 1sur 49

8051 Microcontroller:

Serial Communication
Virendra Singh
Associate Professor
Computer Architecture and Dependable Systems Lab
Department of Electrical Engineering
Indian Institute of Technology Bombay
http://www.ee.iitb.ac.in/~viren/
E-mail: viren@ee.iitb.ac.in

EE-309: Microprocessors

Lecture 15 (20 Aug 2014)

CADSL

8051
Pin
Diagram

P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
RST
(RXD)P3.0
(TXD)P3.1
(INT0)P3.2
(INT1)P3.3
(T0)P3.4
(T1)P3.5
(WR)P3.6
(RD)P3.7
XTAL2
XTAL1
GND

20 Aug 2014

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

EE-309@IITB

8051
(8031)
(8751)
(8951)

40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

Vcc
P0.0(AD0
)P0.1(AD1)
P0.2(AD2
)P0.3(AD3)
P0.4(AD4)
P0.5(AD5)
P0.6(AD6)
P0.7(AD7)
EA/VPP
ALE/PROG
PSEN
P2.7(A15)
P2.6(A14
)P2.5(A13
)P2.4(A12
)P2.3(A11)
P2.2(A10)
P2.1(A9)
P2.0(A8)

CADSL

Serial
Communication
20 Aug 2014

EE-309@IITB

CADSL

RS232 Standard
RS232 is an interfacing standard which is set
by the Electronics Industries AssociaCon (EIA)
in 1960.
RS232 is the most widely used serial I/O
interfacing standard.
RS232A (1963), RS232B (1965) and RS232C (1969),
now is RS232E

Dene the voltage level, pin funcConality,


baud rate, signal meaning, communicaCon
distance.
20 Aug 2014

EE-309@IITB

CADSL

RS232 Voltage Level


The input and output voltage of
RS232 is not of the TTL
compaCble.
RS232 is older than TTL.

We must use voltage converter


(also referred to as line driver)
such as MAX232 to convert the
TTL logic levels to the RS232
voltage level, and vice versa.
MAX232, TSC232, ICL232
20 Aug 2014

EE-309@IITB

RS 232 Voltage

25V
logic 0
3V
-3V

undefined
logic 1

-25V
5

CADSL

MAX232
MAX232 IC chips are commonly referred to
as line drivers.
RS232
PC

COM 1 port

TTL voltage
level

20 Aug 2014

8051

MAX232

RS232 voltage
level

EE-309@IITB

UART
TTL voltage
level

CADSL

RS232 Pins for DTE


1

13

14

25

20 Aug 2014

Pin
1
2
3
4
5
6
7
8
9/10
11
12
13

Descrip+on
ProtecCve ground
TransmiWed data (TxD)
Received data (RxD)
Request to send (RTS)
Clear to send (CTS)
Data set ready (DSR)
Signal ground (GND)
Data carrier detect (DCD)
Reserved for data tesCng
Unassigned
Secondary data carrier detect
Secondary clear to send

EE-309@IITB

CADSL

RS232 Pins for DTE


1

13

14

25

20 Aug 2014

Pin
14
15
16

Descrip+on
Secondary transmiWed data
Transmit signal element Cming
Secondary received data

17
18
19
20
21
22
23
24
25

Receive signal element Cming


Unassigned
Secondary request to sent
Data terminal ready (DTR)
Signal quality detector
Ring indicator
Data signal rate select
Transmit signal element Cming
Unassigned

EE-309@IITB

CADSL

DB-9 Signals for DTE

20 Aug 2014

Pin

Descrip+on

Data carrier detect (DCD)

Received data (RxD)

TransmiWed data (TxD)

Data terminal ready (DTR)

Signal ground (GND)

Data set ready (DSR)

Request to send (RTS)

Clear to send (CTS)

Ring indicator (RI)


EE-309@IITB

CADSL

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)
RTS and CTS are hardware control ow signals.
DCD (carrier detect, or data carrier detect)
RI (ring indicator)

They are not supported by the 8051 UART chips.


20 Aug 2014

EE-309@IITB

10

CADSL

Communication Flow
PC (DTE)

modem (DCE)

Telephone is ringing
ConnecCon between two
modems is set
PC is ready
modem is ready
PC wants to sent data
modem is ready to receive
transmit data

RI
DCD
DTR
DSR
RTS
CTS
TxD, RxD

20 Aug 2014

EE-309@IITB

11

CADSL

DTE and DCE


CommunicaCon Equipments are classied as
DTE (data terminal equipment)
Terminals and computers that send and receive data

DCE (data communicaCon equipment)


CommunicaCon equipment (only for transfer data), modem
DTE view

DCE view

DTE

DCE

DCE

TxD

RxD

RxD

TxD

PC GND
Com1

Telephone
Line

GND
modem

20 Aug 2014

DTE

RxD

TxD

TxD

RxD

GND
modem
EE-309@IITB

GND
PC Com1
12

CADSL

IBM PC/compatible COM ports


IBM PC had 2 COM ports.
Both COM ports have RS232-type connectors.
For mouse, modem

DTE view

DTE view
RS232

PC

COM 1 port

20 Aug 2014

8051

MAX232

EE-309@IITB

UART
13

CADSL

Null Modem Connection


The simplest connecCon between a PC and
microcontroller requires a minimum of three pins,
TxD, RxD, and GND.
null modem connecCon
DTE

DTE

DTE

DTE

TxD

TxD

TxD

TxD

RxD

RxD

RxD

RxD

ground
PC
Com1
20 Aug 2014

8051based
board

PC
Com1
EE-309@IITB

ground

14

PC
Com1

CADSL

TxD and RxD pins in the 8051


In 8051, the data is received from or
transmiWed to
RxD: received data (Pin 10, P3.0)
TxD: transmiWed data (Pin 11, P3.1)

TxD and RxD of the 8051 are TTL compaCble.


The 8051 requires a line driver to make them
RS232 compaCble.
One such line driver is the MAX232 chip.

20 Aug 2014

EE-309@IITB

15

CADSL

MAX232
MAX232 chip converts from RS232 voltage
levels to TTL voltage levels, and vice versa.
MAX232 uses a +5V power source which is the
same as the source voltage for the 8051.
8051
MAX232

P3.1
TxD

11

11

P3.0
RxD

10

12

14

2 7

13

3
DB-25

20 Aug 2014

EE-309@IITB

16

CADSL

MAX 232
MAX232 has two sets of line drivers.
MAX232 requires four capacitors ranging from 1 to 22 mF.
The most widely used value for these capacitors is 22mF.

MAX233 performs the same job as the MAX232 but


eliminates the need for capacitors.
MAX233 and MAX232 are not pin compaCble.

20 Aug 2014

EE-309@IITB

17

CADSL

Inside MAX232
Vcc
+
C1
+
C2
11
12
10
9

TTL side
20 Aug 2014

1
3
4

C3
16

MAX232

6
C4
+

5
T1IN

T1OUT
R1IN

R1OUT
T2IN

T2OUT
R2IN

R2OUT
15
EE-309@IITB

14
13
7
8

RS232 side
18

CADSL

MAX232s Connection to the


8051 (Null Modem)
8051
MAX232
P3.1
TxD

11

P3.0
RxD

10

11

12

14

13

DB-25
TTL-compatible
20 Aug 2014

RS232-compatible
EE-309@IITB

19

CADSL

Inside MAX233
Vcc
13

11

14

MAX233

12
17

2
3
1
20

TTL side
20 Aug 2014

15
16
10

T1IN

T1OUT
R1IN

R1OUT
T2IN

T2OUT
R2IN

R2OUT

EE-309@IITB

5
4
18
19

RS232 side
20

CADSL

MAX233s Connection to the


8051 (Null Modem)
8051
MAX233
P3.1
TxD
P3.0
RxD

11

10

TTL-compatible
20 Aug 2014

DB-25
RS232-compatible
EE-309@IITB

21

CADSL

PC Baud Rates
PC supports several
baud rates.
You can use neWerm,
terminal.exe, sWy, pWy
to send/receive data.

Hyperterminal
supports baud rates
much higher than the
ones list in the Table.

110 bps
150
300
600
1200
2400
4800
9600 (default)
19200
Note: Baud rates supported by
486/PenCum IBM PC BIOS.

20 Aug 2014

EE-309@IITB

22

CADSL

Baud Rates in 8051


The 8051 transfers and receives data serially at
many dierent 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 Cmer 1
11.0592 MHz
XTAL
oscillator

Machine cycle
frequency
12

20 Aug 2014

921.6 kHz

32
By UART

EE-309@IITB

28800 Hz
To timer 1
To set the
Baud rate
23

Timer 1

CADSL

Baud Rates in 8051


Timer 1, mode 2 (8-bit, auto-reload)
Dene 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
Cmer source.
Baud rate = 28,800/3= 9,600 Hz
20 Aug 2014

EE-309@IITB

24

CADSL

Timer 1 TH1 Register Values


for Various Baud Rates
Baud Rate
TH1 (Decimal)
9600
-3
4800
-6
2400
-12
1200
-24
Note: XTAL = 11.0592 MHz.

20 Aug 2014

EE-309@IITB

TH1 (Hex)
FD
FA
F4
E8

25

CADSL

Registers Used in Serial


Transfer Circuit
SBUF (Serial data buer)
SCON (Serial control register)
PCON (Power control register)

20 Aug 2014

EE-309@IITB

26

CADSL

SBUF Register
Serial data register: SBUF
MOV SBUF, #A ;put char A to transmit
MOV SBUF, A ;send data from A
MOV A, SUBF ;copy received data to A
An 8-bit register
Set the usage mode for two Cmers
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.

Not bit-addressable
20 Aug 2014

EE-309@IITB

27

CADSL

SCON Register
Serial control register: SCON
SM0, SM1 Serial port mode specier
REN (Receive enable) set/cleared by somware

to enable/disable recepCon.
TI Transmit interrupt ag.
RI Receive interrupt ag.
SM2 = TB8 = TB8 =0 (not widely used)
(MSB)

(LSB)

SM0 SM1 SM2 REN TB8 RB8

TI

RI

* SCON is bit-addressable.
20 Aug 2014

EE-309@IITB

28

CADSL

SCON Mode bits


SM1 and SM0 determine the framing of data.
SCON.6 (SM1) and SCON.7 (SM0)
.

SM1 SM0
0 0
0 1
1 0
1 1

Mode OperaCng Mode


0
Shim register
1
8-bit UART
2
9-bit UART
3
9-bit UART

20 Aug 2014

EE-309@IITB

Baud Rate
Fosc./12
Variable by +mer1
Fosc./64 or Fosc./32
Variable

29

CADSL

SM2
SM2 enables the mulCprocessor
communicaCon for mode 2 & 3.
We make it 0 since we are not using the 8051
in a mulCprocessor environment.
SM2=0 : Single processor environment
SM2=1 : mulCprocessor environment

20 Aug 2014

EE-309@IITB

30

CADSL

REN (Receive Enable)


Set/cleared by somware to enable/disable recepCon.
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
20 Aug 2014

EE-309@IITB

31

CADSL

TB8 (Transfer Bit 8)


SCON.3
TB8 is used for serial modes 2 and 3.
The 9th bit that will be transmiWed in mode 2
& 3.
Set/Cleared by somware.

20 Aug 2014

EE-309@IITB

32

CADSL

RB8 (Receive Bit 8)


In serial mode 1, RB8 gets a copy of the stop
bit when an 8-bit data is received.

20 Aug 2014

EE-309@IITB

33

CADSL

TI (Transmit Interrupt Flag)



When the 8051 nishes the transfer of the 8-
bit character, it raises the TI ag.
TI is raised by hardware at the beginning of
the stop bit in mode 1.
Must be cleared by somware.

20 Aug 2014

EE-309@IITB

34

CADSL

RI (Receive Interrupt)
Receive interrupt ag. Set by hardware in
mode 1. Must be cleared by somware.
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.
Then 8051 raises RI to indicate that a byte.
RI is raised at the beginning of the stop bit.

20 Aug 2014

EE-309@IITB

35

CADSL

SCON Serial Port Control


Register (Bit Addressable)
SM0 SM1 SM2 REN TB8 RB8

TI

RI

SM0 SCON.7

Serial port mode specier

SM1 SCON.6

Serial port mode specier

SM2 SCON.5

Used for mulCprocessor communicaCon. (Make it 0)

REN

SCON.4

Set/cleared by somware to enable/disable recepCon.

TB8

SCON.3

Not widely used.

RB8

SCON.2

Not widely used.

TI

SCON.1

Transmit interrupt ag. Set by hardware at the beginning of


the stop bit in mode 1. Must be cleared by somware.

RI

SCON.0

Receive interrupt ag. Set by hardware halfway through the


stop bit Cme in mode 1. Must be cleared by somware.

Note: Make SM2, TB8, and RB8 = 0.


20 Aug 2014

EE-309@IITB

36

CADSL

Transfer Data with TI Flag


The following sequence is the steps that the 8051
goes through in transmiong a character via TxD:
1. The byte character to be transmiWed is wriWen into the
SBUF register.
2. It transfers the start bit.
3. The 8-bit character is transferred one bit at a Cme.
4. The stop bit is transferred.
8-bit char
SBUF
bit by bit
TI

20 Aug 2014

UART

EE-309@IITB

TxD

37

CADSL

Transfer Data with the TI flag


5. During the transfer of the stop bit, the 8051 raises the TI ag,
indicaCng that the last character was transmiWed and it is
ready to transfer the next character.
6. By monitoring the TI ag, 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 untransmiWed porCon of the previous byte will be
lost.
We can use interrupt to transfer data
7. Amer SBUF is loaded with a new byte, the TI ag bit must be
cleared by the programmer.
20 Aug 2014

EE-309@IITB

38

CADSL

Transferring Data Serially in 8051


1. Use the Cmer 1 in mode 2
MOV TMOD,#20H

2. Set the value TH1 to chose baud rate.


;Baud rate = 9600bps

MOV TH1,#FDH

3. Set SCON register in mode 1.


MOV SCON,#50H

4. Start the Cmer.


SETB TR1
20 Aug 2014

EE-309@IITB

39

CADSL

Transferring Data Serially in 8051


5. The character byte to be transferred serially is
wriWen into the SBUF register.
MOV SBUF,#A
6. Keep monitoring the Transmit Interrupt (TI) to see if
it is raised.
raise when sending
HERE: JNB TI, HERE
the stop bit
7. Clear TI ag.
TI=0
transfer data
CLR TI

8. To transfer the next character, go to Step 5.

20 Aug 2014

EE-309@IITB

40

CADSL

Example: Serial Transmission


Write a program for the 8051 to transfer leWer A serially at 4800
baud, conCnuously.

MOV TMOD,#20H ;Cmer 1, mode 2
MOV TH1,#-6
;4800 baud rate
MOV SCON,#50H ;8-bit,1 stop,REN enabled
SETB TR1
;start Cmer 1
AGAIN: MOV SBUF,#A ;leWer A to be transferred
HERE: JNB TI,HERE
;wait for the last bit
CLR TI
;clear TI for next char
SJMP AGAIN
;keep sending A
20 Aug 2014

EE-309@IITB

41

CADSL

Example: Serial Transmission


Write a program to transfer the message YES serially at 9600
baud, 8-bit data, 1 stop bit. Do this conCnuously.
MOV TMOD,#20H ;Cmer 1, mode 2
MOV TH1,#-3
;9600 baud
MOV SCON,#50H
SETB TR1

AGAIN:MOV A,#Y
;transfer Y
ACALL TRANS
MOV A,#E
;transfer E
ACALL TRANS
MOV A,#S
;transfer S
ACALL TRANS
SJMP AGAIN
;keep doing it
20 Aug 2014

EE-309@IITB

42

CADSL

Example: Serial Transmission


;serial data transfer subroutine
TRANS:MOV SBUF,A
;load SBUF
HERE: JNB TI,HERE
;wait for last bit to transfer
CLR TI
;get ready for next byte
RET

20 Aug 2014

EE-309@IITB

43

CADSL

Receive Data with 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 indicaCng that the next bit is
the rst bit of the character to be received.
2. The 8-bit character is received one bit at a Cme. When
the last bit is received, a byte is formed and placed in
SBUF.
8-bit character
RxD

bit by bit

UART

SBUF
RI

20 Aug 2014

EE-309@IITB

44

CADSL

Receive Data with TI Flag


3. The stop bit is received. During receiving the stop bit, the
8051 make RI=1, indicaCng that an enCre character was
been received and must be picked up before it gets
overwri]en by an incoming character.
4. By monitoring the RI ag, 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
5. Amer SBUF is copied into a safe place, the RI ag bit must be
cleared by the programmer.
20 Aug 2014

EE-309@IITB

45

CADSL

Receiving Data Serially in 8051


1. Use the Cmer 1 in mode 2
MOV TMOD,#20H

2. Set the value TH1 to chose baud rate.


;Baud rate = 28800bps

MOV TH1,#FDH

3. Set SCON register in mode 1.


MOV SCON,#50H

4. Start the Cmer.


SETB TR1
20 Aug 2014

EE-309@IITB

46

CADSL

Receiving Data Serially in 8051


5. Keep monitoring the Receive Interrupt (RI) to
see if it is raised.
HERE:

JNB RI, HERE

6. When RI is raised, SBUF has the whole byte.


Move the content of SBUF to a safe place.
MOV A,SBUF

raise when getting


the stop bit

7. Clear RI ag.

RI=0
receive data

CLR RI

8. To receive the next character, go to Step 5.


20 Aug 2014

EE-309@IITB

47

CADSL

Example: Serial Data Receive


Program the 8051 to receive bytes of data serially, and put them in
P1. Set the baud rate at 4800, 8-bit data, and 1 stop bit.
MOV TMOD,#20H ;Cmer1, mode 2 (auto reload)
MOV TH1,#-6
;4800 baud
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1
;start Cmer 1
HERE: JNB RI,HERE
;wait for char to come in
MOV A,SBUF
;save incoming byte in A
MOV P1,A
;send to port 1
CLR RI
;get ready to receive next byte
SJMP HERE
;keep geong data
20 Aug 2014

EE-309@IITB

48

CADSL

Thank You
20 Aug 2014

EE-309@IITB

49

CADSL

Vous aimerez peut-être aussi