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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
ORG 0000H
	
	MOV TMOD , #20H;
	MOV SCON , #50H;
	MOV TH1 , #0FDH;	
	SETB TR1
	
	HERE:JNB RI , HERE
	MOV A,SBUF
	MOV P1,A
	CLR RI
	SJMP HERE
END

Explanation

Initialization

  1. MOV TMOD, #20H: Configure Timer 1 in mode 2 (8-bit auto-reload)
    • Used to generate baud rate for serial communication
  2. MOV SCON, #50H: Configure serial port
    • Mode 1: 8-bit UART
    • REN = 1: Enable reception
  3. MOV TH1, #0FDH: Set baud rate to 9600 bps
    • With 11.0592 MHz crystal
    • Reload value = 256 - (Crystal / (384 × Baud Rate))
  4. SETB TR1: Start Timer 1

Reception Loop

  1. JNB RI, HERE: Wait for receive interrupt flag
    • RI = 1 when a byte is received
  2. MOV A, SBUF: Read received byte from serial buffer
  3. MOV P1, A: Display on Port 1
  4. CLR RI: Clear receive interrupt flag
  5. SJMP HERE: Continue receiving

Baud Rate Calculation

For 9600 baud with 11.0592 MHz crystal:

TH1 = 256 - (11059200 / (384 × 9600))
TH1 = 256 - 3 = 253 = 0FDH

The program continuously polls for incoming data and displays it immediately.