SlideShare a Scribd company logo
1 of 31
8051 SERIAL PORTS
8051 Serial Ports
• One of the 8051’s many powerful features is its integrated
Universal Asynchronous Receiver Transmitter (UART), otherwise
known as a serial port. With integrated serial port of 8051, data
can be transmitted and received easily by reading and writing the
data to the serial port registers. The features of the 8051 serial
ports are
• Full duplex operation.
• Receive Buffered.
• Access using single double buffered Register SBUF.
• Four different modes of operation.
• Option to use fixed baud rate or programmable baud rate.
8051 Serial Ports
• Full Duplex serial port means that it can transmit
and receive data simultaneously. It is also receive-
buffered, meaning it can commence reception of a
second byte before a previously received byte has
been read from the SBUF register.
• However, if the first byte still hasn’t been read by
the time reception of the second byte is complete,
one of the bytes will be lost. The serial port receive
and transmit registers are both accessed at Special
Function Register SBUF.
8051 Serial Ports
• Data to be transmitted are written to the register
SBUF and the data received by the serial port are
read from the register SBUF. Physically reading
and writing SBUF actually accesses two separate
registers.
• This technique of having same address for two
different registers is called double buffering.
SERIAL PORT CONTROL SFRS
• The serial port of 8051 is controlled by two
registers in SFR area of 8051 as shown in Table
• The two registers are Serial Port control registers;
SCON and serial port buffer register SBUF.
SFR Name Description SFR Address
SCON
Serial port control
register
98h
SBUF
Serial port buffer
register
99h
SERIAL PORT CONTROL SFRS
• In addition to the above two registers, the MSB of PCON
register named as SMOD bit is used to double the baud
rate of serial transmission and reception.
• If SMOD bit is set to 1, then the baud rate is doubled.
• The individual bits of SCON have the functions as shown
in Table . As the SCON register has many individual status
bits, the individual bits of this register are bit addressable.
The bit address is also given in Table .
• The programmer can use these bit addresses to check the
status of the serial port and set the mode individually.
Bit patterns for SCON (98h) SFR
Bit Name Bit Address Explanation of Function
D7 SM0 9Fh
Serial port mode select bits
D6 SM1 9Eh
D5 SM2 9Dh Multiprocessor Communications Enable bit
D4 REN 9Ch
Receiver Enable. This bit must be set in order to receive
characters.
D3 TB8 9Bh Transmit bit 8. The 9th bit to transmit in mode 2 and 3.
D2 RB8 9Ah Receive bit 8. The 9th bit received in mode 2 and 3.
D1 TI 99h
Transmit Interrupt Flag. Set when a byte has been
completely transmitted.
D0 RI 98h
Receive Interrupt Flag. Set when a byte has been completely
received.
Bit Pattern - Description
• D7 and D6 bits of the SCON register define the operation modes of
the serial port and the basic operating modes are given in Table.
• The SM0 and SM1 bits can select any one of the four operating
modes described in the next section.
Definition of Bits SM0 and SM1 in
SCON SFR
Bit Pattern - Description
• The next bit, SM2, is a flag used for enabling
"Multiprocessor communication" in modes 2 and 3.
If SM2 is set to 1 in modes 2 and 3, the “Receive
Interrupt” RI flag will not be activated if the
received 9th data bit is 0. If SM2 is set in Mode1,
then “Receive Interrupt” RI flag will not be
activated if valid stop bit is not received.
• This can be useful in certain advanced serial
applications. It can be now assumed that SM2 bit
has to be cleared so that RI flag will be set when
any character is received.
Bit Pattern - Description
• The next bit, REN, is "Receiver Enable." This bit is
set in order to receive the characters from the
receive data line of the serial port.
• The TB8 bit is used in modes 2 and 3. In modes 2
and 3, a total of nine data bits are transmitted.
The first 8 bits are the 8 bits of the data to be
transmitted, and the ninth bit is taken from TB8.
The RB8 also operates in modes 2 and 3 on the
reception side.
Bit Pattern - Description
• When a byte is received in modes 2 or 3, a total of
nine bits are received. In this case, the first eight bits
received are the data of the serial byte received and
the value of the ninth bit received will be placed in
RB8.
• TI means "Transmit Interrupt." When a program
writes a data to the serial port buffer SBUF, then the
serial port will start shifting this data in the serial
transmit line bit by bit at the predefined clock speed
or baud rate. 8051 will give TI signal to the
programmer after sending the data completely.
Bit Pattern - Description
• Upon sensing the TI bit set to 1, the programmer can
then write the next data for transmission. When the
TI bit is set, the programmer may assume that the
serial port is "free" and ready to send the next byte.
• Finally, the RI bit means "Receive Interrupt."
Whenever a data is received on the receive data line
of the serial port, this serial data will be shifted in to
a buffer and then stored in the SBUF register.
• Setting of RI bit indicates that a byte has been
received. Upon sensing the RI bit set to 1, the
programmer may read the data from the SBUF.
OPERATING MODES
• Bits SM0 and SM1 are used to set the serial
mode to a value between 0 and 3.
• Selecting the Serial Mode selects the mode of
operation (8-bit/9-bit, UART or Shift Register)
and also determines how the baud rate will be
calculated.
• In modes 0 and 2 the baud rate is fixed based on
the oscillator’s frequency. In modes 1 and 3 the
baud rate is variable based on Timer 1 overflows.
© Oxford University Press 2013
Block Diagram of the serial port of
8051
© Oxford University Press 2013
SBUF
• SBUF is physically two registers with the same
address.
• When data to be transmitted is written to the SBUF
register, then it will be shifted bit by bit into the TXD
line of 8051. The port 3 pin 3.1 acts as the TXD line.
The shifting is done by the transmit clock which
determines the baud rate.
• Similarly, when the data bits are received on the RXD
line (Pin 3.0 of port 3), the bits are shifted serially into
the shift register according to the Receive clock.
• After the reception is complete, the data received will
be placed on SBUF from where it can be read by the
programmer through the internal bus.
Reception / Transmission
• Mode 0: In this mode serial data is entering and exiting through
RxD pin. So, in mode 0, Full duplex is not possible – meaning that
both transmission and reception cannot take place
simultaneously. TxD pin outputs the shift clock. 8 bits are
transmitted/received (LSB first). The baud rate is fixed at 1/12 the
oscillator frequency. Transmission is started by writing a data
byte to the SBUF register and once the transmission is complete,
TI flag is set
• The reception is started by enabling REN in SCON register. Once
the data reception is complete, the RI flag is set
• The baud rate in Mode 0 is fixed at one twelfth of the clock
frequency. Baud rate= (Clock frequency/12)
Signal Transmission / Reception
pattern
© Oxford University Press 2013
Reception / Transmission
• Mode 1: In this mode, 10 bits are transmitted
through TxD and simultaneously 10 bits can be
received through RxD. The 10 bits are made up
of a start bit (0), 8 data bits (LSB first), and a stop
bit (1). On completion of reception, the stop bit
goes into RB8 in Special Function Register SCON.
The baud rate is variable and is set by the Timer
1 overflow rate. The baud rate for mode 1 is fixed
at the following rate.
Reception / Transmission
• Baud rate= (Timer 1 overflow rate /16) if SMOD
bit in PCON SFR is set to 1.
• Baud rate= (Timer 1 overflow rate /32) if SMOD
bit in PCON SFR is set to 0.
• The MSB of PCON register can be set or reset by
the programmer. The baud rate can be doubled
by setting the MSB of PCON.
Reception / Transmission
• In order to generate the baud rate clock from
timer 1, Timer 1 can be configured to act as
timer in auto reload mode with the timer 1
interrupt disabled.
• As in mode 0, the transmission is initiated by
writing a data to SBUF register. Reception is
initiated by a 1 to 0 transition that is the start bit
received and also when REN of SCON SFR is 1.
Reception / Transmission
• Mode 2: In this mode, 11 bits are transmitted through TxD or
received through RxD. The 11 bits are made up of one start bit
(always 0), 8 data bits (LSB first), a programmable 9th data bit, and a
stop bit (always 1). The 9th data bit transmitted is same as TB8 bit in
SCON special function register. It can be assigned the value of 0 or 1
by the programmer. Or, for example, the parity bit (P, in the PSW)
could be moved into TB8.
• On reception, the 9th data bit goes into RB8 in Special Function
Register SCON, while the stop bit is ignored. The baud rate is
programmable to either 1/32 or 1/64 of the oscillator clock
frequency.
• Baud rate= (Clock frequency /32) if SMOD bit in PCON SFR is set to 1.
• Baud rate= (Clock frequency /64) if SMOD bit in PCON SFR is set to 0.
Reception / Transmission
• Mode 3: In this mode, 11 bits are transmitted through TxD
and simultaneously 11 bits are received through RxD. The
11bits are made up of a start bit (0), 8 data bits (LSB first), a
programmable 9th data bit, and a stop bit (1). In fact, Mode 3
is the same as Mode 2 in all respects except baud rate. The
baud rate in Mode 3 is variable.
• The baud rate for mode 3 is fixed at the following rate similar
to mode1.
• Baud rate= (Timer 1 overflow rate /16) if SMOD bit in PCON
SFR is set to 1.
• Baud rate= (Timer 1 overflow rate /32) if SMOD bit in PCON
SFR is set to 0.
Reception / Transmission
• In all four modes, transmission is initiated by any
instruction that uses SBUF as a destination register.
Reception is initiated in Mode 0 by the condition RI =
0 and REN = 1. Reception is initiated in the other
modes by the incoming start bit when REN = 1.
• The flow chart 11.20 shows the steps in
programming the serial port of 8051. These steps are
detailed in the following sections and also in the
example programs.
Programming the Serial Port
Initializing Serial Port
• Once the Serial Port Mode has been selected,
then the programmer must configure the serial
port’s baud rate. Only serial port modes 1 and 3,
the baud rates are programmable.
• The Baud Rate is determined based on the
oscillator’s frequency when in mode 0 and 2. In
mode 0, the baud rate is always the oscillator
frequency divided by 12. This means if the crystal
is 11.059 MHz, mode 0 baud rate will always be
921,583 baud.
• In mode 2 the baud rate is always the oscillator
frequency divided by 64, so an 11.059 MHz
crystal speed will yield a baud rate of 172,797.
© Oxford University Press 2013
Initializing Serial Port
• In modes 1 and 3, the baud rate is determined by how frequently timer
1 overflows. The most common method is to put timer 1 in 8-bit auto-
reload mode (timer mode 2) and set a reload value (TH1) that causes
Timer 1 to overflow at a frequency appropriate to generate a baud rate.
• To determine the value that must be placed in TH1 to generate a given
baud rate, the following equation is used.
• TH1 = 256 - ((Clock frequency / 384) / Baud) if SMOD in PCON SFR is 0.
• TH1 = 256 - ((Clock frequency / 192) / Baud) if SMOD in PCON SFR is 1.
• The following table gives the commonly used baud rates and the
corresponding reload for the timer in mode 2 assuming the clock
frequency of 11.059 MHz and SMOD is reset.
© Oxford University Press 2013
Commonly used baud rates
Baud Rate Timer value TH1
300 A0h
1200 D0h
2400 FAh
9600 FDh
© Oxford University Press 2013
Baud Rate
Following set of instructions will set the timer for
the baud rate of 9600.
• MOV TMOD, #00100000B ;timer/counter 1 set
for mode 2, 8-bit TIMER
• ;operation
• MOV TH1, #0FDh ;
timer/counter 1 is timed for 9600 baud
• SETB TR1 ;
timer/counter 1 is enabled for free run
• For initializing the serial port for mode 3
operation, the following instruction can be used.
• MOV SCON, #11010000B
© Oxford University Press 2013
Transmitting and Receiving Data
Using Serial Port
• Once the Serial Port has been properly configured as
explained above, the serial port is ready to be used to
send data and receive data.
• To write a byte to the serial port one must simply write
the value to be transmitted to the SBUF (99h) SFR. For
example, to send the letter "A" to the serial port, the
following instruction can be written.
MOV SBUF, #’A’
© Oxford University Press 2013
• Upon execution of the above instruction the 8051 will
begin transmitting the character via the serial port.
Once the transmission is complete, the serial port
transmit interrupt flag TI is set. Since the 8051 does not
have a serial output buffer, a character can not be
written to SBUF before the previous written character is
completely transmitted. This can be accomplished by
checking the TI flag.
• Reading data received by the serial port is equally easy.
To read a byte from the serial port one just needs to
read the value stored in the SBUF (99h) SFR after the
8051 has automatically set the RI flag in SCON.
© Oxford University Press 2013
Transmitting and Receiving Data
Using Serial Port

More Related Content

Similar to 8051 SERIAL PORTS.pptx

Seminar on serial communication
Seminar on serial communicationSeminar on serial communication
Seminar on serial communication
Samarth Patel
 
8051 serial communication1
8051 serial communication1 8051 serial communication1
8051 serial communication1
vijaydeepakg
 

Similar to 8051 SERIAL PORTS.pptx (20)

Seminar on serial communication
Seminar on serial communicationSeminar on serial communication
Seminar on serial communication
 
Micro c lab8(serial communication)
Micro c lab8(serial communication)Micro c lab8(serial communication)
Micro c lab8(serial communication)
 
Lecture 10 (serial communication)
Lecture 10 (serial communication)Lecture 10 (serial communication)
Lecture 10 (serial communication)
 
8051 serial communication1
8051 serial communication1 8051 serial communication1
8051 serial communication1
 
SPI Protocol in LPC2148
SPI  Protocol in LPC2148SPI  Protocol in LPC2148
SPI Protocol in LPC2148
 
EC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfEC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdf
 
SPI introduction(Serial Peripheral Interface)
SPI introduction(Serial Peripheral Interface)SPI introduction(Serial Peripheral Interface)
SPI introduction(Serial Peripheral Interface)
 
Serial Peripheral Interface
Serial Peripheral InterfaceSerial Peripheral Interface
Serial Peripheral Interface
 
Serial Communication in 8051
Serial Communication in 8051Serial Communication in 8051
Serial Communication in 8051
 
Serial data transfer
Serial data transferSerial data transfer
Serial data transfer
 
USART - 8251 / usart-8251A Pallav Shukla
USART - 8251 / usart-8251A Pallav ShuklaUSART - 8251 / usart-8251A Pallav Shukla
USART - 8251 / usart-8251A Pallav Shukla
 
Class10
Class10Class10
Class10
 
USART.pptx
USART.pptxUSART.pptx
USART.pptx
 
8051 microcontroller notes continuous
8051 microcontroller notes continuous 8051 microcontroller notes continuous
8051 microcontroller notes continuous
 
I2C protocol and DS1307 RTC interfacing
I2C protocol and DS1307 RTC interfacingI2C protocol and DS1307 RTC interfacing
I2C protocol and DS1307 RTC interfacing
 
I2 c and mpu6050 basics
I2 c and mpu6050 basicsI2 c and mpu6050 basics
I2 c and mpu6050 basics
 
UART
UARTUART
UART
 
4 ql uart_psb_ds_revc
4 ql uart_psb_ds_revc4 ql uart_psb_ds_revc
4 ql uart_psb_ds_revc
 
8251 USART.pptx
8251 USART.pptx8251 USART.pptx
8251 USART.pptx
 
moving message display of lcd
 moving message display of lcd moving message display of lcd
moving message display of lcd
 

More from maheswariM7 (6)

TIMERS.pptx
TIMERS.pptxTIMERS.pptx
TIMERS.pptx
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture8
Lecture8Lecture8
Lecture8
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 

Recently uploaded

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 

Recently uploaded (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 

8051 SERIAL PORTS.pptx

  • 2. 8051 Serial Ports • One of the 8051’s many powerful features is its integrated Universal Asynchronous Receiver Transmitter (UART), otherwise known as a serial port. With integrated serial port of 8051, data can be transmitted and received easily by reading and writing the data to the serial port registers. The features of the 8051 serial ports are • Full duplex operation. • Receive Buffered. • Access using single double buffered Register SBUF. • Four different modes of operation. • Option to use fixed baud rate or programmable baud rate.
  • 3. 8051 Serial Ports • Full Duplex serial port means that it can transmit and receive data simultaneously. It is also receive- buffered, meaning it can commence reception of a second byte before a previously received byte has been read from the SBUF register. • However, if the first byte still hasn’t been read by the time reception of the second byte is complete, one of the bytes will be lost. The serial port receive and transmit registers are both accessed at Special Function Register SBUF.
  • 4. 8051 Serial Ports • Data to be transmitted are written to the register SBUF and the data received by the serial port are read from the register SBUF. Physically reading and writing SBUF actually accesses two separate registers. • This technique of having same address for two different registers is called double buffering.
  • 5. SERIAL PORT CONTROL SFRS • The serial port of 8051 is controlled by two registers in SFR area of 8051 as shown in Table • The two registers are Serial Port control registers; SCON and serial port buffer register SBUF. SFR Name Description SFR Address SCON Serial port control register 98h SBUF Serial port buffer register 99h
  • 6. SERIAL PORT CONTROL SFRS • In addition to the above two registers, the MSB of PCON register named as SMOD bit is used to double the baud rate of serial transmission and reception. • If SMOD bit is set to 1, then the baud rate is doubled. • The individual bits of SCON have the functions as shown in Table . As the SCON register has many individual status bits, the individual bits of this register are bit addressable. The bit address is also given in Table . • The programmer can use these bit addresses to check the status of the serial port and set the mode individually.
  • 7. Bit patterns for SCON (98h) SFR Bit Name Bit Address Explanation of Function D7 SM0 9Fh Serial port mode select bits D6 SM1 9Eh D5 SM2 9Dh Multiprocessor Communications Enable bit D4 REN 9Ch Receiver Enable. This bit must be set in order to receive characters. D3 TB8 9Bh Transmit bit 8. The 9th bit to transmit in mode 2 and 3. D2 RB8 9Ah Receive bit 8. The 9th bit received in mode 2 and 3. D1 TI 99h Transmit Interrupt Flag. Set when a byte has been completely transmitted. D0 RI 98h Receive Interrupt Flag. Set when a byte has been completely received.
  • 8. Bit Pattern - Description • D7 and D6 bits of the SCON register define the operation modes of the serial port and the basic operating modes are given in Table. • The SM0 and SM1 bits can select any one of the four operating modes described in the next section.
  • 9. Definition of Bits SM0 and SM1 in SCON SFR
  • 10. Bit Pattern - Description • The next bit, SM2, is a flag used for enabling "Multiprocessor communication" in modes 2 and 3. If SM2 is set to 1 in modes 2 and 3, the “Receive Interrupt” RI flag will not be activated if the received 9th data bit is 0. If SM2 is set in Mode1, then “Receive Interrupt” RI flag will not be activated if valid stop bit is not received. • This can be useful in certain advanced serial applications. It can be now assumed that SM2 bit has to be cleared so that RI flag will be set when any character is received.
  • 11. Bit Pattern - Description • The next bit, REN, is "Receiver Enable." This bit is set in order to receive the characters from the receive data line of the serial port. • The TB8 bit is used in modes 2 and 3. In modes 2 and 3, a total of nine data bits are transmitted. The first 8 bits are the 8 bits of the data to be transmitted, and the ninth bit is taken from TB8. The RB8 also operates in modes 2 and 3 on the reception side.
  • 12. Bit Pattern - Description • When a byte is received in modes 2 or 3, a total of nine bits are received. In this case, the first eight bits received are the data of the serial byte received and the value of the ninth bit received will be placed in RB8. • TI means "Transmit Interrupt." When a program writes a data to the serial port buffer SBUF, then the serial port will start shifting this data in the serial transmit line bit by bit at the predefined clock speed or baud rate. 8051 will give TI signal to the programmer after sending the data completely.
  • 13. Bit Pattern - Description • Upon sensing the TI bit set to 1, the programmer can then write the next data for transmission. When the TI bit is set, the programmer may assume that the serial port is "free" and ready to send the next byte. • Finally, the RI bit means "Receive Interrupt." Whenever a data is received on the receive data line of the serial port, this serial data will be shifted in to a buffer and then stored in the SBUF register. • Setting of RI bit indicates that a byte has been received. Upon sensing the RI bit set to 1, the programmer may read the data from the SBUF.
  • 14. OPERATING MODES • Bits SM0 and SM1 are used to set the serial mode to a value between 0 and 3. • Selecting the Serial Mode selects the mode of operation (8-bit/9-bit, UART or Shift Register) and also determines how the baud rate will be calculated. • In modes 0 and 2 the baud rate is fixed based on the oscillator’s frequency. In modes 1 and 3 the baud rate is variable based on Timer 1 overflows. © Oxford University Press 2013
  • 15. Block Diagram of the serial port of 8051 © Oxford University Press 2013
  • 16. SBUF • SBUF is physically two registers with the same address. • When data to be transmitted is written to the SBUF register, then it will be shifted bit by bit into the TXD line of 8051. The port 3 pin 3.1 acts as the TXD line. The shifting is done by the transmit clock which determines the baud rate. • Similarly, when the data bits are received on the RXD line (Pin 3.0 of port 3), the bits are shifted serially into the shift register according to the Receive clock. • After the reception is complete, the data received will be placed on SBUF from where it can be read by the programmer through the internal bus.
  • 17. Reception / Transmission • Mode 0: In this mode serial data is entering and exiting through RxD pin. So, in mode 0, Full duplex is not possible – meaning that both transmission and reception cannot take place simultaneously. TxD pin outputs the shift clock. 8 bits are transmitted/received (LSB first). The baud rate is fixed at 1/12 the oscillator frequency. Transmission is started by writing a data byte to the SBUF register and once the transmission is complete, TI flag is set • The reception is started by enabling REN in SCON register. Once the data reception is complete, the RI flag is set • The baud rate in Mode 0 is fixed at one twelfth of the clock frequency. Baud rate= (Clock frequency/12)
  • 18. Signal Transmission / Reception pattern © Oxford University Press 2013
  • 19. Reception / Transmission • Mode 1: In this mode, 10 bits are transmitted through TxD and simultaneously 10 bits can be received through RxD. The 10 bits are made up of a start bit (0), 8 data bits (LSB first), and a stop bit (1). On completion of reception, the stop bit goes into RB8 in Special Function Register SCON. The baud rate is variable and is set by the Timer 1 overflow rate. The baud rate for mode 1 is fixed at the following rate.
  • 20. Reception / Transmission • Baud rate= (Timer 1 overflow rate /16) if SMOD bit in PCON SFR is set to 1. • Baud rate= (Timer 1 overflow rate /32) if SMOD bit in PCON SFR is set to 0. • The MSB of PCON register can be set or reset by the programmer. The baud rate can be doubled by setting the MSB of PCON.
  • 21. Reception / Transmission • In order to generate the baud rate clock from timer 1, Timer 1 can be configured to act as timer in auto reload mode with the timer 1 interrupt disabled. • As in mode 0, the transmission is initiated by writing a data to SBUF register. Reception is initiated by a 1 to 0 transition that is the start bit received and also when REN of SCON SFR is 1.
  • 22. Reception / Transmission • Mode 2: In this mode, 11 bits are transmitted through TxD or received through RxD. The 11 bits are made up of one start bit (always 0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (always 1). The 9th data bit transmitted is same as TB8 bit in SCON special function register. It can be assigned the value of 0 or 1 by the programmer. Or, for example, the parity bit (P, in the PSW) could be moved into TB8. • On reception, the 9th data bit goes into RB8 in Special Function Register SCON, while the stop bit is ignored. The baud rate is programmable to either 1/32 or 1/64 of the oscillator clock frequency. • Baud rate= (Clock frequency /32) if SMOD bit in PCON SFR is set to 1. • Baud rate= (Clock frequency /64) if SMOD bit in PCON SFR is set to 0.
  • 23. Reception / Transmission • Mode 3: In this mode, 11 bits are transmitted through TxD and simultaneously 11 bits are received through RxD. The 11bits are made up of a start bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (1). In fact, Mode 3 is the same as Mode 2 in all respects except baud rate. The baud rate in Mode 3 is variable. • The baud rate for mode 3 is fixed at the following rate similar to mode1. • Baud rate= (Timer 1 overflow rate /16) if SMOD bit in PCON SFR is set to 1. • Baud rate= (Timer 1 overflow rate /32) if SMOD bit in PCON SFR is set to 0.
  • 24. Reception / Transmission • In all four modes, transmission is initiated by any instruction that uses SBUF as a destination register. Reception is initiated in Mode 0 by the condition RI = 0 and REN = 1. Reception is initiated in the other modes by the incoming start bit when REN = 1. • The flow chart 11.20 shows the steps in programming the serial port of 8051. These steps are detailed in the following sections and also in the example programs.
  • 26. Initializing Serial Port • Once the Serial Port Mode has been selected, then the programmer must configure the serial port’s baud rate. Only serial port modes 1 and 3, the baud rates are programmable. • The Baud Rate is determined based on the oscillator’s frequency when in mode 0 and 2. In mode 0, the baud rate is always the oscillator frequency divided by 12. This means if the crystal is 11.059 MHz, mode 0 baud rate will always be 921,583 baud. • In mode 2 the baud rate is always the oscillator frequency divided by 64, so an 11.059 MHz crystal speed will yield a baud rate of 172,797. © Oxford University Press 2013
  • 27. Initializing Serial Port • In modes 1 and 3, the baud rate is determined by how frequently timer 1 overflows. The most common method is to put timer 1 in 8-bit auto- reload mode (timer mode 2) and set a reload value (TH1) that causes Timer 1 to overflow at a frequency appropriate to generate a baud rate. • To determine the value that must be placed in TH1 to generate a given baud rate, the following equation is used. • TH1 = 256 - ((Clock frequency / 384) / Baud) if SMOD in PCON SFR is 0. • TH1 = 256 - ((Clock frequency / 192) / Baud) if SMOD in PCON SFR is 1. • The following table gives the commonly used baud rates and the corresponding reload for the timer in mode 2 assuming the clock frequency of 11.059 MHz and SMOD is reset. © Oxford University Press 2013
  • 28. Commonly used baud rates Baud Rate Timer value TH1 300 A0h 1200 D0h 2400 FAh 9600 FDh © Oxford University Press 2013
  • 29. Baud Rate Following set of instructions will set the timer for the baud rate of 9600. • MOV TMOD, #00100000B ;timer/counter 1 set for mode 2, 8-bit TIMER • ;operation • MOV TH1, #0FDh ; timer/counter 1 is timed for 9600 baud • SETB TR1 ; timer/counter 1 is enabled for free run • For initializing the serial port for mode 3 operation, the following instruction can be used. • MOV SCON, #11010000B © Oxford University Press 2013
  • 30. Transmitting and Receiving Data Using Serial Port • Once the Serial Port has been properly configured as explained above, the serial port is ready to be used to send data and receive data. • To write a byte to the serial port one must simply write the value to be transmitted to the SBUF (99h) SFR. For example, to send the letter "A" to the serial port, the following instruction can be written. MOV SBUF, #’A’ © Oxford University Press 2013
  • 31. • Upon execution of the above instruction the 8051 will begin transmitting the character via the serial port. Once the transmission is complete, the serial port transmit interrupt flag TI is set. Since the 8051 does not have a serial output buffer, a character can not be written to SBUF before the previous written character is completely transmitted. This can be accomplished by checking the TI flag. • Reading data received by the serial port is equally easy. To read a byte from the serial port one just needs to read the value stored in the SBUF (99h) SFR after the 8051 has automatically set the RI flag in SCON. © Oxford University Press 2013 Transmitting and Receiving Data Using Serial Port