Professional Documents
Culture Documents
Receiver
Transmitter
Receiver
Receiver
Transmitter
Transmitter
Receiver
Receiver
Transmitter
Simplex
Half Duplex
Full Duplex
Serial Port
The 8051 has a serial data communication circuit that uses
register SBUF to hold data.
Register SCON controls data communication, register PCON
controls data rates, and pins RXD (P3.0) and TXD (P3.1) connect to
the serial data network.
SBUF is physically two registers. One is write only and is
used to hold data to be transmitted out of the 8051 via TXD. The other
is read only and holds received data from external sources via RXD.
Both mutually exclusive registers use address 99h.
There are four programmable modes for serial data
communication that are chosen by setting the SMX bits in SCON.
Baud rates are determined by the mode chosen.
SCON
7
SM0
SM1
SM2
REN
TB8
RB8
TI
RI
Bit
Symbol
Function
SM0
SM1
SM2
SM0
SM1
Mod
Description
REN
TB8
RB8
Received bit 8. Bit 8 of received data in modes 2 and 3; stop bit in mode 1.
Not used in mode 0.
TI
Transmit interrupt flag. Set to one at the end of bit 7 time in mode 0, and at the beginning of
the stop bit for other modes. Must be cleared by the program.
RI
Receive interrupt flag. Set to one at the end of bit 7 time in mode 0, and halfway through the
stop bit for other modes. Must be cleared by the program.
PCON
7
SMOD
GF1
GF0
PD
IDL
Bit
Symbol
Function
SMOD
GF1
GF0
PD
IDL
Data Transmission
Transmission of serial data bits begins
anytime data is written to SBUF. TI is set
to a 1 when the data has been transmitted
and signifies that SBUF is empty (for
transmission purposes) and that another
data byte can be sent. If the program fails
to wait for the TI flag and overwrites SBUF
while a previous data byte is in the
process of being transmitted, the results
will be unpredictable.
Data Reception
Reception of serial data will begin if the receive enable bit (REN) in
SCON is set to 1 for all modes. In addition, for mode 0 only, RI must
be cleared to 0 also. Receiver interrupt flag RI is set after data has
been received in all modes. Setting REN is the only direct program
control that limits the reception of unexpected data; the requirement
that RI also be 0 for mode 0 prevents the reception of new data until
the program has dealt with the old data and reset RI.
Of the original ten bits, the start bit is discarded, the eight data bits
go to SBUF and the stop bit is saved in bit RB8 of SCON. RI is set to 1.
indicating a new data byte has been received. If RI is found to be set at the
end of the reception, indicating that the previously received data byte has not
been read by the program, or if the other conditions listed are not true, the
new data will not be loaded and will be lost.
f baud
2 SMOD
=
x(timer1 _ overflow _ frequency)
32d
and timer 1 can be run using the internal clock or as a counter that receives
clock pulses from any external source via pin T1.
The oscillator frequency is chosen to help generate both standard
and nonstandard baud rates. If standard baud rates are desired, then an
11.0592 megahertz crystal could be selected. To get a standard rate of 9600
hertz then, the setting of TH 1 may be found as follows(if SMOD is clear):
20 11.0592 x106
= 253d = 0 FDh
TH 1 = 256 x
12 x9600
32
fosc
(MHz)
Bitul
SMOD
Timer 1
Mod
TH1
62.5k
12
FFh
19.2k
11.059
FDh
9.6k
11.059
FDh
4.8k
11.059
FAh
2.4k
11.059
F4h
1.2k
11.059
E8h
300
11.059
CCh
110
72h
110*
12
FEEBh
Mode 2 is similar to mode 1 except 11 bits are transmitted: a start bit, nine
data bits, and a stop bit.The ninth data bit is gotten from bit TB8 in SCON
during transmit and stored in bit RB8 of SCON when data is received. Both
the start and stop bits are discarded. The baud rate is programmed as
follows:
f baud
2 SMOD
=
x frecventa oscilator
64
Here, as in the case for mode 0, the baud rate is much higher than
standard communication rates. This high data rate is needed in
many multi-processor applications. Data can be collected quickly
from an extensive network of communicating microcontrollers if high
baud rates are employed.
Slave 1
RxD
TxD
RxD
Slave 2
TxD
RxD
Slave 3
TxD
1. TMOD register is loaded with the value 20H, indicating the use of
timer 1 in mode 2 (8-bit auto-reload) to set baud rate
2. The TH1 is loaded with one of the values to set baud rate for serial
data transfer
3. The SCON register is loaded with the value 50H, indicating serial
mode 1, where an 8-bit data is framed with start and stop bits
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF
register
7. The TI flag bit is monitored with the use of instruction JNB TI,xx
to see if the character has been transferred completely
8. To transfer the next byte, go to step 5
Example 2
org 0000h
jmp start
org 0100h
message: db "hello everybody!"
db 0
start:
mov tmod,#20h
; timer 1, mod 2
mov th1,#-6
; rata de transfer 4800 baud
mov scon,#50h
; 8 bit, 1 stop REN enable
setb tr1
; start timer 1
mov dptr,#message
; incarca pointer pt. mesaj
mov r0,#00h
again:
mov a, r0
; preia caracterul
movc a,@a+dptr
; daca ultimul caracter este "0" se opreste trasferul
jz termina
mov sbuf,a
; incarca data in portul serial
here:
jnb TI, here
; sta in bucla pana ultimul bit este transmis
Clr TI
; urmatorul caracter poate fi transmis
inc r0
sjmp again
termina:
sjmp termina
end
Using an interrupt
Write a program for 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 ;timer 1,mode 2(auto reload)
MOV TH1,#-6 ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
HERE: JNB RI,HERE ;wait for char to come in
MOV A,SBUF ;saving incoming byte in A
MOV P1,A ;send to port 1
CLR RI ;get ready to receive next
;byte
SJMP HERE ;keep getting data
Example 10-5
Assume that the 8051 serial port is connected to the COM port of
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 PC the message We Are Ready, (b) receive any data send
by PC and put it on LEDs connected to P1, and (c) get data on
switches connected to P2 and send it to PC serially. The program
should perform part (a) once, but parts (b) and (c) continuously, use
4800 baud rate.
Solution:
ORG 0
MOV P2,#0FFH ;make P2 an input port
MOV TMOD,#20H ;timer 1, mode 2
MOV TH1,#0FAH ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
MOV DPTR,#MYDATA ;load pointer for message
H_1: CLR A
MOV A,@A+DPTR ;get the character
Program the 8051 in C 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:
#include <reg51.h>
void main(void){
unsigned char mybyte;
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1; //start timer
while (1) { //repeat forever
while (RI==0); //wait to receive
mybyte=SBUF; //save value
P1=mybyte; //write value to port
RI=0;
}
}