Displaying Characters on an LCD Character Module

INC.
Application Note
Crystal Clear and Visibly Superior LCD Modules
Displaying Characters on an LCD Character Module
I. Introduction:
This application note describes a simple technique to display characters from both the internal character
generator and user designed characters on an LCD character module.The controlling microcontroller is
a Phillips 87C751, a derivitive of the popular Intel 8051. The LCD module is connected to the microcontroller through its I/O ports. It could also be connected directly to the data bus with the addition of
address decoding logic.
The process of displaying character to this module is divided into three steps. First the module must be
initialized. This sets up the built-in LCD controller chip. Second, some user designed characters are
uploaded to the CGRAM. This allows the displaying of up to 8 custom characters in addition to the 192
character permanently stored in the module. Lastly, a message consisting of a mix of standard ASCII
characters and custom designed characters is displayed on the module.
II. Circuit Schematic
5V
Vdd
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
GND
D0
D1
D2
D3
D4
D5
D6
D7
U1
SW1
C3
0.1uF
R2
LCM1
87C751
Char LCD Module
RST
20KOhm
XTR1
X2
16MHz
X1
C1
33pF
C2
33pF
Parts List
LCM1
U1
XTR1
C1,C2
SW1
R1
R2
C3
5V
P3.0
P3.1
P3.2
P3.3
P3.4
P3.5
P3.6
P3.7
Part Number
HDM16216H-5
87C751
NMP160
33pF
-10Kohm
20Kohm
0.1 uF
E
RS
RW
VDD
VL
GND
Description
Hantronix Alphanumeric LCD Module
Phillips Microcontroller
16MHz Crystal
Capacitor
Push Button Switche (Reset)
Pot
Resistor
Capacitor
- Page 1 -
10KOhm
R1
INC.
Application Note
Crystal Clear and Visibly Superior LCD Modules
III. Software Flowchart:
Begin
Write
P1 = 38h, Command_Byte
Data=[DPTR]
4.1mSec Delay
Yes
?
Data = 99h
Return
No
P1 = 38h, Command_Byte
Initialization
Call Data_Byte
100uSec Delay
INC DPTR
P1 = 38h, Command_Byte
P1 = 0ch, Command_Byte
P1 = 01h, Command_Byte
CGRAM
First_Line
Second_Line:
Command_byte
Data_byte
RS low
RS high
IDLE
*
CGRAM: Program in CGRAM DATA
First_Line: Display First Line
Second_Line: Display Second Line
RW low - Enable write
DELAY
E low - generate enable pulse
Start Timer
Timer0 Interrupt:
TestFlag=1
Stop Timer
TestFlag = 0
TestFlag=1
?
Yes
nop
E high - pull up enable signal
Return
No
RW high - Read mode
Configure P1 to input port
DB7 = 1?
Yes
- Page 2 -
No
Return
INC.
Application Note
Crystal Clear and Visibly Superior LCD Modules
;**********************************************************
;;
Application Note:
;
=================
;
Displaying Characters on an LCD Character Module
;
;
Description:
Demo software to display “canned”
message and custom characters.
.;
Controller:
Phillips 87C751
;
LCD controller:
HD44780, KS0066, SED1278
;
;*************************************************************
;
Constant Definition
;*************************************************************
EnableT0
equ
082h
;enable timer0
Disable
equ
000h
;disable timer
D4100h
equ
00ch
;timer reload high byte def.
D4100l
equ
003h
;timer reload = 4.1mSec.
D100h
equ
000h
;timer reload
D100l
equ
04ch
;timer reload = 100uSec.
;*****************************************
;
Ram Definition
;*****************************************
Flags
DATA
020h
;flag
TstFlag
BIT
Flags.0 ;interrupt flag bit
;*****************************************
;
Port Connections
;
=================
;
P1.0 -> D0
;
P1.1 -> D1
;
P1.2 -> D2
;
...
;
P1.7 -> D7
;
P3.0 -> Enable
;
P3.1 -> RS
;
P3.2 -> RW
;*******************************************************
;
Interrupt Vectors
;
-----------------org
000h
jmp
PowerUp
; Power up reset vector
org
003h
jmp
ExInt0
; External interrupt 0 vector
org
00bh
jmp
Timer0
; Counter/ Timer 0 int vector
org
013h
jmp
ExInt1
; External int 1 vector
org
01bh
;
jmp
Timer1
; Timer 1 int vector
org
023h
jmp
I2C
; I2C serial int vector
org
50h
PowerUp:
;*****
Timer 0 Interrrupt preparation
clr
TR
clr
TF
;**************************************
;
LCD Initialization Routine
;**************************************
cinit:
clr
P3.1
clr
P3.2
setb
P3.0
mov
mov
RTL,#D4100l
RTH,#D4100h
;disable timer
;clear overflow
;RS low
;RW low
;Enable
;set timer reload value
;dealy time = 4.1mSec
- Page 3 -
mov
acall
acall
p1,#38h
command_byte
ddelay
mov
mov
mov
acall
acall
RTL,#D100l
RTH,#D100h
p1,#38h
command_byte
ddelay
;set timer reload value
;dealy time = 100uSec
;function set
mov
acall
mov
acall
mov
acall
p1,#38h
command_byte
p1,#0ch
command_byte
p1,#01h
command_byte
;function set
acall
acall
acall
cgram
first_line
second_line
;define custom fonts
;display first line
;display second line
setb
jmp
IDLE
sdone
;power down mode
;initial delay 4.1mSec
;busy flag not avail. yet
;display on
;clear display
sdone:
;********************************************************
;Subroutine: WRITE
;=================
;Purpose: To feed in data/command bytes to the LCD module
;Parameters:dptr = should be set to the beginning of
;
the data byte address
;
Data bytes should be finished with 99H
;Alg:
get a new data/command byte
;
while (new data != 99h) {
;
set port1 with new data
;
call data_byte
;
increment data pointer
;
}
;
return
;********************************************************
write:
write_loop:
mov
a,#0
movc
a,@a+dptr
cjne
a,#99h,write_cont
ret
write_cont:
mov
p1,a
acall
data_byte
inc
dptr
jmp
write_loop
;************************************************
;
Delay Routine:
;
Delay periond = 4/3uSec * DefRth,l
;************************************************
ddelay:
setb
TR
;start timer
mov
IE,#EnableT0
;enable timer
clr
TstFlag
;reset flag
dloop:
jnb
TstFlag,dloop
ret
;********************************
;
set address to beginning
;
of CG RAM
;********************************
INC.
Application Note
Crystal Clear and Visibly Superior LCD Modules
cgram:
mov
p1,#40h
acall
command_byte
mov
dptr,#cgram_data
acall
write
ret
;********************************
;
Set DDRAM to the beginnig of
;
the first line - 00
;********************************
first_line:
mov
p1,#080h ;set DDRAM
acall
command_byte
mov
dptr,#fline_data
acall
write
ret
;********************************
;
Set DDRAM to the beginning of
;
the second line - 40
;********************************
second_line:
mov
p1,#0c0h ;set DDRAM
acall
command_byte
mov
dptr,#sline_data
acall
write
ret
;**********************************************************
;
Feed Command/Data to the LCD module
;***********************************************************
command_byte:
clr
p3.1
; RS low for a command byte.
jmp
bdelay
data_byte:
setb
p3.1
; RS high for a data byte.
nop
bdelay:
clr
p3.2
; R/w low for a write mode
clr
p3.0
nop
setb
p3.0
;enable pulse
nop
;******************* Check Busy Flag
mov
p1,#0ffh ;configure port1 to input mode
setb
p3.2
;set RW to read
clr
p3.1
;set RS to command
clr
p3.0
;generate enable pulse
nop
setb
p3.0
bloop:
nop
mov
a,p1
anl
a,#80h
;check bit#7 busy flag
cjne
a,#00h,bloop;keep waiting until busy flag clears
;*****************************************
;
check busy flag twice
;*****************************************
bwait:
mov
a,p1
anl
a,#80h
cjne
a,#00h,bloop
clr
p3.2
;return to write mode
ret
;******************************************
;
Interrupt Routines
;******************************************
ExInt0:
- Page 4 -
ExInt1:
Timer1:
I2C:
reti
;**********************************************
Timer0:
setb
TstFlag
clr
TR
mov
IE,#Disable
reti
;********************************************
;
Data Bytes
;*******************************************
FLINE_DATA:
db
‘>>> HANTRONIX <<<’
db
099h
SLINE_DATA:
db
00h,01h,02h,03h,04h,05h,06h,07h
db
099h
CGRAM_DATA:
font1:
db
0ah,15h,11h,11h,0ah,04h,00h,00h
font2:
db
04h,0ah,11h,11h,15h,0ah,00h,00h
font3:
db
04h,0eh,15h,04h,04h,04h,04h,00h
font4:
db
04h,04h,04h,04h,15h,0eh,04h,00h
font5:
db
18h,18h,1fh,1fh,1fh,18h,18h,00h
font6:
db
1fh,1fh,03h,03h,03h,1fh,1fh,00h
font7:
db
0ah,15h,0ah,15h,0ah,15h,0ah,00h
font8:
db
15h,0ah,15h,0ah,15h,0ah,15h,00h
db
99h
end
*Display on a 16x2 Character Module
* Display Character Position and DDRAM Address of a
16x2 Character Module.
1
2
3
4
First
Line
00
01
02
03
Second
Line
40
41
42
43
Display Position
14
15
16
....
0D
0E
0F
....
4D
4E
4F
DDRAM Address
BITS
7 6 5 4 3 2 1 0
BYTE 1
BYTE 2
BYTE 3
BYTE 4
BYTE 5
BYTE 6
BYTE 7
BYTE 8
BITS 5,6,&7 NOT USED
* Custom characters memory map.