0% found this document useful (0 votes)
12 views

MC Module 4 Serial Communication

Uploaded by

Shashank Patgar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

MC Module 4 Serial Communication

Uploaded by

Shashank Patgar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 44

Module -4

8051
Serial Communication
Communication
• Exchange of information between transmitter
& receiver (source & destination).

• Two types:
1)Serial communication
2)Parallel communication
Serial Communication Parallel Communication

1)Only one bit of data is 1)Group of data is


transferred at a time. transferred at a time.
2)Data transfer rate is low. 2)Data transfer rate is
high.
3)Preferred for long
3) Preferred for short
distance. distance.
4) Cabling cost is less. 4) Cabling cost is more.
5) More reliable. 5) Less reliable.

Ex: Keyboards transferring Ex: Data transfer from


data to the motherboard computer to printer.
Serial data transmission classification
1)Simplex (2) Half duplex (3) Full duplex
Simplex
Data is transmitted in only one direction
There is no possibility of data transfer in other
direction
Example : Data transmission from computer to Printer
Half duplex
Allows data transmission in both the directions but
not simultaneously.
(Data transmission is possible only one way at a time)
Example : Walkie-Talkie
Full duplex
Allows data transmission in both the
directions simultaneously.
Example : Transmission through telephone lines
Serial Data Transmission Formats
1) Synchronous serial communication

 Uses common clock signal


 Start & stop bits are eliminated by synchronizing
transmitter & receiver
 Synchronous bits are inserted instead of start & stop bits
 Transfers a block of data (character) at a time
2) Asynchronous serial communication
 Share a separate clock for transmitter &
receiver
 Each data (character) is placed between start
& stop bits (Framing)
 Start bit is only one-bit but the stop bits can
be one or two bits
 Start bit is always zero(Low called as Space)
followed by character & one or two stop bits
(High called as Mark)
 Some systems include Parity bit along with
start & stop bits.
Synchronous Communication Asynchronous Communication

1) Transmitter & receiver are 1) Transmitter & receiver are


synchronized by clock not synchronized by clock
2) Data bits are transmitted 2) Data bits are transmitted
at constant rate with no fixed rate
3) Data transfer takes place in 3) Data transfer is character
blocks oriented (single bit
oriented)
4) Start & stop bits are not 4) Start bit, data , stop bit are
required. Synchronization required
bits are required.
5) Used in high speed 5) Used in low speed
transmission transmission
Baud Rate
• Baud rate is the number of signal changes per
second.
• Bit rate is the number of bits transmitted per
second.
• Baud rate is the MODEM terminology and a
single change of signal sometimes transfers
several bits of data.
• Baud rate is set by Timer1 using Mode 2
(8-bit auto reload )
SCON (Serial Port Control Register)-98H

SM0 SM1 SM2 REN TB8 RB8 TI RI

•SM0, SM1 - Serial mode specifier


SM0 SM1 MODE Description
0 0 0 Shift Register ; Baud =f/12
0 1 1 8-bit UART ; Baud = Variable
1 0 2 9-bit UART ; Baud=f/32 or f/64
1 1 3 9-bit UART; Baud =Variable
SM2 - Used for multiprocessor communications
in Mode 2 & 3.

REN-Receive Enable Bit


REN = 1 - Enable reception
REN = 0 – Disable reception
TB8-Transmit Bit 8
Set or cleared by hardware to determine the
state of the bit 8 of the transmitted data in
Modes 2&3.
RB8 –Receive bit 8
Set or cleared by hardware to determine the
state of the bit 8 of the received data in
Modes 2&3 ; stop bit in mode 1 .
Not used in mode 0.
TI –Transmit Interrupt flag
Set to one at the end of bit 7 in Mode 0.
Set to one at the beginning of the stop bit for
other modes.
TI must be cleared by the program.
RI –Receive Interrupt flag
Set to one at the end of bit 7 in Mode 0.
Set to one at the halfway through the stop bit
for other modes.
RI must be cleared by the program.
PCON (Power Mode Control Register )-87H
- - -
SMOD GP1 GP0 PD IDL
Bit-7-SMOD- Serial Baud Rate modify bit
SMOD=1 - To double the Baud Rate using
Timer 1 for Modes 1, 2 & 3
SMOD =0 – To use Timer 1 Baud Rate
Bit 6 -4 : Not implemented
Bit 3- GP1-General purpose user flag bit 1
Set /cleared by the program
Bit 2-GP0-General purpose user flag bit 0
Set /cleared by the program
Bit 1- PD- Power Down bit
Used to control power configurations for the
device
Bit-0- Idl – Idle Mode bit
Used by the program to set the device in idle
mode
SBUF Register(Serial Port Data Buffer)-99H

 8-bit register used only for serial communication in


8051.
 When 8051 transfers a byte of data via TxD line
(P3.1) it must be placed in the SBUF register.
 Similarly during receiving SBUF holds the byte of data
when received by RxD line (P3.0).

MOV SBUF, # ‘D’ ; Load SBUF=44H ASCII for ‘D’


MOV SBUF, A ;Copy Accumulator content into SBUF
MOV A, SBUF ; Copy SBUF into Accumulator
RS232 Standards
 Most widely used serial I/O interfacing standard.
 To allow the compatibility among data
communication equipment made by various
manufacturers an interfacing standard called RS232
was set by Electronic Industries Association (EIA) in
1960.
 Other modified versions: RS232A, RS232B (1965),
RS232C (1969)
 In RS232 Logic ‘1’ is represented by -3 to -25V
 Logic ‘0’ by +3 to +25V making -3 to +3 undefined.
 To connect any RS232 to a microcontroller system
voltage converter (Line Driver) such as
MAX 232 IC chip is used to convert TTL logic levels to
RS232 voltage levels & vice versa.
 RS232 Pins :
1) RS232 Connector –DB25
2) RS232 Connector- DB9
RS232-DB 9 Pin signals
Pin Description
1 Data Carrier Detect (DCD)
2 Received Data (Rxd)
3 Transmitted Data (Txd)
4 Data Terminal Ready (DTR)
5 Signal ground (GND)
6 Data Set Ready (DSR)
7 Request To Send (RTS)
8 Clear To Send (CTS)
9 Ring Indicator (RI)
MAX 232
 MAX 232 IC designed by Maxim Corp.
 TxD (Pin 11 –P3.1) and RxD (Pin 10-P3.0) pins
are TTL compatible. Therefore require a line
driver to make them RS232 compatible which
is done by MAX232 IC chip.
 It requires +5V power supply source which is
same as the 8051 microcontroller. Hence a
single power supply is sufficient for the
working.
 Uses 4 capacitors ranging from 1 to 22 µ F
 Line drivers used for TxD are T1 and T2 and
for RxD are R1 and R2.
 T1 line driver has a designation of T1 in and
T1out on pin no 11 and 14 respectively.
 T1 in pin is the TTL side and is connected to
TxD of 8051 while T1out is RS232 side that is
connected to the RxD pin of RS232 DB
connector
 R1 line driver has a designation of R1in and
R1out on pin no. 13 and 12 respectively.
 R1in (pin13) is the RS232 side that is
connected to the TxD pin of RS232 DB
Connector and R1 out (pin 12) is the TTL side
that is connected to the RxD pin of the 8051.
Procedure to transfer data serially
1) The TMOD register is loaded with the value
20H,indicating the use of Timer 1 in Mode 2
(8-bit auto reload ) to set the baud rate.
2) The TH1 is loaded with one of the values in
Table 1 to set the baud rate for serial data
transfer.
3) The SCON register is loaded with the value
50H,indicating serial Mode1,where an 8-bit data
is framed with start and stop bits.
4) TR1 is set to 1 to start Timer1.
5) TI is cleared by the “CLR TI” instruction.
6) The character byte to be transferred serially is
written into the SBUF register.
7) The TI flag bit is monitored with the use of
the instruction “JNB TI,Label” to see if the
character has been transferred completely.
8) To transfer the next character go to step 5.
Importance of the TI Flag
1) The byte character to be transmitted is written into
the SBUF register.
2)The start bit is transferred.

3)The 8-bit character is transferred 0ne bit at a time.

4) The stop bit is transferred. It is during the transfer of


the stop bit that the 8051 raises the TI Flag
(TI=1), indicating that the last character was
transmitted & it is ready to transfer the next
character.
5) By monitoring the TI flag ,we make sure that we are
not overloading the SBUF register. If we write another
byte into SBUF register before TI is raised, the
untransmitted portion of the previous byte will be lost.
In other words ,when the 8051 finishes transferring a
byte, it raises the TI flag to indicate it is ready for the
next character.

6) After SBUF is loaded with a new byte, the TI flag bit


must be forced to zero by the “CLR TI” instruction in
order for this new byte to be transferred.
Procedure to receive data serially
1) The TMOD register is loaded with the value
20H,indicating the use of Timer 1 in Mode 2
(8-bit auto reload ) to set the baud rate.
2) The TH1 is loaded with one of the values in Table 1
to set the baud rate .
3) The SCON register is loaded with the value
50H,indicating serial Mode1,where an 8-bit data is
framed with start and stop bits & receive enable (REN)
is turned on.
4) TR1 is set to 1 to start Timer 1.
5) RI is cleared with “CLR RI” instruction.
6)The RI flag bit is monitored with the use of the
instruction “JNB RI,Label” to see if an entire character
has been received .
7) When RI is raised ,SBUF has the byte.Its contents are
moved into safe place.
8) To receive the next character, go to step 5.
Importance of the RI Flag
1)It receives the start bit indicating that the next bit is
the first bit of the character byte it is about to receive.
2) The 8-bit character is received one bit at a time.
When the last bit is received, a byte is formed & placed
in SBUF.
3) The stop bit is received. When receiving the stop bit
the 8051 makes RI =1, indicating that an entire
character byte has been received & must be picked up
before it gets overwritten by an incoming character.
4) By checking the RI flag bit when it is raised, we know
that a character has been received & is sitting in the
SBUF register. We copy the SBUF contents to a safe
place in some other register or memory before it is lost.

5) After the SBUF contents are copied into a safe place,


the RI flag bit must be forced to zero by the “CLR RI”
instruction in order to allow the next received
character byte to be placed in SBUF. Failure to do this
causes loss of the received character.
Baud Rate calculation
Baud rate = Crystal Frequency x 1
12 x 32 256- TH1

TH1 = 256 - Crystal Frequency


12 x 32 x Baud Rate
Doubling the Baud Rate
 Two ways to increase the baud rate of data
transfer :
1)Use a higher frequency crystal (not feasible )
2)Change a bit in the PCON register.
 When the 8051 is powered up, SMOD bit of
PCON register is zero (SMOD=0).We can set it
to high (SMOD=1) by software & thus double
the baud rate.
SMOD =0 :
Baud Rate = Crystal frequency X 1
12 X 32 256 –TH1

SMOD=1:
Baud Rate = Crystal frequency x 1
12 x 16 256- TH1
 The following sequence of instructions must be used
to set high SMOD of PCON register.

MOV A, PCON ; place a copy of PCON in accumulator


SETB ACC.7 ; Make D7 bit of accumulator=1
MOV PCON, A ; Now SMOD=1 ,without changing any
any other bits
Operating modes for Serial Port
• Mode 0
 Serial data enters & exits through RxD.
 TxD outputs the shift clock.
 8-bits are transmitted/received
 The baud rate is fixed at 1/12 the oscillator
frequency
 Reception is initiated in Mode 0 by the
condition RI=0 & REN=1
• Mode 1
 10-bits are transmitted through TxD or
received through RxD.
( A start bit (0) , 8 data bits and a stop bit(1)
 On receive, the stop bit goes into RB8 in SCON
register
 The baud rate is variable
• Mode 2
 11-bits are transmitted through TxD or received through RxD
(A start bit (0) , 8 data bits, a programmable 9th data bit and a
stop bit (1) )
 On transmit, the 9th data bit (TB8 in SCON) can be assigned
the value of 0 or 1 or for example the Parity bit in PSW
could be moved into TB8

 On receive ,the 9th data bit goes into RB8 in SCON register
while stop bit is ignored

 The baud rate is programmable to either 1/32 or 1/64 the


oscillator frequency
• Mode 3
 11-bits are transmitted through TxD or
received through RxD
(A start bit (0), 8 data bits , a programmable 9th
data bit and a stop bit (1) )
 The baud rate is variable
 Reception is initiated in the other modes by
the incoming start bit if REN=1
Generating Baud Rate
Serial port in Mode 0
Baud Rate = Oscillator frequency
12
Serial port in Mode 1
Timer 1 is used in Mode 2 (Auto –reload mode)
Baud rate = K x Oscillator frequency
32x 12x (256-TH1)
If SMOD=0 then K=1
If SMOD =1 then K=2
TH1 = 256 - K x Oscillator frequency
384x Baud Rate
Serial port in Mode 2
SMOD =1, Baud rate = 1 Oscillator frequency
32

SMOD =0, Baud rate = 1 Oscillator frequency


64

Serial port in Mode 3


The baud rate in mode 3 is variable and sets up the same as in
mode 1

You might also like