The 8051 Microcontroller - Chapter 5: Serial Port Operation - Lê Chí Thông
Serial Port
• RXD (P3.0) and TXD (P3.1) pins
• Full Duplex: simultaneous transmission and reception
• 2 special function registers: SCON and SBUF
• SCON: status bits and control bits
• SBUF: same address but 2 buffers; 1 buffer for
transmission and 1 buffer for reception
• Baud rate (serial port frequency of operation) is
supplied and programmed by Timer1
• RXD (P3.0) and TXD (P3.1) pins
• Full Duplex: simultaneous transmission and reception
• 2 special function registers: SCON and SBUF
• SCON: status bits and control bits
• SBUF: same address but 2 buffers; 1 buffer for
transmission and 1 buffer for reception
• Baud rate (serial port frequency of operation) is
supplied and programmed by Timer1
Bạn đang xem tài liệu "The 8051 Microcontroller - Chapter 5: Serial Port Operation - Lê Chí Thông", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.
File đính kèm:
- the_8051_microcontroller_chapter_5_serial_port_operation_le.pdf
Nội dung text: The 8051 Microcontroller - Chapter 5: Serial Port Operation - Lê Chí Thông
- ĐH Bách Khoa TP.HCM Lê Chí Thông The 8051 Microcontroller Chapter 5 Serial Port Operation Lê Chí Thông chithong@hcmut.edu.vn sites.google.com/site/chithong Ref. I. Scott Mackenzie, The 8051 Microcontroller Serial Port • RXD (P3.0) and TXD (P3.1) pins • Full Duplex: simultaneous transmission and reception • 2 special function registers: SCON and SBUF • SCON: status bits and control bits • SBUF: same address but 2 buffers; 1 buffer for transmission and 1 buffer for reception • Baud rate (serial port frequency of operation) is supplied and programmed by Timer1 Ref. I. Scott Mackenzie Lê Chí Thông 2 sites.google.com/site/chithong 1
- ĐH Bách Khoa TP.HCM Lê Chí Thông SCON Register Ref. I. Scott Mackenzie Lê Chí Thông 5 Mode 0: 8-Bit Shift Register • RXD is used for both data input and output • Serial data enter and exit (LSB first) through RXD • TXD line serves as the clock • TXD outputs the shift clock • Baud rate = 1/12 fOSC Ref. I. Scott Mackenzie Lê Chí Thông 6 sites.google.com/site/chithong 3
- ĐH Bách Khoa TP.HCM Lê Chí Thông Mode 0: 8-Bit Shift Register • One application of shift register mode is to expand the out capability of the 8051 • A serial-to-parallel shift register IC can be connected to the 8051 TXD and RXD lines to provide an extra output lines Ref. I. Scott Mackenzie Lê Chí Thông 9 Mode 1: 8-Bit UART with Variable Baud Rate • UART : Universal Asynchronous Receiver/Transmitter • A data frame includes a start bit (low), data bits, and a stop bit (high) . • A parity bit is sometimes inserted between the last data bit and the stop bit. • Mode 1: 10 bits are transmitted on TXD or received on RXD, including a start bit (0), 8 data bits (LSB first), and a stop bit (1). • The stop bit goes into RB8 in SCON. • Baud rate is set by the Timer 1 overflow rate. Ref. I. Scott Mackenzie Lê Chí Thông 10 sites.google.com/site/chithong 5
- ĐH Bách Khoa TP.HCM Lê Chí Thông Serial Port Baud Rates 1MHz (12 MHz crystal) 375K/187.5K (12 MHz crystal) To set SMOD: MOV A,PCON SETB ACC.7 Ref. I. Scott Mackenzie Lê Chí Thông MOV PCON,A 13 Using Timer 1 as the Baud Rate Clock • Usually use Timer 1 Mode 2 to provide baud rate clock • Baud Rate = Timer 1 overflow rate / 32 (SMOD=0) • Baud Rate = Timer 1 overflow rate / 16 (SMOD=1) • Eg. Calculate Timer 1 overflow rate to provide 1200 baud operation (12 MHz crystal) • fOSC = 12 MHz fCLK = 1 MHz TCLK = 1 μs • Assume SMOD=0: Timer 1 overflow rate = 1200 x 32 = 38.4 KHz Toverflow = 1/38.4 kHz = 26.04 μs • An overflow requires T overflow /T CLK ≈ 26 clocks The reload value for Timer 1 is -26 Ref. I. Scott Mackenzie Lê Chí Thông 14 sites.google.com/site/chithong 7
- ĐH Bách Khoa TP.HCM Lê Chí Thông Initialize the Serial Port ORG 0000H MOV SCON,#01010010B ;Serial port mode 1 MOV TMOD,#00100000B ;Timer 1 mode 2 MOV TH1,#-26 ;reload count for 1200 baud SETB TR1 ;start Timer 1 Ref. I. Scott Mackenzie Lê Chí Thông 17 Initialize the Serial Port (SMOD=1) ORG 0000H MOV SCON,#01010010B ;Serial port mode 1 MOV A,PCON SETB ACC.7 ;SMOD=1 MOV PCON,A MOV TMOD,#00100000B ;Timer 1 mode 2 MOV TH1,#-26 ;reload count for 2400 baud SETB TR1 ;start Timer 1 Ref. I. Scott Mackenzie Lê Chí Thông 18 sites.google.com/site/chithong 9
- ĐH Bách Khoa TP.HCM Lê Chí Thông Solution Assume that a string of data is stored in internal RAM at address 30H to 50H. Write a program that sends this string to serial port using UART 8-bit, 2400 baud, 11.059-MHz crystal. ORG 0000H MOV SCON,#01010010B MOV TMOD,#00100000B MOV TH1,#-12 SETB TR1 MOV R0,#30H LOOP: MOV A,@R0 ACALL SEND INC R0 CJNE R0,#51H,LOOP SJMP DONE SEND: JNB TI,$ CLR TI MOV SBUF,A RET DONE: NOP Ref. I. Scott MackenzieEND Lê Chí Thông 21 Example 2: Reception Write a program that receives a 20-byte string from the 8051 serial port (2400 baud, crystal 11.0592 MHz) and then stores in the internal RAM from the location 40H. ORG 0000H MOV SCON,#01010010B ;Serial port mode 1 MOV TMOD,#00100000B ;Timer 1 mode 2 MOV TH1,#-12 ;reload count for 2400 baud SETB TR1 ;start Timer 1 MOV R2,#20 ;number of loops MOV R0,#40H ;starting address LOOP: ACALL RECEIVE ;receive data MOV @R0,A ;store data INC R0 ;increase pointer DJNZ R2,LOOP ;loop 20 times SJMP DONE RECEIVE: JNB RI,$ ;receive buffer full? No: check again CLR RI ;yes: clear flag and MOV A,SBUF ; receive data RET ;return DONE: NOP Ref. I. ScottEND Mackenzie Lê Chí Thông 22 sites.google.com/site/chithong 11
- ĐH Bách Khoa TP.HCM Lê Chí Thông Problem 2 Ref. I. Scott Mackenzie Lê Chí Thông 25 Mode 2: 9-Bit UART with Fixed Baud Rate • Mode 2: 11 bits are transmitted on TXD or received on RXD, including a start bit (0), 9 data bits (LSB first), and a stop bit (1). • On transmission , the 9 th bit is whatever has been put in TB8 in SCON. • On reception , the 9 th bit received is placed in RB8 in SCON. • Baud rate is either fOSC /64 (SMOD=0) or fOSC /32 (SMOD=1) Ref. I. Scott Mackenzie Lê Chí Thông 26 sites.google.com/site/chithong 13
- ĐH Bách Khoa TP.HCM Lê Chí Thông Adding a Parity Bit • Eg. Put odd parity bit in TB8, which becomes the 9 th data bit to be transmitted: MOV C,P ;put even parity bit in C flag CPL C ;convert to odd parity MOV TB8,C ;and move to the 9 th data bit MOV SBUF,A;move from A to SBUF to transmit Ref. I. Scott Mackenzie Lê Chí Thông 29 Example 3 Assume a 10-byte string of 8-bit ASCII codes is stored in internal RAM from the location 30H. Write a program that transmits this string out the 8051 serial port (4800 baud, crystal 11.0592 MHz) with odd parity added as the 9th bit Ref. I. Scott Mackenzie Lê Chí Thông 30 sites.google.com/site/chithong 15
- ĐH Bách Khoa TP.HCM Lê Chí Thông Multiprocessor Communications • When SM2=1 , reception is done only if RB8=1 . • The master first sends out an address byte that has 1 in the 9 th bit . So all slave can receive the address byte and examine it to test if it is being addressed. • The addressed slave will clear its SM2 bit and prepare to receive the data bytes that follow. The 9 th bit in data byte is 0 . • The slaves that were not addressed leave their SM2 bits set and ignore the incoming data bytes Ref. I. Scott Mackenzie Lê Chí Thông 33 References • I. Scott Mackenzie, The 8051 Microcontroller • Các tài li ệu trên Internet không trích d ẫn ho ặc không ghi tác gi ả Ref. I. Scott Mackenzie Lê Chí Thông 34 sites.google.com/site/chithong 17