Serial Communication
Serial Communication
By
1
Objective
You are able to (1/2)
•Contract and compare serial versus parallel communication
•List the advantages of serial communication over parallel
•Explain serial communication protocol
•Contrast synchronous versus asynchronous communication
•Contrast half- versus full-duplex transmission
•Explain the process of data framing
•Describe data transfer rate and bps rate
2
You are able to (2/2)
• Define the RS232 standard
• Explain the use of the MAX232 and MAX232 chips
• Interface the 8051 with an RS232 connector
• Discuss the baud rate of the 8051
• Describe serial communication features of the 8051
• Program the 8051 for serial data communication
• Program the 8051 serial port in C
3
Sections from text book - Mazidi
4
Section 10.1
Basics of Serial Communication
5
Figure 4-1. 8051 Pin Diagram
PDIP/Cerdip
P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0
P1.2 3 38 )P0.1(AD1)
P1.3 4 8051 37 P0.2(AD2
)P0.3(AD3)
P1.4 5 36
P1.5 6 (8031) 35 P0.4(AD4)
P1.6 7 34 P0.5(AD5)
P1.7 8 33 P0.6(AD6)
RST 9 32 P0.7(AD7)
(RXD)P3.0 10 31 EA/VPP
(TXD)P3.1 11 30 ALE/PROG
(INT0)P3.2 12 29 PSEN
(INT1)P3.3 13 28 P2.7(A15)
(T0)P3.4 14 27 P2.6(A14)
(T1)P3.5 15 26 P2.5(A13)
(WR)P3.6 16 25 P2.4(A12)
(RD)P3.7 17 24 P2.3(A11)
XTAL2 18 23 P2.2(A10)
XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8) 6
Inside Architecture of 8051
External interrupts
On-chip Timer/Counter
CPU
Bus Serial
4 I/O Ports
OSC Control Port
P0 P1 P2 P3 TxD RxD
Address/Data
Figure 1-2. Inside the 8051 Microcontroller Block Diagram
7
8051 and PC
RS232
PC 8051
Transmitter Receiver
Device 1 Device 2
• Duplex transmission: the data can be transmitted
and received.
Transmitter Transmitter
Receiver Receiver
Device 1 Device 2 9
Half vs. Full Duplex
Receiver Transmitter
• Full duplex: if the data can go both ways at the
same time.
– Two wire conductors for the data lines.
Transmitter Receiver
Receiver Transmitter 10
Figure 10-2. Simplex, Half-, and Full-
Duplex Transfers
Receiver Transmitter
Receiver Transmitter
11
Parallel vs. Serial
12
Figure 10-1. Serial versus Parallel Data
Transfer (1/2)
D7
13
Figure 10-1. Serial versus Parallel Data
Transfer (2/2)
14
Serial Communication
11101000001011
8
register 1
register
8-bit
parallel-in ‘A’ serial-in character 15
serial-out parallel-out
Asynchronous vs. Synchronous
18
Framing (1/3)
stop start
mark
bit
0 1 0 0 0 0 0 1 mark
bit
20
Framing (3/3)
21
Data Transfer Rate (1/2)
22
Data Transfer Rate (2/2)
23
Example of Data Transfer Rate (1/2)
stop stop 00 11 01 00 11
start
mark 10 10 11 mark
bit bit bit
24
8-bit character
Example of Data Transfer Rate (2/2)
25
RS232 Standard
PC 8051
ground ground
8051-based 8051-based PC PC
board board Com1 Com1 29
RS232 Voltage Level
30
MAX232
RS232
PC 8051
1 13
14 25
33
Table 10-1: RS232 Pins (DB-25) for DTE
Pin Description
(1/2)
1 Protective ground
2 Transmitted data (TxD)
3 Received data (RxD)
4 Request to send (RTS)
5 Clear to send (CTS)
6 Data set ready (DSR)
7 Signal ground (GND)
8 Data carrier detect (DCD)
9/10 Reserved for data testing
11 Unassigned
12 Secondary data carrier detect
13 Secondary clear to send 34
Table 10-1: RS232 Pins (DB-25) for DTE
Pin Description (2/2)
14 Secondary transmitted data
15 Transmit signal element timing
16 Secondary received data
17 Receive signal element timing
18 Unassigned
19 Secondary request to sent
20 Data terminal ready (DTR)
21 Signal quality detector
22 Ring indicator (RI)
23 Data signal rate select
24 Transmit signal element timing
25 Unassigned 35
Figure 10-5. DB-9 9-Pin Connector
1 5
6 9
36
Table 10-2: 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) 37
DB9 - DB25 conversion
39
Communication Flow
41
Section 10.2
8051 Connection to RS232
42
TxD and RxD pins in the 8051
43
MAX232 (1/2)
8051
MAX232
P3.1 11 11
TxD 14 2 5
13 3
P3.0 10 12
RxD
DB-9 44
MAX232 (2/2)
45
Figure 10-7 (a): Inside MAX232
Vcc
C3
+ 1 16 +
C1 2
22 F
3 MAX232 6
+ 4 C4
C2 5 +
T1IN T1OUT
11 14
R1OUT R1IN
12 13
T2IN T2OUT
10 7
R2OUT R2IN
9 8
8051
MAX232
P3.1 11 11
TxD 14 2 5
13 3
P3.0 10 12
RxD
DB-9
TTL-compatible RS232-compatible 47
Figure 10-8: (a) Inside MAX233
Vcc
13 7
14 11
12 MAX233 15
17 16
10
2 T1IN T1OUT
5
R1OUT R1IN
3 4
T2IN T2OUT
1 18
R2OUT R2IN
20 19
8051
MAX233
2
P3.1 11
TxD 5 2 5
4 3
P3.0 10 3
RxD
DB-9
TTL-compatible RS232-compatible 49
Section 10.5
Serial Port Programming in C
50
Transmitting and Receiving Data in C
51
Example 10-15
Write an 8051 C program to transfer the letter “A” serially at
4800 baud continuously. Use 8-bit data and 1 stop bit.
Solution :
#include <reg51.h>
void main(void) {
TMOD=0x20; SCON=0x50;
TH1=0xFA; //4800 baud rate
TR1=1;
while (1) {
SBUF=‘A’;
while (TI==0);
TI=0;
} } 52