Sie sind auf Seite 1von 52

Unit 2

8051 Serial Port Programming in C

By

Dr Narayana Swamy Ramaiah


Professor and HoD, Dept of CSE
FET, JU

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

10.1 Basics of serial communication


10.2 8051 connection to RS232
10.5 Serial port programming in C

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

Interrupt ROM for


On-chip Timer 1 Counter
Control program
RAM Timer 0 Inputs
code

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

• The 8051 module connects to PC by using RS232.


• RS232 is a protocol which supports full-duplex,
synchronous/asynchronous, serial communication.
• We discuss these terms in following sections.

RS232

PC 8051

COM 1 port MAX232 UART


8
Simplex vs. Duplex Transmission

• Simplex transmission: the data can sent in one


direction.
– Example: the computer only sends data to the printer.

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

• Half duplex: if the data is transmitted one way at a


time.
Transmitter Receiver

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

Simplex Transmitter Receiver

Half Duplex Transmitter Receiver

Receiver Transmitter

Full Duplex Transmitter Receiver

Receiver Transmitter
11
Parallel vs. Serial

• Computers transfer data in two ways:


– Parallel
• Data is sent a byte or more at a time (fast)
• Only very short distance between two systems
• The 8-bit data path is expensive
• Example: printer, hard disks
– Serial
• The data is sent one bit at a time (slow) with simple wire
• Relative long distance (rarely distortion)
• cheap
• For long-distance data transfers using communication lines such as a
telephone, it requires a modem to modulate (0/1 to analog) and demoulate
(analog to 0/1).

12
Figure 10-1. Serial versus Parallel Data
Transfer (1/2)

Serial Transfer Parallel Transfer


D0
Sender Receiver Sender Receiver

D7

13
Figure 10-1. Serial versus Parallel Data
Transfer (2/2)

Serial Transfer Parallel Transfer


D0 D0-D7
Sender Receiver Sender Receiver

Other control lines

Other control lines

14
Serial Communication

• How to transfer data?


– Sender:
• The byte of data must be converted to serial bits using a
parallel-in-serial-out shift register.
• The bit is transmitted over a single data line.
– Receiver
• The receiver must be a serial-in-parallel-out shift register to
receive the serial data and pack them into a byte.

11101000001011
8
register 1
register
8-bit
parallel-in ‘A’ serial-in character 15
serial-out parallel-out
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 16
UART & USART

• It is possible to write software to use both


methods, but the programs can be tedious and
long.
• Special IC chips are made for serial
communication:
– USART (universal synchronous-asynchronous
receiver-transmitter)
– UART (universal asynchronous receiver-transmitter)
• The 8051 chip has a built-in UART.
17
8051 Serial Communication

• The 8051 has serial communication capability


built into it.
– Full-duplex
– Asynchronous mode only.
• How to detect that a character is sent via the line
in the asynchronous mode?
– Answer: Data framing!

18
Framing (1/3)

• Each character is placed in between start and stop


bits. This is called framing.
– Figure 10-3. Framing ASCII “A” (41H)

Time (D0 first)

stop start
mark
bit
0 1 0 0 0 0 0 1 mark
bit

goes out last D7 D0 goes out first


19
Framing (2/3)

• The start bit is 0 (low) and always one bit.


• The LSB is sent out first.
• The stop bits is 1 (high).
• The stop bit can be one (if 8 bits used in ASCII) or
two bits (if 7 bits used in ASCII).
– It is programmed for data that is 7 or 8 bits.
• When there is no transfer, the signal is 1 (high),
which is referred to as mark.

20
Framing (3/3)

• We have a total of 10 bits for each character:


– 8-bits for the ASCII code
– 2-bits for the start and stop bits
– 20% overhead
• In some systems in order to maintain data integrity, the
parity bit is included in the data frame.
– In an odd-parity bit system the total number of bits,
including the parity bit, is odd.
– UART chips allow programming of the parity bit for odd-,
even-, and no-parity options.

21
Data Transfer Rate (1/2)

• 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

22
Data Transfer Rate (2/2)

• The data transfer rate depends on communication


ports incorporated into that system.
– Ex: 100-9600 bps in the early IBM PC/XT
– Ex: 56K bps in Pentium-based PC
– The baud rate is generally limited to 100kHz.

23
Example of Data Transfer Rate (1/2)

• Assume that data is sent in the following


asynchronous mode:
– 2400 baud rate
– each signal has 4 voltage levels (-5V, -3V, 3V, 5V)
– one start bit, 8-bit data, 2 stop bits
Time (D0 first)

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)

• 2400 baud = 2400 signals per second =2400 Hz


• 4 voltage level
– Log24=2, 2 bits is sent in every signal change
– Data rate = 2 * 2400 Hz = 4800 bps
• Effective ratio = 8 / (1+8+2) =8/11
• Effective data rate = data rate * effective ratio =
4800 * 8 /11=3490.9 bps

25
RS232 Standard

• RS232 (Recommended Standard 232) is an interfacing


standard which is set by the Electronics Industries
Association (EIA) in 1960.
– RS232 is the most widely used serial I/O interfacing standard.
– RS232A (1963), RS232B (1965) and RS232C (1969), now is
RS232E
– RS-232 is a standard for connecting between a DTE (Data Terminal
Equipment) and a DCE (Data Circuit-terminating Equipment).
• Define the voltage level, pin functionality, baud rate, signal
meaning, communication distance.
26
DTE and DCE

• DTE (Data Terminal Equipment)


– DTE refers to PC, 8051, or other equipments.
• DCE (Data Communication Equipment)
– DCE refers to communication equipment, such as
modems, that are responsible for transferring the data.
DTE view
DCE view
DTE DCE DCE DTE
TxD RxD Telephone
RxD TxD
Line
RxD TxD TxD RxD
PC GND GND GND GND 27
Com1 modem modem PC Com1
IBM PC/compatible COM ports

• IBM PC has 2 COM ports.


– Both COM ports have RS232-type connectors.
– For mouse, modem
• We can connect the 8051 serial port to the COM
port of a PC for serial communication
experiments.
DTE view DTE view
RS232

PC 8051

COM 1 port MAX232 28


UART
Null Modem Connection

• The simplest connection between a PC and


microcontroller requires a minimum of three pins,
TxD, RxD, and GND.
– Figure 10-6 shows null modem connection

DTE DTE DTE DTE


TxD TxD TxD TxD
RxD RxD RxD RxD

ground ground
8051-based 8051-based PC PC
board board Com1 Com1 29
RS232 Voltage Level

• The input and output voltage of RS232


RS 232 Voltage
is not of the TTL (Transistor-Transistor-
Logic) compatible. 25V
– RS232 is older than TTL.
logic 0
• We must use voltage converter (also
referred to as line driver) such as 3V
undefined
MAX232 to convert the TTL logic -3V
levels to the RS232 voltage level, and logic 1
vice versa.
– MAX232, TSC232, ICL232 -25V

30
MAX232

• MAX232 IC chips are commonly referred to as


line drivers.

RS232

PC 8051

COM 1 port MAX232


UART

RS232 voltage TTL voltage


level level 31
RS232 pins

• Figure 10-4 shows the RS232 connector DB-25.


• Table 10-1 shows the pins and their labels for the RS232
cable.
– DB-25P : plug connector (male)
– DB-25S: socket connector (female)
• Figure 10-5 shows DB9 connector and Table 10-2 shows
the signals.
– IBM version for PC.
• All the RS 232 pin function definitions of Tables 10-1
and 10-2 are from the DTE point of view.
32
Figure 10-4. RS232 Connector DB-25

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

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
38
9 22 Ring indicator
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)

39
Communication Flow

• While signals DTR and DSR are used by the PC and


modem, respectively, to indicate that they are alive
and well.
• RTS and CTS control the flow of data.
• When the PC wants to send data, it asserts RTS.
• If the modem is ready (has room) to accept the data, it
sends back CTS.
• If, for lack of room, the modem does not activate CTS,
and PC will de-assert DTR and try again.
40
RS422 & RS485

• By using RS232, the limit distance between two


PCs is about 15m.
• It works well even the distance=30m.
• If you want to transfer data with long distance (ex:
300m), you can use RS422 or RS485.

41
Section 10.2
8051 Connection to RS232

42
TxD and RxD pins in the 8051

• Many of the pins of the RS232 connector are used for


handshaking signals. However, they are not supported by
the 8051 UART chips.
• In 8051, the data is received from or transmitted to
– RxD: received data (Pin 10, P3.0)
– TxD: transmitted data (Pin 11, P3.1)
• TxD and RxD of the 8051 are TTL compatible.
• The 8051 requires a line driver to make them RS232
compatible.
– One such line driver is the MAX232 chip.

43
MAX232 (1/2)

• 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 11 11
TxD 14 2 5

13 3
P3.0 10 12
RxD
DB-9 44
MAX232 (2/2)

• MAX232 has two sets of line drivers.


– Figure 10.7 shows the inside of MAX232.
– MAX232 requires four capacitors ranging from 1 to 22 F.
The most widely used value for these capacitors is 22F.
• MAX233 performs the same job as the MAX232 but
eliminates the need for capacitors.
– Note that MAX233 and MAX232 are not pin compatible.
– Figure 10.8 (a) shows the inside of MAX233
– Figure 10.8 (b) shows the connection to the 8051

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

TTL side 15 RS232 side 46


Figure 10-7: (b) MAX232’s Connection to
the 8051 (Null Modem)

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

TTL side 6 9 RS232 side


48
Figure 10-8: (b) MAX233’s Connection to
the 8051 (Null Modem)

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

• Using C to program the serial port.


– Example 10-15 does the same work as Example 10-11.
• Please read other examples by yourself.

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

Das könnte Ihnen auch gefallen