1 / 107

Chapter 10 8051 Serial Port Programming in Assembly and C

Chapter 10 8051 Serial Port Programming in Assembly and C. Objective. 8051 提供序列傳輸 (serial communication) 的功能,允許 programmer 每次一個 bit 一個 bit 的傳送或接收外界的資料 。 由於 IBM PC 經常與 8051 通訊,因此我們特別強調 8051 利用 RS232 與 PC 的 COM port 溝通 。 所以要瞭解序列傳輸的基本特性, RS232 的規格,轉換電壓準位的 MAX232 IC。

ronnie
Download Presentation

Chapter 10 8051 Serial Port Programming in Assembly and C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 8051 Serial Port Programming in Assembly and C

  2. Objective • 8051 提供序列傳輸(serial communication)的功能,允許 programmer 每次一個 bit 一個 bit 的傳送或接收外界的資料。 • 由於 IBM PC 經常與 8051 通訊,因此我們特別強調 8051 利用 RS232 與 PC 的 COM port 溝通。 • 所以要瞭解序列傳輸的基本特性,RS232 的規格,轉換電壓準位的MAX232 IC。 • 我們也要瞭解 8051 內部相關 registers 的功能,學習撰寫 8051 序列傳輸的程式。

  3. Sections 10.1 Basics of serial communication 10.2 8051 connection to RS232 10.3 8051 serial port programming in Assembly 10.4 Programming the second serial port 10.5 Serial port programming in C

  4. Section 10.1Basics of Serial Communication

  5. 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

  6. Inside Architecture of 8051 External interrupts On-chip ROM for program code Timer/Counter Interrupt Control Timer 1 On-chip RAM Counter Inputs Timer 0 CPU Serial Port Bus Control 4 I/O Ports OSC 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 MAX232 COM 1 port UART

  8. 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 received. Transmitter Receiver Device 1 Device 2 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. • 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

  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. Parallel Transfer Serial Transfer D0 Sender Receiver Sender Receiver D7 Figure 10-1. Serial versus Parallel Data Transfer (1/2)

  13. 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

  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 register register 8 1 8-bit character parallel-in serial-out serial-in parallel-out ‘A’

  15. Asynchronous vs. Synchronous • Serial communication uses two methods: • In synchronous communication, data is sent in blocks of bytes. • In asynchronous communication, data is sent without continuity. time 10101010 10101111 byte byte byte byte preamble receiver sender byte byte byte sender receiver stop bit start 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 bit start bit 0 1 0 0 0 0 0 1 mark mark D7 D0 goes out last 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 inHz (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. 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) • 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

  24. 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 DTE DCE Telephone Line TxD RxD RxD TxD RxD TxD TxD RxD PC Com1 GND GND GND GND modem modem PC Com1

  27. 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

  28. 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 8051-based board 8051-based board PC Com1 PC Com1

  29. 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 (Transistor-Transistor-Logic) 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

  30. RS232 PC 8051 MAX232 COM 1 port UART MAX232 • MAX232 IC chips are commonly referred to as line drivers. RS232 voltage level TTL voltage 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. 1 13 14 25 Figure 10-4. RS232 Connector DB-25

  33. Table 10-1: RS232 Pins (DB-25) for DTE (1/2)

  34. Table 10-1: RS232 Pins (DB-25) for DTE (2/2)

  35. 1 5 9 6 Figure 10-5. DB-9 9-Pin Connector

  36. Table 10-2: IBM PC DB-9 Signals for DTE

  37. DB9 - DB25 conversion

  38. 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.28051 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. 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.

  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 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

  45. 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 22 mF

  46. 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

  47. 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

  48. 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

  49. Section 10.38051 Serial Port Programming in Assembly

  50. 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.

More Related