Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
/************************************************************************************
Function Name : lcd_cmdwrt()
Return value : None
Purpose of this function: To send the command to LCD
************************************************************************************/
void lcd_cmdwrt(unsigned char cmd)
{
unsigned char LCD;
LCD=((cmd & 0x0F0)>>2);
lcd=LCD;
RS=0;
EN=1;
delay(1);
EN=0;
LCD=((cmd & 0x0F)<<2);
lcd=LCD;
RS=0;
EN=1;
delay(1);
EN=0;
}
/************************************************************************************
Function Name : dat()
Arguments :data to be print on lcd
Return value : None
Purpose of this function: To send the data to LCD
************************************************************************************/
void dat(unsigned char dt)
{
unsigned char LCD;
LCD=((dt & 0x0F0)>>2);
lcd=LCD;
RS=1;
EN=1;
delay(1);
EN=0;
LCD=((dt & 0x0F)<<2);
lcd=LCD;
RS=1;
EN=1;
delay(1);
EN=0;
delay(1);
}
/************************************************************************************
Function Name : lcd_datawrt()
Arguments : String pointer of the string to be displayed
Return value : None
Purpose of this function: To display the string on LCD
************************************************************************************/
void lcd_datawrt(unsigned char *str)
{
while(*str)
{
dat(*str);
str++;
}
}
/************************************************************************************
Function Name : lcd_init()
Return value : None
Purpose of this function: To initialising the LCD
/************************************************************************************/
void lcd_init()
{
lcd_cmdwrt(0x28);
delay(1);
lcd_cmdwrt(0x0f);
delay(1);
lcd_cmdwrt(0x01);
delay(1);
lcd_cmdwrt(0x06);
delay(1);
lcd_cmdwrt(0x80);
delay(1);
}
#include<reg51.h>
/* SFR declaration */
sfr lcd=0x90;
/*single bit declaration */
sbit RS=0x90; //p 1.0
sbit EN=0x91; //p1.1 rw pin is grounded
/* Function prototype declaration */
/* LCD function decalration */
void lcd_cmdwrt(unsigned char cmd);
void lcd_datawrt(unsigned char *);
void dat(unsigned char);
void lcd_init();
void delay(unsigned int );
We dont do your home work here...
modify the code for your need....
this is lcd.c
Code:/************************************************************************************ Function Name : lcd_cmdwrt() Return value : None Purpose of this function: To send the command to LCD ************************************************************************************/ void lcd_cmdwrt(unsigned char cmd) { unsigned char LCD; LCD=((cmd & 0x0F0)>>2); lcd=LCD; RS=0; EN=1; delay(1); EN=0; LCD=((cmd & 0x0F)<<2); lcd=LCD; RS=0; EN=1; delay(1); EN=0; } /************************************************************************************ Function Name : dat() Arguments :data to be print on lcd Return value : None Purpose of this function: To send the data to LCD ************************************************************************************/ void dat(unsigned char dt) { unsigned char LCD; LCD=((dt & 0x0F0)>>2); lcd=LCD; RS=1; EN=1; delay(1); EN=0; LCD=((dt & 0x0F)<<2); lcd=LCD; RS=1; EN=1; delay(1); EN=0; delay(1); } /************************************************************************************ Function Name : lcd_datawrt() Arguments : String pointer of the string to be displayed Return value : None Purpose of this function: To display the string on LCD ************************************************************************************/ void lcd_datawrt(unsigned char *str) { while(*str) { dat(*str); str++; } } /************************************************************************************ Function Name : lcd_init() Return value : None Purpose of this function: To initialising the LCD /************************************************************************************/ void lcd_init() { lcd_cmdwrt(0x28); delay(1); lcd_cmdwrt(0x0f); delay(1); lcd_cmdwrt(0x01); delay(1); lcd_cmdwrt(0x06); delay(1); lcd_cmdwrt(0x80); delay(1); }
this is lcd.h
Code:#include<reg51.h> /* SFR declaration */ sfr lcd=0x90; /*single bit declaration */ sbit RS=0x90; //p 1.0 sbit EN=0x91; //p1.1 rw pin is grounded /* Function prototype declaration */ /* LCD function decalration */ void lcd_cmdwrt(unsigned char cmd); void lcd_datawrt(unsigned char *); void dat(unsigned char); void lcd_init(); void delay(unsigned int );
the data lines are P1.2 to P1.5...
compile these 2 and check if the port pins are same...
#include <reg52.h>
#include "lcd.h"
main( )
{ while(1)
{lcd_init();
lcd_datawrt("Hello");
}
}
/************************************************************************************
Function Name : lcd_cmdwrt()
Return value : None
Purpose of this function: To send the command to LCD
************************************************************************************/
void lcd_cmdwrt(unsigned char cmd)
{
unsigned char LCD;
LCD=((cmd & 0x0F0)>>2);
lcd=LCD;
RS=0;
EN=1;
delay();
EN=0;
LCD=((cmd & 0x0F)<<2);
lcd=LCD;
RS=0;
EN=1;
delay();
EN=0;
}
/************************************************************************************
Function Name : dat()
Arguments :data to be print on lcd
Return value : None
Purpose of this function: To send the data to LCD
************************************************************************************/
void dat(unsigned char dt)
{
unsigned char LCD;
LCD=((dt & 0x0F0)>>2);
lcd=LCD;
RS=1;
EN=1;
delay();
EN=0;
LCD=((dt & 0x0F)<<2);
lcd=LCD;
RS=1;
EN=1;
delay();
EN=0;
delay();
}
/************************************************************************************
Function Name : lcd_datawrt()
Arguments : String pointer of the string to be displayed
Return value : None
Purpose of this function: To display the string on LCD
************************************************************************************/
void lcd_datawrt(unsigned char *str)
{
while(*str)
{
dat(*str);
str++;
}
}
/************************************************************************************
Function Name : lcd_init()
Return value : None
Purpose of this function: To initialising the LCD
/************************************************************************************/
void lcd_init()
{
lcd_cmdwrt(0x28);
delay();
lcd_cmdwrt(0x0f);
delay();
lcd_cmdwrt(0x01);
delay();
lcd_cmdwrt(0x06);
delay();
lcd_cmdwrt(0x80);
delay();
}
void delay()
{
unsigned char i;
for (i=0; i<500 ;i++);
}
#include<reg52.h>
/* SFR declaration */
sfr lcd=0x80;
/*single bit declaration */
sbit RS=0x80; //p 0.0
sbit EN=0x82; //p0.2 rw pin is grounded
/* Function prototype declaration */
/* LCD function decalration */
void lcd_cmdwrt(unsigned char cmd);
void lcd_datawrt(unsigned char *);
void dat(unsigned char);
void lcd_init();
void delay();
I see two versions of basic calculator main.c, one in the archive and the text posted in the recent mail. None of them corresponds to the actual pin assignment. The archive version has 8-bit port access which is obviously wrong. And the last code simply copies the code dedicated for bit2..5 LCD port, as posted by ckshivaram. But you have bit4..7 instead.
LCD= (dt & 0x0F0);
lcd=LCD;
RS=1;
EN=1;
delay();
EN=0;
LCD=((dt & 0x0F)<<4);
The data shift should change to the below code to access bit4..7
Code:LCD= (dt & 0x0F0); lcd=LCD; RS=1; EN=1; delay(); EN=0; LCD=((dt & 0x0F)<<4);
I hope, the code can work after these changes. There are however some weaknesses involved with the code.
- The init code will depend on a previous hardware reset (e.g. power on reset) of the LCD controller. It won't work, if the controller state has been messed up somehow. In this case the more complex "Initializing by Instruction" procedure would required, refer to the HD44780 datasheet.
- The display timing is ineffective by applying rather long delays for each write. Using optimized timing and checking LCD busy flag would considerably speed it up.
It should be also noted, that writing to the lcd data port (P0) implicitely resets all LCD control lines as well. But it's no problem with the present code.
#include <reg52.h>
#include "lcd.h"
main( )
{
lcd_init();
lcd_datawrt("Hello");
while(1);
}
/************************************************************************************
Function Name : lcd_cmdwrt()
Return value : None
Purpose of this function: To send the command to LCD
************************************************************************************/
void lcd_cmdwrt(unsigned char cmd)
{
unsigned char LCD;
LCD=(cmd & 0x0F0);
lcd=LCD;
RS=0;
EN=1;
delay();
EN=0;
LCD=((cmd & 0x0F)<<4);
lcd=LCD;
RS=0;
EN=1;
delay();
EN=0;
}
/************************************************************************************
Function Name : dat()
Arguments :data to be print on lcd
Return value : None
Purpose of this function: To send the data to LCD
************************************************************************************/
void dat(unsigned char dt)
{
unsigned char LCD;
LCD=(dt & 0x0F0);
lcd=LCD;
RS=1;
EN=1;
delay();
EN=0;
LCD=((dt & 0x0F)<<4);
lcd=LCD;
RS=1;
EN=1;
delay();
EN=0;
delay();
}
/************************************************************************************
Function Name : lcd_datawrt()
Arguments : String pointer of the string to be displayed
Return value : None
Purpose of this function: To display the string on LCD
************************************************************************************/
void lcd_datawrt(unsigned char *str)
{
while(*str)
{
dat(*str);
str++;
}
}
/************************************************************************************
Function Name : lcd_init()
Return value : None
Purpose of this function: To initialising the LCD
/************************************************************************************/
void lcd_init()
{
lcd_cmdwrt(0x28);
delay();
lcd_cmdwrt(0x0f);
delay();
lcd_cmdwrt(0x01);
delay();
lcd_cmdwrt(0x06);
delay();
lcd_cmdwrt(0x80);
delay();
}
void delay()
{
unsigned char i;
for (i=0; i<200;i++);
}
#include<reg52.h>
/* SFR declaration */
sfr lcd=0x80;
/*single bit declaration */
sbit RS=0x80; //p 0.0
sbit EN=0x81; //p0.2 rw pin is grounded
/* Function prototype declaration */
/* LCD function decalration */
void lcd_cmdwrt(unsigned char cmd);
void lcd_datawrt(unsigned char *);
void dat(unsigned char);
void lcd_init();
void delay();