970 likes | 1.23k Vues
Chapter 10 8051 Serial Communication. Objective. 8051 提供序列傳輸 (serial communication) 的功能,允許 programmer 每次一個 bit 一個 bit 的傳送或接收外界的資料 。 由於 IBM PC 經常與 8051 通訊,因此我們特別強調 8051 利用 RS232 與 PC 的 COM port 溝通 。 所以要瞭解序列傳輸的基本特性, RS232 的規格,轉換電壓準位的 MAX232 IC。
E N D
Objective • 8051 提供序列傳輸(serial communication)的功能,允許 programmer 每次一個 bit 一個 bit 的傳送或接收外界的資料。 • 由於 IBM PC 經常與 8051 通訊,因此我們特別強調 8051 利用 RS232 與 PC 的 COM port 溝通。 • 所以要瞭解序列傳輸的基本特性,RS232 的規格,轉換電壓準位的MAX232 IC。 • 我們也要瞭解 8051 內部相關 registers 的功能,學習撰寫 8051 序列傳輸的程式。
Sections 10.1 Basics of serial communication 10.2 8051 connection to RS232 10.3 8051 serial communication programming
RS232 PC 8051 MAX232 COM 1 port UART 8051 and PC • The 8051 module connects to PC by using RS232. • RS232 is a protocol which supports half-duplex, synchronous/asynchronous, serial communication. • We discuss these terms in following sections.
Simplex vs. Duplex Transmission • Simplex transmission: the data can sent in one direction. • Example: the computer only sends data to the printer. • Duplex transmission: the data can be transmitted and receive Transmitter Receiver Transmitter Transmitter Receiver Receiver
Half vs. Full Duplex • Half duplex: if the data is transmitted one way at a time. • Full duplex: if the data can go both ways at the same time. • Two wire conductors for the data lines. Transmitter Receiver Receiver Transmitter Transmitter Receiver Receiver Transmitter
Figure 10-2. Simplex, Half-, and Full-Duplex Transfers Half Duplex Transmitter Receiver Receiver Transmitter Full Duplex Transmitter Receiver Receiver Transmitter
Parallel vs. Serial • Computers transfer data in two ways: • Parallel • data is sent a byte or more a time (fast) • Only 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) • Long distance (rarely distortion) • cheap • The data can be transferred on the telephone line (by using modem.)
Parallel Transfer Serial Transfer D0 Sender Receiver Sender Receiver D7 Figure 10-1. Serial versus Parallel Data Transfer (1/2)
Figure 10-1. Serial versus Parallel Data Transfer (2/2) Parallel Transfer Serial Transfer D0 D0-D7 Sender Receiver Sender Receiver Other control lines Other control lines
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 register register 8 1 8-bit character parallel-in serial-out serial-in parallel-out ‘A’
Asynchronous vs. Synchronous • Serial communication uses two methods: • In synchronous communication, data is sent in blocks of bytes. • In asynchronous communication, data is sent in bytes. byte byte byte byte 01011111 01010101 preamble sender receiver byte byte byte sender receiver stop bit start bit
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.
8051 Serial Communication • The 8051 has serial communication capability built into it. • Half-duplex • Asynchronous mode only. • How to detect that a character is sent via the line in the asynchronous mode? • Answer: Data framing!
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 bit start bit 0 1 0 0 0 0 0 1 mark mark D7 D0 goes out last goes out last
Framing (2/3) • The LSB is sent out first. • The start bit is 0 (low) and always one bit. • 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). • In asynchronous serial communication, peripheral chips and modems can be programmed for data that is 7 or 8 bits. • When there is no transfer, the signal is 1 (high), which is referred to as mask.
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 • 25% 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.
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 inHz (used in modem). • Date 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 (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.
Time (D0 first) stop bit start bit stop bit 00 11 01 00 11 mark 10 10 11 mark 8-bit character Example of Data Transfer Rate (1/2) • 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
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
RS232 Standard • RS232 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 • Define the voltage level, pin functionality, baud rate, signal meaning, communication distance.
RS 232 Voltage 25V logic 0 3V undefined -3V logic 1 -25V RS232 Voltage Level • The input and output voltage of RS232 is not of the TTL compatible. • 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
RS232 PC 8051 MAX232 COM 1 port UART MAX232 • MAX232 IC chips are commonly referred to as line drivers. TTL voltage level RS232 voltage level TTL voltage level
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.
1 13 14 25 Figure 10-4. RS232 Connector DB-25
1 5 9 6 Figure 10-5. DB-9 9-Pin Connector
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 flow signals. • DCD (carrier detect, or data carrier detect) • RI (ring indicator) • They are not supported by the 8051 UART chips.
Communication Flow (1/2) PC (DTE) modem (DCE) Telephone is ringing Connection 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
Communication Flow (2/2) • While signals DTR and DSR are used by the PC and modem, respectively, to indicate that they are alive and well. • TRS 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 deassert DTR and try again.
DTE and DCE • Communication Equipments are classified as • DTE (data terminal equipment) • Terminals and computers that send and receive data • DCE (data communication equipment) • Communication equipment (only for transfer data) • Ex: modem DTE view DCE view DTE DCE DTE DCE Telephone Line TxD RxD RxD TxD RxD TxD TxD RxD PC Com1 GND GND GND GND modem modem PC Com1
RS232 PC 8051 MAX232 COM 1 port UART 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
DTE DTE DTE DTE TxD TxD TxD TxD RxD RxD RxD RxD ground ground 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 PC Com1 8051-based board PC Com1 PC Com1
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.
TxD and RxD pins in the 8051 • 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.
Vcc P1.0 1 40 P0.0(AD0) P1.1 2 39 P0.1(AD1) P1.2 3 38 P0.2(AD2) P1.3 4 37 8051 (8031) P0.3(AD3) P1.4 5 36 P0.4(AD4) P1.5 6 35 P0.5(AD5) P1.6 7 34 P0.6(AD6) P1.7 8 33 P0.7(AD7) RST 9 32 (RXD)P3.0 EA/VPP 10 31 (TXD)P3.1 ALE/PROG 11 30 PSEN (INT0)P3.2 12 29 P2.7(A15) (INT1)P3.3 13 28 (T0)P3.4 P2.6(A14) 14 27 (T1)P3.5 P2.5(A13) 15 26 P2.4(A12) (WR)P3.6 16 25 P2.3(A11) (RD)P3.7 17 24 P2.2(A10) XTAL2 18 23 P2.1(A9) XTAL1 19 22 P2.0(A8) GND 20 21 Figure 4-1. 8051 Pin Diagram PDIP/Cerdip data transfer start/stop input event
8051 MAX232 11 11 P3.1 5 14 2 TxD 13 3 10 12 P3.0 RxD DB-9 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.
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 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. • 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
Vcc C3 + 1 + 16 2 C1 MAX232 3 6 4 C4 + + C2 5 T1IN T1OUT 14 11 R1IN R1OUT 13 12 T2IN T2OUT 7 10 R2IN R2OUT 8 9 15 TTL side RS232 side Figure 10-7 (a): Inside MAX232
8051 MAX232 11 11 P3.1 5 14 2 TxD 13 3 10 12 P3.0 RxD DB-9 Figure 10-7: (b) MAX232’s Connection to the 8051 (Null Modem) TTL-compatible RS232-compatible
Vcc 13 7 11 14 15 MAX233 12 16 17 10 T1IN T1OUT 2 5 R1IN R1OUT 4 3 T2IN T2OUT 1 18 R2IN R2OUT 19 20 6 9 TTL side RS232 side Figure 10-8: (a) Inside MAX233
8051 MAX233 2 11 P3.1 5 5 2 TxD 4 3 10 3 P3.0 RxD DB-9 Figure 10-8: (b) MAX233’s Connection to the 8051 (Null Modem) TTL-compatible RS232-compatible
PC Baud Rates • PC supports several baud rates. • You can use netterm, terminal.exe, stty, ptty to send/receive data. • Hyperterminal supports baud rates much higher than the ones list in the Table.
11.0592 MHz Machine cyclefrequency XTAL oscillator ÷ 12 ÷ 32 By UART 28800 Hz Timer 1 921.6 kHz To timer 1 To set the Baud rate Baud Rates in the 8051 (1/2) • 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