Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Serial Port Programming in Assembly and C

Similar presentations


Presentation on theme: "Chapter Serial Port Programming in Assembly and C"— 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 序列傳輸的程式。 使用RS232的優點是使用架構簡單, 較少的傳輸線, 就可做遠距離的資料傳輸.

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

4 Section 10.1 Basics of Serial Communication

5 Figure 4-1. 8051 Pin Diagram 8051 (8031) PDIP/Cerdip 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (T0)P3.4 (T1)P3.5 XTAL2 XTAL1 GND (INT0)P3.2 (INT1)P3.3 (RD)P3.7 (WR)P3.6 Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) EA/VPP ALE/PROG PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 8051 (8031)

6 Inside Architecture of 8051
External interrupts On-chip ROM for program code Timer/Counter Interrupt Control On-chip RAM Timer 1 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 RS232 是 full-duplex, 所以可以同時接收與傳送, 對於要接收與傳送的資料都是使用 buffer, 所以很方便 (都稱為 SBUF). 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 Simplex: 廣播, 電視 Duplex: Interactive service, 電話 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 Half duplex: 對講機, 計程車, 貨車派遣, 警車的中繼式無線電對講機. Full duplex: 電話, FDD 模式的 2G. Transmitter Receiver Receiver Transmitter

10 Figure 10-2. Simplex, Half-, and Full-Duplex Transfers
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). 磁碟機的排線使用的是 UltraATA33 (133MB/sec), 有 40 條線; 但到了UltraATA66, 有 80 條線, 而且每一線路旁都要有地線, 以降低 parallel 傳輸時產生的串音干擾 (cross talk). 如此造成 PC 內維修, 熱氣排放的困擾. 這些被稱為 Parallel ATA (PATA) 因此 APT, Dell, Intel, Maxtor, Seagate 等業者推出 SerialATA (SATA). Intel 在 2000 年提出概念, 2001 正式提出 1.0 版本, 2002 年有 2.0 的提案 年五月提出 SerialATA II. SeialATA 會提供 1.5Gbps 的傳輸速度, 目前在商品化的階段. SATA 共有 7 條線, 3 條地線, 另外兩對形成 transmit/receive 1 bit 的組合, 使用差動電壓方式傳輸. 另外, 如果 parallel bus 一但更新, ex: UltraATA33 變成 UltraATA66, 不但硬體改變, 所有相關 software/firmware 都要修改才能 work. SCSI (之前 8bit, 目前16bit parallel) 也是與ATA一樣的, 都沒有往更大資料量寬度發展的意願. 新的串列規格 Serial Attached SCSI (SAS) 已經推出, 速度預計為 3Gbps, 但長度較短只有 8m. 所以末代的 parallel SCSI 可能是 Ultra320 SCSI (320 MB/sec), 長度可達 12m. 另外 PCI bus 也從 32 bit/64 bit 轉成串列的 PCIExpress (PCI-E) 規格. 相反的, serial bus 若要加快速度, 基本上只要增快 clock 即可, 改變的幅度小, 研發所需的心力較小, 也無需面對 cross talk 的問題, 排線可以比較長. 容易做到熱插拔 (hot plug) Ex: PATA 約只能 46cm, 不可 hot plug; SATA 1.0 可長到 1m, 可以 hot plug. 這代表目前科技的進步適合解決 synchronization, 增快 clock 這部份的問題, 對於 cross talk 無法處理. 但未來也許 cross talk 也能解決也說不定, 因此 parallel bus 的未來仍是未知數. SATA 將 1.5Gbps 寫成 150MB/sec 的原因在於 user data 與 control data 都將在相同的線路上傳送, 所以扣掉約 20% 的控制傳輸後, 在轉換 Gpbs 到 MB/sec 時, 原本 1.5Gbps 不是除以 8, 而是約略的除以 10. (不像 parallel bus, 控制訊號有獨立的 lines 傳送.) USB 1.1 也是一樣的, 約有 10% control data. 而且若連續 6 個 0 後一定會插入 1, 以避免造成解讀偏差.

12 Figure 10-1. Serial versus Parallel Data Transfer (1/2)
Sender Receiver Serial Transfer Parallel Transfer D0 D7 Serial communication Single data line sends one bit per time unit. Ex: COM port, USB (Universal Serial Bus), IEEE 1394 Parallel communication Several data lines send data together. Ex: LPT1 sends 8 bits data in parallel, SCSI Note that there needs extra control lines for the negotiation between two ends. A serial bus provides a simple point-to-point connection allowing scalable capability with technology improvements. A serial cable and its connector are less expensive to manufacture and requires less room than a parallel cable -- a quality not lost on the manufacturers of laptop computers, where every square inch of space is at a premium. A parallel cable has more electrical interference between the multiple wires, the physical cable size is larger and more expensive since there are more wires and shielding, and synchronization between wires can be a problem at higher speeds.

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: Receiver
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. 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 byte byte byte byte preamble receiver sender byte byte byte sender receiver 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 Framing ASCII “A” (41H) Time (D0 first) 對於 8051 mode 2, start bit=0, Stop bit=1. mark 會維持在 1. UART 發現收到的資料與 mode 規劃的格式不同, 則會放棄此次的資料, 去接收下一筆. stop bit start bit mark 1 1 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. In asynchronous serial communication, peripheral chips and modems can be programmed for data that is 7 or 8 bits.

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: 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 mark stop bit start 00 10 01 11 Time (D0 first) 8-bit character Ex: BPRS, 一個 symbol代表 0 或 1 的一個bit. QPSK 一個 symbol代表 2 個bit. 16QAM 一個 symbol代表 4 個bit. 64QAM 一個 symbol代表 6 個bit.

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= 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. In telecommunications, RS-232 (Recommended Standard 232) is a standard for serial binary data signals connecting between a DTE (Data Terminal Equipment) and a DCE (Data Circuit-terminating Equipment). It is commonly used in computer serial ports. A similar ITU-T standard is V.24.

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

27 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 PC 8051 COM 1 port RS232 MAX232 UART

28 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 TxD RxD DTE ground TxD RxD DTE ground 8051-based board 8051-based board PC Com1 PC Com1

29 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 logic 0 -3V -25V 3V 25V logic 1 undefined RS 232 Voltage TTL is introduced at page 571. In 1968 the first logic family made of bipolar transistors was marketed and commonly referred to as the standard TTL family.

30 MAX232 MAX232 IC chips are commonly referred to as line drivers. PC
8051 COM 1 port RS232 MAX232 UART 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. connector 好像是指連接線 plug 就是 male socket 就是 female

32 Figure 10-4. RS232 Connector DB-25
14 1 25 13

33 Table 10-1: RS232 Pins (DB-25) for DTE (1/2)
Description 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 (2/2)
Description 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 25

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

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 Transmit data 4 20 Data terminal ready 5 7 Signal ground 6 Data set ready Request to send Clear to send 9 22 Ring indicator

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) They are not supported by the 8051 UART chips. 1. Data Carrier Detect (/DCD): modem 送出 DCD 告訴 PC 線路 (與其他 modem間) 已經建好 (valid carrier). 2. Received data (RxD) 3. Transmitted data (TxD) 4. Data terminal ready (DTR): 在 PC 開機自我測試後, 會送 DTR=0 告訴modem自己已經可以開始通訊. 5. Signal ground (GND) 6. Data set ready (/DSR): ): 在 modem 開機自我測試後, 會送 DTR=0 告訴PC自己已經可以開始通訊. 7. Request to send (/RTS): 表示有一個 byte 的資料要送出. 8. Clear to send (/CTR): 回應 RTS, 表示已準備好可接收此資料. 9. Ring indicator (RI): 通常 modem 會自己 answer the phone. 如果有 PC 負責接電話, 則 modem 用 RI 告訴 PC 有電話.

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. assert: 聲稱, 宣告

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. RS422, RS485 和 RS232 只是轉換電壓不一樣 (i.e., 電壓準位不同),

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 TxD DB-9 P3.0 RxD 11 10 12 14 13 2 3 5

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 Figure 10-7 (a): Inside MAX232
16 1 3 4 5 2 6 14 13 7 8 11 12 10 9 15 Vcc C1 C2 + C3 C4 TTL side RS232 side T1IN R1OUT T2IN R2OUT T1OUT R1IN T2OUT R2IN 22 mF

46 Figure 10-7: (b) MAX232’s Connection to the 8051 (Null Modem)
P3.1 TxD DB-9 P3.0 RxD 11 10 12 14 13 2 3 5 TTL-compatible RS232-compatible

47 Figure 10-8: (a) Inside MAX233
7 15 10 11 16 5 4 18 19 2 3 1 20 6 Vcc 12 17 14 9 TTL side RS232 side T1IN R1OUT T2IN R2OUT T1OUT R1IN T2OUT R2IN 13

48 Figure 10-8: (b) MAX233’s Connection to the 8051 (Null Modem)
P3.1 TxD DB-9 P3.0 RxD 11 2 10 3 5 4 TTL-compatible RS232-compatible

49 Section 10.3 8051 Serial Port Programming in Assembly

50 PC Baud Rates PC supports several baud rates.
110 bps 150 300 600 1200 2400 4800 9600 (default) 19200 Note: Baud rates supported by 486/Pentium IBM PC BIOS. 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.

51 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 XTAL oscillator ÷ 12 ÷ 32 By UART Machine cycle frequency 28800 Hz To timer 1 To set the Baud rate 921.6 kHz MHz Timer 1 default SMOD in PCOM =0, baud rate 是 降頻為 1/32. 在後面會提到, SMOD=1, baud rate 是 降頻為 1/16, 換句話說, baud rate 是 double.

52 Baud Rates in the 8051 (2/2) Timer 1, mode 2 (8-bit, auto-reload)
Define TH1 to set the baud rate. XTAL = MHz The system frequency = MHz / 12 = kHz Timer 1 has kHz/ 32 = 28,800 Hz as source. TH1=FDH means that UART sends a bit every 3 timer source. Baud rate = 28,800/3= 9,600 Hz Serial communication 中的 mode 2 使用 Timer 1. 對於 8052 還會利用 timer 2 來設定 baud rate.

53 Example 10-1 With XTAL = MHz, find the TH1 value needed to have the following baud rates. (a) (b) (c) 1200 Solution: With XTAL = MHz, we have: The frequency of system clock = MHz / 12 = kHz The frequency sent to timer 1 = kHz/ 32 = 28,800 Hz (a) 28,800 / 3 = where -3 = FD (hex) is loaded into TH1 (b) 28,800 / 12 = where -12 = F4 (hex) is loaded into TH1 (c) 28,800 / 24 = where -24 = E8 (hex) is loaded into TH1 Notice that dividing 1/12th of the crystal frequency by 32 is the default value upon activation of the 8051 RESET pin.

54 Table 10–4 Timer 1 TH1 Register Values for Various Baud Rates

55 Registers Used in Serial Transfer Circuit
SBUF (Serial data buffer) SCON (Serial control register) PCON (Power control register) You can see Appendix H (pages ) for details. PC also has several registers to control COM1, COM2.

56 SBUF Register Serial data register: SBUF
An 8-bit register to store the serial data For a byte of data to be transferred via the TxD line, it must be placed in the SBUF. MOV SBUF,A ;send data from A SBUF holds the byte of data when it is received by the 8051’s RxD line. MOV A,SBUF ;receive and copy to A Not bit-addressable Receiver 有一個 SBUF, Transmitter 也有一個 SBUF, 兩個 SBUF 不會混用, 表示可以同時傳輸與接收. MOV SBUF,#’A’ ;put char ‘A’ to transmit

57 SCON Register Serial control register: SCON SM0 SM1 SM2 REN TB8 RB8 TI
SM0, SM1 Serial port mode specifier REN (Receive enable) set/cleared by software to enable/disable reception. TI Transmit interrupt flag. RI Receive interrupt flag. SM2 = TB8 = RB8 =0 (not widely used) Default SMOD (D7 in PCON) =0, the baud rate is XTLA÷12÷32; SMOD =1, the baud rate is XTLA÷12÷16, double the frequency. (MSB) (LSB) SM0 SM1 SM2 REN TB8 RB8 TI RI * SCON is bit-addressable.

58 SM0, SM1 SM0 and SM1 determine the framing of data.
SCON.6 (SM1) a1d SCON.7 (SM0) Only mode 1 is compatible with COM port of IBM PC. See Appendix A.3. and Appendix H SM0 SM Mode Operating Mode Baud Rate Shift register Fosc./12 bit UART Variable by timer1 bit UART Fosc./64 or Fosc./32 bit UART Variable Fosc: 石英晶體的振盪頻率. (Frequency of oscillator) In mode =0, baud rate 一定是 Fosc./12, 所以只要設定 SCON, 不需要設定 timer/counter. Example, 外部石英晶體的振盪頻=12 MHz, 則 baud rate =1M bps. In mode 0, 若 shift register 有 8 bits, 所以加上 start bit 和 stop bit, 共 10 bit. 接收時 stop bit 會自動存入 SCON RB8. 接收的順利為 D0, D1,...,D7. In mode 1, 則 8 bits data 加上 start bit 和 stop bit, 共傳送 10 bit. Baud rate 由 timer 1 設定, 所以是 variable (只能用 timer 1). 當 SMOD =0, baud rate 為 Fosc./[384*(256-TH1)]. 當 SMOD=1, baud rate 為 Fosc./[192*(256-TH1)]. In mode 2 & 3, 使用 9-bit UART, 表示 8 bit data 加上 1 bit 的 SCON TB8. 再加上 1 start bit 和 1 stop bit, 共 11 bit 的傳輸量. 除了 baud rate 外, mode 2 與 mode 3 的工作模式是相同的. In mode =2, 當 SMOD =0, baud rate 只能是 Fosc./32 (default); 當 SMOD =1, baud rate 只能是 Fosc./64. Timer 無需設定. In mode 3 與 mode 1 的 baud rate 規劃的方式是相同的. (也會受到 SMOD 的影響).

59 REN (Receive Enable) SCON.4
Set/cleared by software to enable/disable reception. REN=1 It enable the 8051 to receive data on the RxD pin of the 8051. If we want the 8051 to both transfer and receive data, REN must be set to 1. SETB SCON.4 REN=0 The receiver is disabled. The 8051 can not receive data. CLR SCON.4

60 TI (Transmit Interrupt Flag)
SCON.1 When the 8051 finishes the transfer of the 8-bit character, it raises the TI flag. TI is raised by hardware at the beginning of the stop bit in mode 1. Must be cleared by software. In mode 0, TI 在傳送第 8 個 bit 之後與送出stop bit 前會被 (hardware) set. 在其他 mode, TI 在開始傳送 stop bit 時被 set. Programmer 必須自己在傳送資料開始前就將 TI 清為 0. 否則 JNB 檢查 TI, 或以 TI 觸發 interrupt, 都會讓程式不等待 URAT 的傳送就往下執行.

61 RI (Receive Interrupt)
SCON.0 Receive interrupt flag When the 8051 receives data serially via RxD, it gets rid of the start and stop bits and place the byte in the SBUF register. Set by hardware halfway through the stop bit time in mode 1. Must be cleared by software. get rid of 除去 = be rid of; halfway 中途 In mode 0, RI 在收到第 8 個 bit (D7) 之後會被 set. 在其他 mode, RI 在接收到 stop bit 的中途會被 set. Programmer 必須自己在接收資料開始前就將 RI 清為 0. 否則 JNB 檢查 RI, 或以 RI 觸發 interrupt, 都會讓程式不等待 URAT 的接收就往下執行, 就會得到錯誤的答案.

62 SM2 SCON.5 SM2 enables the multiprocessor communication for mode 2 & 3. SM2=0 : to receive data  two processors talks SM2=1 : to receive address  multiprocessor talks 1 2 3 4 5 TxD RxD Multiprocessor communication 的功能可以讓一群 8051 互傳資料. 例如我們可以將主控制器 (master) 安裝於工廠的主控室中, 而其他 8051 安裝於各生產單位 (slave). 當主控制器需要某一生產單位 8051 的資料, 只要由主控制器送出該 生產單位 8051 的 address, 就可以和此 8051 開始通訊. 其他 slave 看到不是自己的 address, 就不會收下 data. 許多紅綠燈連線控制也會需要 multiprocessor 的功能. 首先所有的 slave 先進入 multiprocessor mode, SM2=1, RI=0, 等待 master 傳出的 address. 主控制器會先送出生產單位 8051 的 address (TB8=1), 接著才送出 data (TB8=0). 每一個接收端都會將第9個bit (即傳送端的 TB8) copy 到自己的 RB8. 對於 master 想要通訊的 slave (address 相符), 就要把自己設成 SM2=0, RI=0, 以便接收資料. 當最後收到 master 送來包含 結束碼 的 data, 就會把自己的 SM2 設成 1, 回到接收 address 的其情況. 對於其他 slave (address 不相符), 則維持 SM2=1, RI=0 的狀態. 所以當之後的 data 被收下來之後, 馬上就會被丟棄, 不會讓 RI=1, 而繼續收下一筆資料. 在 mode 0, SM2 必須 = 0. (mode 0 不支援 multiprocessor communications) 在 mode 1, 若 SM2 =1, 則在接收到正確的 stop bit 後, RI 才會 activated 變成 1. 在 mode 2 & 3, 若RI=0 且 SM2 =1, 則在接收第 9 個 bit RB8, 其值為 1 (表示是 address), RI 才會變成 1, 將 address 收下來. 相反的, RI=0 且 SM2 =1 的情況下, 若得到的 RB8=0 (表示是 data), RI 將不會變為 1. 換言之, SM2 = 1 表示要多檢查 9 個 bit, 才算正確. 若RI=0 且 SM2 =0, 收完 data 後, 就會將資料送到SBUF, 且令 RI=1. data data address=5 1 time TB8(the 9th bit) 8 bits

63 TB8 (Transfer Bit 8) SCON.3 For multiprocessor communication
The 9th bit will be transmitted in mode 2 & 3. TB8 = 1: address TB8 = 0: data Set/Cleared by software. In mode 0 & 1, TB8 is not used. In mode 2 & 3, TB8 會被當作第 9-bit 送出. 對於多處理機通訊時 (multiprocessor communication), 一群 8051 互相通訊. 這些 8051 以 address 表示目的地, 再送出 data. 接收端為了要區分是 address 還是 data, (而且辨別是否是給自己的), 便使用 TB8. 若 TB8=1, it is address. 若 TB8=0, it is data.

64 RB8 (Receive Bit 8) SCON.2 For multiprocessor communication
In serial mode 1, RB8 gets a copy of the stop bit when an 8-bit data is received. In serial mode 2 & 3, RB8 gets a copy of the 9th bit. In fact, it is the TB8 in transmitter. In mode 0, RB8 is not used. In mode 1, stop bit 會自動存入 SCON RB8. In mode 2 & 3, 第 9-bit 會自動存入 SCON RB8.

65 Figure 10-9. SCON Serial Port Control Register (Bit Addressable)
SM0 SM1 SM2 REN TB8 RB8 TI RI SM0 SCON.7 Serial port mode specifier SM1 SCON.6 SM2 SCON.5 Used for multiprocessor communication. (Make it 0) REN SCON.4 Set/cleared by software to enable/disable reception. TB8 SCON.3 Not widely used. RB8 SCON.2 TI SCON.1 Transmit interrupt flag. Set by hardware at the beginning of the stop bit in mode 1. Must be cleared by software. RI SCON.0 Receive interrupt flag. Set by hardware halfway through the stop bit time in mode 1. Must be cleared by software. Note: Make SM2, TB8, and RB8 = 0. 注意: mode 0 運作的方式與其他 mode 不相同. 在 mode 0 下, 由 RxD 發送 bit, TxD 發送 shift clock. 經過 8 個 clock, 會停止發射, 並令 TI=1. 接收資料時, 先令 REN=1, 當設定 CLR RI 使 RI=0, RxD 就開始接收資料, TxD 輸出 shift clock. 當收完 8 bit data, RI 被設為 1, 以通知 programmer 用 MOV A, SBUF 將資料取走.

66 Transfer Data with the TI flag (1/2)
The following sequence is the steps that the 8051 goes through in transmitting a character via TxD: The byte character to be transmitted is written into the SBUF register. It transfers the start bit. The 8-bit character is transferred one bit at a time. The stop bit is transferred. 這裡介紹的是 mode 2. 由 TxD 傳送 data, RxD 接收 data, 所以可以做到 full duplex. 8-bit data, TB8/RB8, start and stop bit, 共接收或送出 11 bit. Transfer 所使用的 SBUF 是 shift register, 一個Write To SBUF的動作 (應該是 MOV SBUF, A) 就會啟動 transfer. 當 8 bit 都傳完後, SBUF=0, 零偵測器就會通知 UART 傳完了, TxD 之後就會維持為 1. UART 自動加上 start and stop bits. 圖中沒有畫出 TB8, 在 mode 2 and mode 3, TB8 會當做第 9 個 bit of data 被送出. 藍色小正方形代表一些logic gates. 8-bit char bit by bit 8-bit A SBUF TxD zero detect shift add start and stop bits UART TI

67 Transfer Data with the TI flag (2/2)
Sequence continuous: During the transfer of the stop bit, the 8051 raises the TI flag, indicating that the last character was transmitted and it is ready to transfer the next character. By monitoring the TI flag, we know whether or not the 8051 is ready to transfer another byte. We will not overloading the SBUF register. If we write another byte into the SBUF before TI is raised, the untransmitted portion of the previous byte will be lost. We can use interrupt to transfer data in Chapter 11. After SBUF is loaded with a new byte, the TI flag bit must be cleared by the programmer.

68 Programming the 8051 to Transfer data Serially (1/2)
Use the timer 1 in mode 2 MOV TMOD,#20H Set the value TH1 to chose baud rate. Look at the Table 10-4. MOV TH1,#0FDH ;Baud rate = 9600Hz Set SCON register in mode 1. MOV SCON,#50H Start the timer. SETB TR1 對於 serial communication, 啟動 timer 後 (TR1=1) mode 2 後, timer 就會不斷地 count up, round off, reload. 此時完全不在意 timer 是否 overflow (不需要檢查 TF1=1).

69 Programming the 8051 to Transfer data Serially (2/2)
raise when sending the stop bit Clear TI flag. CLR TI The character byte to be transferred serially is written into the SBUF register. MOV SBUF,#’A’ Keep monitoring the Transmit Interrupt (TI) to see if it is raised. HERE: JNB TI, HERE To transfer the next character, go to Step 5. TI=0 transfer data

70 Example 10-2 Write a program for the 8051 to transfer letter “A” serially at 4800 baud, continuously. Solution: MOV TMOD,#20H ;timer 1, mode 2 MOV TH1,#-6 ;4800 baud rate MOV SCON,#50H ;8-bit,1 stop, REN enabled SETB TR ;start timer 1 AGAIN: MOV SBUF,#”A” ;letter “A” to be transferred HERE: JNB TI,HERE ;wait for the last bit CLR TI ;clear TI for next char SJMP AGAIN ;keep sending A 對 asem 這個 assembler 而言, 如果指令為 MOV B,#“A“ 會被翻成 75 F0 41. 若使用單引號: MOV B.#‘A’ 也會翻成 75 F0 41. 以下是存在 ROM 中的資料, 表示 asem 不管單引號還是雙引號. 15: 001A 41 MYDATA: DB "A" 16: 001B 41 EM: DB 'A' 17: 001C D1: DB "ABC" 18: 001F D2 : DB 'ABC'

71 Example 10-3 (1/2) Write a program to transfer the message “YES” serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously. Solution: MOV TMOD,#20H ;timer 1, mode 2 MOV TH1,#-3 ;9600 baud MOV SCON,#50H SETB TR1 AGAIN:MOV A,#”Y” ;transfer “Y” ACALL TRANS MOV A,#”E” ;transfer “E” MOV A,#”S” ;transfer “S” SJMP AGAIN ;keep doing it

72 Example 10-3 (2/2) ;serial data transfer subroutine
TRANS:MOV SBUF,A ;load SBUF HERE: JNB TI,HERE ;wait for last bit to transfer CLR TI ;get ready for next byte RET

73 Receive Data with the RI flag (1/2)
The following sequence is the steps that the 8051 goes through in receiving a character via RxD: 8051 receives the start bit indicating that the next bit is the first bit of the character to be received. The 8-bit character is received one bit at a time. When the last bit is received, a byte is formed and placed in SBUF. 接收端必須要配合發射端的 baud rate 來設定. 通常接收端的 sampling rate 均假設為 n 倍的傳送端時脈頻率 (n=1, ,4,16, 64). 當數到第 n/2 個 sample, 就會收下當作輸入的信號. 只要 RxD 收到一個 1-to-0 transition, 就會當作是 start bit, 通知 UART, 開始接收 data. 接收資料時, 先令 REN=1, 當設定 CLR RI 使 RI=0, RxD 就開始接收資料. 收進來的 bit 會存入 shift register, 當收滿 8 bit data, 會被 load 到 SBUF, RI 被設為 1, 以通知 programmer 用 MOV A, SBUF 將資料取走. 圖中沒有畫出 RB8. 在 mode 0, RB8 沒有用到. 在 mode 1, if SMOD=0, the stop bit 會 copy 到 RB8. 在 mode 2 and mode 3, the 9th bit of data 會被 copy 到 RB8. In mode 2, 3, 正確接收資料且被存到 SBUF 的條件: ( RI=0 而且 TB8=1 ) or ( RI=0 而且 SM2=0 ) . 第一項是 multiprocessor 接收 address 的情況, 第二項是 1-to-1的情況下接收 data. remove/check the start and stop bits bit by bit 8-bit 8-bit shift regiter SBUF A RxD load shift REN=1 UART RI

74 Receive Data with the TI flag (2/2)
Sequence continuous: The stop bit is received. During receiving the stop bit, the 8051 make RI=1, indicating that an entire character was been received and must be picked up before it gets overwritten by an incoming character. By monitoring the RI flag, we know whether or not the 8051 has received a character byte. If we fail to copy SBUF into a safe place, we risk the loss of the received byte. We can use interrupt to transfer data in Chapter 11. After SBUF is copied into a safe place, the RI flag bit must be cleared by the programmer.

75 Programming the 8051 to Receive data Serially (1/2)
Use the timer 1 in mode 2 MOV TMOD,#20H Set the value TH1 to chose baud rate. Look at the Table 10-4. MOV TH1,#0FDH ;Baud rate = 9600Hz Set SCON register in mode 1. MOV SCON,#50H Start the timer. SETB TR1

76 Programming the 8051 to Receive data Serially (2/2)
raise when getting the stop bit Clear RI flag. CLR RI Keep monitoring the Receive Interrupt (RI) to see if it is raised. HERE: JNB RI, HERE When RI is raised, SBUF has the whole byte. Move the content of SBUF to a safe place. MOV A,SBUF To receive the next character, go to Step 5. RI=0 receive data

77 Example 10-4 Program the 8051 to receive bytes of data serially, and put them in P1. Set the baud rate at 4800, 8-bit data, and 1 stop bit. Solution: MOV TMOD,#20H ;timer1, mode 2 (auto reload) MOV TH1,#-6 ;4800 baud MOV SCON,#50H ;8-bit, 1 stop, REN enabled SETB TR ;start timer 1 HERE: JNB RI,HERE ;wait for char to come in MOV A,SBUF ;save incoming byte in A MOV P1,A ;send to port 1 CLR RI ;get ready to receive next byte SJMP HERE ;keep getting data

78 Example 10-5 (1/4) Assume that the 8051 serial port is connected to the COM port of the IBM PC, and on the PC we are using the terminal.exe program to send and receive data serially. P1 and P2 of the 8051 are connected to LEDs and switches, respectively. Write an 8051 program to (a) send to the PC the message ”We Are Ready”, (b) receive any data sent by the PC and put it on LEDs connected to P1, and (c) get data on switches connected to P2 and send it to the PC serially. The program should perform part (a) once, but parts (b) and (c) continuously. Use the 4800 baud rate.

79 Example 10-5 (2/4) Solution: ORG 0 MOV P2,#0FFH ;make P2 an input port
MOV TMOD,#20H MOV TH1,#0FAH ;4800 baud rate MOV SCON,#50H SETB TR ;start timer 1 MOV DPTR,#MYDATA ;load pointer for message TxD RxD P1 P2 LED SW To PC COM port 8051

80 Example 10-5 (3/4) MOV DPTR,#MYDATA ;load pointer for message
H_1: CLR A MOVC ;get the character JZ B_ ;if last character get out ACALL SEND INC DPTR SJMP H_ ;next character B_1: MOV A,P ;read data on P2 ACALL SEND ;transfer it serially ACALL RECV ;get the serial data MOV P1,A ;display it on LEDs SJMP B_ ;stay in loop indefinitely (a) (c) (b)

81 Example 10-5 (4/4) ;-----------serial data transfer. ACC has the data
SEND: MOV SBUF,A ;load the data H_2: JNB TI,H_2 ;stay here until last bit gone CLR TI ;get ready for next char RET ; Receive data serially in ACC RECV: JNB RI,RECV ;wait here for char MOV A,SBUF ;save it in ACC CLR RI ;get ready for next char ; The message to send MYDATA:DB “We Are Ready”,0 END

82 Doubling the Baud Rate in the 8051
There are two ways to increase the baud rate of data transfer in the 8051: To use a higher frequency crystal. It is not feasible in many situations since the system crystal is fixed. Many new crystal may not be compatible with the IBM PC serial COM ports baud rate. To change a bit in the PCON register. This is a software way by setting SMOD=1.

83 PCON Register -- GF1 GF2 PD IDL
SMOD Double baud rate. If Timer 1 is used to generate baud and SMOD=1, the baud rate is doubled when the Serial Port is used in modes 1,2,3. GF1,GF0 General purpose flag bit. PD Power down bit. Setting this bit activates “Power Down” operation in the 80C51BH. (precedence) IDL Idle Mode bit. Setting this bit activates “Idle Mode” operation in the 80C51BH. 8051有兩種power-controlled mode: Idle mode (閒置模式) & power down mode (電源關閉模式) 正常模式: 12MHz, Icc 最大值為 20mA. 閒置模式: IDL=1, OSC 正常運作, 送給 interrupt. serial port, timer, I/O 等電路, 但停止供應 clock 給 CPU. 內部 RAM, SFR 等資料都不會改變, 只是 CPU 不再運作. (software and hardware) Interrupt 可以叫醒 CPU 繼續工作. RST 也可以重新啟動 CPU. Icc 最大值為 5mA. 由 interrupt 叫醒 CPU 工作後, 在 RETI 之後再加上設定 idle 的指令, 就可以讓 CPU 繼續睡覺. 電源關閉模式: PD=1, OSC 停止工作, 除了內部 RAM, SFR 等資料不會改變外, 所有動作均停止. 唯一能終止電源關閉模式的只有 RST, 也有些版本可用 INT0,INT1 來重新啟動 OSC, 8051 開始運作. 電壓會降到 2V, Icc 最大值為 100uA (Vcc=6V) 或 40uA (Vcc=3V). (MSB) (LSB) SMOD -- GF1 GF2 PD IDL * PCON is not bit-addressable. See Appendix H. p410

84 SMOD Flag of the PCON Register
Power control register: PCON MOV A, PCON SETB ACC.7 MOV PCON,A ;we don’t want to modify other bits An 8-bit register Not bit-addressable SCOM=0: default SCOM=1: double the baud rate See Table 10-5

85 Table 10-5: Baud Rate Comparison for SMOD = 0 and SMOD =1
TH1 (Decimal) (Hex) SMOD = 0 SMOD = 1 -3 FD 9,600 19,200 -6 FA 4,800 -12 F4 2,400 -24 E8 1,200 Note: XTAL = MHz. XTAL oscillator ÷ 12 ÷ 16 ÷ 32 Machine cycle freq. 921.6 kHz 57600 Hz 28800 Hz To timer 1 to set baud rate MHz SMOD = 1 SMOD = 0

86 Baud Rates for SMOD=0 When SMOD=0, the 8051 divides 1/12 of the crystal frequency by 32, and uses that frequency for timer 1 to set the baud rate. XTAL = MHz The system frequency = MHz / 12 = kHz Timer 1 has kHz/32 = 28,800 Hz as source. TH1=256 - Crystal frequency/(12*32*Baud rate) Default on reset See Appendix A, page 363

87 Baud Rates for SMOD=1 When SMOD=1, the 8051 divides 1/12 of the crystal frequency by 16, and uses that frequency for timer 1 to set the baud rate. XTAL = MHz The system frequency = MHz / 12 = kHz Timer 1 has kHz/16 = 57,600 Hz as source. TH1=256 - Crystal frequency/(12*16*Baud rate)

88 Example 10-6 (1/2) Assuming that XTAL = MHz for the following program, state (a) what this program does, (b) compute the frequency used by timer 1 to set the baud rate, and (c) find the baud rate of the data transfer. Solution: (a) This program transfers ASCII letter B ( binary) continuously. (b) and (c) With XTAL = MHz and SMOD = 1 / 12 = kHz machine cycle frequency. 921.6 /16 = 57,600 Hz frequency used by timer 1 to set the baud rate. 57,600 / 3 = 19,200, the baud rate.

89 Example 10-6 (2/2) SETB ACC.7 MOV PCON,A ;SMOD=1, double baud rate
MOV A,PCON SETB ACC.7 MOV PCON,A ;SMOD=1, double baud rate MOV TMOD,#20H ;Timer 1, mode 2, auto reload MOV TH1,#-3 ;19200 baud rate MOV SCON,#50H ;8-bit data,1 stop bit, RI enabled SETB TR ;start Timer 1 MOV A,#”B” ;transfer letter B A_1:CLR TI ;make sure TI=0 MOV SBUF,A ;transfer it H_1:JNB TI H_1 ;check TI SJMP A_ ;do again

90 Example 10-7 x Find the TH1 value (in both decimal and hex) to set the baud rate to each of the following: (a) 9600 Hz (b) 4800 Hz if SMOD =1 Assume that XTAL = MHz. Solution: With XTAL = and SMOD = 1, / 12 = kHz machine cycle frequency. 921.6 / 16 = 57,600 Hz frequency used by the timer 1 (a) 57,600 / 9600 = 6  TH1 = -6 or TH1 = FAH. (b) 57,600 / 4800 = 12  TH1 = -12 or TH1 = F4H.

91 Example 10-8 x Find the baud rate if TH1 = -2, SMOD = 1, and XTAL = MHz. Is this baud rate supported by IBM/compatible PCs? Solution: With XTAL = and SMOD = 1, we have timer 1 frequency = 57,600 Hz. The baud rate is 57,600 / 2 = 28,800. This baud rate is not supported by the BIOS of the PC; however, the PC can be programmed to do data transfer at such a speed. The software of many modems can do this. Also, Hyperterminal in Windows 95 (and higher) supports this and other baud rates.

92 Table 10–5 Baud Rate Comparison for SMOD = 0 and SMOD = 1

93 Section 10.4 Programming the Second Serial Port

94 Second Serial Port Many of the new generations of the microcontrollers come with two serial ports. DS89C4x0, DS89C320 But not all versions of 8051/52 microcontroller. There two ports are called as Port 0 and Port 1. Port 0 is as same as the original 8051. Port 1 uses new pins and new registers. See Table 10-6 and Figure for registers See Figure for 89C4x0 See Figure for connection with MAX232

95 Table 10–6 SFR Byte Addresses for DS89C4x0 Serial Ports
shared as same as the original 8051 serial port new SCON1,SBUF1 Compare with p556 Table A-2

96 Table 10–7 SFR Addresses for the DS89C4x0 (420, 430, etc.)

97 Figure 10–12 SCON0 and SCON1 Bit Addresses (TI and RI bits must be noted)

98 Figure 10–10 DS89C4x0 Pin Diagram.
port 1 port 0 Note: Notice P1.2 and P1.3 pins are used by Rx and Tx lines of the 2nd serial port

99 Figure 10–11 Connection with MAX232
(b) MAX232’s Connection to the DS89C4x0 (a) Inside MAX232

100 Assembly Language for Second Port
The older 8051 assemblers do not support the second serial port, we need to define them by their SFR address. Example Also, many C compiler do not support the second serial port, we have to declare the byte addresses of the new SFR register by using the SFR keyword. Example 10-20

101 Example (1/2) Write a program for the second serial port of the DS89C4x0 to continuously transfer the letter “A” serially at 4800 baud. User 8-bit data and 1 stop bit. User Timer 1. Solution: SBUF1 EQU 0C1H ;defined by yourself SCON1 EQU 0C0H TI1 BIT 0C1H RI1 BIT 0C0H ORG 0H MAIN: MOV TMOD,#20H MOV TH1,#-6 MOV SCON1,#50H SETB TR1

102 Example 10-11 (2/2) ANAIN: MOV A,#’A’ ;send ‘A’ continuously
ACALL SENDCON2 SJMP AGAIN SENDCOM2: ;send by using port 1 MOV SBUF1,A JNB TI1,HERE CLR TI1 RET END

103 Section 10.5 Serial Port Programming in C

104 Transmitting and Receiving Data in C
Using C to program the serial port. Example does the same work as Example Please read other examples by yourself.

105 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; } }

106 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

107 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 Assembly and C Program the second serial port of DS89C4x0 in Assembly and C


Download ppt "Chapter Serial Port Programming in Assembly and C"

Similar presentations


Ads by Google