8051 SERIAL PORT PROGRAMMING IN ASSEMBLY

SECTION 10.3: 8051 SERIAL PORT PROGRAMMING IN ASSEMBLY


In this section we discuss the serial communication registers of the 8051 and show how to program them to transfer and receive data serially. Since IBM PC/compatible computers are so widely used to communicate with 8051-based systems, we will emphasize serial communications of the 8051 with the COM port of the PC. To allow data transfer between the PC and an 8051 system without any error, we must make sure that the baud rate of the 8051 system matches the baud rate of the PC’s COM port. Some of the baud rates supported by PC BIOS are listed in Table 10-3. You can examine these baud rates by going to the Windows Hyper Terminal program and clicking on the Communication Settings option. The HyperTerminal program comes with Windows. HyperTerminal supports baud rates much higher than the ones listed in Table 10-3.
Baud rate in the 8051
The 8051 transfers and receives data serially at many different baud rates. The baud rate in the 8051 is programmable. This is done with the help of Timer 1. Before we discuss how to do that, we will look at the relationship between the crystal frequency and the baud rate in the 8051.
As discussed in previous chapters, the 8051 divides the crystal frequency by 12 to get the machine cycle frequency. In the case of XTAL = 11.0592 MHz, the machine cycle frequency is 921.6 kHz (11.0592 MHz / 12 = 921.6 kHz). The 8051 ‘s serial communication UART circuitry divides the machine cycle frequency of 921.6 kHz by 32 once more before it is used by Timer 1 to set the baud rate. Therefore, 921.6 kHz divided by 32 gives 28,800 Hz. This is the number we will use throughout this section to find the Timer 1 value to set the baud rate. When Timer 1 is used to set the baud rate it must be programmed in mode 2, that is 8-bit, auto-reload. To get baud rates compatible with the PC, we must load TH1 with the values shown in Table 10-4. Example 10-1 shows how to verify the data in Table 10-4.



Note: XTAL = 11.0592 MHz.



Example 10-1
With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud
rates. (a) 9600 (b) 2400 (c) 1200

Solution:
With XTAL = 11.0592 MHz, we have:
The machine cycle frequency of the 8051 = 11.0592 MHz / 12 = 921.6 kHz, and 921.6 kHz / 32 = 28,800 Hz is the frequency provided by UART to Timer 1 to set baud rate.

Notice that 1/12th of the crystal frequency divided by 32 is the default value upon activation of the 8051 RESET pin. We can change this default setting. This is explained at the end of this chapter.
11.0592MHz

SBUF register
SBUF is an 8-bit register used solely for serial communication in the 8051. For a byte of data to be transferred via the TxD line, it must be placed in the SBUF register. Similarly, SBUF holds the byte of data when it is received by the 8051 ‘s RxD line. SBUF can be accessed like any other register in the 8051. Look at the following examples of how this register is accessed:

The moment a byte is written into SBUF, it is framed with the start and stop bits and transferred serially via the TxD pin. Similarly, 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 the SBUF.
SCON (serial control) register
The SCON register is an 8-bit register used to program the start bit, stop bit, and data bits of data framing, among other things.
The following describes various bits of the SCON register.



Figure 10-9. SCON Serial Port Control Register (Bit-Addressable)
SMO, SM1
SMO and SMI are D7 and D6 of the SCON register, respectively. These two bits determine the framing of data by specifying the number of bits per character, and the start and stop bits. They take the following combinations.


Of the 4 serial modes, only mode I is of interest to us. Further explanation for the other three modes is in Appendix A.2. They are rarely used today. In the SCON register, when serial mode 1 is chosen, the data framing is 8 bits, 1 stop bit, and 1 start bit, which makes it compatible with the COM port of IBM/compatible PCs. More importantly, serial mode 1 allows the baud rate to be variable and is set by Timer 1 of the 8051. In serial mode 1, for each character a total of 10 bits are transferred, where the first bit is the start bit, followed by 8 bits of data, and finally 1 stop bit.
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.
TBS
TBS (transfer bit 8) is bit D3 of SCON. It is used for serial modes 2 and 3. We make TBS = 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 TBS) 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.
Tl
TI (transmit interrupt) is bit Dl 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.
Rl
RI (receive interrupt) is the DO 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 serially.
Programming the 8051 to transfer data serially
In programming the 8051 to transfer 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. The TH1 is loaded with one of the values in Table 10-4 to set the baud rate for
    serial data transfer (assuming XTAL = 11.0592 MHz).
  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.
  1. TR1 is set to 1 to start Timer 1.
  2. TI is cleared by the “CLR TI” instruction.
  3. The character byte to be transferred serially is written into the SBUF register.
    1. The TI flag bit is monitored with the use of the instruction ” JNB TI, xx” to
      see if the character has been transferred completely.
  4. To transfer the next character, go to Step 5.
    Example 10-2 shows a program to transfer data serially at 4800 baud. Example 10-3 shows how to transfer “YES” continuously.




    Example 10-2
    Example 10-3
    Write a program to transfer the message “YES” serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.


    Importance of the Tl flag
    To understand the importance of the role of TI, look at the following sequence of steps that the 8051 goes through in transmitting a character via TxD.
    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.
      1. 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.
      2. 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
        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 0 by
    the “CLR TI” instruction in order for this new byte to be transferred.
    From the above discussion we conclude that by checking the TI flag bit, we know whether or not the 8051 is ready to transfer another byte. More importantly, it must be noted that the TI flag bit is raised by the 8051 itself when it finishes the transfer of data, whereas it must be cleared by the programmer with an instruction such as “CLR TI”. It also must be noted that if we write a byte into SBUF before the TI flag bit is raised, we risk the loss of a portion of the byte being transferred. The TI flag bit can be checked by the instruction “JNB TI, . . .” or we can use an interrupt, as we will see in Chapter 11. In Chapter 11 we will show how to use interrupts to transfer data serially, and avoid tying down the microcontroller with instructions such as “JNB TI, xx”.
    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 10-4 to set the baud rate (assum
      ing XTAL = 11.0592MHz).
    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.
  5. TR1 is set to 1 to start Timer 1.
  6. RI is cleared with the “CLR RI” instruction.
    1. 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.
    Examples 10-4 and 10-5 shows the coding of the above steps.
    Example 10-4
    Program the 8051 to receive bytes of data serially, and put them in PI. Set the baud rate at 4800, 8-bit data, and 1 stop bit.


    Example 10-5
    Assume that the 8051 serial port is connected to the COM port of the IBM PC, and on the PC we are using the HyperTerminal program to send and receive data serially. PI and P2 of the 8051 are connected to LEDs and switches, respectively. Write an 8051 program to (a) send to the PC the message “We Are Ready”, (b) receive any data sent by the PC and put it on LEDs connected to PI, and (c) get data on switches connected to P2 and send it to the PC serially. The program should perform part (a) once, but parts (b) and (c) continuously. Use the 4800 baud rate.




    Importance of the Rl flag bit
    In receiving bits via its RxD pin, the 8051 goes through the following steps.
    1. It receives the start bit indicating that the next bit is the first bit of the charac
      ter byte it is about to receive.
    2. The 8-bit character is received one bit at time. When the last bit is received, a
      byte is formed and 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 and 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 and 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 0 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.
    From the above discussion we conclude that by checking the RI flag bit we know whether or not the 8051 has received a character byte. If we fail to copy SBUF into a safe place, we risk the loss of the received byte. More importantly, it must be noted that the RI flag bit is raised by the 8051, but it must be cleared by the programmer with an instruction such as “CLR RI”. It also must be noted that if we copy SBUF into a safe place before the RI flag bit is raised, we risk copying garbage. The RI flag bit can be checked by the instruction “JNB RI, xx” or by using an interrupt, as we will see in Chapter 11.
    Doubling the baud rate in the 8051
    There are two ways to increase the baud rate of data transfer in the 8051.
    1. Use a higher-frequency crystal.
    2. Change a bit in the PCON register, shown below.


    Option 1 is not feasible in many situations since the system crystal is fixed. More importantly, it is not feasible because the new crystal may not be compatible with the IBM PC serial COM port’s baud rate. Therefore, we will explore option 2. There is a software way to double the baud rate of the 8051 while the crystal frequency is fixed. This is done with the register called PCON (power control). The PCON register is an 8-bit register. Of the 8 bits, some are unused, and some are used for the power control capability of the 8051. The bit that is used for the serial communication is D7, the SMOD (serial mode) bit. When the 8051 is powered up, D7 (SMOD bit) of the PCON register is zero. We can set it to high by




    software and thereby double the baud rate. The following sequence of instructions must be used to set high D7 of PCON, since it is not a bit-addressable register:
    To see how the baud rate is doubled with this method, we show the role of the SMOD bit (D7 bit of the PCON register), which can be 0 or 1. We discuss each case.
    Baud rates for SMOD = 0
  1. When SMOD = 0, the 8051 divides 1/12 of the crystal frequency by 32 and uses that frequency for Timer 1 to set the baud rate. In the case of XTAL = 11.0592 MHz we have:
    Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHz
    and
    921.6 kHz / 32 = 28,800 Hz since SMOD = 0
    This is the frequency used by Timer 1 to set the baud rate. This has been the basis of all the examples so far since it is the default when the 8051 is powered up. The baud rate for SMOD = 0 was listed in Table 10-4.
    Baud rates for SMOD = 1
    With the fixed crystal frequency, we can double the baud rate by making SMOD – 1. When the SMOD bit (D7 of the PCON register) is set to 1, 1/12 of XTAL is divided by 16 (instead of 32) and that is the frequency used by Timer 1 to set the baud rate. In the case of XTAL = 11.0592 MHz, we have:
    Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHz
    and
    921.6 kHz / 16 = 57,600 Hz since SMOD = 1
    This is the frequency used by Timer 1 to set the baud rate. Table 10-5: Baud Rate Comparison for SMOD = 0 and SMOD = 1


    Note: XTAL = 11.0592 MHz.
    Table 10-5 shows that the values loaded into TH1 are the same for both cases; however, the baud rates are doubled when SMOD = 1. Look at the following examples to clarify the data given in Table 10-5. See also Examples 10-6 through 10-10.


    Example 10-6
    Assuming that XTAL = 11.0592 MHz for the following program, state (a) what this program does, (b) compute the frequency used by Timer 1 to set the baud rate, and (c) find the baud rate of the data transfer.


    1. This program transfers ASCII letter B (01000010 binary) continuously.
    2. With XTAL = 11.0592 MHz and SMOD = 1 in the above program, we have:
      11.0592 MHz / 12 = 921.6 kHz machine cycle frequency
    921.6 kHz 716 = 57,600 Hz frequency used by Timer 1 to set the baud rate
    57,600 Hz / 3 = 19,200 baud rate
    Example 10-7
    Find the TH1 value (in both decimal and hex) to set the baud rate to each of the following.
    (a) 9600 (b) 4800 if SMOD = 1 Assume that XTAL – 11.0592 MHz.

    Solution:
    With XTAL = 11.0592 MHz and SMOD = 1, we have Tinier 1 frequency = 57,600 Hz.
    1. 57,600 / 9600 = 6; therefore, TH1 = -6 or TH1 = FAH.
    2. 57,600 / 4800 = 12; therefore, TH1 = -12 or TH1 = F4H.




    Example 10-8
    Find the baud rate if TH1 = -2, SMOD = 1, and XTAL – 11.0592 MHz. Is this baud rate supported by IBM/compatible PCs?
    Solution:
    With XTAL – 11.0592 MHz and SMOD = 1, we have Timer 1 frequency = 57,600-Hz. The baud rate is 57,600 / 2 = 28,800. This baud rate is not supported by the BIOS of the PCs; however, the PC can be programmed to do data transfer at such a speed, Also, HyperTerminal in Windows supports this and other baud rates.
    Examole 10-9
    Assume a switch is connected to pin PL7. Write a program to monitor its status and
    send two messages to serial port continuously as follows:
    SW=0 send “NO”
    SW=1 send “YES”
    Assume XTAL = 11.0592 MHz, 9600 baud, 8-bit data, and 1 stop bit.
    Solution:




    Example 10-10


    Interrupt-based data transfer
    By now you might have noticed that it is a waste of the microcontroller’s time to poll the TI and RI flags. In order to avoid wasting the microcontroller’s time we use interrupts instead of polling. In Chapter 11, we will show how to use interrupts to program the 8051 ‘s serial communication port.

Next post:

Previous post: