Embedded

8051 Serial Communication

By CourseTron Team ·  7 min read ·  Updated Oct 2026 💬 𝕏 in f
Table of Contents
    ? Print / Save as PDF

    In short: The 8051's built-in UART uses two registers — SCON to pick one of four modes and control the bits, and SBUF to shift data in and out — with Timer 1 in 8-bit auto-reload mode generating the baud clock, so a few lines of setup give you a full asynchronous serial port.

    Almost every 8051-family part — the classic Intel 8051, the Atmel AT89C51/52, and modern SiLabs C8051 derivatives — ships with one hardware UART. For embedded engineers it is the first peripheral you reach for when a microcontroller must talk to a PC terminal, a GPS module, or another MCU. This guide walks through the register model, the Timer 1 baud-rate math, and the four operating modes, with concrete numbers you can drop straight into a project.

    Why a hardware UART beats bit-banging

    You can toggle a GPIO pin in software to fake serial output, but the CPU then has to count cycles for every single bit. At 9600 baud each bit lasts ~104 µs; on a 12 MHz 8051 (1 µs machine cycle) that is ~104 instruction cycles the core can do nothing else useful with. The on-chip UART instead shifts bits in and out of SBUF in the background and raises an interrupt flag when a full byte has moved, freeing the CPU for real work. That is the whole value proposition: offload the timing-critical shifting to dedicated hardware.

    SCON: the control and status register

    SCON (address 98H, bit-addressable) is where you configure and monitor the port. Its eight bits:

    BitNamePurpose
    7,6SM0, SM1Select mode 0–3
    5SM2Multiprocessor / 9-bit enable
    4RENReceive enable (set to allow RX)
    3TB89th transmit bit (modes 2/3)
    2RB89th received bit / stop bit
    1TITransmit interrupt flag
    0RIReceive interrupt flag

    ⚠️ TI and RI are set by hardware but cleared only by software. Forgetting to clear them in your ISR is the single most common 8051 serial bug — the interrupt either never re-fires or fires in an endless loop.

    SCON (98H) bit layoutbit 7SM0bit 6SM1bit 5SM2bit 4RENbit 3TB8bit 2RB8bit 1TIbit 0RIBits 7,6: mode selectBit 5 SM2: multiprocessor / 9-bitBit 4 REN: receive enableBit 3 TB8: 9th transmit bitBit 2 RB8: 9th received bit / stopBits 1,0 TI, RI: flags, cleared by softwareTI and RI are set by hardware, cleared only in softwareCourseTron · coursetron.com
    SCON bit layout: bits 7 to 0 are SM0, SM1, SM2, REN, TB8, RB8, TI, RI.

    SBUF: one address, two physical registers

    SBUF (address 99H) looks like a single byte but is actually two registers sharing one address. Writing to it (SBUF = 'A';) loads the transmit register and starts transmission. Reading from it (x = SBUF;) returns the last fully received byte from the receive register. This elegant trick means the same C statement direction (read vs. write) selects the right silicon automatically.

    8051 UART datapath8051 coreSBUF (TX)SBUF (RX)Shift logicBaud clockfrom Timer 1PinsTxD (P3.1)RxD (P3.0)writereadCourseTron · coursetron.com
    Datapath: writing SBUF feeds the TX shifter; the RX shifter fills the read side. Timer 1 clocks both.

    Baud-rate generation with Timer 1

    In modes 1 and 3 the baud rate is set by Timer 1 running in Mode 2 (8-bit auto-reload). You load a reload value into TH1; each overflow produces a tick, and the UART divides that by 16 (×32 if SMOD in PCON is set). The formula:

    Baud = (2^SMOD / 32) × (Fosc / (12 × (256 − TH1)))

    With a 11.0592 MHz crystal — the classic "magic" 8051 clock chosen precisely because it divides cleanly — for 9600 baud with SMOD=0:

    TH1 = 256 − (11059200 / (12 × 32 × 9600)) = 256 − 3 = 253 = 0xFD

    ? 11.0592 MHz gives 0% baud error at 1200–57600 baud. A 12 MHz crystal forces TH1≈0xFD too but with ~7% error — enough to corrupt frames at higher rates. Chosen crystal frequency is a design decision, not an afterthought. See our 8051 timers and counters guide for Timer 1 mode details.

    Serial frame on TxD (start, 8 data, stop)S01234567Stop10Marker steps through one bit per baud period (LSB first)CourseTron · coursetron.com
    The marker tracks one bit sampled at the baud clock, sliding from the start bit through the data bits to the stop bit.

    The four serial operating modes

    Mode (SM0 SM1)TypeFrameBaud source
    0 (0 0)Synchronous shift8 data, clock on TxDFixed Fosc/12
    1 (0 1)8-bit UARTStart + 8 + stopTimer 1 variable
    2 (1 0)9-bit UARTStart + 9 + stopFixed Fosc/32 or /64
    3 (1 1)9-bit UARTStart + 9 + stopTimer 1 variable

    Mode 0 is really a synchronous shift register — handy for driving 74HC595 I/O expanders, not for PC comms. Mode 1 is the everyday 8-bit asynchronous port you use 90% of the time. Modes 2 and 3 add a programmable 9th bit (TB8/RB8), which powers the SM2 multiprocessor feature: slaves ignore frames whose 9th bit is 0, so a master can address one node on a shared bus with the address byte's 9th bit set to 1.

    A minimal Mode-1 setup

    Bringing the port up is four writes: configure Timer 1, load the reload, start it, then arm the UART.

    • TMOD = 0x20; — Timer 1, mode 2 (8-bit auto-reload)
    • TH1 = 0xFD; — 9600 baud at 11.0592 MHz
    • TR1 = 1; — run Timer 1
    • SCON = 0x50; — mode 1, REN enabled

    To send: SBUF = c; while(!TI); TI = 0;. To receive: while(!RI); c = SBUF; RI = 0;. In production you replace the polling loops with an interrupt service routine vectored at 0x23 so the CPU never blocks. Teams building commercial firmware verify frame timing on a logic analyzer or against a Siemens/Keil simulator before committing, because a wrong reload value is invisible until bytes garble on real hardware.

    Want to go from register bits to a working RS-232 link on real silicon? Our hands-on CourseTron 8051 & Embedded C track builds a full UART driver with interrupt-driven RX/TX buffering. For industry-grade VLSI and chip-design training, explore ChipXpert's programs.

    Common pitfalls

    Beyond forgetting to clear TI/RI, watch for: starting Timer 1 before loading TH1 (first byte uses a garbage rate); leaving REN cleared so RX silently never works; and mixing a 12 MHz crystal with high baud rates. If your bytes are consistently off by a fixed bit pattern, suspect a baud mismatch between the two endpoints — not wiring. Our companion article on 8051 interrupts covers wiring the serial ISR correctly.

    FAQ

    Why is 11.0592 MHz used for the 8051 UART?

    Because it divides evenly by the UART's internal divisors, giving exact standard baud rates (1200–57600) with 0% error. A 12 MHz crystal introduces several percent of error that corrupts frames at higher speeds.

    What is the difference between modes 1 and 3?

    Both use Timer 1 for a variable baud rate, but mode 1 sends 8 data bits while mode 3 sends 9 (the extra TB8/RB8 bit), enabling multiprocessor addressing via SM2.

    Why must I clear TI and RI in software?

    The hardware sets them to signal "byte done" but leaves clearing to you so the ISR can distinguish TX from RX events. An uncleared flag re-triggers the interrupt endlessly or blocks the next transfer.

    Can the 8051 UART do full-duplex?

    Yes. Because SBUF is two physically separate transmit and receive registers behind one address, the chip can transmit and receive simultaneously in modes 1, 2, and 3.

    Advertisement