Professional Documents
Culture Documents
8051 Chap5 Serial PDF
8051 Chap5 Serial PDF
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
sites.google.com/site/chithong 1
ĐH Bách Khoa TP.HCM Lê Chí Thông
P→
→S buffer S→
→P buffer
SCON Register
sites.google.com/site/chithong 2
ĐH Bách Khoa TP.HCM Lê Chí Thông
SCON Register
sites.google.com/site/chithong 3
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 4
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 5
ĐH Bách Khoa TP.HCM Lê Chí Thông
stop
synchronization event
Ref. I. Scott Mackenzie Lê Chí Thông 11
sites.google.com/site/chithong 6
ĐH Bách Khoa TP.HCM Lê Chí Thông
To set SMOD:
MOV A,PCON
SETB ACC.7
Ref. I. Scott Mackenzie Lê Chí Thông MOV PCON,A 13
sites.google.com/site/chithong 7
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 8
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 9
Đ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 ;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 times
SJMP DONE
SEND: JNB TI,$ ;transmit buffer empty? No:check again
CLR TI ;yes: clear flag and
MOV SBUF,A ; send data
RET ;return
DONE: NOP
END
Ref. I. Scott Mackenzie 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.
sites.google.com/site/chithong 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
END
Ref. I. Scott Mackenzie 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
END
Ref. I. Scott Mackenzie Lê Chí Thông 22
sites.google.com/site/chithong 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
Problem 1
sites.google.com/site/chithong 12
ĐH Bách Khoa TP.HCM Lê Chí Thông
Problem 2
sites.google.com/site/chithong 13
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 14
ĐH Bách Khoa TP.HCM Lê Chí Thông
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
sites.google.com/site/chithong 15
ĐH Bách Khoa TP.HCM Lê Chí Thông
Example 3
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 9th data bit
ACALL SEND ;send data
INC R0 ;increase pointer
DJNZ R2,LOOP ;loop 10 times
SJMP DONE
SEND: JNB TI,$ ;check TI empty? No: check again
CLR TI ;yes: clear flag and
MOV SBUF,A ; send data
RET ;return
DONE: NOP
END
Ref. I. Scott Mackenzie Lê Chí Thông 31
Example 4
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 ;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 data
CLR ACC.7 ;clear the 8th bit of A
MOV C,P ;put even parity bit in C flag
CPL C ;convert to odd parity
MOV ACC.7,C ;and move to the 8th bit of A
ACALL SEND ;send data
INC R0 ;increase pointer
DJNZ R2,LOOP ;loop 10 times
SEND: JNB TI,$ ;check TI empty? No: check again
CLR TI ;yes: clear flag and
MOV A,SBUF ; send data
RET ;return
END
Ref. I. Scott Mackenzie Lê Chí Thông 32
sites.google.com/site/chithong 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 1 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 0.
• The slaves that were not addressed leave their SM2 bits set and
ignore the incoming data bytes
References
sites.google.com/site/chithong 17