NPEDUCATIONS, Electronic circuits development, electronics tutorials, microcontroller tutorials and projects, advanced microcontroller (ARM) based tutorials and projects, Embedded c development, Embedded c for ARM cortex M, Intel IoT based projects, IoT projects, CC3200 launch pad projects, MSP430 Launchpad tutorials and projects, Tiva C launch pad tutorials and projects, 8051 tutorials and projects, sensor interfacing with microcontroller tutorials, data communications and networking tutorials, peripheral interfacing with microcontroller, led message scrolling display, arduino based tutorials and projects, intel galileo based tutorials and project, ADC interfacing tutorials, LED Blinking, LCD interfacing, Embedded System tutorials and projects, B.tech projects, M.tech projects, online Embedded C training

8051 based 16x2 LCD interfacing in 4-bit mode without using busy status flag



1. Introduction 4-bit LCD interface:

The 4-bit mode LCD interface requires four data lines and two or three control lines. In this tutorial, 16x2 LCD interfaced with 8051 microcontroller in 4-bit mode without using busy status flag. In 4-bit mode LCD interface, MCU sends the data or command byte in two 4-bit bus transfers to LCD as nibbles. A nibble is 4bit data, which is half a byte.

2. Hardware interface with 8051 Circuit:

Figure 1 LCD Hardware interface with 8051 circuit.

In the above figure, the port0 upper four lines of the 8051 microcontroller is connected to data lines (D4-D7) of the LCD. In 8051 microcontroller, the port0 doesn’t have the internal pull-up resistors and all these port0 pins are open drain configuration. So we need to connect an external pull-up resistor to each port pin to improve the driving capability of the port0. The pull-up resistor values should be between 4.7kohms to 10kohms as recommended in the data sheet of the microcontroller.

The control pins (Enable, R/W, RS) of the LCD are connected to port2 of the 8051MCU (P2.2, P2.1, P2.0) respectively.


A 10k preset is connected at 3rd pin of the LCD to vary the contrast of the LCD. The 10k value is better chosen that not to vary the contrast drastically (If we use 1k or below preset value, the variation of the contrast is very drastic which make the operation of preset more sensitive). The 1k resistor is connected in series with 10k preset, which will limit the maximum current flow when the preset wiper is at its minimum position. A reset circuit made up of resistor and capacitor which is connected at 9th pin of the microcontroller and it is named as power on reset. This reset circuit is used to apply a proper reset pulse after the power supply is settled down, hardly it takes 100 milliseconds (TC = 10uf*10K). A clock circuit of 12Mhz with two 33pF capacitors are connected at the XTAL1 and XTAL2 pins.

3. LCD interface timing diagrams:

Figure 1.2 Timing diagram of LCD interface without using busy flag shows the wastage of CPU time.

In the timing diagram as shown in the figure 1.2, the data can be sent to LCD in two different 4-bit transfers. Hence two enable pulses are shown for each byte transfer. During the data or command transfer to LCD, the higher nibble (D7-D4) of a byte should be transferred first. After the command or data byte is transferred, the LCD will take some time to process it. A time delay N milliseconds should be provided to LCD for internal processing. Here I provided 100 milliseconds as time delay between each byte transfer (Check out the source code below). If delay is not provided between each byte transfers, the LCD will not accept the second byte until the first byte is completed. Therefore the LCD will not show desire output to user.


The main disadvantage of this procedure is unnecessary wastage of CPU or MCU time, because there is no exact time provided in the data sheet that an LCD will take to process the command or data. This can be solved by using busy status flag which is discussed in next tutorial.
“8051 based 8-bit mode 16x2 LCD interfacing using busy status flag”

4. Description about the LCD functions used:

There are five functions are implemented in embedded c (keil c). In each function, the data byte is divided into two parts (nibbles) by using logical masking, and then transferred. The higher nibble is transferred first and then lower nibble.
1.      Send_cmd() à  Microcontroller send command to LCD using “send_cmd()” function, in which the RD and R/W signals both are at logic low.

2.      Send_string() à Data string can be sent to LCD using this “Send_string()” function, in which the RS should be at logic high and R/W should be at logic low. When the number of characters in a string is greater than display size (> 16 characters) then the function “send_string()” will automatically shifts the display to right side.

3.      Send_char()à Using this function, we can send single char at a time.

4.      delayms(1)à this function generates 1ms delay approximately and it is tested in keil software. It uses a new function _nop_(), which generates 1us time delay and it available in intrins.h header file.


5.      LCD_Init() àby using this function, LCD is configured as 2 line with 5x7 matrix. This function makes the LCD display ON with cursor blinking and also has automatic cursor increment to right side. The commands used in this function are 0x83, 0x01, 0xc0. 0x80. Refer table 1.1 for description about the commands.

5. LCD Commands used:

In this tutorial basic commands are used to display alpha numeric characters on LCD. A list of LCD commands used in this tutorial shown in the table 1.1

S.No
Command
Description
1
0x01
Clear Display Screen
2
0x28
Configuring LCD as 2 line 5x7 matrix in 4-bit mode
3
0x0E
Display on, cursor blinking
4
0x06
Increment cursor right side
5
0x80
Force cursor to beginning of 1st line, if the number is 0x85 then force the cursor to 5th position
6
0xC0
Force cursor to beginning of 2nd line
7
0x1C
Shift the display to right side
8
0x02
Return Home
9
0x30
Configure LCD as 1-line 5x7 matrix

6. Keil c code for LCD interface with 8051 microcontroller:
/******************************************************************************************************************************/
//http://www.npeducations.com
//LCDwithDelay.c - implementing the 4-bit mode LCD interface with 89s52 microcontroller using delay
// Author - lovakiranvarma, (M.tech)
/******************************************************************************************************************************/
/********************************************** Header Files Declaration ******************************************************/
#include
#include
#include

/********************************************* LCD control signals declaration *************************************************/

sbit RS = P2^0;     // Register Select line
sbit RW = P2^1;  // Read/write line
sbit Enable = P2^2; // Enable line
sbit LED = P2^5;
#define LCD_PORT P1

/********************************************* LCD function prototypes *********************************************************/

void send_cmd(unsigned char);
void send_string(unsigned char*);
void delayms(unsigned int);
void send_Char(unsigned char);
void LCD_Init(void);

/********************************************* Main Funciton declaration *******************************************************/

void main()
{

P1 = 0x00;
P2 = 0x00;
/********************************************* LCD reset Process sequence as per datasheet***************************************/
delayms(15); // wait for more than 15ms after supply rises to 4.5V
send_cmd(0x30);
delayms(4);  // wait more than 4.1ms
send_cmd(0x30);
delayms(1);  // wait more than 100us, but delayms(1) will provide 1ms
send_cmd(0x30);
delayms(1);
send_cmd(0x02); // return to home
delayms(1);

LCD_Init();
   

 while(1)
 {
  send_cmd(0x80);       // Force cursor to beginning of 1st line, if the number is 0x85 then force the cursor to 5th position
  delayms(1);
  //send_Char('A');    // displays at first line
  //delayms(5);
  send_string("npeducations");  
  delayms(50);
  send_cmd(0xC0);       // Force cursor to beginning of 2nd line
  delayms(1);
  send_string("http://www.npeducations.com"); // displays at first line
  delayms(20);
  send_cmd(0x01);       // Force cursor to beginning of 2nd line
                        
 }

}

void LCD_Init()
{

 send_cmd(0x28);       // configuring LCD as 2 line 5x7 matrix in 4-bit mode
 delayms(1);
 send_cmd(0x0C);       // Display on, Cursor blinking
 delayms(1);
 send_cmd(0x01);       // Clear Display Screen
 delayms(1);
 send_cmd(0x06);       // Increment Cursor (Right side)
 delayms(1);

}


/*********************************************LCD Command Sending Function declaration*******************************************/

void send_cmd(unsigned char Command)
{
 unsigned char ch=0;

    LCD_PORT &= 0x00; // make upper bits of port as zero
 LCD_PORT =(Command);    //mask lower nibble and send upper nibble
 RS = 0;      // Select Command Register
 RW = 0;    // write operation
 Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
 delayms(1);
 Enable = 0;
 delayms(1);
 
 LCD_PORT &= 0x00; // make upper bits of port as zero
 
 LCD_PORT =(Command<<4); // upper nibble

 RS = 0;    // Select Data Register
 RW = 0;    // write operation
 Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
 delayms(1);
 Enable = 0;
 delayms(1);

}
/******************************************* LCD data sending Function declaration************************************************/

void send_string(unsigned char *String)
{
unsigned char i=0, ch=0, temp=0;

while(String[i]!='\0')
{
     
 temp = String[i++];

 LCD_PORT &= 0x00; // make upper bits of port as zero
 LCD_PORT = (temp);    //mask lower nibble and send upper nibble

 RS = 1;    // Select Data Register
 RW = 0;    // write operation
 Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
 delayms(1);
 Enable = 0;
 delayms(1);

 
 LCD_PORT &= 0x00; // make upper bits of port as zero
 
 LCD_PORT = ((temp<<4)); // lower nibble

 RS = 1;    // Select Data Register
 RW = 0;    // write operation
 Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
 delayms(1);
 Enable = 0;
 delayms(1);

 if(i>=16)
 send_cmd(0x1C);   
 delayms(100); 

}
 
 return;

}
/******************************************* LCD single character sending Function declaration************************************/

void send_Char(unsigned char character)
{
unsigned char ch=0;
             
 LCD_PORT &= 0x00; // make upper bits of port as zero
 LCD_PORT = (character);    //mask lower nibble and send upper nibble

 RS = 1;      // Select Command Register
 RW = 0;    // write operation
 Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
 delayms(1);
 Enable = 0;
 delayms(1);

 
 LCD_PORT &= 0x00; // make upper bits of port as zero
 
 LCD_PORT = ((character<<4)); // lower nibble
 RS = 1;    // Select Data Register
 RW = 0;    // write operation
 Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
 delayms(1);
 Enable = 0;
 delayms(1);

 delayms(100);
 

}

/******************************************* delayms Function declaration *******************************************************/
void delayms(unsigned int value)
{
 unsigned int i,j;
  
 for(i=0;i<=value;i++)
 {
  for(j=0;j<100;j++)
  _nop_();   // no operation produce 1us time delay
 }
 
}


7. Proteus simulation Circuit:

A Proteus simulation circuit output of LCD interface with 8051 microcontroller without using busy flag is shown in figure 1.3.
Figure 1.3 Proteus simulation circuit output of LCD interface with 8051 microcontroller in 4-bit mode without using busy flag


8. Assembly program:


9 comments :

  1. code for 4 bit is working. only following change
    [ _nop_(); ==> unsigned int b = 0;]

    ReplyDelete
    Replies
    1. hi kavita! did you face any problem with my code. Actually i placed this code after testing. it works fine for me. If any problems please reply me

      Delete
  2. Barbecue beef brisket is the national dish of the Republic of Texas. Here's the recipe for how to cook it the way the BBQ champions abd BBQ restaurants cook it. Learn how to grill Brisket steak in best weather now!

    ReplyDelete
  3. There are so many different prospects when it comes to this subject. Thanks for sharing your knowledge on this particular aspect and making it easier for us.Honeymoon to Brazil

    ReplyDelete
  4. source code related to the Searchdaimon enterprise search engine to GitHub, with full commit history. We will be using the GitHub repository internally as our only source code repository from now on. It is currently about 100K lines of code…hot water repairs perth

    ReplyDelete
  5. Nice blog thanks for sharing this post every body can share this post:>

    ReplyDelete
  6. I really enjoyed reading your articles. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing.

    ReplyDelete
  7. This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post!refinance home loan comparison

    ReplyDelete

Thanks for visiting NPEDUCATIONS. We will contact you soon

9 comments :

kavita Rane said...

code for 4 bit is working. only following change
[ _nop_(); ==> unsigned int b = 0;]

Lovakiranvarma Myla said...

hi kavita! did you face any problem with my code. Actually i placed this code after testing. it works fine for me. If any problems please reply me

how to cook a brisket said...

Barbecue beef brisket is the national dish of the Republic of Texas. Here's the recipe for how to cook it the way the BBQ champions abd BBQ restaurants cook it. Learn how to grill Brisket steak in best weather now!

products said...

I really like this coding :)

Honeymoon to Brazil said...

There are so many different prospects when it comes to this subject. Thanks for sharing your knowledge on this particular aspect and making it easier for us.Honeymoon to Brazil

hot water repairs perth said...

source code related to the Searchdaimon enterprise search engine to GitHub, with full commit history. We will be using the GitHub repository internally as our only source code repository from now on. It is currently about 100K lines of code…hot water repairs perth

grunge clothing uk said...

Nice blog thanks for sharing this post every body can share this post:>

printed tee shirts said...

I really enjoyed reading your articles. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing.

refinance home loan comparison said...

This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post!refinance home loan comparison

Post a Comment

Thanks for visiting NPEDUCATIONS. We will contact you soon

If you really like this tutorial, Don't forget to give the comment or please subscribe to the RSS feed by submitting your E-mail or like our Facebook page.
Related Posts Plugin for WordPress, Blogger...

 
Powered by Blogger