This program demonstrates how to interface a 16x2 character LCD with the 8051 microcontroller and display text.

Description

The program initializes an LCD in 8-bit mode and displays the message “HELLO " on the screen.

Hardware Connections

  • Port 2: LCD data lines (D0-D7)
  • P3.7: RS (Register Select)
  • P3.6: R/W (Read/Write)
  • P3.5: E (Enable)

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
ORG 0000H
	MOV DPTR , #MYCOM
	
	C1:
	CLR A
	MOVC A , @A+DPTR
	ACALL COMWRT
	ACALL DELAY
	INC DPTR
	JZ SEND_DATA
	SJMP C1
	
	SEND_DATA:
	MOV DPTR , #MYDATA
	D1:CLR A
	MOVC A,@A+DPTR
	ACALL DATAWRT
	ACALL DELAY
	INC DPTR
	JZ AGAIN
	SJMP D1
	
	AGAIN: SJMP AGAIN
	
	DATAWRT:
	MOV P2,A
	CLR P3.7 ; Rs
	CLR P3.6 ; R
	SETB P3.5 ; E
	ACALL DELAY
	CLR P3.5
	RET
	
	COMWRT:
	MOV P2,A ; D0-D7
	CLR P3.7 ; Rs
	CLR P3.6 ; R->1/W->0
	SETB P3.5 ; E -> a short arc
	ACALL DELAY
	CLR P3.5
	RET
	
	DELAY:MOV R1,#200
	AGAIN2:MOV R2,#255
	HERE: NOP
	DJNZ R2,HERE
	DJNZ R1,AGAIN2
	RET
ORG 300H
	MYCOM: DB 38H,0EH,01H,06H,84H,0
	MYDATA: DB "HELLO ",0
END

Explanation

LCD Initialization Commands

  • 38H: Function Set - 8-bit mode, 2 lines, 5x7 font
  • 0EH: Display ON, Cursor ON, Blink OFF
  • 01H: Clear Display
  • 06H: Entry Mode - Increment cursor, No shift
  • 84H: Set cursor position to line 1, position 4

Command Write Function (COMWRT)

  1. Send command byte to Port 2
  2. RS = 0 (Command mode)
  3. R/W = 0 (Write mode)
  4. Generate enable pulse (high to low transition)

Data Write Function (DATAWRT)

  1. Send data byte to Port 2
  2. RS = 1 (Data mode)
  3. R/W = 0 (Write mode)
  4. Generate enable pulse

The program first sends initialization commands, then displays the text “HELLO " character by character.