how to display numbers on lcd?
In this article I have written about how number get displayed on lcd.
Lcd only displays character ranging from '0' to '9' ( in terms of numbers).So the question is how to display numbers which are greater than 9 ?
If we want to display any number which is greater than 9 on an lcd then decompose(break) the number and add 48 to each separated digit of that number and send the resulting value one by one to the lcd.
Here I have taken four digit number as an example.Suppose I want to display 1352 on lcd.
First basic step is initialization of lcd.Please refer my article about lcd display interfacing with 8051 to know lcd display interfacing.(http://embeddedprojects222.blogspot.com/2020/07/lcd-interfacing-using-8051-micro.html).
Then the next step is decompose(break) the number.
Procedure to decompose(break) 4 digit number:
1.First divide the number by 1000.So we get first number.
Ex.1352/1000=1;
2.Divide the number by 1000 and take the remainder of the division.Divide that remainder by 100 so that we can get second number.
Ex.(1352%1000)/100= 3;
Note: [ ( / ) divide operator returns quotient of the division and
(%) modulus operator returns remainder of the division.]
3.Divide the number by 1000 and take the remainder of the division.Divide that remainder by 100.Again take the remainder of the division and divide it by 10.So that we can get third digit of the number.
Ex.((1352%1000)%100)/10= 5;
4.Divide the number by 10 and take the remainder of the division so that we can get last digit of the number.
Ex. 1352%10=2;
There are two ways to display characters on an LCD:
1.One is sending character by writing it into single inverted commas
Ex. a = '0';
2.The other way is sending the ASCII value of the character.
Ex. a = 48; (ASCII value of '0')
Here for displaying the number I have used ASCII value of the characters.ASCII value of '0' to '9' are ranging from 48 to 57 respectively.
Now after decomposing the number,next step is to add 48 to each separated digit so that we can able to get ASCII value of that digit.
Ex.
1+48=49;( ASCII value of 1)
3+48=51;(ASCII value of 3)
5+48=53;(ASCII value of 5)
2+48=50;(ASCII value of 2)
Send those values to lcd display one by one.So that we will be able to display complete number 1352.This is the basic procedure to display number on lcd.
I have written a program to display number upto 9000.
PROGRAM:
#include<reg51.h>
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
void delay(int t);
void lcd_init(void);
void lcd_command(char c);
void lcd_data(char d);
unsigned int q,r,s,t,i=0;
void print(unsigned int p);
void main()
{
lcd_init();
while(1)
{
if(i<=9000)
{
print(i);
i++;
}
}
}
void print(unsigned int p) // function to display numbers
{
q=p/1000; // first digit
q=q+48;
r=((p%1000)/100); // second digit
r=r+48;
t=(((p%1000)%100)/10); // third digit
t=t+48;
s=p%10; //last digit
s=s+48;
lcd_data(q); // display first digit
lcd_data(r); // display second digit
lcd_data(t); // display third digit
lcd_data(s); // display last digit
lcd_command(0x01); // clear display
}
void lcd_init(void) // initialization of lcd
{
lcd_command(0x38);
lcd_command(0x01);
lcd_command(0x0f);
lcd_command(0x06);
lcd_command(0x0c);
lcd_command(0x80);
}
void lcd_command(char c) // function to execute command
{
P1=c;
RS=0;
RW=0;
EN=1;
delay(5);
EN=0;
delay(5);
}
void lcd_data(char d) // function to display data
{
P1=d;
RS=1;
RW=0;
EN=1;
delay(5);
EN=0;
delay(5);
}
void delay(int t) // delay function
{
int j;
for(j=0;j<t*1275;j++);
}
VIDEO:
Comments
Post a Comment