SlideShare a Scribd company logo
1 of 10
Download to read offline
1
8051 Experiment
16x2 Line LCD with 89C51
Objective
To interface parallel 16 X 2 Characters LCD using 4 bit mode (6 I/O pins) with 89C2051 micro-
controller.
Liquid crystal displays commonly come in one, two, or four line versions. The length of line is typically
sixteen or twenty characters. Virtually all models are based on a common design with the same mi-
crocontroller driver. As a result, the pin-outs are identical. A few models simply renumber the pins
from the opposite direction.
The instruction set to all liquid crystal displays is common between manufacturers. The standard
character set is built into the onboard processor. Graphics can be created for the display, but these
typically require bit mapping.
Because of the control sequence, the LCD appears somewhat tedious. There are three groups of
routines.
LCD control lines. LCD commands: initialize, instruction, data, and busy. LCD communicate: display
message. The LCD has fourteen pins for connecting power, controls, and data. Connect contrast to
ground for maximum viewing intensity.
The liquid crystal display has three control lines -
Enable (pin 6)
Read/Write (pin5), and
Register select (pin 7).
It is wise to have the enable line asserted low before the other control lines are asserted. The regis-
ter select and the read/write are activated by software control. The commands are executed on the
low to high transition of the enable line. The high also allows data to be set-up. Data is transferred
with the enable line high.
Display of information requires initiating a control sequence. First the enable line is pulled low. Then
the control operation is implemented. Then the enable line is pulled high. Then the data is sent.
Then the enable line is pulled low. The timing diagram illustrates the relationship between the lines.
2
8051 Experiment
Circuit schematic
Command Control Codes
main.c
1
2 #include <REGX52.H>
3 static void write_cmde (unsigned char cmde);
4 static char check_busy_flag (void);
5 static void write_nibble (unsigned char nibble);
6 static void putlcd (unsigned char disp_data);
7 void put_char (char c) ;
8 void lcd_init(void);
9 void lcd_clr(void);
10 void cursor_off (void);
11 void cursor_on (void);
12 void delay_ms(unsigned int interval);
13 void set_cursor (char rownr, char colnr);
14 void put_string_lcd(char* dat);
15 void clean_lcd(void);
16
17 void main(void)
18 {
19 lcd_init();
20 lcd_clr();
21 delay_ms(50);
22 cursor_on();
23 //cursor_off();
24 set_cursor(0,0);
25 lcd_clr();
26 set_cursor(0,0);
27 put_string_lcd(" 16x2 Char LCD");
28 set_cursor(1,0);
29 put_string_lcd(" Cubix Kits ");
30 delay_ms(1000);
31 set_cursor(1,0);
32 put_string_lcd(" and Projects");
33 clean_lcd();
34 while(1)
35 {
36 }
37 }
delay.c
1 //General delay
2 void delay(unsigned int interval)
3 {
4 while(interval){interval--;}
5 }
6
7
8 //milli second delay @12MHz Crystal
9 void delay_ms(unsigned int interval)
10 {
11 while(interval)
12 {
13 delay(124);
14 interval--;
15 }
16 }
LCD.C
1 #include<P89V51Rx2.H>
2 static void write_cmde (unsigned char cmde);
3 static char check_busy_flag (void);
4 static void write_nibble (unsigned char nibble);
5 static void putlcd (unsigned char disp_data);
6 void put_char (char c) ;
7
8 #define LCD_PORT P1 /* LCD port definition */
9 sbit RS_PORT = P1^6;
10 sbit RW_PORT = P1^5;
11 sbit E_PORT = P1^4;
12
13 #define LCD_DAT_MSK 0x0F /* data bus mask */
14 #define BF_BIT 0x08 /* busy flag mask */
15
16 #define NB_ROW 0x04 /* 4 display lines */
17 #define NB_COL 0x10 /* 16 characters per line */
18
19 /*
20 Controller commands
21 */
22 #define ROW0 0x80 /* row 0 command */
23 #define ROW1 0xC0 /* row 1 command */
24 #define ROW2 0x90 /* row 2 command */
25 #define ROW3 0xD0 /* row 3 command */
26 #define CG_RAM 0x40 /* character graphic RAM command */
27 #define DD_RAM 0x80 /* data display RAM command */
28 #define CLR_LCD 0x01 /* clear LCD command */
29 #define CUR_ON 0x0F /* cursor ON command */
30 #define CUR_OFF 0x0C /* cursor OFF command */
31
32
33
34 #define C_R 0x0D
35 #define L_F 0x0A
36 #define B_S 0x08
37
38 /*
39 ** Definitions
40 */
41 static int row=0, col=0; /* save the current cursor position */
42
43 /*
44 ** ---------------------------------------------------------------------------
45 ** lcd_init - LCD controller initialization and configuration routine
46 ** ---------------------------------------------------------------------------
47 ** Inputs:
48 **
49 ** Outputs:
50 **
51 ** ---------------------------------------------------------------------------
52 ** Comments: This function is called once to initialize the LCD display.
53 ** The HD44780 controller contains 8 RAM loactions where user
54 ** characters can be defined.
55 ** Four of those locations are used in the example to define
56 ** the local symbols å, Ö, Ä and Å used from the function lcd_preter
57 ** ---------------------------------------------------------------------------
58 */
59 void lcd_init (void) {
60 char i;
61 static const char code initdata[] = {0x30, 0x30, 0X30,0X20, 0x28, 0x04, 0x06, 0x01};
62
63 for (i = 0; i < sizeof initdata; ++i) {
64 write_cmde(initdata[i]); /* write init data to the controller */
65
66 }
67 }
68
69 /** ---------------------------------------------------------------------------
70 ** set_cursor - set the cursor position
71 ** ---------------------------------------------------------------------------
LCD.C
72 ** Inputs: row and column position
73 **
74 ** Outputs:
75 **
76 ** ---------------------------------------------------------------------------
77 ** Comments: Rows and columns start from 0 (zero-based)
78 ** ---------------------------------------------------------------------------
79 */
80 void set_cursor (char rownr, char colnr) {
81 int cmd;
82 rownr= rownr%NB_ROW; /* make sure position is in limits */
83 colnr= colnr%NB_COL; /* make sure position is in limits */
84 switch (rownr) {
85 case 0:
86 cmd = ROW0;
87 break;
88 case 1:
89 cmd = ROW1;
90 break;
91 case 2:
92 cmd = ROW2;
93 break;
94 case 3:
95 cmd = ROW3;
96 break;
97 }
98 write_cmde(cmd | colnr); /* write the new cursor position */
99 row = rownr; /* update the global row position */
100 col = colnr; /* update the global column position */
101 }
102
103 /*
104 ** ---------------------------------------------------------------------------
105 ** lcd_clr - clear display and set cusor to home
106 ** ---------------------------------------------------------------------------
107 ** Inputs:
108 **
109 ** Outputs:
110 **
111 ** ---------------------------------------------------------------------------
112 ** Comments:
113 ** ---------------------------------------------------------------------------
114 */
115 void lcd_clr(void) {
116 write_cmde(CLR_LCD);
117 }
118
119 /*
120 ** ---------------------------------------------------------------------------
121 ** cursor_on - enable cursor
122 ** cursor_off - disable cursor
123 ** ---------------------------------------------------------------------------
124 ** Inputs:
125 **
126 ** Outputs:
127 **
128 ** ---------------------------------------------------------------------------
129 ** Comments:
130 ** ---------------------------------------------------------------------------
131 */
132 void cursor_on (void) {
133 write_cmde(CUR_ON);
134 }
135
136 void cursor_off (void) {
137 write_cmde(CUR_OFF);
138 }
139
140
141 /** ---------------------------------------------------------------------------
142 ** write_cmde - write a command byte to the display
LCD.C
143 ** ---------------------------------------------------------------------------
144 ** Inputs: command byte to write
145 **
146 ** Outputs:
147 **
148 ** ---------------------------------------------------------------------------
149 ** Comments:
150 ** ---------------------------------------------------------------------------
151 */
152 static void write_cmde (unsigned char cmde) {
153 while (check_busy_flag()); /* wait if the display is busy */
154 RS_PORT = 0; /* select command register */
155 write_nibble(cmde >> 4); /* write the high nibble */
156 write_nibble(cmde); /* write the low nibble */
157 }
158
159 /*
160 ** ---------------------------------------------------------------------------
161 ** write_data - write a data byte to the display
162 ** ---------------------------------------------------------------------------
163 ** Inputs: data byte to write
164 **
165 ** Outputs:
166 **
167 ** ---------------------------------------------------------------------------
168 ** Comments:
169 ** ---------------------------------------------------------------------------
170 */
171 static void write_data (unsigned char byte) {
172 while (check_busy_flag()); /* wait if the display is busy */
173 RS_PORT = 1; /* select data register */
174 write_nibble(byte >> 4); /* write the high nibble */
175 write_nibble(byte); /* write the low nibble */
176 }
177
178 /*
179 ** ---------------------------------------------------------------------------
180 ** check_busy_flag - check the display busy flag
181 ** ---------------------------------------------------------------------------
182 ** Inputs:
183 **
184 ** Outputs:
185 **
186 ** ---------------------------------------------------------------------------
187 ** Comments: invoked from write_cmde and write_data
188 ** ---------------------------------------------------------------------------
189 */
190 static char check_busy_flag (void) {
191 char bf;
192 LCD_PORT |= LCD_DAT_MSK; /* data bus high */
193 RS_PORT = 0; /* select command register */
194 RW_PORT = 1; /* read access */
195 E_PORT = 1; /* Set E high */
196 bf = LCD_PORT & BF_BIT; /* read busy flag */
197 E_PORT = 0; /* set E low */
198 E_PORT = 1; /* second read due to 4 bit interface */
199 E_PORT = 0;
200 return (bf); /* return busy flag */
201 }
202
203 /*
204 ** ---------------------------------------------------------------------------
205 ** write_nibble - write a 4-bit nibble to the display
206 ** ---------------------------------------------------------------------------
207 ** Inputs: nibble to write
208 **
209 ** Outputs:
210 **
211 ** ---------------------------------------------------------------------------
212 ** Comments:
213 ** ---------------------------------------------------------------------------
LCD.C
214 */
215 static void write_nibble (unsigned char nibble)
216 {
217 LCD_PORT &= ~LCD_DAT_MSK; /* data bus low */
218 RW_PORT = 0; /* write access */
219 nibble &= LCD_DAT_MSK; /* clear high nibble */
220 LCD_PORT |= nibble; /* write the nibble */
221 E_PORT = 1; /* set E high */
222 E_PORT = 0; /* set E low */
223 }
224
225 /*
226 ** ---------------------------------------------------------------------------
227 ** lcd_preter - lcd display interpreter
228 ** ---------------------------------------------------------------------------
229 ** Inputs:
230 **
231 ** Outputs:
232 **
233 ** ---------------------------------------------------------------------------
234 ** Comments: invoked from putchar
235 ** ---------------------------------------------------------------------------
236 */
237
238 void lcd_preter (char c) {
239 switch (c) {
240 case C_R: /* Carriage Return */
241 set_cursor(row, 0);
242 break;
243 case L_F: /* Line Feed */
244 row = (row+1)%NB_ROW;
245 set_cursor(row, 0);
246 break;
247 case B_S: /* Back Space */
248 if (col != 0) {
249 set_cursor(row, --col);
250 putlcd(' ');
251 set_cursor(row, --col);
252 }
253 break;
254 case 'ö':
255 putlcd(0xef); /* display special character */
256 break;
257 case 'ä':
258 putlcd(0xe1); /* display special character */
259 break;
260 case 'å':
261 putlcd(0); /* display special char from display RAM */
262 break;
263 case 'Ö':
264 putlcd(1); /* display special char from display RAM */
265 break;
266 case 'Ä':
267 putlcd(2); /* display special char from display RAM */
268 break;
269 case 'Å':
270 putlcd(3); /* display special char from display RAM */
271 break;
272 default:
273 if (c>0x1f && c<0x80) { /* check if it's a valid character */
274 putlcd(c); /* write the character to the display */
275 }
276 break;
277 }
278 }
279
280 /*
281 ** ---------------------------------------------------------------------------
282 ** putlcd - write one character to the display
283 ** ---------------------------------------------------------------------------
284 ** Inputs: data to display
LCD.C
285 **
286 ** Outputs:
287 **
288 ** ---------------------------------------------------------------------------
289 ** Comments: invoked from lcd_preter
290 ** ---------------------------------------------------------------------------
291 */
292
293 static void putlcd (unsigned char disp_data) {
294 if (col<NB_COL && col>=0) { /* check if we are inside the limits */
295 write_data(disp_data); /* write the character to the display */
296 col++; /* update the column position */
297 }
298 }
299 //====================================================================================================
=======
300
301
302 void put_string_lcd(char* dat)
303 {
304 int j=0;
305
306
307 while( dat[j] != 0) // send string
308 {
309 put_char(dat[j]); // start sending one byte
310 j++;
311 }
312 }
313
314 unsigned char hextoascii(unsigned char value)
315 {
316 idata unsigned char add;
317 if (value>9)
318 {
319 add=0x37;
320 }
321 else
322 add=0x30;
323
324 return (value + add );
325 }
326
327
328
329 void put_char (char c)
330 {
331 lcd_preter(c); /* write the character to the display */
332 }
333
334 void clean_lcd(void)
335 {
336 set_cursor(0,0);
337 put_string_lcd(" ");
338 set_cursor(1,0);
339 put_string_lcd(" ");
340 }
3
8051 Experiment

More Related Content

What's hot

Advanced motion controls mc1xdzr02 hp1
Advanced motion controls mc1xdzr02 hp1Advanced motion controls mc1xdzr02 hp1
Advanced motion controls mc1xdzr02 hp1Electromate
 
Introduction to Vortex86EX Motion Control Modules
Introduction to Vortex86EX Motion Control ModulesIntroduction to Vortex86EX Motion Control Modules
Introduction to Vortex86EX Motion Control Modulesroboard
 
Configure ospf v3 single areaa
Configure ospf v3 single areaaConfigure ospf v3 single areaa
Configure ospf v3 single areaajebong03
 
Advanced motion controls mc1xdzc02 hp1
Advanced motion controls mc1xdzc02 hp1Advanced motion controls mc1xdzc02 hp1
Advanced motion controls mc1xdzc02 hp1Electromate
 
Lathe Spindle Sensor
Lathe Spindle SensorLathe Spindle Sensor
Lathe Spindle SensorJoeCritt
 
Advanced motion controls dzxralte 015l080
Advanced motion controls dzxralte 015l080Advanced motion controls dzxralte 015l080
Advanced motion controls dzxralte 015l080Electromate
 
VGA VHDL RTL design tutorial
VGA  VHDL   RTL design tutorialVGA  VHDL   RTL design tutorial
VGA VHDL RTL design tutorialNabil Chouba
 
伺服馬達控制
伺服馬達控制伺服馬達控制
伺服馬達控制艾鍗科技
 
Advanced motion controls dzxralte 008l080
Advanced motion controls dzxralte 008l080Advanced motion controls dzxralte 008l080
Advanced motion controls dzxralte 008l080Electromate
 
konfig routing paling cepat
konfig routing paling cepatkonfig routing paling cepat
konfig routing paling cepatBelajar Konfig
 
Advanced motion controls dzxcante 040l080
Advanced motion controls dzxcante 040l080Advanced motion controls dzxcante 040l080
Advanced motion controls dzxcante 040l080Electromate
 
Advanced motion controls dzxralte 040l080
Advanced motion controls dzxralte 040l080Advanced motion controls dzxralte 040l080
Advanced motion controls dzxralte 040l080Electromate
 
Advanced motion controls dzxcante 015l080
Advanced motion controls dzxcante 015l080Advanced motion controls dzxcante 015l080
Advanced motion controls dzxcante 015l080Electromate
 
Cataloge ge 3.control and_automation_dienhathe.com-4_20_vat300_e_c6-6-4_1_rev_b
Cataloge ge 3.control and_automation_dienhathe.com-4_20_vat300_e_c6-6-4_1_rev_bCataloge ge 3.control and_automation_dienhathe.com-4_20_vat300_e_c6-6-4_1_rev_b
Cataloge ge 3.control and_automation_dienhathe.com-4_20_vat300_e_c6-6-4_1_rev_bDien Ha The
 

What's hot (19)

Advanced motion controls mc1xdzr02 hp1
Advanced motion controls mc1xdzr02 hp1Advanced motion controls mc1xdzr02 hp1
Advanced motion controls mc1xdzr02 hp1
 
P sim.val
P sim.valP sim.val
P sim.val
 
Introduction to Vortex86EX Motion Control Modules
Introduction to Vortex86EX Motion Control ModulesIntroduction to Vortex86EX Motion Control Modules
Introduction to Vortex86EX Motion Control Modules
 
Configure ospf v3 single areaa
Configure ospf v3 single areaaConfigure ospf v3 single areaa
Configure ospf v3 single areaa
 
Advanced motion controls mc1xdzc02 hp1
Advanced motion controls mc1xdzc02 hp1Advanced motion controls mc1xdzc02 hp1
Advanced motion controls mc1xdzc02 hp1
 
Lathe Spindle Sensor
Lathe Spindle SensorLathe Spindle Sensor
Lathe Spindle Sensor
 
Day 5.3 routercomponents
Day 5.3 routercomponentsDay 5.3 routercomponents
Day 5.3 routercomponents
 
Advanced motion controls dzxralte 015l080
Advanced motion controls dzxralte 015l080Advanced motion controls dzxralte 015l080
Advanced motion controls dzxralte 015l080
 
VGA VHDL RTL design tutorial
VGA  VHDL   RTL design tutorialVGA  VHDL   RTL design tutorial
VGA VHDL RTL design tutorial
 
伺服馬達控制
伺服馬達控制伺服馬達控制
伺服馬達控制
 
Advanced motion controls dzxralte 008l080
Advanced motion controls dzxralte 008l080Advanced motion controls dzxralte 008l080
Advanced motion controls dzxralte 008l080
 
konfig routing paling cepat
konfig routing paling cepatkonfig routing paling cepat
konfig routing paling cepat
 
Advanced motion controls dzxcante 040l080
Advanced motion controls dzxcante 040l080Advanced motion controls dzxcante 040l080
Advanced motion controls dzxcante 040l080
 
MK1_Addendum
MK1_AddendumMK1_Addendum
MK1_Addendum
 
Advanced motion controls dzxralte 040l080
Advanced motion controls dzxralte 040l080Advanced motion controls dzxralte 040l080
Advanced motion controls dzxralte 040l080
 
OSPF 3
OSPF 3OSPF 3
OSPF 3
 
Rota 1
Rota 1Rota 1
Rota 1
 
Advanced motion controls dzxcante 015l080
Advanced motion controls dzxcante 015l080Advanced motion controls dzxcante 015l080
Advanced motion controls dzxcante 015l080
 
Cataloge ge 3.control and_automation_dienhathe.com-4_20_vat300_e_c6-6-4_1_rev_b
Cataloge ge 3.control and_automation_dienhathe.com-4_20_vat300_e_c6-6-4_1_rev_bCataloge ge 3.control and_automation_dienhathe.com-4_20_vat300_e_c6-6-4_1_rev_b
Cataloge ge 3.control and_automation_dienhathe.com-4_20_vat300_e_c6-6-4_1_rev_b
 

Similar to 16x2 LCD with 89C51 Microcontroller

Functions for Nano 5 Card
Functions for Nano 5 CardFunctions for Nano 5 Card
Functions for Nano 5 CardOmar Sanchez
 
Dam gate open close lpc prog
Dam gate open close lpc progDam gate open close lpc prog
Dam gate open close lpc prognikhil dixit
 
MicrocontrollersII (1).pptx
MicrocontrollersII (1).pptxMicrocontrollersII (1).pptx
MicrocontrollersII (1).pptxnodov66591
 
Kernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesKernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesAnne Nicolas
 
Real Time Clock Interfacing with FPGA
Real Time Clock Interfacing with FPGAReal Time Clock Interfacing with FPGA
Real Time Clock Interfacing with FPGAMafaz Ahmed
 
Wan Interface Configuration
Wan Interface ConfigurationWan Interface Configuration
Wan Interface ConfigurationKishore Kumar
 
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
DEF CON 23 - Rodringo Almeida - embedded system design from electronics DEF CON 23 - Rodringo Almeida - embedded system design from electronics
DEF CON 23 - Rodringo Almeida - embedded system design from electronics Felipe Prado
 
Microcontrollers ii
Microcontrollers iiMicrocontrollers ii
Microcontrollers iiKumar Kumar
 
OpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersOpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersConnor McDonald
 
Embedded systems design @ defcon 2015
Embedded systems design @ defcon 2015Embedded systems design @ defcon 2015
Embedded systems design @ defcon 2015Rodrigo Almeida
 
What will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdfWhat will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdfSIGMATAX1
 

Similar to 16x2 LCD with 89C51 Microcontroller (20)

LCD_Example.pptx
LCD_Example.pptxLCD_Example.pptx
LCD_Example.pptx
 
131080111003 mci
131080111003 mci131080111003 mci
131080111003 mci
 
report cs
report csreport cs
report cs
 
Functions for Nano 5 Card
Functions for Nano 5 CardFunctions for Nano 5 Card
Functions for Nano 5 Card
 
Dam gate open close lpc prog
Dam gate open close lpc progDam gate open close lpc prog
Dam gate open close lpc prog
 
Intel Quark HSUART
Intel Quark HSUARTIntel Quark HSUART
Intel Quark HSUART
 
Lcd n PIC16F
Lcd n PIC16FLcd n PIC16F
Lcd n PIC16F
 
chapter 4
chapter 4chapter 4
chapter 4
 
MicrocontrollersII (1).pptx
MicrocontrollersII (1).pptxMicrocontrollersII (1).pptx
MicrocontrollersII (1).pptx
 
8051 MC.pptx
8051 MC.pptx8051 MC.pptx
8051 MC.pptx
 
Kernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesKernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering Oopsies
 
Lcd & keypad
Lcd & keypadLcd & keypad
Lcd & keypad
 
Real Time Clock Interfacing with FPGA
Real Time Clock Interfacing with FPGAReal Time Clock Interfacing with FPGA
Real Time Clock Interfacing with FPGA
 
Wan Interface Configuration
Wan Interface ConfigurationWan Interface Configuration
Wan Interface Configuration
 
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
DEF CON 23 - Rodringo Almeida - embedded system design from electronics DEF CON 23 - Rodringo Almeida - embedded system design from electronics
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
 
Microcontrollers ii
Microcontrollers iiMicrocontrollers ii
Microcontrollers ii
 
OpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersOpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developers
 
Basic standard calculator
Basic standard calculatorBasic standard calculator
Basic standard calculator
 
Embedded systems design @ defcon 2015
Embedded systems design @ defcon 2015Embedded systems design @ defcon 2015
Embedded systems design @ defcon 2015
 
What will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdfWhat will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdf
 

Recently uploaded

GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 

Recently uploaded (20)

GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 

16x2 LCD with 89C51 Microcontroller

  • 1. 1 8051 Experiment 16x2 Line LCD with 89C51 Objective To interface parallel 16 X 2 Characters LCD using 4 bit mode (6 I/O pins) with 89C2051 micro- controller. Liquid crystal displays commonly come in one, two, or four line versions. The length of line is typically sixteen or twenty characters. Virtually all models are based on a common design with the same mi- crocontroller driver. As a result, the pin-outs are identical. A few models simply renumber the pins from the opposite direction. The instruction set to all liquid crystal displays is common between manufacturers. The standard character set is built into the onboard processor. Graphics can be created for the display, but these typically require bit mapping. Because of the control sequence, the LCD appears somewhat tedious. There are three groups of routines. LCD control lines. LCD commands: initialize, instruction, data, and busy. LCD communicate: display message. The LCD has fourteen pins for connecting power, controls, and data. Connect contrast to ground for maximum viewing intensity. The liquid crystal display has three control lines - Enable (pin 6) Read/Write (pin5), and Register select (pin 7). It is wise to have the enable line asserted low before the other control lines are asserted. The regis- ter select and the read/write are activated by software control. The commands are executed on the low to high transition of the enable line. The high also allows data to be set-up. Data is transferred with the enable line high. Display of information requires initiating a control sequence. First the enable line is pulled low. Then the control operation is implemented. Then the enable line is pulled high. Then the data is sent. Then the enable line is pulled low. The timing diagram illustrates the relationship between the lines.
  • 3. main.c 1 2 #include <REGX52.H> 3 static void write_cmde (unsigned char cmde); 4 static char check_busy_flag (void); 5 static void write_nibble (unsigned char nibble); 6 static void putlcd (unsigned char disp_data); 7 void put_char (char c) ; 8 void lcd_init(void); 9 void lcd_clr(void); 10 void cursor_off (void); 11 void cursor_on (void); 12 void delay_ms(unsigned int interval); 13 void set_cursor (char rownr, char colnr); 14 void put_string_lcd(char* dat); 15 void clean_lcd(void); 16 17 void main(void) 18 { 19 lcd_init(); 20 lcd_clr(); 21 delay_ms(50); 22 cursor_on(); 23 //cursor_off(); 24 set_cursor(0,0); 25 lcd_clr(); 26 set_cursor(0,0); 27 put_string_lcd(" 16x2 Char LCD"); 28 set_cursor(1,0); 29 put_string_lcd(" Cubix Kits "); 30 delay_ms(1000); 31 set_cursor(1,0); 32 put_string_lcd(" and Projects"); 33 clean_lcd(); 34 while(1) 35 { 36 } 37 }
  • 4. delay.c 1 //General delay 2 void delay(unsigned int interval) 3 { 4 while(interval){interval--;} 5 } 6 7 8 //milli second delay @12MHz Crystal 9 void delay_ms(unsigned int interval) 10 { 11 while(interval) 12 { 13 delay(124); 14 interval--; 15 } 16 }
  • 5. LCD.C 1 #include<P89V51Rx2.H> 2 static void write_cmde (unsigned char cmde); 3 static char check_busy_flag (void); 4 static void write_nibble (unsigned char nibble); 5 static void putlcd (unsigned char disp_data); 6 void put_char (char c) ; 7 8 #define LCD_PORT P1 /* LCD port definition */ 9 sbit RS_PORT = P1^6; 10 sbit RW_PORT = P1^5; 11 sbit E_PORT = P1^4; 12 13 #define LCD_DAT_MSK 0x0F /* data bus mask */ 14 #define BF_BIT 0x08 /* busy flag mask */ 15 16 #define NB_ROW 0x04 /* 4 display lines */ 17 #define NB_COL 0x10 /* 16 characters per line */ 18 19 /* 20 Controller commands 21 */ 22 #define ROW0 0x80 /* row 0 command */ 23 #define ROW1 0xC0 /* row 1 command */ 24 #define ROW2 0x90 /* row 2 command */ 25 #define ROW3 0xD0 /* row 3 command */ 26 #define CG_RAM 0x40 /* character graphic RAM command */ 27 #define DD_RAM 0x80 /* data display RAM command */ 28 #define CLR_LCD 0x01 /* clear LCD command */ 29 #define CUR_ON 0x0F /* cursor ON command */ 30 #define CUR_OFF 0x0C /* cursor OFF command */ 31 32 33 34 #define C_R 0x0D 35 #define L_F 0x0A 36 #define B_S 0x08 37 38 /* 39 ** Definitions 40 */ 41 static int row=0, col=0; /* save the current cursor position */ 42 43 /* 44 ** --------------------------------------------------------------------------- 45 ** lcd_init - LCD controller initialization and configuration routine 46 ** --------------------------------------------------------------------------- 47 ** Inputs: 48 ** 49 ** Outputs: 50 ** 51 ** --------------------------------------------------------------------------- 52 ** Comments: This function is called once to initialize the LCD display. 53 ** The HD44780 controller contains 8 RAM loactions where user 54 ** characters can be defined. 55 ** Four of those locations are used in the example to define 56 ** the local symbols å, Ö, Ä and Å used from the function lcd_preter 57 ** --------------------------------------------------------------------------- 58 */ 59 void lcd_init (void) { 60 char i; 61 static const char code initdata[] = {0x30, 0x30, 0X30,0X20, 0x28, 0x04, 0x06, 0x01}; 62 63 for (i = 0; i < sizeof initdata; ++i) { 64 write_cmde(initdata[i]); /* write init data to the controller */ 65 66 } 67 } 68 69 /** --------------------------------------------------------------------------- 70 ** set_cursor - set the cursor position 71 ** ---------------------------------------------------------------------------
  • 6. LCD.C 72 ** Inputs: row and column position 73 ** 74 ** Outputs: 75 ** 76 ** --------------------------------------------------------------------------- 77 ** Comments: Rows and columns start from 0 (zero-based) 78 ** --------------------------------------------------------------------------- 79 */ 80 void set_cursor (char rownr, char colnr) { 81 int cmd; 82 rownr= rownr%NB_ROW; /* make sure position is in limits */ 83 colnr= colnr%NB_COL; /* make sure position is in limits */ 84 switch (rownr) { 85 case 0: 86 cmd = ROW0; 87 break; 88 case 1: 89 cmd = ROW1; 90 break; 91 case 2: 92 cmd = ROW2; 93 break; 94 case 3: 95 cmd = ROW3; 96 break; 97 } 98 write_cmde(cmd | colnr); /* write the new cursor position */ 99 row = rownr; /* update the global row position */ 100 col = colnr; /* update the global column position */ 101 } 102 103 /* 104 ** --------------------------------------------------------------------------- 105 ** lcd_clr - clear display and set cusor to home 106 ** --------------------------------------------------------------------------- 107 ** Inputs: 108 ** 109 ** Outputs: 110 ** 111 ** --------------------------------------------------------------------------- 112 ** Comments: 113 ** --------------------------------------------------------------------------- 114 */ 115 void lcd_clr(void) { 116 write_cmde(CLR_LCD); 117 } 118 119 /* 120 ** --------------------------------------------------------------------------- 121 ** cursor_on - enable cursor 122 ** cursor_off - disable cursor 123 ** --------------------------------------------------------------------------- 124 ** Inputs: 125 ** 126 ** Outputs: 127 ** 128 ** --------------------------------------------------------------------------- 129 ** Comments: 130 ** --------------------------------------------------------------------------- 131 */ 132 void cursor_on (void) { 133 write_cmde(CUR_ON); 134 } 135 136 void cursor_off (void) { 137 write_cmde(CUR_OFF); 138 } 139 140 141 /** --------------------------------------------------------------------------- 142 ** write_cmde - write a command byte to the display
  • 7. LCD.C 143 ** --------------------------------------------------------------------------- 144 ** Inputs: command byte to write 145 ** 146 ** Outputs: 147 ** 148 ** --------------------------------------------------------------------------- 149 ** Comments: 150 ** --------------------------------------------------------------------------- 151 */ 152 static void write_cmde (unsigned char cmde) { 153 while (check_busy_flag()); /* wait if the display is busy */ 154 RS_PORT = 0; /* select command register */ 155 write_nibble(cmde >> 4); /* write the high nibble */ 156 write_nibble(cmde); /* write the low nibble */ 157 } 158 159 /* 160 ** --------------------------------------------------------------------------- 161 ** write_data - write a data byte to the display 162 ** --------------------------------------------------------------------------- 163 ** Inputs: data byte to write 164 ** 165 ** Outputs: 166 ** 167 ** --------------------------------------------------------------------------- 168 ** Comments: 169 ** --------------------------------------------------------------------------- 170 */ 171 static void write_data (unsigned char byte) { 172 while (check_busy_flag()); /* wait if the display is busy */ 173 RS_PORT = 1; /* select data register */ 174 write_nibble(byte >> 4); /* write the high nibble */ 175 write_nibble(byte); /* write the low nibble */ 176 } 177 178 /* 179 ** --------------------------------------------------------------------------- 180 ** check_busy_flag - check the display busy flag 181 ** --------------------------------------------------------------------------- 182 ** Inputs: 183 ** 184 ** Outputs: 185 ** 186 ** --------------------------------------------------------------------------- 187 ** Comments: invoked from write_cmde and write_data 188 ** --------------------------------------------------------------------------- 189 */ 190 static char check_busy_flag (void) { 191 char bf; 192 LCD_PORT |= LCD_DAT_MSK; /* data bus high */ 193 RS_PORT = 0; /* select command register */ 194 RW_PORT = 1; /* read access */ 195 E_PORT = 1; /* Set E high */ 196 bf = LCD_PORT & BF_BIT; /* read busy flag */ 197 E_PORT = 0; /* set E low */ 198 E_PORT = 1; /* second read due to 4 bit interface */ 199 E_PORT = 0; 200 return (bf); /* return busy flag */ 201 } 202 203 /* 204 ** --------------------------------------------------------------------------- 205 ** write_nibble - write a 4-bit nibble to the display 206 ** --------------------------------------------------------------------------- 207 ** Inputs: nibble to write 208 ** 209 ** Outputs: 210 ** 211 ** --------------------------------------------------------------------------- 212 ** Comments: 213 ** ---------------------------------------------------------------------------
  • 8. LCD.C 214 */ 215 static void write_nibble (unsigned char nibble) 216 { 217 LCD_PORT &= ~LCD_DAT_MSK; /* data bus low */ 218 RW_PORT = 0; /* write access */ 219 nibble &= LCD_DAT_MSK; /* clear high nibble */ 220 LCD_PORT |= nibble; /* write the nibble */ 221 E_PORT = 1; /* set E high */ 222 E_PORT = 0; /* set E low */ 223 } 224 225 /* 226 ** --------------------------------------------------------------------------- 227 ** lcd_preter - lcd display interpreter 228 ** --------------------------------------------------------------------------- 229 ** Inputs: 230 ** 231 ** Outputs: 232 ** 233 ** --------------------------------------------------------------------------- 234 ** Comments: invoked from putchar 235 ** --------------------------------------------------------------------------- 236 */ 237 238 void lcd_preter (char c) { 239 switch (c) { 240 case C_R: /* Carriage Return */ 241 set_cursor(row, 0); 242 break; 243 case L_F: /* Line Feed */ 244 row = (row+1)%NB_ROW; 245 set_cursor(row, 0); 246 break; 247 case B_S: /* Back Space */ 248 if (col != 0) { 249 set_cursor(row, --col); 250 putlcd(' '); 251 set_cursor(row, --col); 252 } 253 break; 254 case 'ö': 255 putlcd(0xef); /* display special character */ 256 break; 257 case 'ä': 258 putlcd(0xe1); /* display special character */ 259 break; 260 case 'å': 261 putlcd(0); /* display special char from display RAM */ 262 break; 263 case 'Ö': 264 putlcd(1); /* display special char from display RAM */ 265 break; 266 case 'Ä': 267 putlcd(2); /* display special char from display RAM */ 268 break; 269 case 'Å': 270 putlcd(3); /* display special char from display RAM */ 271 break; 272 default: 273 if (c>0x1f && c<0x80) { /* check if it's a valid character */ 274 putlcd(c); /* write the character to the display */ 275 } 276 break; 277 } 278 } 279 280 /* 281 ** --------------------------------------------------------------------------- 282 ** putlcd - write one character to the display 283 ** --------------------------------------------------------------------------- 284 ** Inputs: data to display
  • 9. LCD.C 285 ** 286 ** Outputs: 287 ** 288 ** --------------------------------------------------------------------------- 289 ** Comments: invoked from lcd_preter 290 ** --------------------------------------------------------------------------- 291 */ 292 293 static void putlcd (unsigned char disp_data) { 294 if (col<NB_COL && col>=0) { /* check if we are inside the limits */ 295 write_data(disp_data); /* write the character to the display */ 296 col++; /* update the column position */ 297 } 298 } 299 //==================================================================================================== ======= 300 301 302 void put_string_lcd(char* dat) 303 { 304 int j=0; 305 306 307 while( dat[j] != 0) // send string 308 { 309 put_char(dat[j]); // start sending one byte 310 j++; 311 } 312 } 313 314 unsigned char hextoascii(unsigned char value) 315 { 316 idata unsigned char add; 317 if (value>9) 318 { 319 add=0x37; 320 } 321 else 322 add=0x30; 323 324 return (value + add ); 325 } 326 327 328 329 void put_char (char c) 330 { 331 lcd_preter(c); /* write the character to the display */ 332 } 333 334 void clean_lcd(void) 335 { 336 set_cursor(0,0); 337 put_string_lcd(" "); 338 set_cursor(1,0); 339 put_string_lcd(" "); 340 }