8/17/2019 8051 Interfacing LCD 16x2
1/14
1
A1.8051 Interfacing:LCD 16x2
Introduction:LCDLiquid Crystal Display(LCDs) provide a cost effective way to put a text output unit for a microcontroller. As
we have seen in the previous tutorial L!Ds or " #e$ments do no have the flexi%ility to display informative
messa$es. &his display has ' lines and can display characters on each line. *onetheless when it is
interfaced with the microcontroller we can scroll the messa$es with software to display information which is
more than characters in len$th.
LCD Internal Controller
Fig 1: LCD Block diagram
&he LCD is a simple device to use %ut the internal details are complex. +ost of the x' LCDs use a Hitachi
HD44780 or a compati%le controller. ,es a micrcontroller is present inside a Liquid crystal display as shown
in fi$ure .
8/17/2019 8051 Interfacing LCD 16x2
2/14
2
&he Display Controller ta-es commands and data from a external microcontroller and drivers the LCD panel(LCDP). It ta-es a A#CII value as input and $enerate a patter for the dot matrix. !.$. to display letter A
it ta-es its value 0X42(hex) or 66(dec) decodes it into a dot matrix of /x" as shown in fi$ure '.
Baic Command
&he LCD controller uses 0# and 01 lines alon$ with ! to operate the LCD.
• Resister Select (RS): Determine !eat"er a command#$% & 0' i ent #to et
() t"e di)la*' or act(al data#$%&1' i ent.
• Read/Write $+&0, !rite to t"e LCD. $+&1,$ead from t"e LCD.
&he commonly used instructions are shown in the instruction set %elow. 2%serve the Bit names: I/D, , D,
C etc at the %ottom of instruction set to decode the instructions completely.
• Clear Di)la*
• C(ror -ome
• %et ntr* /ode
• Di)la* ono control
• C(rordi)la* "ift
• F(nction %et
8/17/2019 8051 Interfacing LCD 16x2
3/14
• $ead B(* Flag
• Data $ead
• Data +rite
Intr(ction %et
HD44780U based instruction set
Instruction
Code Description
R
S
R/
W
B
7
B
B
!
B
4
B
"
B
#
B
$
B
0
Clear di)la* 0 0 0 0 0 0 0 0 0 1Clear di)la* and ret(rn c(ror to t"e
"ome )oition #addre 0'.
C(ror "ome 0 0 0 0 0 0 0 0 1 3
$et(rn c(ror to "ome )oition. Alo
ret(rn di)la* 4eing "ifted to t"e
original )oition. DD$A/ content
remain (nc"anged.
ntr* mode
et0 0 0 0 0 0 0 1
I
D%
%et c(ror moe direction #ID',
)ecie to "ift t"e di)la* #%'. 7"ee
o)eration are )erformed d(ring data
read!rite.
Di)la*
ono
control
0 0 0 0 0 0 1 D C B
%et ono of all di)la* #D' c(ror
ono #C' and 4link of c(ror )oition
c"aracter #B'.
C(rordi)la
* "ift
0 0 0 0 0 1 %
C
$
L
3 3 %et c(ror9moe or di)la*9"ift #%C'
"ift direction #$L'. DD$A/ content
8/17/2019 8051 Interfacing LCD 16x2
4/14
remain (nc"anged.
F(nction et 0 0 0 0 1DL
; F 3 3%et interface data lengt" #DL' n(m4erof di)la* line #;' and c"aracter font
#F'.
$ead 4(*
$A/DD$A/
addre
$ead 4(* $A/ or
DD$A/
1 0 +rite Data +rite data to C>$A/ or DD$A/.
+rite
C>$A/ or
DD$A/
1 0 +rite Data +rite data to C>$A/ or DD$A/.
Instruction bit na%es &
I/D 3 4 5 decrement cursor position 5 increment cursor position6
3 4 5 no display shift 5 display shift6
D 3 4 5 display off 5 display on6
C 3 4 5 cursor off 5 cursor on6
B 3 4 5 cursor %lin- off 5 cursor %lin- on 6
/C 3 4 5 move cursor 5 shift display6
!/L 3 4 5 shift left 5 shift ri$ht6
DL 3 4 5 73%it interface 5 83%it interface6
" 3 4 5 98 or 9 duty ( line) 5 9 duty (' lines)6
# 3 4 5 /8 dots 5 /4 dots6
B# 3 4 5 can accept instruction 5 internal operation in pro$ress.
LCD ?;I7
8/17/2019 8051 Interfacing LCD 16x2
5/14
5
Let ( look at a )in diagram of a commerciall* aaila4le LCD like 'HD$# !"ic" (e
a HD44780 controller and t"en decri4e it o)eration.
All the pins are identically to the lcd internal controller discussed a%ove
I U*B+R ,UC-I.
1 >ro(nd
2 @CC
Contrat ad(tment #@'
$egiter %elect #$%'. $%&0: Command $%&1: Data
5 $ead+rite #$+'. $+&0: +rite $+&1: $ead
8/17/2019 8051 Interfacing LCD 16x2
6/14
6
6 Clock #na4le'. Falling edge triggered
Bit 0 #;ot (ed in 94it o)eration'
8 Bit 1 #;ot (ed in 94it o)eration'
Bit 2 #;ot (ed in 94it o)eration'
10 Bit #;ot (ed in 94it o)eration'
11 Bit
12 Bit 5
1 Bit 6
1 Bit
15 Back9lig"t Anode#E'
16 Back9Lig"t Cat"ode#9'
8/17/2019 8051 Interfacing LCD 16x2
7/14
Interfacing LCD !it" 8051
LCD can 4e interfaced !it" t"e 8051 micrcontroller in t!o mode 8 4it and 4it. Let (
Interface it in 8 4it mode rt.
8 4it /ode
4ectie
g LCD di)la*
&here is lot of stuff that can %e done with the LCDs to start with we will simple display a couple of strin$s on
the ' lines of the LCD as shown in the ima$e.
%c"ematic Dicri)tion• Data ines In t"i mode all of t"e 8 dataline DB0 to DB are connected from
t"e microcontroller to a LCD mod(le a "o!n t"e c"ematic.
• Control ines1 7"e $% $+ and are control line a dic(ed earlier.
• o2er 3 contrast:A)art from t"at t"e LCD "o(ld 4e )o!ered !it" 5@
4et!een I #(CC) and I $(5nd). I " i t"e contrat )in and i o(t)(t of
center terminal of )otentiometer#oltage diider' !"ic" arie oltage 4et!een 0
to 5 to ar* t"e contrat.
• Bac6li5t 7"e I; 15 and 16 are (ed a 4acklig"t. 7"e led 4acklig"t can 4e
)o!ered t"ro(g" a im)le c(rrent limiting reitor a !e do !it" normal led.
8/17/2019 8051 Interfacing LCD 16x2
8/14
8
%c"ematic
Code
As with all the interfaces to simplify thin$ we have separated code into two files main.c and lcd;8;%it.c. ,ou
may $o throu$h the tools setup tutorial on confi$urin$ the code.
&he main.c is very simple it includes the standard li%rary files. &hen it uses several functions from the
lcd;8;%it.c file to set up and display messa$es. As you can see it ma-es thin$s very simple when the li%raries
are well written. 1e will discuss the implementation while discussin$ the lcd;8;%it.c file.
http://exploreembedded.com/wiki/8051_ToolsSetuphttp://exploreembedded.com/wiki/8051_ToolsSetuphttp://exploreembedded.com/wiki/8051_ToolsSetup
8/17/2019 8051 Interfacing LCD 16x2
9/14
The main fle main.c
/* Reg51.h contains the defnition of all ports and SFRs */
#include
#include "lcd.h" //Xplore labz LCD library#include "delay.h" //Xplore Labz Delay library
/* start the main program */
void main!
/* Initilize the lcd before displaying any thing on the lcd */
LCD$ni%!&
/* Display "hello !orld" on first line*/
LCDDi'play(%ring"hello) *orld"!&
/*o to second line and display "good morning" */
LCD+o,oLine,*o!&
LCDDi'play(%ring"good morning"!&
*hile1!&
-
lcd_8_bit.c: 8 bit lcd library fle
7"e lcd989bit:c conit of ario( f(nction t"at are reG(ired to initialiHe and (e t"eLCD. Let ( look at fe! im)ortant line and f(nction. Speci;
8/17/2019 8051 Interfacing LCD 16x2
10/14
10
are shown in the fi$ure
• te)1: %end t"e I command to LCD.
•
te)2: %elect t"e Control $egiter 4* making $% lo!.
• te): %elect +rite o)eration making $+ lo!.
• te): %end a -ig"9to9Lo! )(le on na4le I; !it" ome dela*(.
void LCDCmd6ri%e char cmd!
da%abu'3cmd& // Send the command to ()D
r'34& // Select the )ommand Register by plling RS (+%
r*34& // Select the %rite +peration by plling R% (+%
en31& // Send a ,igh-to-(o! #sle at &nable #in
delayu'14!&
en34&
delaym'1!&
-
8/17/2019 8051 Interfacing LCD 16x2
11/14
11
2' id LCD%Data*+ite( cha+ dat) &his function sends a character to %e displayed on LCD in the followin$
steps.
• te)1: %end t"e c"aracter to LCD.
• te)2: %elect t"e Data $egiter 4* making $% "ig".
• te): %elect +rite o)eration making $+ lo!.
• te): %end a -ig"9to9Lo! )(le on na4le I; !it" ome dela*(.
&he timin$s are similar as a%ove only chan$e is that ! is made hi$h.
void LCDDa%a6ri%e char da%!
da%abu'3da%& // Send the data to ()D
r'31& // Select the Data Register by plling RS ,I,
r*34& // Select the %rite +peration by plling R% (+%
en31& // Send a ,igh-to-(o! #sle at &nable #in
delayu'14!&
en34&
delaym'1!&
-
' id LCD%Init()
Loo-in$ at the instruction set of the LCD controller we can initiali
8/17/2019 8051 Interfacing LCD 16x2
12/14
12
7"e remaining code i lited 4elo!. Jo( co(ld o4ere t"at t"e 4aic f(nction to read
data and !rite !rite command are (ed exteniel* and entire li4rar* i 4(ilt ()on
t"em. 7"ere are alo ot"er f(nction t"at are (ed to di)la* n(m4er etc in t"e lcd
li4rar* le.
Kincl(dereg51."M Kincl(de Ndela*."N Kincl(de Nlcd."N Kdene data4( 2 LCD
data4( connected to $72 4it r& 0O0, $egiter elect )in connected to 0.0
4it r!& 0O1, $ead +rite )in connected to 0.1 4it en& 0O2, na4le )in
connected to 0.2 3 16x2 LCD %)ecication 3 Kdene LCD/axLine 2 Kdene
LCD/axC"ar 16 Kdene Linene 0x80 Kdene Line7!o 0xc0 Kdene Blank%)ace P P
oid LCDInit#' Q dela*(#5000', LCDCmd+rite#0x8', LCD 2line 53 matrix
LCDCmd+rite#0x0', Di)la* ; c(ror ; Blinking o LCDCmd+rite#0x01', Clear
t"e LCD LCDCmd+rite#0x80', C(ror to Firt line Firt oition R oid
LCDCmd+rite# c"ar cmd' Q data4(&cmd, %end t"e command to LCD r&0, %elect
Command $egiter 4* )(lling $% L+ r!&0, %elect +rite )eration 4* )(lling $+
L+ en&1, %end a -ig"9to9Lo! (le at na4le in dela*(#10', en&0,
dela*m#1', R oid LCDData+rite# c"ar dat' Q data4(&dat, %end t"e data to
LCD r&1, %elect t"e Data $egiter 4* )(lling $% -I>- r!&0, %elect t"e +rite
)eration 4* )(lling $+ L+ en&1, %end a -ig"9to9Lo! (le at na4le in
dela*(#10', en&0, dela*m#1', R oid LCD>o7oSJ#c"ar ro! c"ar col' Q c"ar )o,
if#ro!LCD/axLine' Q )o& Linene T #ro! 6', take t"e line n(m4er ro!09
M)o&0x80 ro!19M)o&0xc0 if#colLCD/axC"ar' )o& )oEcol, take t"e c"ar
n(m4er no! )o )oint to SJ )o LCDCmd+rite#)o' R R oid
LCDDi)la*%tring#c"ar 3tring)tr' Q !"ile#3tring)tr' LCDData+rite#3tring)trE
E', R
4it /ode
%c"ematic
&here are followin$ differences in 7 %it mode.
• nl* data line D to D are (ed a "o!n in t"e c"ematic 4elo!.
• In code !e need to end t"e command to elect 4it mode a "o!n in t"e
intr(ction et a4oe.
8/17/2019 8051 Interfacing LCD 16x2
13/14
1
&he main pro$ram remains exactly as in 8 %it mode we simply include the $cd%4%&it'c to wor- in 7 %it mode.
&he important and required code is listed %elow
CodeKincl(dereg51."M Kincl(de Ndela*."N Kincl(de Nlcd."N Kdene data4( 0 LCD
data4( connected to $70 4it r& data4(O0, $egiter elect )in connected to
0.0 4it r!& data4(O1, $ead +rite )in connected to 0.1 4it en& data4(O2,
na4le )in connected to 0.2 3 16x2 LCD %)ecication 3 Kdene LCD/axLine 2
Kdene LCD/axC"ar 16 Kdene Linene 0x80 Kdene Line7!o 0xc0 Kdene
Blank%)ace P P oid LCDInit#' Q dela*(#5000', LCDCmd+rite#0x02', InitiliHe t"e LCD
8/17/2019 8051 Interfacing LCD 16x2
14/14
1
in 4it /ode LCDCmd+rite#0x28', LCDCmd+rite#0x0', Di)la* ; c(ror ;
LCDCmd+rite#0x01', Clear t"e LCD LCDCmd+rite#0x80', C(ror to Firt line Firt
oition R oid LCDCmd+rite# c"ar cmd' Q data4(cmd = 0xf0', %end t"e
-ig"er ;i44le r&0, %elect t"e Command $egiter 4* )(lling $% L+ r!&0, %elect
t"e +rite )eration 4* )(lling $+ L+ en&1, %end a -ig"9to9Lo! (le at na4le in
dela*(#1', en&0, dela*(#10', !ait for ome time data4(#cmd' =0xf0', %end t"e Lo!er ;i44le r&0, %elect t"e Command $egiter 4* )(lling $% L+
r!&0, %elect t"e +rite )eration 4* )(lling $+ L+ en&1, %end a -ig"9to9Lo!
(le at na4le in dela*(#1', en&0, dela*m#1', R oid LCDData+rite# c"ar
dat' Q data4(dat = 0xf0', %end t"e -ig"er ;i44le r&1, %elect t"e Data $egiter
4* )(lling $% -I>- r!&0, %elect t"e +rite )eration 4* )(lling $+ L+ en&1, %end
a -ig"9to9Lo! (le at na4le in dela*(#1', en&0, dela*(#10', !ait for ome
time. data4(#dat ' = 0xf0', %end t"e Lo!er ;i44le r&1, %elect t"e Data
$egiter 4* )(lling $% -I>- r!&0, %elect t"e +rite )eration 4* )(lling $+ L+
en&1, %end a -ig"9to9Lo! (le at na4le in dela*(#1', en&0, dela*m#1', R
oid LCDDi)la*%tring#c"ar 3tring)tr' Q !"ile#3tring)tr'
LCDData+rite#3tring)trEE', R oid LCD>o7oLine7!o#' Q LCDCmd+rite#Line7!o', /oe t"e C(ror to %econd line R
Downloads=&he code is $iven for 7 and 8 %it modes with and without %usy >la$=
8051ILCDI7(torialICode.Hi)