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.

sample code for 20*4 character lcd using embedded c

Status
Not open for further replies.

sudhakar a

Junior Member level 3
Joined
Jun 3, 2013
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,503
Hi any body help to me how to write a single character in 20*4 character lcd
 

Hi,
You din't mention the controller......
any way i will give basic instruction.


0x38 for 8bit and 0x28 for 4bit //first of all you have to set the mode,LCD can act both 8 bit and 4 bit.
0x0e //lcd on
0x01 // clear the previous character
0x06 // cursor blink
0x80 // set first line first character

these are the basic commands.
if you are going to use 4bot mode LCD PIN 1,5,7,8,9,10,16 these pins are connected to ground
11,12,13,14 are data pins,
6(en)and 4(rs) are connected in to port pins,
2nd and 15th pins are connected to ground,3rd pin is connected to pot.


Note:in 4bit mode you can not send data fullly,first you have to mask Lsb and send MSB,then mask MSB and send LSB
 

Thanks arun for replying me
I am use the R8c24 Controller.
In 20*4 Character i will send the 8 bit mode.
I applied the basic Instructions but i can't write single character on lcd.Plz... give me brief information write the single character.
 

Thanks Jayanth for replying me
I applied the delay
My code is

Code:
LCD_Command(0x01);	//position of charater
	LCD_Command(0x83);	//cursor position
	LCD_SendData('A');	//write character A
LCD_Build(1);

void LCD_Init()
{
	LCD_DATA=0x38;	//8 bit mode
	LCD_RS=0;		//Selected command register
	LCD_EN=1;		//Enable
	LCD_EN=0;
	LCD_Busy();		//delay
	
	LCD_DATA=0x0F;	//Display on cusrnor blinking
	LCD_RS=0;
	LCD_EN=1;
	LCD_EN=0;
	LCD_Busy();
	
	LCD_DATA=0x01;	//clear lcd
	LCD_RS=0;
	LCD_EN=1;
	LCD_EN=0;
	LCD_Busy();
	
	LCD_DATA=0x06;	//Entry Mode
	LCD_RS=0;
	LCD_EN=1;
	LCD_EN=0;
	LCD_Busy();
}
void LCD_Command(_UBYTE var)	//LCD COmmand ex:var=0x01
{
	LCD_DATA=var;
	LCD_RS=0;
	LCD_EN=1;
	LCD_EN=0;
	LCD_Busy();
}

void LCD_SendData(_UBYTE var)	//Send Data write characte ex:var='A'
{
	LCD_DATA=var;
	LCD_RS=1;
	LCD_EN=1;
	LCD_EN=0;
	LCD_Busy();
}
void LCD_Busy()	//Delay
{
	_UBYTE i,j;
	for(i=0;i<50;i++)
		for(j=0;j<255;j++);
}	

void LCD_Build(_UBYTE ch)	//character buliding
{
	LCD_Command(0x40+(ch*8));
	LCD_SendData(0x04);
	LCD_SendData(0x0E);
	LCD_SendData(0x0E);
	LCD_SendData(0x0E);
	LCD_SendData(0x1F);
	LCD_SendData(0x00);
	LCD_SendData(0x04);
	LCD_SendData(0x00);
}
 
Last edited by a moderator:

I am using the 8 bit mode.
Code:
#define LCD_RS 		p1_6
#define LCD_EN 		p1_7		//LCD ENABLE
#define LCD_BF 		p2_7		//LCD DB7(BUSY FLAG)
#define LCD_DATA 	p2_4		//LCD DATA
 
Last edited by a moderator:

What is this? Is it a PORT or PIN?

Code C - [expand]
1
#define LCD_DATA p2_4    //LCD DATA

If it is PIN 5 of PORT2 then change it to P2 if P2 is PORT 2 which is connected to LCD.
 

yaa this is the PIN 5 of the Port2.I changed the LCD_DATA p2_4 to LCD_DATA p2 (port 2).Still i couldn't out put.Plz told any modifications of my code
 

There are a lot of 8 bit LCD examples. Google for it and use it. Your delay function doesn't give proper delays. Read the book "The 8051 Microcontroller and Embedded Systems: Using Assembly and C by Muhammed Ali Mazidi". It has example for 8 bit LCD in C. Just use it by changing the LCD data PORT and control pin defines.
 

Thanks Jayanth for replying me.
Im confused in usage of R/w pin.
Actually for LCD write R/W pin assigned to 0.So thats why i am not usage R/W pin in my code.
But in all other sample codes for R/W assign the pin.
Actually in my controller R/W pin are connected to the Ground.
is it necessary for assign the pin to R/W?
 

Thanks for replying me.I applied the same way but i can't get output.Plz tell me if any modifications in my code
 

LCD is LM20400 character lcd and i am using the r8c24 controller
Code:
#define LCD_RS_DIR	p1_6
#define LCD_EN_DIR	p1_7
#define LCD_BF_DIR	p2_7
#define LCD_DATA_DIR        p2

#define LCD_RS 		p1_6
#define LCD_EN 		p1_7		//LCD ENABLE
#define LCD_BF 		p2_7		//LCD DB7
#define LCD_DATA 	p2		//LCD DATA

LCD_Command(0x01);	//position of charater
	LCD_Command(0x83);	//cursor position
	LCD_SendData('A');	//write character A
void LCD_Init()
{
	LCD_DATA=0x38;	//8 bit mode
	LCD_RS=0;		//Selected command register
	LCD_EN=1;		//Enable
		LCD_Busy();		//delay
	LCD_EN=0;

	
	LCD_DATA=0x0F;	//Display on cusrnor blinking
	LCD_RS=0;
	LCD_EN=1;
	LCD_Busy();
	LCD_EN=0;
	
	
	LCD_DATA=0x01;	//clear lcd
	LCD_RS=0;
	LCD_EN=1;
	LCD_Busy();
	LCD_EN=0;
	
	
	LCD_DATA=0x06;	//Entry Mode
	LCD_RS=0;
	LCD_EN=1;
	LCD_Busy();
	LCD_EN=0;
	
}

void LCD_Command(_UBYTE var)	//LCD COmmand ex:var=0x01
{
	//LCD_Busy();
	LCD_DATA=var;
	LCD_RS=0;
	LCD_EN=1;
	LCD_Busy();
	//LCD_Busy1(1);
	LCD_EN=0;
	
}

void LCD_SendData(_UBYTE var)	//Send Data write characte ex:var='A'
{
	//LCD_Busy();
	LCD_DATA=var;
	LCD_RS=1;
	LCD_EN=1;
	LCD_Busy();
	//LCD_Busy1(1);
	LCD_EN=0;

}
void LCD_Busy()	//Delay
{
	_UINT i,j;
	for(i=0;i<50;i++)
		for(j=0;j<255;j++);
}
 

I have not seen the datasheet of your MCU but if it has different register for PORT and PORT DIRECTION then your DIRECTION defines are wrong as it is using the sane PORT and PIN values used for LCD DATA and CONTROL lines. See datasheet for register used for setting the pins as output pins.


PDx register is used for Port Direction settings. I don't know whether it can be used like PD2_7 but try and see.


Code C - [expand]
1
2
3
4
5
6
7
8
9
#define LCD_RS_DIR  PD1_6
#define LCD_EN_DIR  PD1_7
#define LCD_BF_DIR  PD2_7
#define LCD_DATA_DIR    PD2
 
#define LCD_RS  P1_6
#define LCD_EN  P1_7        
#define LCD_BF      P2_7        
#define LCD_DATA    P2

 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top