RS232 Serial Port Programming
Learning Goals
- Apply multiprocessor mode communication: recognizing SM2 bit enabling multiprocessor mode, understanding address byte recognition (interrupting slave only on address match), allowing broadcast capability through address filtering, and managing multiple-device networks
- Calculate baud rate: determining reload values for Timer 1 from desired baud rate and crystal frequency (11.0592 MHz standard for 9600 baud), recognizing baud rate = fosc/(32 × (256 - TH1) × 12) for mode 1
- Master serial port registers: SBUF (Serial Buffer) for data transmission/reception, SCON (Serial Control) register selecting mode (SM0, SM1 bits) and control flags (TI, RI, SM2), PCON for baud rate selection (SMOD bit doubling baud rate when set)
- Program character transmission: loading character into SBUF, monitoring TI flag for transmission completion, clearing TI flag for next transmission, and implementing string transmission sequences (WELCOME TO INDIA example at 9600 baud)
- Implement character reception: monitoring RI flag for character arrival, reading character from SBUF, clearing RI flag, and utilizing interrupt-driven reception for all character capture without polling overhead
Serial communication is a fundamental method for microprocessors to exchange data with external devices using a minimum number of wires. In the context of the 8051 microcontroller, the Universal Asynchronous Receiver/Transmitter (UART) handles this process, allowing for full-duplex communication (simultaneous transmit and receive) .
The hardware interface typically follows the RS232 standard, which defines signal levels and connector pinouts. While the 8051 uses TTL logic levels (0V and 5V), an external level shifter like the MAX232 is required to convert these to RS232 levels (+3V to +15V for logic 0 and -3V to -15V for logic 1) .
Core Serial Registers
Serial operations are managed through three primary Special Function Registers (SFRs):
- SBUF (Serial Buffer): Physically two separate registers. Writing to SBUF loads the transmit register; reading from SBUF accesses the received data.
- SCON (Serial Control): Controls the communication mode, multiprocessor settings, and interrupt flags.
- PCON (Power Control): Contains the SMOD bit, which can double the baud rate when set to 1.
Footnotes
-
Part2 8051 Serial Communication Interface - Detailed breakdown of SCON and serial modes. ↩
-
8051 Serial Port Programming Guide - Overview of SBUF and flag management. ↩
Baud Rate Setting and Serial Communication in 8051
The SCON Register Architecture
The SCON register (Address 98H) is bit-addressable and defines how the serial port behaves.
| Bit | Symbol | Function |
|---|---|---|
| 7 | SM0 | Serial Port Mode Specifier bit 0 |
| 6 | SM1 | Serial Port Mode Specifier bit 1 |
| 5 | SM2 | Multiprocessor Communication Enable |
| 4 | REN | Receiver Enable (Set to 1 to receive data) |
| 3 | TB8 | 9th Transmit Bit (used in Modes 2 & 3) |
| 2 | RB8 | 9th Received Bit (used in Modes 2 & 3) |
| 1 | TI | Transmit Interrupt Flag (Set by hardware at end of stop bit) |
| 0 | RI | Receive Interrupt Flag (Set by hardware at end of stop bit) |
Serial Modes:
- Mode 0: Shift Register (Fixed Baud = )
- Mode 1: 8-bit UART (Variable Baud via Timer 1) — Most Common
- Mode 2: 9-bit UART (Fixed Baud = or )
- Mode 3: 9-bit UART (Variable Baud via Timer 1)
Footnotes
-
Serial Port Control Register (SCON) - Mode definitions and multiprocessor bit functions. ↩
Calculating Baud Rate for Mode 1
- 1Step 1
Standard 8051 systems use an 11.0592 MHz crystal. This frequency is chosen because it can be divided exactly to reach standard baud rates like 9600.
- 2Step 2
Divide the crystal frequency by 12: .
- 3Step 3
The UART internal hardware further divides the machine cycle by 32 (if SMOD=0). .
- 4Step 4
To find the TH1 reload value for a desired baud rate: For 9600 baud with SMOD=0: (0xFD) .
Footnotes
-
8051 Serial Baudrate Calculation - Formula and crystal frequency logic. ↩
-
Why 11.0592 MHz?
Using a 12 MHz crystal results in a non-integer reload value for 9600 baud (), leading to timing errors. The 11.0592 MHz crystal ensures exactly 3 ticks per bit at 9600 baud, resulting in 0% error .
Footnotes
-
8051 Serial Baudrate Calculation - Formula and crystal frequency logic. ↩
1; Transmit 'WELCOME TO INDIA' at 9600 Baud 2MOV TMOD, #20H ; Timer 1, Mode 2 (8-bit auto-reload) 3MOV TH1, #0FDH ; 9600 baud @ 11.0592MHz 4MOV SCON, #50H ; Mode 1, 8-bit UART, REN enabled 5SETB TR1 ; Start Timer 1 6 7MAIN: MOV DPTR, #MYSTR 8LOOP: CLR A 9 MOVC A, @A+DPTR 10 JZ STOP ; End of string 11 ACALL SEND 12 INC DPTR 13 SJMP LOOP 14 15SEND: MOV SBUF, A ; Load character 16WAIT: JNB TI, WAIT ; Wait for TI to be set 17 CLR TI ; Clear TI for next char 18 RET 19 20STOP: SJMP STOP 21MYSTR: DB 'WELCOME TO INDIA', 0 22END
Multiprocessor Communication (Mode 2 & 3)
In networks with one Master and multiple Slaves, the SM2 bit in SCON allows for efficient "Address Filtering."
- Address Recognition: The Master sends an 11-bit frame where the 9th data bit (TB8) is set to 1 to signify an Address Byte.
- Slave Filtering: All slaves initially have SM2 = 1. When a byte arrives with the 9th bit = 1, all slaves are interrupted.
- Address Match: Each slave compares the received byte with its own assigned address. The matching slave clears its SM2 bit to 0.
- Data Exchange: The Master then sends data bytes with the 9th bit = 0. Only the slave with SM2 = 0 will be interrupted; others (with SM2=1) will ignore the data .
Footnotes
-
Operation in Multiprocessor mode in 8051 - Detailed explanation of address vs data byte recognition. ↩
Timer 1 Reload Values (TH1)
Reload values for standard baud rates at 11.0592 MHz (SMOD=0)
Software Responsibility
Unlike some modern peripherals, the 8051 hardware does NOT clear the TI and RI flags automatically. You MUST clear them in your code (CLR TI or RI=0) after every transmission or reception, or the next operation will fail to trigger correctly .
Footnotes
-
8051 Serial Port Programming Guide - Overview of SBUF and flag management. ↩
Advanced Serial Features & FAQs
Knowledge Check
Which register bit must be set to allow the 8051 to receive serial data?