Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] text Lcd (2x16) 4-bit mode with at89c52 ...... code required ?

Status
Not open for further replies.

#MAAM#

Full Member level 2
Joined
Nov 21, 2009
Messages
131
Helped
29
Reputation
58
Reaction score
27
Trophy points
1,308
Location
Egypt
Activity points
1,920
Hi guys

i need code for lcd with at89c52 ............. this my image of my schematic

**broken link removed**

i upload my schematic in proteus. thanks in advance.
 

Attachments

  • Schematic.rar
    42.5 KB · Views: 68

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...



We dont do your home work here...

modify the code for your need....
 
  • Like
Reactions: #MAAM#

    #MAAM#

    Points: 2
    Helpful Answer Positive Rating
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...

thanks for your support. but it`s still not working !!!!!!!!!!!!!!!!!!

my code

basic calculator.c
Code:
#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++);	
}

lcd.h
Code:
#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 upload my files including firmware with keil c and proteus file
 

Attachments

  • basic calculator.rar
    68.4 KB · Views: 107

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.
 
  • Like
Reactions: #MAAM#

    #MAAM#

    Points: 2
    Helpful Answer Positive Rating
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.

how can i chose the actual pin assignment ?
 

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.
 
  • Like
Reactions: #MAAM#

    #MAAM#

    Points: 2
    Helpful Answer Positive Rating
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.

thanks for your support. code is working with me now very well.
 

thanks to every body help me :)

my working code
basic calculator.c
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++);	
}

lcd.h
Code:
#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();

i also upload my working code and proteus simulation file .
 

Attachments

  • lcd (2x16) 4bit mode with at89c52.rar
    68 KB · Views: 52

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top