On this page
Serial Communication - Receive
This program demonstrates how to receive data via UART serial communication on the 8051.
Description
The program configures the serial port and continuously receives data, displaying each received byte on Port 1.
Configuration
- Baud Rate: 9600 bps
- Mode: 8-bit UART, variable baud rate
- Timer 1: Used as baud rate generator
Code
| |
Explanation
Initialization
- MOV TMOD, #20H: Configure Timer 1 in mode 2 (8-bit auto-reload)
- Used to generate baud rate for serial communication
- MOV SCON, #50H: Configure serial port
- Mode 1: 8-bit UART
- REN = 1: Enable reception
- MOV TH1, #0FDH: Set baud rate to 9600 bps
- With 11.0592 MHz crystal
- Reload value = 256 - (Crystal / (384 × Baud Rate))
- SETB TR1: Start Timer 1
Reception Loop
- JNB RI, HERE: Wait for receive interrupt flag
- RI = 1 when a byte is received
- MOV A, SBUF: Read received byte from serial buffer
- MOV P1, A: Display on Port 1
- CLR RI: Clear receive interrupt flag
- SJMP HERE: Continue receiving
Baud Rate Calculation
For 9600 baud with 11.0592 MHz crystal:
TH1 = 256 - (11059200 / (384 × 9600))
TH1 = 256 - 3 = 253 = 0FDHThe program continuously polls for incoming data and displays it immediately.