8051 Microcontroller: Understanding SBUF and SCON Registers for Serial Communication

In the 8051 microcontroller, the Serial Control (SCON) and Serial Buffer (SBUF) registers are crucial for serial communication.

The 8051 microcontroller has a full-duplex serial port, allowing it to transmit and receive simultaneously. The port employs a receive buffer. Consequently, it can begin receiving a new byte before the previous byte has been read from the SBUF register. A collision occurs if a byte arrives in SBUF before the prior byte is read. Both the transmit and receive registers are accessed through SBUF.

The serial port can operate in four modes, which we will explore in detail.

Format of the SCON Register in the 8051

The SCON register is an 8-bit register that controls the serial port’s operation. It configures the serial port’s mode, handles interrupt flags, and governs data transmission and reception. SCON is a Special Function Register that controls the serial port and provides status information. It houses mode selection bits, the 9th data bit for transmit and receive (TB8 and RB8), and the serial port interrupt bits (TI and RI). SCON is bit-addressable.

Here’s the bit structure of the SCON register:

SCON register

  • SM0 (SCON.7): Serial port mode specifier (read Table 1)
  • SM1 (SCON.6): Serial port mode specifier (read Table 1)
  • SM2 (SCON.5): Enables multiprocessor communication in modes 2 and 3.
  • REN (SCON.4): Set/cleared by software to enable/disable reception.
  • TB8 (SCON.3): The 9th bit to be transmitted in modes 2 and 3, set/cleared by software.
  • RB8 (SCON.2): In modes 2 and 3, this is the 9th bit that was received. In mode 1, if SM2 = 0, RB8 is the stop bit that was received. In mode 0, it is not used.
  • TI (SCON.1): Transmit Interrupt flag, set by hardware at the end of the 8th bit time in mode 0, or at the beginning of the stop bit in other modes. Must be cleared by software.
  • RI (SCON.0): Receive Interrupt flag, set by hardware and must be cleared by software.

Table 1: Mode selection based on SM0 and SM1

SM0SM1Mode/Description/Baud Rate
000, Shift register, (FoscF_{osc}/12)
011, 8-bit UART, Variable
102, 9-bit UART, (FoscF_{osc}/64) OR (FoscF_{osc}/32)
113, 9-bit UART, Variable

Function of the SBUF Register in the 8051

The SBUF register is an 8-bit register for serial data transmission and reception. It’s a buffer, holding the data to be transmitted or the received data.

  • Writing to SBUF: Loads the data to be transmitted into the transmit buffer.
  • Reading from SBUF: Fetches the received data from the receive buffer.

Microcontroller Serial Port Initialization C Program

Microcontroller serial port initialization program

The C program above initializes Timer 0 for delay and Timer 1 for a baud rate of 9600 bps.

Transmission via SBUF

SBUF = 'S';
while (!TI);
TI = 0;  /* One character at a time */

for (i = 0; i < max; i++) {
    SBUF = transmit_array[i++];  /* Transmitting character array */
    while (!TI);
    TI = 0;
}

Summary

  • SCON Register: Configures the serial port’s mode, controls data transmission and reception, and handles interrupt flags.
  • SBUF Register: Acts as a buffer for serial data transmission and reception.
  • Understanding these registers is essential for implementing and managing serial communication in 8051-based systems.