0% found this document useful (0 votes)
5 views

eperiment3

This document is a tutorial on interfacing an LCD in 4-bit mode with the 8051 microcontroller, detailing the initialization, command sending, and data handling processes. It compares 4-bit and 8-bit modes, emphasizing the advantages of using 4-bit mode to reduce the number of required GPIO pins. The document includes code snippets and explanations for each step of the interfacing process.

Uploaded by

Aziz Bohra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

eperiment3

This document is a tutorial on interfacing an LCD in 4-bit mode with the 8051 microcontroller, detailing the initialization, command sending, and data handling processes. It compares 4-bit and 8-bit modes, emphasizing the advantages of using 4-bit mode to reduce the number of required GPIO pins. The document includes code snippets and explanations for each step of the interfacing process.

Uploaded by

Aziz Bohra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

‭Introduction‬

‭Experiment 3‬ ‭LCD Initializing‬


‭Sending command‬
‭Sending Data‬
‭PIN DIAGRAM‬
‭CODE‬
‭OUTPUT‬
‭Discover more from‬

‭8051 – LCD 4‬ ‭AmbeTronicS‬


‭LCD 4-bit interfacing with 8051‬
‭Microcontroller‬

‭bit Interfacing‬ ‭Introduction‬


‭LCD Initializing‬
‭by‬‭Admin‬ ‭Sending command‬
‭Tags‬ ‭Sending Data‬
‭8051‬‭,‬‭LCD Interfacing‬‭,‬‭Tutorials‬ ‭PIN DIAGRAM‬
‭Categories‬ ‭CODE‬
‭8051‬ ‭OUTPUT‬
‭Discover more from‬
‭Last Updated on: November 23rd, 2024‬ ‭AmbeTronicS‬

‭ i‬ ‭dudes…!!!‬ ‭Here‬ ‭I’m‬ ‭coming‬ ‭with‬ ‭a‬


H
‭new‬ ‭tutorial.‬ ‭We‬ ‭have‬ ‭already‬ ‭seen‬ ‭LCD 4-bit‬
‭lots‬ ‭of‬‭tutorials‬‭using‬‭LCD.‬‭If‬‭you‬‭want‬
‭to‬‭see‬‭those‬‭tutorials‬‭please‬‭click‬‭here‬‭.‬
‭In‬ ‭previous‬ ‭LCD‬ ‭tutorials,‬ ‭we‬ ‭were‬
‭interfacing with‬
‭used‬ ‭that‬‭LCD‬‭in‬‭8-bit‬‭mode.‬‭But‬‭here‬
‭we‬ ‭will‬ ‭see‬ ‭LCD‬ ‭4-bit‬ ‭interfacing‬ ‭with‬ ‭8051‬
‭8051‬ ‭Microcontroller.‬ ‭Okay,‬ ‭can‬ ‭you‬
‭tell‬ ‭me‬ ‭what‬ ‭is‬ ‭8‬ ‭bit‬ ‭mode‬ ‭and‬ ‭4‬ ‭bit‬
‭mode? Any guess. Well.‬
‭Microcontroller‬

‭Table of Content‬
‭Introduction‬
‭LCD 4-bit interfacing with 8051‬
‭Microcontroller‬
‭ -bit‬ ‭mode‬ ‭–‬ ‭Using‬ ‭8‬ ‭data‬ ‭lines‬ ‭in‬
8 cmd‬
‭ (‬
‭ 0x06‬
‭ )‬
‭ ;‬

‭LCD (totally 8 data lines are there)‬

cmd‬
‭ (‬
‭ 0x8f‬
‭ )‬
‭ ;‬

‭ -bit‬ ‭mode‬ ‭–‬ ‭Using‬‭only‬‭4‬‭data‬‭lines‬
4
‭in the‬‭LCD module‬
}‬

‭ -bit‬‭mode‬‭is‬‭already‬‭working‬‭and‬‭that‬
8
‭looks‬ ‭awesome.‬ ‭Then‬ ‭why‬ ‭are‬ ‭we‬
‭going‬ ‭to‬ ‭4-bit‬ ‭mode?‬ ‭This‬ ‭is‬ ‭the‬ ‭Sending command‬
‭question‬ ‭that‬ ‭comes‬ ‭to‬ ‭everyone’s‬
‭mind‬‭whenever‬‭I‬‭say‬‭4-bit‬‭mode.‬‭Yeah,‬
‭that‬ ‭8-bit‬ ‭mode‬ ‭is‬ ‭nice.‬ ‭But‬ ‭but‬ ‭but…‬ ‭ ere‬ ‭everything‬ ‭is‬ ‭the‬ ‭same‬ ‭except‬
H
‭Just‬ ‭assume.‬ ‭I’m‬ ‭doing‬ ‭one‬ ‭project‬ ‭the‬ ‭way‬ ‭of‬ ‭data‬‭writing.‬‭Here‬‭we‬‭have‬
‭which‬ ‭requires‬ ‭more‬ ‭hardware.‬ ‭But‬ ‭only‬‭4‬‭bits.‬‭So‬‭we‬‭need‬‭to‬‭send‬‭nibble‬
‭8051‬ ‭has‬ ‭only‬ ‭32‬ ‭GPIOs.‬ ‭So‬ ‭in‬ ‭that‬ ‭by‬‭nibble.‬‭So‬‭first‬‭we‬‭need‬‭to‬‭send‬‭the‬
‭time‬ ‭I‬ ‭can‬ ‭use‬ ‭this‬ ‭4-bit‬ ‭mode‬ ‭and‬ ‭first‬ ‭nibble‬ ‭then‬ ‭followed‬ ‭by‬ ‭the‬
‭reduce‬ ‭the‬ ‭pin‬ ‭required‬ ‭for‬ ‭the‬ ‭LCD‬ ‭second.‬‭See‬‭that‬‭code.‬‭I’m‬‭writing‬‭into‬
‭module.‬ ‭Am‬ ‭I‬‭right?‬‭Great.‬‭That’s‬‭why‬ ‭Port‬ ‭2’s‬ ‭last‬‭4‬‭bits.‬‭Because‬‭the‬‭last‬‭4‬
‭4-bit‬ ‭mode‬ ‭is‬ ‭also‬ ‭important.‬ ‭Already‬ ‭bits are connected to the LCD.‬
‭we‬ ‭know‬ ‭the‬ ‭LED’s‬ ‭operation.‬ ‭If‬ ‭we‬
‭want‬ ‭to‬ ‭enable‬ ‭4-bit‬ ‭mode‬‭we‬‭have‬‭to‬
void‬‭
‭ cmd‬
(‭
‭u‬nsigned‬‭
char‬‭
a‭
)‬‬
‭do‬ ‭small‬ ‭modifications‬ ‭in‬ ‭the‬ ‭normal‬
‭method. Let’s see that.‬
{‬

I‭n‬ ‭initializing‬ ‭time‬ ‭we‬ ‭have‬ ‭to‬ ‭give‬ ‭a‬
‭0x28 command. That’s all.‬ rs=0;‬

‭LCD Initializing‬ rw=0;‬


P2&=‬
‭ 0x0F‬
‭ ;‬
‭ //clearing‬

void‬‭
‭ lcd_init‬
()‬

the last 4 bits without‬

disturbing first 4 bits‬

{‬

P2|=‬
‭ (‬
‭ a&‬
‭ 0xf0‬
‭ )‬
‭ ;‬
‭ //writing‬

cmd‬
‭ (‭
‭0‬x02‬
)‬
‭ ;‬

higher nibble‬

cmd‬
‭ (‭
‭0‬x28‬
)‬
‭ ;‬

e=1;‬

cmd‬
‭ (‭
‭0‬x0c‬
)‬
‭ ;‬

delay‬
‭ ()‬
‭ ;‬

e=0;‬
‭ P2&=‬
‭ 0x0F‬
‭ ;‬

//clearing the last 4 bits‬

delay‬
‭ ()‬
‭ ;‬
‭ without disturbing first 4 bits‬

P2&=‬
‭ 0x0f‬
‭ ;‬
‭ //clearing‬
‭ P2|=‬
‭ (‬
‭ b&‬
‭ 0xf0‬
‭ )‬
‭ ;‬

the last 4 bits without‬


‭ //writing higher nibble‬

disturbing first 4 bits‬



e=1;‬

P2|=‬
‭ (‬
‭ a‭
‭<‬<‬
4&‬
‭ 0xf0‬
‭ )‬
‭ ;‬
‭ //writing‬

lower nibble‬
‭ delay‬
‭ ()‬
‭ ;‬

e=1;‬
‭ e=0;‬

delay‬
‭ ()‬
‭ ;‬
‭ delay‬
‭ ()‬
‭ ;‬

e=0;‬
‭ P2&=‬
‭ 0x0f‬
‭ ;‬

//clearing the last 4 bits‬

delay‬
‭ ()‬
‭ ;‬
‭ without disturbing first 4 bits‬

}‬
‭ P2|=‬
‭ (‬
‭ b‭
‭<‬<‬
4&‬
‭ 0xf0‬
‭ )‬
‭ ;‬

//writing lower nibble‬

‭Sending Data‬ e=1;‬


delay‬
‭ ()‬
‭ ;‬

‭Same as sending data.‬

void‬‭
‭ dat‬
(‭
‭u‬nsigned‬‭
char‬‭
b‭
)‬‬ e=0;‬

{‬
‭ delay‬
‭ ()‬
‭ ;‬

rs=1;‬
‭ }‬

rw=0;‬

‭PIN DIAGRAM‬
void‬‭
‭ show‬
(‬
‭ unsigned‬‭
‭ char‬‭
*s‬
)‭
‭;‬‬

void‬‭
‭ delay‬
()‬
‭ ;‬

void‬‭
‭ main‬
()‬

{‬

int‬‭
‭ j;‬

‭RS is connected to P2.0‬


lcd_init‬
‭ ()‬
‭ ;‬

‭RW is connected to P2.1‬


show‬
‭ (‬
‭ "embetronicx.com"‬
‭ )‬
‭ ;‬

‭EN is connected to P2.2‬
while‬
‭ (‭
‭1‬‬
)‬

‭ ‬ ‭data‬ ‭lines‬ ‭are‬ ‭connected‬ ‭into‬ ‭P2.‬ ‭4‬
4
‭to P2.7‬
{‬

‭CODE‬ cmd‬
‭ (‬
‭ 0x18‬
‭ )‬
‭ ;‬

#include<reg51.h>‬
‭ for‬
‭ (‬
‭ j=0;j‬
‭ <‭
‭2‬5000;j++‬
)‭
‭;‬‬

sbit rs=P2^0;‬
‭ }‬

sbit rw=P2^1;‬
‭ }‬

sbit e=P2^2;‬
‭ void‬‭
‭ lcd_init‬
()‬

void‬‭
‭ lcd_init‬
()‬
‭ ;‬
‭ {‬

void‬‭
‭ cmd‬
(‭
‭u‬nsigned‬‭
char‬‭
a‭
)‬‬
;‬
‭ cmd‬
‭ (‬
‭ 0x02‬
‭ )‬
‭ ;‬

void‬‭
‭ dat‬
(‭
‭u‬nsigned‬‭
char‬‭
b‭
)‬‬
;‬
‭ cmd‬
‭ (‬
‭ 0x28‬
‭ )‬
‭ ;‬

cmd‬
‭ (‭
‭0‬x0c‬
)‬
‭ ;‬
‭ delay‬
‭ ()‬
‭ ;‬

cmd‬
‭ (‭
‭0‬x06‬
)‬
‭ ;‬
‭ e=0;‬

cmd‬
‭ (‭
‭0‬x8f‬
)‬
‭ ;‬
‭ delay‬
‭ ()‬
‭ ;‬

}‬
‭ }‬

void‬‭
‭ cmd‬
(‭
‭u‬nsigned‬‭
char‬‭
a‭
)‬‬ void‬‭
‭ dat‬
(‭
‭u‬nsigned‬‭
char‬‭
b‭
)‬‬

{‬
‭ {‬

rs=0;‬
‭ rs=1;‬

rw=0;‬
‭ rw=0;‬

P2&=‬
‭ 0x0F‬
‭ ;‬
‭ P2&=‬
‭ 0x0F‬
‭ ;‬

P2|=‬
‭ (‬
‭ a&‬
‭ 0xf0‬
‭ )‬
‭ ;‬
‭ P2|=‬
‭ (‬
‭ b&‬
‭ 0xf0‬
‭ )‬
‭ ;‬

e=1;‬
‭ e=1;‬

delay‬
‭ ()‬
‭ ;‬
‭ delay‬
‭ ()‬
‭ ;‬

e=0;‬
‭ e=0;‬

delay‬
‭ ()‬
‭ ;‬
‭ delay‬
‭ ()‬
‭ ;‬

P2&=‬
‭ 0x0f‬
‭ ;‬
‭ P2&=‬
‭ 0x0f‬
‭ ;‬

P2|=‬
‭ (‬
‭ a‭
‭<‬<‬
4&‬
‭ 0xf0‬
‭ )‬
‭ ;‬
‭ P2|=‬
‭ (‬
‭ b‭
‭<‬<‬
4&‬
‭ 0xf0‬
‭ )‬
‭ ;‬

e=1;‬
‭ e=1;‬

delay‬
‭ ()‬
‭ ;‬

‭OUTPUT‬
e=0;‬

delay‬
‭ ()‬
‭ ;‬

}‬

void‬‭
‭ show‬
(‬
‭ unsigned‬‭
‭ char‬‭
*s‬
)‬

{‬

‭[‬‭Please find the output image‬‭]‬


while‬
‭ (‭
‭*‬s‬
)‬

{‬

dat‬
‭ (‬
‭ *s++‬
‭ )‬
‭ ;‬

}‬

}‬

void‬‭
‭ delay‬
()‬

{‬

unsigned‬‭
‭ int‬‭
k,l;‬

for‬
‭ (‭
‭k‬=0;k‬
<‭
‭2‬;k++‬
)‬

for‬
‭ (‭
‭l‬=0;l‬
<‭
‭1‬275;l++‬
)‬
‭ ;‬

}‬

You might also like