8051 Microcontroller   153
Basics of Serial Communication
• Computers transfer data in two ways:
   • Parallel: Often 8 or more lines (wire conductors) are used to
     transfer data to a device that is only a few feet away.
   • Serial: To transfer to a device located many meters away, the
     serial method is used. The data is sent one bit at a time.
   8051 Microcontroller                                     154
   Basics of Serial Communication
• Serial data communication uses two methods
   • Synchronous method transfers a block of data at a time
   • Asynchronous method transfers a single byte at a time
• There are special IC’s made by many manufacturers for
  serial communications.
   • UART (universal asynchronous Receiver transmitter)
   • USART (universal synchronous-asynchronous Receiver-
     transmitter)
   8051 Microcontroller                                       155
Asynchronous – Start & Stop Bit
• Asynchronous serial data communication is widely
  used for character-oriented transmissions
      • Each character is placed in between start and stop bits,
        this is called framing.
      • Block-oriented data transfers use the synchronous
        method.
• The start bit is always one bit, but the stop bit can
  be one or two bits
• The start bit is always a 0 (low) and the stop bit(s)
  is 1 (high)
8051 Microcontroller                                           156
Asynchronous – Start & Stop Bit
8051 Microcontroller              157
Data Transfer Rate
• The rate of data transfer in serial data communication is
  stated in bps (bits per second).
• Another widely used terminology for bps is baud rate.
   • It is modem terminology and is defined as the number of signal
     changes per second
   • In modems, there are occasions when a single change of signal
     transfers several bits of data
• As far as the conductor wire is concerned, the baud rate
  and bps are the same.
   8051 Microcontroller                                      158
8051 Serial Port
• Synchronous and Asynchronous
• SCON Register is used to Control
• Data Transfer through TXd & RXd pins
• Some time - Clock through TXd Pin
• Four Modes of Operation:
      Mode 0              :Synchronous Serial Communication
      Mode 1              :8-Bit UART with Timer Data Rate
      Mode 2              :9-Bit UART with Set Data Rate
      Mode 3              :9-Bit UART with Timer Data Rate
   8051 Microcontroller                                       159
8051 Microcontroller   160
1.DTR (data terminal ready).
When a terminal (or a PC COM port) is turned on, after going
through a self-test, it sends out signal DTR to indicate that it is
ready for communication.
If there is something wrong with the COM port, this signal will not
be activated. This is an active-low signal and can be used to
inform the modem that the computer is alive and kicking. This is
an output pin from DTE (PC COM port) and an input to the
modem.
2. DSR (data set ready).
When DCE (modem) is turned on and has gone through the self
test, it asserts DSR to indicate that it is ready to communicate.
Thus, it is an output from the modem (DCE) and input to the PC
(DTE). This is an active-low signal. If for any reason the modem
cannot make a connection to the telephone, this signal remains
inactive, indicating to the PC (or terminal) that it cannot accept or
send                                                            data.
    8051 Microcontroller                                      161
3. RTS (request to send).
When the DTE device (such as a PC) has a byte to
transmit, it asserts RTS to signal the modem that it has a
byte of data to transmit. RTS is an active-low output from
the DTE and an input to the modem.
4. CTS (clear to send).
 In response to RTS, when the modem has room for
storing the data it is to receive, it sends out signal CTS to
the DTE (PC) to indicate that it can receive the data now.
This input signal to the DTE is used by the DTE to start
                        transmission
8051 Microcontroller                                      162
5. DCD (carrier detect, or DCD, data carrier detect).
The modem asserts signal DCD to inform the DTE (PC)
that a valid carrier has been detected and that contact
between it and the other modem is established.
Therefore, DCD is an output from the modem and an
input to the PC (DTE).
6. RI (ring indicator).
An output from the modem (DCE) and an input to a PC
(DTE) indicates that the telephone is ringing. It goes on
and off in synchronization with the ringing sound.
8051 Microcontroller                                        163
MAX232
Since the RS232 is not compatible with today’s microprocessors and microcontrollers,
we need a line driver (voltage converter) to convert the RS232’s signals to TTL voltage
levels that will be acceptable to the 8051’s TxD and RxD pins. One example of such a
converter is MAX232 from Maxim Corp. (www. maxim-ic.com). The MAX232 converts
from RS232 voltage levels to TTL voltage levels, and vice versa. One advantage of the
MAX232 chip is that it uses a +5 V power source which is the same as the source
voltage for the 8051. In other words, with a single +5 V power supply we can power
both the 8051 and MAX232, with no need for the dual power supplies that are common
in many  older
      8051       systems.
           Microcontroller                                                     164
Registers related to Serial Communication
                          1. SBUF Register
                          2. SCON Register
                          3. PCON Register
   8051 Microcontroller                      165
SBUF Register
• SBUF is an 8-bit register used solely for serial communication.
• For a byte data to be transferred via the TxD line, it must be
  placed in the SBUF register.
• The moment a byte is written into SBUF, it is framed with the
  start and stop bits and transferred serially via the TxD line.
• SBUF holds the byte of data when it is received by 8051 RxD
  line.
• When the bits are received serially via RxD, the 8051 deframes
  it by eliminating the stop and start bits, making a byte out of
  the data received, and then placing it in SBUF.
8051 Microcontroller                                            166
SBUF Register
• Sample Program:
   8051 Microcontroller   167
SCON Register
        SM0 SM1 SM2 REN TB8 RB8                     TI    RI
                                                         Set when a Cha-
                         Set to Enable                   ractor received
                         Serial Data
                         reception                Set when Stop bit Txed
  Enable Multiprocessor      9th Data Bit       9th Data Bit
  Communication Mode         Sent in Mode 2,3   Received in Mode 2,3
  8051 Microcontroller                                              168
8051 Microcontroller   169
SM2
SM2 is the D5 bit of the SCON register. This bit enables the multiprocessing
capability of the 8051 and is beyond the discussion of this chapter. For
our applications, we will make SM2 = 0 since we are not using the 8051 in a
multiprocessor environment.
REN
The REN (receive enable) bit is D4 of the SCON register. The REN bit
is also referred to as SCON.4 since SCON is a bit-addressable register. When
the REN bit is high, it allows the 8051 to receive data on the RxD pin of the
8051. As a result if we want the 8051 to both transfer and receive data, REN
must be set to 1. By making REN = 0, the receiver is disabled. Making REN =
1 or REN = 0 can be achieved by the instructions “SETB SCON.4” and “CLR
SCON.4”, respectively. Notice that these instructions use the bit-addressable
features of register SCON. This bit can be used to block any serial data
reception and is an extremely important bit in the SCON register.
      8051 Microcontroller                                          170
TB8
TB8 (transfer bit 8) is bit D3 of SCON. It is used for serial modes 2 and 3.
We make TB8 = 0 since it is not used in our applications.
RB8
RB8 (receive bit 8) is bit D2 of the SCON register. In serial mode 1, this
bit gets a copy of the stop bit when an 8-bit data is received. This bit (as is the
case for TB8) is rarely used anymore. In all our applications we will make RB8
= 0. Like TB8, the RB8 bit is also used in serial modes 2 and 3.
TI
TI (transmit interrupt) is bit D1 of the SCON register. This is an extremely
important flag bit in the SCON register. When the 8051 finishes the transfer
of the 8-bit character, it raises the TI flag to indicate that it is ready to transfer
another byte. The TI bit is raised at the beginning of the stop bit. We will discuss
its role further when programming examples of data transmission are given.
RI
RI (receive interrupt) is the D0 bit of the SCON register. This is another
extremely important flag bit in the SCON register. When the 8051 receives data
serially via RxD, it gets rid of the start and stop bits and places the byte in the
SBUF register. Then it raises the RI flag bit to indicate that a byte has been
received and should be picked up before it is lost. RI is raised halfway through
the stop bit, and we will soon see how this bit is used in programs for receiving
data 8051 Microcontroller                                                     171
                                                                                  serially
8051 Serial Port – Mode 0
The Serial Port in Mode-0 has the following features:
1. Serial data enters and exits through RXD
2. TXD outputs the clock
3. 8 bits are transmitted / received
4. The baud rate is fixed at (1/12) of the oscillator frequency
     8051 Microcontroller                                         172
8051 Serial Port – Mode 1
 The Serial Port in Mode-1 has the following features:
 1. Serial data enters through RXD
 2. Serial data exits through TXD
 3. On receive, the stop bit goes into RB8 in SCON
 4. 10 bits are transmitted / received
    1. Start bit (0)
    2. Data bits (8)
    3. Stop Bit (1)
 5. Baud rate is determined by the Timer 1 over flow rate.
  8051 Microcontroller                                       173
8051 Serial Port – Mode 2
  The Serial Port in Mode-2 has the following features:
  1. Serial data enters through RXD
  2. Serial data exits through TXD
  3. 9th data bit (TB8) can be assign value 0 or 1
  4. On receive, the 9th data bit goes into RB8 in SCON
  5. 11 bits are transmitted / received
        1.Start bit (0)
        2.Data bits (9)
        3.Stop Bit (1)
  6. Baud rate is programmable
  8051 Microcontroller                                    174
8051 Serial Port – Mode 3
  The Serial Port in Mode-3 has the following features:
  1. Serial data enters through RXD
  2. Serial data exits through TXD
  3. 9th data bit (TB8) can be assign value 0 or 1
  4. On receive, the 9th data bit goes into RB8 in SCON
  5. 11 bits are transmitted / received
        1.Start bit (0)
        2.Data bits (9)
        3.Stop Bit (1)
  6. Baud rate is determined by Timer 1 overflow rate.
  8051 Microcontroller                                    175
Programming Serial Data Transmission
 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
    8051 Microcontroller                                               176
Programming Serial Data Reception
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. TH1 is loaded to set baud rate
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. RI is cleared by CLR RI instruction
6. The RI flag bit is monitored with the use of instruction JNB RI, xx to see if
  an entire character has been received yet
7. When RI is raised, SBUF has the byte, its contents are moved into a safe
  place.
8. To receive the next character, go to step 5.
     8051 Microcontroller                                                 177
Doubling Baud Rate
• There are two ways to increase the baud rate of data
  transfer
   1. By using a higher frequency crystal
   2. By changing a bit in the PCON register
• PCON register is an 8-bit register.
     •When 8051 is powered up, SMOD is zero
     •We can set it to high by software and thereby double the baud rate.
   8051 Microcontroller                                            178
Doubling Baud Rate (cont…)
  8051 Microcontroller       179
8051 Microcontroller   180
8051 Microcontroller   181
8051 Microcontroller   182
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 one 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 and 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 the
SBUF register before TI is raised, the un transmitted 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 0 by the “CLR TI” instruction in order for this new byte
to be8051transferred
           Microcontroller .                                    183
Programming the 8051 to receive data serially
In the programming of the 8051 to receive character bytes serially, the
following steps must be taken.
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. TH1 is loaded with one of the values in Table 4 to set the baud rate
(assuming XTAL = 11.0592 MHz).
3. The SCON register is loaded with the value 50H, indicating serial mode
1, where 8-bit data is framed with start and stop bits and receive enable is
turned on.
4. TR1 is set to 1 to start Timer 1.
5. RI is cleared with the “CLR RI” instruction.
6. The RI flag bit is monitored with the use of the instruction “JNB RI,xx”
to see if an entire character has been received yet.
7. When RI is raised, SBUF has the byte. Its contents are moved into a safe
place.
8. To receive the next character, go to Step 5.
     8051 Microcontroller                                               184
8051 Microcontroller   185
8051 Microcontroller   186
8051 Microcontroller   187
8051 Microcontroller   188
8051 Microcontroller   189
8051 Microcontroller   190
8051 Microcontroller   191