Serial Port of 8051
Serial Port of 8051
2 MGR,ECE
3 MGR,ECE
4 MGR,ECE
5 MGR,ECE
6 MGR,ECE
Asynchronous: Transmission of 5Dh
STOP 0 1 0 1 1 1 0 1 START
BIT BIT
MSB LSB
7 MGR,ECE
8051 serial port
The serial port is full duplex, meaning it can transmit
and receive simultaneously.
Separate TxD & RxD lines
It is also, meaning it can commence reception of a second
receive buffered byte before a previously received byte
has been read from the receive register.
The serial port receive and transmit registers are both
accessed at Special Function Register SBUF.
Writing to SBUF loads the transmit register, and reading
SBUF physically access separate receive register.
8 MGR,ECE
Registers of serial port
SBUF register
-RAM address:99h
-For a byte of data to be transferred via TxD line, it
must be placed in SBUF register.
MOV SBUF,#4Dh
MOV SBUF,#’d’ :ASCII value
MOV SBUF,A
- SBUF hold the byte of data received over RxD line
MOV A,SBUF
9 MGR,ECE
SBUF register….
10 MGR,ECE
SCON register(Bit addressable)
•RAM address:98h
SCON .7 SCON .0
•SM0,SM1 :serial mode bits
11 MGR,ECE
SM2: Multiprocessor communication enable bit in
modes 2 and 3.
REN: Receive enable bit
1=Enable reception
0=Disable reception
TB8: is the 9th bit that will be transmitted in modes
2 and 3.
-set/cleared by software as desired
RB8: is the 9th bit received in modes 2 and 3.
12 MGR,ECE
TI: Transmit interrupt flag
-Set by H/W at the end of the 8 bit in mode 0 or
at the beginning at the stop bit in other modes.
-Must be cleared by S/W
RI: Receive interrupt flag
-Set by H/W at the end of the 8 bit reception in in mode
0
-Set by H/W at halfway through stop bit reception other
modes.
-Must be cleared by S/W
Not implemented
14
IDL: IDLE mode bit
MGR,ECE
Data transmission
Transmission begin any time data is written
into SBUF.
TI is set to 1 when data has been transmitted
& signifies that SBUF is empty and another
byte can be set.
Transferring accumulator contents continuously
back :MOV SBUF,A
again:JNB TI ,again
CLR TI
SJMP back
15 MGR,ECE
Data reception
REN must be enable to start data reception for all
modes.
For mode 0, RI must be cleared to 0.
18 MGR,ECE
Mode 0 Example:HONEY BEE COUNTER
• The bees are assumed to enter the bee hive in
rectangular box through a small hole.
•Another hole is made for the bees to exit.
• Assume suitable sensors are placed at entry & exit
holes.
• The system is designed to display the number of
bees in hive at any time.
• Assume initially there are no bees in hive.
• Display count on seven segment interface using
serial port.
19 MGR,ECE
20 MGR,ECE
Algorithm
Initialize serial port in mode 0 & Counter 0 in
mode 0.
Maintain T0 at logic high.
On bee entry, assert logic 0 on T0 for time >
1machine cycle & < 2 machine cycle.
Display counter value on seven segment
interface in intervals of 1 second.
Take care of hardware constraints.
21 MGR,ECE
Serial Data Mode 1:Standard UART
10 bits are transmitted (through TXD) or received( through
RXD) : a start bit (0), 8 data bits (LSB first), and a stop
bit (l).
On receive, the stop bit goes into RB8 in SCON.
The baud rate variable.
Each bit interval is inverse of the baud rate frequency.
Transmission is initiated by any instruction that uses SBUF
as a destination register
22 MGR,ECE
Mode 1…..
The following figure shows the way the bits are
transmitted/ received.
23 MGR,ECE
In receiving mode, data bits are shifted into the receiver
at the programmed baud rate.
The data word (8-bits) will be loaded to SBUF if the
following conditions are true.
1. RI must be zero. (i.e., the previously received byte has
been cleared from SBUF) and
2. Mode bit SM2 = 0 or stop bit = 1.
In mode-1, if SM2 is set to 1, no receive interrupt (RI) is
generated unless a valid stop bit is received.
24 MGR,ECE
Baud rate for mode 1
Timer-1 is used to generate baud rate for mode-1
serial communication by using overflow flag of the
timer to determine the baud frequency.
The baud is determined by, timer 1 overflow
frequency & SMOD bit of PCON register as
follows.
26 MGR,ECE
Mode 1…..
Example:
If standard baud rate is desired, then 11.0592
MHz crystal could be selected.
To get a standard 9600 baud rate, the setting of
TH1 is calculated as follows.
Assuming SMOD to be '0' .
27 MGR,ECE
Mode 1…..
Programming 8051 for serial transfer of data
in mode 1:
1. Load TMOD register with 20h (T1, mode-2).
2. Load TH1 with baud rate selector.
3. Load SCON register with 40h (mode-1, 8 bit data,1
start bit. 1 stop bit).
4. Start timer-1 (TR1=1).
5. Clear TI flag.
6. Write data into SBUF register.
7. Monitor TI flag continuously to transfer data
completely.
8. Go to step-5 to transfer next data.
28 MGR,ECE
Mode 1…..
PROGRAM TO TRANSFER CHARACTER
‘A’SERIAL LY:
29 MGR,ECE
Mode 1…..
PROGRAM TO TRANSFER MESSAGE ‘YES’
SERIAL LY:
MOV TMOD, #20h ;Timer-1, mode-2.
MOV TH1, #-3h ;baud rate of 9600.
MOV SCON, #40h ;mode-1, 8 bit data,1
start bit. 1 stop bit.
SETB TR1
AGN: MOV A, #’Y’
ACALL TRANS
MOV A, #’E’
//SUB ROUTINE
ACALL TRANS
TRANS: MOV SBUF, A
MOV A, #’S’
HERE: JNB TI, HERE
ACALL TRANS
CLR TI
SJMP AGN
RET
30 MGR,ECE
Mode 1…..
Programming 8051 for serial reception of
data in mode 1:
1. Load TMOD register with 20h (T1, mode-2).
2. Load TH1 with baud rate selector.
3. Load SCON register with 50h (mode-1, 8 bit data,1
start bit. 1 stop bit).
4. Start timer-1 (TR1=1).
5. Clear RI flag.
6. Monitor RI flag continuously to receive data
completely; if RI is SET then save SBUF in to
destination.
7. Go to step-5 to receive next data.
31 MGR,ECE
Mode 1…..
PROGRAM TO RECEIVE CHARACTERS
SERIALLY :
MOV TMOD, #20h ;Timer-1, mode-2.
MOV TH1, #-6h ;baud rate of 4800.
MOV SCON, #50h ;mode-1, 8 bit data,1
start bit. 1 stop bit.
SETB TR1
HERE: JNB RI, HERE ;monitor RI flag.
MOV A, SBUF
MOV P1, A
CLR RI
SJMP HERE
32 MGR,ECE
Mode 1…..
//PROGRAM TO MONITOR A SJMP REP
SWITCH & DISPLAY ITS NEXT: MOV DPTR, #MES2
STATUS ON SERIAL PORT: REP -1 : CLR A
SW1 EQU P1.7 MOVC A, @A+DPTR
ORG 0h JZ AGN
MOV TMOD, #20h ACALL SENDCOM
MOV TH1, #-3h INC DPTR
MOV SCON, #50h SJMP REP -1
SETB TR1 //SUB ROUTINE
SETB SW1 ;Switch i/p SENDCOM: MOV SBUF, A
AGN: JB SW1, NEXT HERE: JNB TI, HERE
MOV DPTR, #MES1 CLR TI
REP: CLR A RET
MOVC A, @A+DPTR MES1: DB “OPEN”,0
JZ AGN MES2: DB “CLOSED”,0
ACALL SENDCOM
33 INC DPTR
MGR,ECE
Mode 1…..
PROGRAM TO SEND A
TEXT STRING TO //SUB ROUTINE
SERIAL PORT: SENDCOM: MOV SBUF, A
ORG 0h HERE: JNB TI, HERE
MOV TMOD, #20h CLR TI
MOV TH1, #-3h RET
MOV SCON, #40h MES1: DB
SETB TR1 “HELLO FOLKS”,0
MOV DPTR, #MES1 END
AGN: CLR A
MOVC A, @A+DPTR
JZ REP
ACALL SENDCOM
INC DPTR
SJMP AGN
REP: SJMP REP
34 MGR,ECE
Mode 1….
C-PROGRAM TO TRANSFER CHARACTER
‘A’ SERIAL LY:
//A L Program //C – program
MOV TMOD, #20h include<reg51.h>
MOV TH1, #-6h void main (void)
MOV SCON, #40h { TMOD= 0x20;
SETB TR1
TH1=0xFA;
AGN: MOV SBUF, #’A’
HERE: JNB TI, HERE SCON=0x40;
CLR TI TR1=1;
SJMP AGN while(1) //for ever
{
SBUF= ‘A’;
While(TI==0);//wait
TI = 0; }
35 MGR,ECE }
Mode 1…..
PROGRAM TO TRANSFER MESSAGE ‘YES’:
include<reg51.h>
MOV TMOD, #20h void sertx (unsigned char);
MOV TH1, #-3h void main (void)
MOV SCON, #50h
{
SETB TR1 TMOD= 0x20;
AGN: MOV A, #’Y’ TH1=0xFD;
ACALL TRANS
MOV A, #’E’ SCON=0x40;
ACALL TRANS TR1=1;
MOV A, #’S’ while(1) //for ever
ACALL TRANS { sertx ( ‘Y’);
SJMP AGN
TRANS: MOV SBUF, A
sertx ( ‘E’);
HERE: JNB TI, HERE sertx ( ‘S’); }
CLR TI void sertx (unsigned char x)
RET {
SBUF =x;
36 MGR,ECE While (TI == 0); TI=0;
}
Mode 1…..
PROGRAM TO RECEIVE A MESSAGE:
//A L Program //C – program
MOV TMOD, #20h include<reg51.h>
;Timer-1, mode-2. void main (void)
MOV TH1, #-6h { Unsigned char mybyte;
;baud rate of 4800. TMOD= 0x20;
MOV SCON, #50h TH1=0xFA;
;mode-1, 8 bit data,1 SCON=0x50;
start bit. 1 stop bit. TR1=1;
SETB TR1 while(1) //for ever
HERE: JNB RI, {while (RI == 0);
HERE ;monitor RI flag. mybyte = SBUF;
MOV A, SBUF P1= mybyte ;
MOV P1, A RI=0;
CLR RI }
SJMP HERE }
37 MGR,ECE
Mode 1…..
38 MGR,ECE
Mode 2:Multiprocessor mode
11 bits are transmitted (through TXD) received( through RXD):
a start bit (0), 8 data bits (LSB first), a programmable 9th data
bit, and a stop bit (l)
On Transmit, the 9th data bit (TB8 in SCON)can be assigned the
value
of 0 or 1.
E.g. The parity bit (P, in the PSW) could be moved into TB8.
On receive, the 9th data bit goes into RB8 in Special Function
Register SCON.
The baud rate is programmable to either 1/32 or 1\64 the oscillator
frequency.
Both start & stop bits are discarded
39 MGR,ECE
Mode 2.....
Transmission is initiated by any instruction that uses
SBUF as a destination register.
The baud rate of mode 2 is programmed as follows:
40 MGR,ECE
Mode 3:
Multiprocessor mode with variable baud rate
Mode-3 is same as mode-2, except the fact that the
baud rate in mode-3 is variable.
The baud rate is programmed as follows:
41 MGR,ECE
Operation in Multiprocessor mode
8051 operates in multiprocessor mode for serial
communication Mode-2 and Mode-3.
In multiprocessor mode, a Master processor can
communicate with more than one slave processors.
The Master communicates with one slave at a time.
11 bits are transmitted by the Master, viz, One start
bit (usually '0'), 8 data bits (LSB first), TB8 and a
stop bit (usually '1').
42 MGR,ECE
Connection diagram
TB8
-'1' for an address byte
- '0' for a data byte
43 MGR,ECE
It should be noted that in Mode 2&3, receive interrupt
flag RI is set if REN=1, RI=0 and the following
condition is true.
1.SM2=1 and RB8=1 and a valid stop bit is
received. Or
2. SM2=0 and a valid stop bit is received.
Sequence of operations:
If the Master wants to communicate with certain slave,
it first sends the address of the slave with TB8=1.
This address is received by all the slaves. Slaves
initially have their SM2 bit set to '1'.
All slaves check this address and the slave who is
being addressed, responds by clearing its SM2 bit to '0'
so that the data bytes can be received.
44 MGR,ECE
After the communication between the Master and a
slave has been established, the data bytes are sent by
the Master with TB8=0.
Hence other slaves do not respond /get interrupted
by this data as their SM2 is pulled high (1).
45 MGR,ECE
Power saving modes of operation
8051 has two power saving modes. They are -
1. Idle Mode
2. Power Down mode.
The structure of PCON register is as follows.
46 MGR,ECE
Schematic diagram for 'Power down' mode and
'Idle' mode
47 MGR,ECE
Idle Mode
Idle mode is entered by setting IDL bit to 1 ( IDL =0).
The clock signal is gated off to CPU, but not to the
interrupt, timer and serial port functions.
The CPU status is preserved entirely. SP, PC, PSW,
Accumulator and other registers maintain their data
during IDLE mode.
The port pins hold their logical states they had at the
time Idle was initiated. ALE and PSEN are held at
logic high levels.
48 MGR,ECE
Ways to exit Idle Mode:
1. Activation of any enabled interrupt will clear PCON.0
bit and hence the Idle Mode is exited.
- The program goes to the Interrupt Service Routine
(ISR). After RETI is executed at the end of the ISR,
the next instruction will start from the one following
the instruction that enabled Idle Mode.
2. A hardware reset exits the idle mode. The CPU starts
from the instruction following the instruction that
invoked the 'Idle' mode.
49 MGR,ECE
Power Down Mode
The Power down Mode is entered by setting the PD bit to 1.
The internal clock to the entire microcontroller is stopped
(frozen). However, the program is not dead.
The Power down Mode is exited (PCON.1 is cleared to 0) by
Hardware Reset only.
The CPU starts from the next instruction where the Power
down Mode was invoked. Port values are not changed/
overwritten in power down mode.
Vcc can be reduced to as low as 2V in Power Down mode.
However, Vcc has to be restored to normal value before
Power Down mode is exited.
50 MGR,ECE
RS-232
RS-232 (Recommended Standard 232) is a
standard for serial binary single-ended data and
control signals connecting between a DTE (Data
Terminal Equipment) and a DCE (Data Circuit-
terminating Equipment).
It is commonly used in computer serial ports.
The standard defines the electrical characteristics
and timing of signals, the meaning of signals, and
the physical size and pin out of connectors.
51 MGR,ECE
RS-232…….
52 MGR,ECE
RS 232 is bipolar
-Logic 1: Low voltage levels(-3 to -15V)Noise
margin
-Logic 0: High voltage levels(+3 to +15V)
increases
53 MGR,ECE