Thursday, December 8, 2011

How to display number on 16x2 LCD using 8051 microcontroller

Introduction 

LCD  is a very commonly used output device to display alphanumeric characters. The LCD displays the character corresponding to the data received on its input data port i.e., it treats the data as the ASCII value of the character to be displayed. So if the value 65 is given to its data port the character “A” will be displayed and if the value 49 is given to the LCD the numeric digit “1” will be displayed.  At many instances it is required to display a number like 123 on LCD. Displaying this number is tricky. If the data port of the LCD is loaded with the number 123, then the character corresponding to it will be displayed. This article shows the concepts behind displaying a number on LCD.The article uses 8051 microcontroller (AT89C51) to demonstrate the above principle.

Decription
The circuit is divided into two units: the controller unit and the display unit. The controller unit consists of a microcontroller circuit. The microcontroller used here is P89V51RD2. The display unit consists of a LCD interfaced to the microcontroller (for details of LCD interfacing circuit, refer ‘lcd single character display’ section). To overcome the problem stated above, the number to be displayed is store in a variable. Then each digit of the number is fetched individually in an array. Since the ASCII value of ‘0’ is 48, 1 is 49 and so on. Thus we come to the conclusion that the ASCII value of a digit (from 0 to 9) can be calculated by adding 48 to it. Thus 48 is added to each value of that array and assigned to the dataport of the LCD one by one. In this way, each digit of the number is displayed on the LCD and as a whole we have succeeded in displaying an integer number on the LCD.
In this circuit, port 2 of the microcontroller has been used as dataport (or command port) and pin 0 of port 3 is connected to RS (resistor select) pin, pin 1 of port 3 is connected to RW (read or write) pin and pin 6 of port 3 is connected to enable pin of the LCD.
Circuit Diagram: 

Source Code

//Program to display numeric digit 0-9 on LCD

#include<reg51.h>
#define lcd_data_pin P2
sbit rs = P3^0;   //Register select (RS) pin
sbit rw = P3^1;   //Read write (RW) pin
sbit en = P3^6;   //Enable (EN) pin
unsigned char c;
int num[10];

void delay(int delay_time)    // Time delay function
{
int j,k;
for(j=0;j<=delay_time;j++)
  for(k=0;k<=1000;k++);
}

void lcd_cmd(unsigned char cmd_addr)   // Funtion to send command on LCD
{         
lcd_data_pin = cmd_addr;
en = 1;
rs = 0;
rw = 0;
delay(1);
en = 0;
return;
}

void lcd_data(unsigned int i)     //Function to send data on LCD
{   int p;
int k=0;
while(i>0)
{
  num[k]=i%10;
  i=i/10;
  k++;
}
k--;
for (p=k;p>=0;p--)
{
  c=num[p]+48;
  lcd_data_pin = c;
  rw = 0;
  rs = 1;
  en = 1;
  delay(1);
  en = 0;
}
return;
}

void lcd_ini()  //Function to initialize LCD
{
lcd_cmd(0x38);    // Configuring settings to 8-bit 2 row 
delay(250);
lcd_cmd(0x0E); 
delay(250);
lcd_cmd(0x06);    //Display on
delay(250);
lcd_cmd(0x81);    //Set cursor to blink at line 1 positon 1
delay(250);
lcd_data(12345);
delay(250);
}

void main()
{
lcd_ini();
} 
 
8051 DEVELOPMENT TRAINER KIT FROM  VEDIKLABS INC. FOR DETAILS Click Here

2 comments:

  1. It is better to connect a 330ohms resistor at LEDA pin (LCD backlight), other wise the LCD backlight may get damaged.
    kiranvarma - npeducations
    http://www.npeducations.com

    ReplyDelete
  2. How to display 1 to 1000 numbers on lcd using counter...

    ReplyDelete