Hiệu đính từ slide của thầy Hồ Trung Mỹ
Bộ mơn Điện tử - DH BK TPHCM
CHƯƠNG 3
HỌ VI ĐiỀU KHIỂN 8051
1
3.6 Cổng nối tiếp
(Serial Port)
2
Các hoạt động cổng nối tiếp của 8051
• Giới thiệu
• Chuẩn EIA(Electronic Industries
Association)-232E hoặc RS232
• Giao tiếp cổng nối tiếp MCS-51/52
• Ứng dụng của chế độ 0
• Ứng dụng của chế độ 1
• Ứng dụng của chế độ 2 & 3
3
Truyền dữ liệu nối tiếp
• Truyền dữ liệu khoảng cách xa
• Tốc độ truyền dữ liệu thấp
• Truyền dữ liệu nối tiếp đồng bộ
– Dùng tín hiệu clock riêng để đồng bộ hóa mạch thu
và phát.
– Thường dùng khung dữ liệu lớn hơn và nhanh hơn.
• Truyền dữ liệu nối tiếp bất đồng bộ
– Khơng cần tín hiệu clock riêng để đồng bộ hoá
chuyển dữ liệu.
– Dựa trên ký tự. Mỗi ký tự được đóng khung bằng
“start bit” và “stop bit”.
– Bên thu cần nhận dạng “start bit” và “stop bit” để
nhận đúng ký tự dữ liệu.
4
Truyền dữ liệu nối tiếp đồng bộ (synchronous)
và bất đồng bộ (asynchronous) với 8051
5
Đường truyền dữ liệu cơ bản
DTE: Data Terminal Equipment
DCE: Data Communications Equipmnent
DTE
Computer or
terminal
Communication link
DCE
DCE
Modem
Modem
DTE
Computer or
terminal
• Người ta có thể sử dụng đường điện thoại công cộng
hoặc dành riêng làm môi trường truyền dữ liệu nối
tiếp bất đồng bộ.
• Modem được dùng để chuyển đổi dữ liệu số thành
dạng sóng analog thích hợp cho việc truyền trên
đường dây điện thoại và ngược lại.
• Có 2 kiểu đường truyền: Điểm đến điểm và Đa điểm.
6
Đường truyền Điểm đến điểm
và Đa điểm
Rx
Tx
StationRx
Station
Tx
Peer
Peer
Master
Slave 1
Slave 2
Slave n
Address 1
Address 2
Address n
7
Cơ bản về các đường truyền
• Hệ thống 2 dây và 4 dây:
– 2 dây: tín hiệu và đất.
– 4 dây: 2 bộ 2 dây.
• Các kiểu đường truyền:
– Đơn công (Simplex link): đường dây dành riêng
cho thu hoặc phát chứ không cả hai.
– Bán song công (Half-duplex link): đường dây có
thể dùng cho phát hoặc thu nhưng mỗi lần chỉ
có 1 chiều.
– Song cơng (Full-duplex link): Phát và thu có thể
tiến hành đồng thời. Đường truyền cần 4 dây. 8
Cơ bản về các đường truyền
Signal pattern for letter “A” (41H)
Start bit
Stop bit
5V
0V
0 1 0 0 0 0 0 1
0 1
Serial port
D0 D1 D2 D3 D4 D5 D6 D7
>+3V
<-3V
RS232
mark
space
9
EIA-232-E or RS-232
• EIA-232-E or RS-232 can apply to the
following data communication schemes:
–
–
–
–
–
–
Serial communications
Synchronous and asynchronous
Dedicated leased or private lines
Switched service
Two wire or four wire
Point to point or multipoint
• 4 aspects in EIA-232-E interface:
mechanical, functional, procedural, and
electrical.
10
EIA-232-E Mechanical
Characteristics
• 25-pin DB25 /9-pin DB9 plug connector.
11
DB-9 Signals
12
EIA-232-E Connector and Pin
Assignment
Signal direction
Signal Name
Signal Name
Signal direction
to DCE
Secondary TxD
14 1
protective ground
Both
to DTE
Tx clock
15 2
TxD
to DCE
to DTE
Secondary RxD
16 3
RxD
to DTE
to DTE
Rx clock
17 4
Request To Send
to DCE
unassigned
18 5
Clear To Send
to DTE
to DCE
Secondary Request To Send
19 6
Data Set Ready
to DTE
to DCE
Data Terminal Ready
20 7
Signal ground
Both
to DTE
Signal Quality Detect
21 8
Carrier Detect
to DTE
to DTE
Ring indicator
22 9
Reserved
Data Rate Select
23 10
Reserved
Tx clock
24 11
Unassigned
unassigned
25 12
Secondary Carrier Detect
to DTE
13
Secondary Clear To Send
to DTE
Both
to DCE
13
Functions of EIA-232-E Interchange Circuits
1
2
3
4
5
6
7
8
9,10,11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
BA
BB
CA/CJ
CB
CC
AB
CF
SCF/CI
SCB
SBA
DB
SBB
DD
LL
SCA
CD
RL/CG
CE
CH/CI
DA
TM
Shield
Transmitted data
Received data
Request to send/ready for receiving
Clear to send
DCE
Signal common
Received line signal detector
(Reserved for testing, unassigned)
Secondary received line signal detection/data range selector (DCE source)
Secondary clear to send
Secondary transmitted data
Transmitter signal element timing (DCE source)
Secondary received data
Received signal element timing
Local loopback
Secondary request to send
DTE ready
Remote loopback/signal quality detector
Ring indicator
Data signal rate selector (DTE/DCE source)
Transmitter signal element timing (DTE source)
Test mode
14
EIA-232-E Procedural Characteristics
DTE
X
EIA-232
Signal GND (7)
DTE ready (20)
Request to send(4)
data send (2)
send timing (24)
EOT send (2)
send timing (24)
RTS off (4)
DCE
X
->
<- DCE ready (6)
-> carrier on
<- clear to send (5)
-> data send
->
-> EOT send
->
->
carrier off
-< CTS off (5)
<- carrier detect (8)
<- ACK message (3)
<-receive timing (17)
<- EOT received (3)
<- receive timing (17)
<- carrier off (8)
DCE
Y
EIA-232
DTE
Y
DCE ready (6)
carrier detect (8)
Signal GND (7)
<- DTE ready (20)
->
->
data receive (3)
receive timing (17)
EOT receive (3)
receive timing (17)
->
->
->
->
error check
carrier off (8)
->
<- RTS (4)
carrier on
clear to send (5)
->
modulated ACK MSG <- ACK message (2)
<- send timing (24)
modulate EOT send
<- EOT send (2)
<- send timing (24)
carrier off
<-RTS off (4)
15
CTS off (5)
->
Data Transmission Errors
• Framing error: when a received
character is improperly framed by the
start and stop bits; it is set by the
absence of stop bit.
• Receive overrun: one or more
characters in the data stream were lost.
• Parity error: received character parity
error.
16
Null Modem Connection
When 2 DTEs are very close, no need to use modems to connect them.
Pin
Circuit name
DTE X
DTE Y
22
20
8
6
5
4
3
2
24
17
7
Ring indicator
Data terminal ready
Data carrier detect
Data set ready
Clear to send
Request to send
Receive data
Transmit data
Transmitter timing
Receiver timing
Signal ground
CE
CD
CF
CC
CB
CA
BB
BA
DA
DD
AB
CE
CD
CF
CC
CB
CA
BB
BA
DA
DD
AB
17
Interfacing a μp with 232
• Transmission:
– Convert data from parallel to serial format
– Add start, stop, and optional parity bits to
the serial data.
– Translate the voltage levels to those of the
EIA-232-E standard. (transceiver chip: MAX
232, MC145403, etc.)
• Reception:
– Translate the voltage levels of the incoming
data stream to those used in the μp.
– Detect the start and stop bits.
– Optionally perform parity checking
– Convert serial data into parallel format.
18
Sơ đồ khổi cổng nối tiếp của 8051
Cù
ng
địa
ch
ỉ
19
Các đặc tính của cổng nối tiếp 8051
•
•
•
•
Song cơng (ful-duplex)
Có đệm khi thu (bộ đệm 1 byte)
Có 4 chế độ làm việc (0 - 3)
Lập trình dễ dàng để:
• Chọn tốc độ (cố định (on-chip OSC/12 hoặc OSC/64),
hoặc thay đổi được (T1))
• Chọn chế độ làm việc
• Cho chạy (SCON)
• Dễ sử dụng
• Truyền dữ liệu:
MOV SBUF, A
• Nhận dữ liệu: MOV A, SBUF
20