LCD-LED Display

Interfacing 16×2 LCD with 8051

In this project, we’ll go through how to connect a Interfacing 16×2 LCD with 8051 module to an AT89C51, a microcontroller from the 8051 series. We utilise LCD displays to show messages in a more interactive fashion, such as while operating the system or when displaying error messages. Interfacing a 162 LCD with an 8051 microcontroller is simple if you understand how LCDs work.

As a result, in this project, I will not only share knowledge about the LCD, but also code written in C that is error-free.

Outline

  • A Brief Note on Interfacing 16×2 LCD with 8051
  • Interfacing 16×2 LCD with 8051 Circuit Diagram
  • Components Required 
  • Circuit Explanation
  • Programming LCD to 8051
    • Initializing LCD
    • Sending Commands to the LCD
    • Writing the Data to the LCD
  • Code 
  • Additional Codes 
    • Code 1
    • Code 2

A Brief Note on 16×2 LCD

The 16×2 Liquid Crystal Display (LCD) showcases 32 characters in a dual-row format, with each row accommodating 16 characters. The individual character size within the display constitutes a 57% pixel matrix. It’s worth noting that this pixel matrix may vary slightly depending on the specific 16×2 LCD module you select. For instance, if you opt for the JHD162A module, the matrix size is 58%. The LCD module incorporates a total of 16 pins, and the pin arrangement is depicted below.

PIN NO NAME FUNCTION
1 VSS Ground pin
2 VCC Power supply pin of 5V
3 VEE Used for adjusting the contrast commonly attached to the potentiometer.
4 RS RS is the register select pin used to write display data to the LCD (characters), this pin has to be high when writing the data to the LCD. During the initializing sequence and other commands this pin should low.
5 R/W Reading and writing data to the LCD for reading the data R/W pin should be high (R/W=1) to write the data to LCD R/W pin should be low (R/W=0)
6 E Enable pin is for starting or enabling the module. A high to low pulse of about 450ns pulse is given to this pin.
7 DB0  
8 DB1  
9 DB2  
10 DB3  
11 DB4 DB0-DB7 Data pins for giving data(normal data like numbers characters or command data) which is meant to be displayed
12 DB5  
13 DB6  
14 DB7  
15 LED+ Back light of the LCD which should be connected to Vcc
16 LED- Back light of LCD which should be connected to ground.

So, after reading the table above, you should have a good concept of how to show a character. To display a character, first enable the enable pin (pin 6) with a 450ns pulse, then pick the register select pin (pin 4) in write mode. To enable write mode, set the register select pin to high (RS=1), and then set the R/W to low (R/W=0).

Follow these simple steps for displaying a character or data:

  • E=1; enable pin should be high.
  • RS=1; Register select should be high.
  • R/W=0; Read/Write pin should be low.

To send a command to the LCD just follows these steps:

  • E=1; enable pin should be high.
  • RS=0; Register select should be low.
  • R/W=0; Read/Write pin should be low.

Commands: There are some preset commands which will do a specific task in the LCD. These commands are very important for displaying data in LCD. The list of commands given below:

COMMAND FUNCTION
0F For switching on LCD, blinking the cursor.
1 Clearing the screen
2 Return home.
4 Decrement cursor
6 Increment cursor
E Display on and also cursor on
80 Force cursor to beginning of the first line
C0 Force cursor to beginning of second line
38 Use two lines and 5×7 matrix
83 Cursor line 1 position 3
3C Activate second line
0C3 Jump to second line position 3
0C1 Jump to second line position1

Interfacing 16×2 LCD with 8051 Circuit Diagram

Interfacing 16×2 LCD with 8051

Components Required 

  • AT89C51 (8051 Microcontroller)
  • 16X2 LCD Display
  • 11.0592MHz Crystal
  • 2 X 33pF Capacitors 
  • 2 X 10 KΩ Resistors
  • 1 KΩ X 8 Resistor Pack 
  • 10 KΩ Potentiometer 
  • 330Ω Resistor 
  • Push Button 
  • 10μF/16V Capacitor 
  • 8051 Programmer 
  • 5V Power Supply 
  • Connecting Wires

Circuit Explanation

The crystal oscillator is linked to XTAL1 and XTAL2, providing the system clock to the microcontroller, along with two 33pF capacitors.

To ensure the RST Pin remains low, a 10K resistor is employed. For resetting the 8051 Microcontroller, a 10F Capacitor and a Push Button can be used. The EA pin is held high with the assistance of a 10K resistor.

The LCD data pins are connected to PORT0 (prior to which, the PORT0 pins must be pulled high using a 1K Resistor Pack). RS and E are connected via pins P2.0 and P2.1 on PORT2.

To adjust the LCD contrast, a 10K potentiometer is employed.

Programming LCD to 8051

Coming to the programming you should follow these steps:

  • STEP1: Initialization of LCD.
  • STEP2: Sending commands to LCD.
  • STEP3: Writing the data to LCD.

Initializing LCD

To initialize LCD to the 8051 the following instruction and commands are to be embed in to the functions

  • 0x38 is used for 8-bit data initialization.
  • 0xoC for making LCD display on and cursor off.
  • 0X01 for clearing the display of the LCD.
  • 0x80 for positioning the cursor at first line .

Sending Commands to the LCD

  • E=1; enable pin should be high
  • RS=0; Register select should be low for sending commands
  • Placing the data on the data registers
  • R/W=0; Read/Write pin should be low for writing the data.

Writing the Data to the LCD

  • E=1; enable pin should be high
  • RS=1; Register select should be high for writing data
  • Placing the data on the data registers
  • R/W=0; Read/Write pin should be low for writing the data.

Code 

  #include<reg51.h>
  #define lcd P0
   
  sbit rs=P2^0;
  sbit e=P2^1;
   
  void delay (int);
  void cmd (char);
  void display (char);
  void custom (void);
  void string (char *);
  void init (void);
   
  unsigned char custom_char[]= {0x00,0x04,0x0E,0x04,0x15,0x0E,0x04,0x00,
  0x00,0x04,0x0E,0x0E,0x0E,0x1F,0x04,0x00,
  0x00,0x0A,0x15,0x11,0x11,0x0A,0x04,0x00,
  0x0E,0x11,0x11,0x1F,0x1B,0x1B,0x1B,0x1F,
  0x0E,0x11,0x10,0x1F,0x1B,0x1B,0x1B,0x1F,
  0x00,0x0E,0x15,0x1B,0x0E,0x0E,0x00,0x00,
  };
   
   
  void delay (int d)
  {
  unsigned char i;
  for(;d>0;d–)
  {
  for(i=250;i>0;i–);
  for(i=248;i>0;i–);
  }
  }
  void cmd (char c)
  {
  lcd=c;
  rs=0;
  e=1;
  delay(5);
  e=0;
  }
  void display (char c)
  {
  lcd=c;
  rs=1;
  e=1;
  delay(5);
  e=0;
  }
  void custom (void)
  {
  int k;
  cmd(0x40);
  for(k=0;k<48;k++)
  display(custom_char[k]);
  cmd(0x80);
  }
   
  void string (char *p)
  {
  while(*p)
  {
  display(*p++);
  }
  }
  void init (void)
  {
  cmd(0x38);
  cmd(0x0c);
  cmd(0x01);
  cmd(0x80);
  }
  void main()
  {
  init();
  custom();
  string(“Electronics Hub “);
  cmd(0xc0);
  string(” Presents “);
  delay(2000);
  cmd(0x01);
  cmd(0x80);
  string(” Custom Chars “);
  cmd(0xc0);
  string(” on 16×2 LCD “);
  delay(2000);
  cmd(0x01);
  cmd(0x80);
  string(“ABCDEFGHIJKLMNOP”);
  cmd(0xc0);
  string(“1234567890”);
  cmd(0xca);
  display(0);
  display(1);
  display(2);
  display(3);
  display(4);
  display(5);
  while(1);
  }

Additional Codes 

The programs given below will use above functions and display the complete string which is given by the programmer to display the data. We have provided two demo codes working properly and easy to understand.

Code 1

  #include <REG51.H>
  #include <string.h>
  #include <stdio.h>
   
  sfr  LCD=0x80;
   
  sbit EN=P2^1;
  sbit RS=P2^0;
   
  void nop(void);
  void delay_1s(unsigned char t);
  void initial_lcd(void);
  void delay(void);
  void string_to_lcd(unsigned char *s);
  void write_lcd(unsigned char dat,unsigned int com);
  void delay_50ms(unsigned char x);
   
  void main()
  {
   
  P1=0xff;
  P2=0xff;
  P3=0xff;
  delay_50ms(4);
  initial_lcd();
  write_lcd(0x80,0);
  string_to_lcd(”   Welcome to   “);
  write_lcd(0xc0,0);
  string_to_lcd(“Electronics Hub “);
   
  }
   
  void nop(void)
  {
   
  unsigned char n;
  for(n=0;n<20;n++);
   
  }
   
  //………………delay routine……………..//
   
  void delay_1s(unsigned char t)
  {
   
  unsigned char i,j;
  for(i=0;i<t;i++)
   
  {
   
  for(j=0;j<20;j++)
   
  {
   
  TMOD=0x01;
  TH0=0x3c;              //for 12MHz   (12/12MHZ)=1u>per cycle operation
  TL0=0xb0;            //50ms delay get (50m/1u)=50000;
  TR0=1;                 //Load value is =65536-50000=15536(=3cb0H)
   
  while(TF0!=1);  //wait for overflow flag
  TF0=0;
   
  }
   
  }
   
  }
   
  void initial_lcd(void)
  {
   
  write_lcd(0x38,0);
  write_lcd(0x0c,0);
  write_lcd(0x01,0);
   
  }
   
  void write_lcd(unsigned char dat,unsigned int com)
  {
   
  RS=com;
  LCD=dat;nop();
  EN=1;nop();
  EN=0;
  nop();
   
  }
   
  void string_to_lcd(unsigned char *s)
   
  {
   
  unsigned char i,l;
  l=strlen(s);
  for(i=0;i<l;i++)
   
  {
   
  write_lcd(*s,1);delay_50ms(1);
  s++;
   
  }
   
  }
   
  void delay_50ms(unsigned char x)
  {
   
  unsigned char i;
  for(i=0;i<x;i++)
   
  {
   
  TMOD=0x01;
  TH0=0x3c;
  TL0=0xb0;
  TR0=1;
  while(!TF0);
  TF0=0;
  TR0=0;
   
  }
   
  }

Code 2

  #include<reg51.h>
  #define cmdport P2
  #define dataport P0
   
   
  sbit rs = cmdport^0;        //register select pin
  sbit e = cmdport^1;        //enable pin
   
  void delay(unsigned int msec)    //Function to provide time delay in msec.
   
  {
   
  int i,j ;
  for(i=0;i<msec;i++)
  for(j=0;j<1275;j++);
   
  }
   
  void lcdcmd(unsigned char item)    //Function to send command to LCD
   
  {
   
  dataport = item;
  rs= 0;
  e=1;
  delay(1);
  e=0;
   
  }
   
  void lcddata(unsigned char item)    //Function to send data to LCD
   
  {
   
  dataport = item;
  rs= 1;
  e=1;
  delay(1);
  e=0;
   
  }
   
  void main()
   
  {
   
  lcdcmd(0x38);    //for using 8-bit 2 row mode of LCD
  delay(100);
  lcdcmd(0x0C);    //turn display ON and cursor OFF
  delay(100);
  lcdcmd(0x01);    //clear screen
  delay(100);
  lcdcmd(0x86);    //bring cursor to position 6 of line 1
  delay(100);
  lcddata(‘A’);
   
  }

 

Tags

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button
Close
Close