1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

8051 chap5 serial VI XỬ LÝ

17 0 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

ĐH Bách Khoa TP.HCM Lê Chí Thơng The 8051 Microcontroller Chapter 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 special function registers: SCON and SBUF SCON: status bits and control bits SBUF: same address but buffers; buffer for transmission and buffer for reception • Baud rate (serial port frequency of operation) is supplied and programmed by Timer1 Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng ĐH Bách Khoa TP.HCM Lê Chí Thơng Serial port block diagram P→ →S buffer S→ →P buffer Writing to SBUF loads data to be transmitted Reading SBUF accesses received data Ref I Scott Mackenzie Lê Chí Thơng SCON Register Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng ĐH Bách Khoa TP.HCM Lê Chí Thơng SCON Register Ref I Scott Mackenzie Lê Chí Thơng 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 sites.google.com/site/chithong Lê Chí Thơng ĐH Bách Khoa TP.HCM Lê Chí Thơng Mode 0: 8-Bit Shift Register • Transmission is initiated by instruction that writes data to SBUF (eg MOV SBUF,A) Ref I Scott Mackenzie Lê Chí Thơng Mode 0: 8-Bit Shift Register • Reception is initiated when REN is and RI is • Set REN at the beginning of a program • Clear RI to begin a data input operation Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng Đ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 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), data bits (LSB first), and a stop bit (1) • The stop bit goes into RB8 in SCON • Baud rate is set by the Timer overflow rate Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 10 ĐH Bách Khoa TP.HCM Lê Chí Thơng Mode 1: 8-Bit UART with Variable Baud Rate • Transmission is initiated by writing to SBUF • TI is set at the end of character transmission and indicates “transmit buffer empty” WAIT:JNB TI,WAIT ;Check TI until set CLR TI ;Clear TI MOV SBUF,A ;Send character stop synchronization event Ref I Scott Mackenzie Lê Chí Thông 11 Mode 1: 8-Bit UART with Variable Baud Rate • Reception is initiated by a 1-to-0 transition on RXD The stop bit goes into RB8 in SCON SBUF is loaded with data bits RI is set at the end of character reception and indicates “receiver buffer full” • Conditions for reception: RI=0, and SM2=0, or SM2=1 and the received stop bit = WAIT:JNB RI,WAIT ;Check RI until set CLR RI ;Clear RI MOV A,SBUF ;Read character Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 12 ĐH Bách Khoa TP.HCM Lê Chí Thông Serial Port Baud Rates 1MHz (12 MHz crystal) 375K/187.5K (12 MHz crystal) Ref I Scott Mackenzie Lê Chí Thơng To set SMOD: MOV A,PCON SETB ACC.7 MOV PCON,A 13 Using Timer as the Baud Rate Clock • • • • Usually use Timer Mode to provide baud rate clock Baud Rate = Timer overflow rate / 32 (SMOD=0) Baud Rate = Timer overflow rate / 16 (SMOD=1) Eg Calculate Timer overflow rate to provide 1200 baud operation (12 MHz crystal) • fOSC = 12 MHz  fCLK = MHz  TCLK = μs • Assume SMOD=0: Timer overflow rate = 1200 x 32 = 38.4 KHz  Toverflow = 1/38.4 kHz = 26.04 μs • An overflow requires Toverflow/TCLK ≈ 26 clocks  The reload value for Timer is -26 Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 14 ĐH Bách Khoa TP.HCM Lê Chí Thơng Error in Baud Rate • Due to rounding, there is a slight error Generally, a 5% error is tolerable • Exact baud rates are possible using an 11.0592 MHz crystal • Eg Calculate Timer overflow rate to provide 1200 baud operation (11.0592 MHz crystal) • fOSC = 11.0592 MHz  TCLK = 12/11.0592 μs • Assume SMOD=0: Timer overflow rate = 1200 x 32 = 38.4 KHz  Toverflow = 1/0.0384 [μs] • An overflow requires Toverflow/TCLK = 24 clocks  The initial value for Timer is -24 Ref I Scott Mackenzie Lê Chí Thơng 15 Baud Rate Summary Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 16 ĐH Bách Khoa TP.HCM Lê Chí Thơng Initialize the Serial Port ORG 0000H MOV SCON,#01010010B MOV TMOD,#00100000B MOV TH1,#-26 SETB TR1 … Ref I Scott Mackenzie ;Serial port mode ;Timer mode ;reload count for 1200 baud ;start Timer Lê Chí Thơng 17 Initialize the Serial Port (SMOD=1) ORG 0000H MOV SCON,#01010010B MOV A,PCON SETB ACC.7 MOV PCON,A MOV TMOD,#00100000B MOV TH1,#-26 SETB TR1 … Ref I Scott Mackenzie sites.google.com/site/chithong ;Serial port mode ;SMOD=1 ;Timer mode ;reload count for 2400 baud ;start Timer Lê Chí Thơng 18 ĐH Bách Khoa TP.HCM Lê Chí Thơng Example 1: Transmission Assume a 10-byte string of data is stored in the internal RAM from the location 30H Write a program that sends this string to the 8051 serial port (1200 baud, crystal 11.0592 MHz) ORG 0000H MOV SCON,#01010010B MOV TMOD,#00100000B MOV TH1,#-24 SETB TR1 MOV R2,#10 MOV R0,#30H LOOP: MOV A,@R0 ACALL SEND INC R0 DJNZ R2,LOOP SJMP DONE SEND: JNB TI,$ CLR TI MOV SBUF,A RET DONE: NOP END Ref I Scott Mackenzie ;Serial port mode ;Timer mode ;reload count for 1200 baud ;start Timer ;number of loops ;starting address ;get data ;send data ;increase pointer ;loop 10 times ;transmit buffer empty? No:check again ;yes: clear flag and ; send data ;return Lê Chí Thơng 19 Your Turn! 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 Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 20 10 Đ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 Mackenzie END 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 MOV TMOD,#00100000B MOV TH1,#-12 SETB TR1 MOV R2,#20 MOV R0,#40H LOOP: ACALL RECEIVE MOV @R0,A INC R0 DJNZ R2,LOOP SJMP DONE RECEIVE: JNB RI,$ CLR RI MOV A,SBUF RET DONE: NOP END Ref I Scott Mackenzie sites.google.com/site/chithong ;Serial port mode ;Timer mode ;reload count for 2400 baud ;start Timer ;number of loops ;starting address ;receive data ;store data ;increase pointer ;loop 20 times ;receive buffer full? No: check again ;yes: clear flag and ; receive data ;return Lê Chí Thơng 22 11 ĐH Bách Khoa TP.HCM Lê Chí Thơng Your Turn! Write a program that receives a 20-byte string from serial port using UART 8-bit, 4800 baud, 11.059-MHz crystal, and then writes data to internal RAM from address 40H Ref I Scott Mackenzie Lê Chí Thơng 23 Problem Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 24 12 ĐH Bách Khoa TP.HCM Lê Chí Thơng Problem 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), data bits (LSB first), and a stop bit (1) • On transmission, the 9th bit is whatever has been put in TB8 in SCON • On reception, the 9th 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 sites.google.com/site/chithong Lê Chí Thơng 26 13 ĐH Bách Khoa TP.HCM Lê Chí Thơng Mode 3: 9-Bit UART with Variable Baud Rate • 9-bit UART: same as mode • Variable baud rate: same as mode Ref I Scott Mackenzie Lê Chí Thơng 27 Adding a Parity Bit • A common use for the 9th bit is to add parity to a character • The P bit in PSW register is set or cleared to establish even parity with bits in A register • Eg Put even parity bit in TB8, which becomes the 9th data bit to be transmitted: MOV C,P ;put even parity bit in C flag MOV TB8,C ;and move to the 9th data bit MOV SBUF,A;move from A to SBUF to transmit Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 28 14 ĐH Bách Khoa TP.HCM Lê Chí Thơng Adding a Parity Bit • Eg Put odd parity bit in TB8, which becomes the 9th 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 9th data bit MOV SBUF,A;move from A to SBUF to transmit Ref I Scott Mackenzie Lê Chí Thông 29 Example 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 sites.google.com/site/chithong Lê Chí Thơng 30 15 ĐH Bách Khoa TP.HCM Lê Chí Thơng Example ORG 0000H MOV SCON,#11010010B MOV TMOD,#00100000B MOV TH1,#-6 SETB TR1 MOV R2,#10 MOV R0,#30H LOOP: MOV A,@R0 MOV C,P CPL C MOV TB8,C ACALL SEND INC R0 DJNZ R2,LOOP SJMP DONE SEND: JNB TI,$ CLR TI MOV SBUF,A RET DONE: NOP END Ref I Scott Mackenzie ;Serial port mode (9-bit) ;Timer mode ;reload count for 4800 baud ;start Timer ;number of loops ;starting address ;get data ;put even parity bit in C flag ;convert to odd parity ;and move to the 9th data bit ;send data ;increase pointer ;loop 10 times ;check TI empty? No: check again ;yes: clear flag and ; send data ;return Lê Chí Thơng 31 Example Assume a 10-byte string of 7-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 8th bit ORG 0000H MOV SCON,#01010010B MOV TMOD,#00100000B MOV TH1,#-6 SETB TR1 MOV R2,#10 MOV R0,#30H LOOP: MOV A,@R0 CLR ACC.7 MOV C,P CPL C MOV ACC.7,C ACALL SEND INC R0 DJNZ R2,LOOP SEND: JNB TI,$ CLR TI MOV A,SBUF RET END Ref I Scott Mackenzie sites.google.com/site/chithong ;Serial port mode (8-bit) ;Timer mode ;reload count for 4800 baud ;start Timer ;number of loops ;starting address ;get data ;clear the 8th bit of A ;put even parity bit in C flag ;convert to odd parity ;and move to the 8th bit of A ;send data ;increase pointer ;loop 10 times ;check TI empty? No: check again ;yes: clear flag and ; send data ;return Lê Chí Thơng 32 16 Đ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 in the 9th 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 9th bit in data byte is • 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 Internet khơng trích dẫn khơng ghi tác giả Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 34 17

Ngày đăng: 13/04/2023, 08:09

w