View
239
Download
0
Category
Preview:
Citation preview
7/29/2019 8051 Chap5 Serial
1/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
1
Chapter 5Serial Port Operation
The 8051 Microcontroller
L Ch Thng
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 fortransmission and 1 buffer for reception
Baud rate (serial port frequency of operation) issupplied and programmed by Timer1
Ref. I. Scott Mackenzie 2L Ch Thng
7/29/2019 8051 Chap5 Serial
2/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
2
Serial port block diagram
Writing to SBUF loads data to be transmittedReading SBUF accesses received data
P S buffer S P buffer
Ref. I. Scott Mackenzie 3L Ch Thng
SCON Register
Ref. I. Scott Mackenzie 4L Ch Thng
7/29/2019 8051 Chap5 Serial
3/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
3
SCON Register
Ref. I. Scott Mackenzie 5L Ch Thng
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 TXDoutputs the shift clock
Baud rate = 1/12 f OSC
Ref. I. Scott Mackenzie 6L Ch Thng
7/29/2019 8051 Chap5 Serial
4/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
4
Mode 0: 8-Bit Shift Register
Transmission is initiated by instruction that writesdata to SBUF (eg. MOV SBUF,A)
Ref. I. Scott Mackenzie 7L Ch Thng
Mode 0: 8-Bit Shift Register
Reception is initiated when REN is 1 and RI is 0 Set REN at the beginning of a program Clear RI to begin a data input operation
Ref. I. Scott Mackenzie 8L Ch Thng
7/29/2019 8051 Chap5 Serial
5/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
5
Mode 0: 8-Bit Shift Register
One application of shift register mode is to expand theout capability of the 8051
A serial-to-parallel shift register IC can beconnected to the 8051 TXD and RXD lines toprovide an extra output lines
Ref. I. Scott Mackenzie 9L Ch Thng
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 10L Ch Thng
7/29/2019 8051 Chap5 Serial
6/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
6
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
synchronization event
stop
Ref. I. Scott Mackenzie 11L Ch Thng
Mode 1: 8-Bit UART with Variable Baud Rate
Reception is initiated by a 1-to-0 transition on RXD.1. The stop bit goes into RB8 in SCON.2. SBUF is loaded with 8 data bits.3. RI is set at the end of character reception and
indicates receiver buffer full. Conditions for reception:
1. RI=0, and2. SM2=0, or SM2=1 and the received stop bit = 1
WAIT:JNB RI,WAIT ;Check RI until setCLR RI ;Clear RI
MOV A,SBUF ;Read characterRef. I. Scott Mackenzie 12L Ch Thng
7/29/2019 8051 Chap5 Serial
7/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
7
Serial Port Baud Rates
1MHz (12 MHz crystal)
To set SMOD: MOV A,PCON
SETB ACC.7 MOV PCON,A
375K/187.5K (12 MHz crystal)
Ref. I. Scott Mackenzie 13L Ch Thng
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) f OSC = 12 MHz f CLK= 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 /TCLK 26 clocksThe reload value for Timer 1 is -26
Ref. I. Scott Mackenzie 14L Ch Thng
7/29/2019 8051 Chap5 Serial
8/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
8
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 MHzcrystal .
Eg. Calculate Timer 1 overflow rate to provide 1200baud operation (11.0592 MHz crystal)
f OSC = 11.0592 MHz TCLK= 12/11.0592 s
Assume SMOD=0: Timer 1 overflow rate = 1200 x32 = 38.4 KHz Toverflow = 1/0.0384 [s]
An overflow requires T overflow /TCLK= 24 clocksThe initial value for Timer 1 is -24
Ref. I. Scott Mackenzie 15L Ch Thng
Baud Rate Summary
Ref. I. Scott Mackenzie 16L Ch Thng
7/29/2019 8051 Chap5 Serial
9/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
9
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 17L Ch Thng
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 18L Ch Thng
7/29/2019 8051 Chap5 Serial
10/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
10
Example 1: Transmission
ORG 0000H MOV SCON,#01010010B ;Serial port mode 1
MOV TMOD,#00100000B ;Timer 1 mode 2
MOV TH1,#-24 ;reload count for 1200 baud
SETB TR1 ;start Timer 1 MOV R2,#10 ;number of loops MOV R0,#30H ;starting address
LOOP: MOV A,@R0 ;get data ACALL SEND ;send data
INC R0 ;increase pointer
DJNZ R2,LOOP ;loop 10 timesSJMP DONESEND: JNB TI,$ ;transmit buffer empty? No:check again
CLR TI ;yes: clear flag and MOV A,SBUF ; send data
RET ;returnDONE: NOP
END
Assume a 10-byte string of data is stored in the internal RAM from the location 30H.Write a program that sends this st ring to the 8051 serial port (1200 baud, crystal11.0592 MHz)
19L Ch ThngRef. I. Scott Mackenzie
Example 2: Reception
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 pointerDJNZ R2,LOOP ;loop 10 timesSJMP DONE
RECEIVE:JNB RI,$ ;receive buffer full? No: check againCLR RI ;yes: clear flag and
MOV SBUF,A ; send dataRET ;return
DONE: NOPEND
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.
Ref. I. Scott Mackenzie 20L Ch Thng
7/29/2019 8051 Chap5 Serial
11/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
11
Mode 2: 9-Bit UART with Fixed Baud Rate
Mode 2: 11 bits are transmitted on TXD or received onRXD, including a start bit (0), 9 data bits (LSB first),and a stop bit (1).
On transmission , the 9 th bit is whatever has beenput in TB8 in SCON.
On reception , the 9 th bit received is placed in RB8in SCON.
Baud rate is either f OSC/64 (SMOD=0)or f OSC/32 (SMOD=1)
Ref. I. Scott Mackenzie 21L Ch Thng
Mode 3: 9-Bit UART with Variable Baud Rate
9-bit UART: same as mode 2 Variable baud rate: same as mode 1
Ref. I. Scott Mackenzie 22L Ch Thng
7/29/2019 8051 Chap5 Serial
12/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
12
Adding a Parity Bit
A common use for the 9 th bit is to add parity to acharacter.
The P bit in PSW register is set or cleared to establisheven parity with 8 bits in A register .
Eg. Put even parity bit in TB8, which becomes the 9 thdata bit to be transmitted:
MOV C,P ;put even parity bit in C flag
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 23L Ch Thng
Adding a Parity Bit
Eg. Put odd parity bit in TB8, which becomes the 9 thdata 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 24L Ch Thng
7/29/2019 8051 Chap5 Serial
13/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
13
Example 3Assume a 10-byte string of 8-bit ASCII codes is stored in internal RAM from thelocation 30H. Write a program that transmits this string out the 8051 serial port (4800baud, crystal 11.0592 MHz) with odd parity added as the 9 th bit
Ref. I. Scott Mackenzie 25L Ch Thng
ORG 0000H MOV SCON,#11010010B ;Serial port mode 3 (9-bit) MOV TMOD,#00100000B ;Timer 1 mode 2 MOV TH1,#-6 ;reload count for 4800 baud
SETB TR1 ;start Timer 1 MOV R2,#10 ;number of loops MOV R0,#30H ;starting address
LOOP: MOV A,@R0 ;get data 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
ACALL SEND ;send dataINC R0 ;increase pointerDJNZ R2,LOOP ;loop 10 times
SEND: JNB TI,$ ;check TI empty? No: check againCLR TI ;yes: clear flag and
MOV A,SBUF ; send dataRET ;returnEND
Example 4Assume a 10-byte string of 7-bit ASCII codes is stored in internal RAM from thelocation 30H. Write a program that transmits this string out the 8051 serial port (4800baud, crystal 11.0592 MHz) with odd parity added as the 8 th bit
Ref. I. Scott Mackenzie 26L Ch Thng
ORG 0000H MOV SCON,#01010010B ;Serial port mode 1 (8-bit) MOV TMOD,#00100000B ;Timer 1 mode 2 MOV TH1,#-6 ;reload count for 4800 baud
SETB TR1 ;start Timer 1 MOV R2,#10 ;number of loops MOV R0,#30H ;starting address
LOOP: MOV A,@R0 ;get dataCLR ACC.7 ;clear the 8 th bit of A
MOV C,P ;put even parity bit in C flagCPL C ;convert to odd parity
MOV ACC.7,C ;and move to the 8 th bit of A ACALL SEND ;send data
INC R0 ;increase pointerDJNZ R2,LOOP ;loop 10 times
SEND: JNB TI,$ ;check TI empty? No: check againCLR TI ;yes: clear flag and
MOV A,SBUF ; send dataRET ;returnEND
7/29/2019 8051 Chap5 Serial
14/14
H Bch Khoa TP.HCM L Ch Thngwww.tinyurl.com/thongchile
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 totest if it is being addressed.
The addressed slave will clear its SM2 bit and prepare to receivethe data bytes that follow. The 9 th bit in data byte is 0 .
The slaves that were not addressed leave their SM2 bits set andignore the incoming data bytes
Ref. I. Scott Mackenzie 27L Ch Thng
28
References
L Ch Thng
I. Scott Mackenzie, The 8051 Microcontroller Cc ti li u trn Internet khng trch d n hoc khng ghi tc
gi
Ref. I. Scott Mackenzie
Recommended