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.

4-bit LCD problem while interfacing, program fluctuation

Status
Not open for further replies.

ucsam

Advanced Member level 4
Joined
Oct 12, 2010
Messages
119
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
kathmandu,nepal
Activity points
2,058
i have interfaced LCD in 8 bit mode, but when i tried to do in 4bit mode, then there is fluctuation while running the program, i am using 8051, it all works well with 8bit mode, but the program on 4bit sometimes works and sometime doesn't. What might be the reason?
 

the lcd initialisation commands are different for 8 bit and 4 bit... for 8 bit it is 0x30, 0x38, and for 4 bit it is 0x20 and 0x28.. the remaining commands are same...

you cannot run 8 bit program by just configuring the hardware to 4 bit... the data and commands has to be shifted and sent from controller...

put your code and circuit......
 
post your code and circuit... Is it on simulation or actual hardware.....
 

this link explains how to set the lcd for 4 or 8 bit, and how to send the commands **broken link removed**
 
i have changed the initialization codes as well, other wise how would it run once and not run the other time. I have attached the code, this is actually a library/header file.

---------- Post added at 03:30 ---------- Previous post was at 03:28 ----------

sorry i m going to paste the code..some thing i going wrong with the server


#ifndef __LCD_H__
#define __LCD_H__

typedef unsigned char bit_8;


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LCD Port Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

sbit rs=P2^0; //lcd pin (4)

sbit en=P2^2; //lcd pin (6)

bit status=0;
sbit lcd_on=P3^7;

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LCD Command MACROS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#define lcd_delay 400
#define LCD P2

#define LCD_CLEAR 0x1
#define RETURN_HOME 0x2
#define DEC_CURSOR 0x4
#define INC_CURSOR 0x6
#define DISP_OFF_CUR_OFF 0x8
#define DISP_OFF_CUR_ON 0xA
#define DISP_ON_CUR_OFF 0xC
#define DISP_ON_CUR_BLINK 0xE
#define SHIFT_CUR_LEFT 0x10
#define SHIFT_CUR_RIGHT 0x14
#define SHIFT_DISP_LEFT 0x18
#define SHIFT_DISP_RIGHT 0x1C

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Prototypes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void lcd_init_write(unsigned char);
void lcd_ini(void); // Initialize LCD
void lcd_busy(void); // Check Busy
void lcd_com(bit_8); // Sending Command
void lcd_data(bit_8); // Sending single character
void string(unsigned char item[],unsigned char); // Sending String
void wrt_value(bit_8); // Sending 8-bit Decimal value
void cursor(bit_8,bit_8); // Bringing Cursor to (X,Y) location X -> (1,2) and Y -> (1-16)
void delay(unsigned int);
void buzz(unsigned char);
void delay_(unsigned int time);






/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LCD Initialization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void lcd_ini(void)
{ unsigned char i;
for(i=0;i<3;i++)
{delay(lcd_delay);
lcd_init_write(0x30);
}

lcd_init_write(0x20); // 0x20 for 4-bit
delay(lcd_delay);
lcd_com(0x28); //Display Off, Cursor Off, Blink Off
delay(lcd_delay);
lcd_com(4); // Clear Screen & Returns the Cursor Home
delay(lcd_delay);
lcd_com(0x85);
delay(lcd_delay);
lcd_com(6); //Inc cursor to the right when writing and don’t shift screen
delay(lcd_delay);
lcd_com(1);
delay(lcd_delay);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
waiting for LCD to execute
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing command to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void lcd_com(unsigned char a)
{
unsigned char temp;
if(status)
{
status=0;
goto next;
}
rs=0;
next:

temp=a;
temp&=0xf0; // Mask Lower 4 Bits
LCD&=0x0f; // Make No Affect on 0ther Port Pins
LCD|=temp; // Send Higher Nibble to LCDPORT
en=1;
delay(lcd_delay); //Send Enable Signal to LCD
en=0;
temp=a<<4; //Left Shift Byte Four Times
temp&=0xf0; // Mask Higher 4 Bits
LCD&=0x0f; // Make No Affect on 0ther Port Pins
LCD|=temp; // Send Lower Nibble to LCDPORT
en=1;
delay(lcd_delay); // Send Enable Signal to LCD
en=0;




}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing string to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


void string(unsigned char item[16],unsigned char line)
{
unsigned char fac,i,j,cmd;
for(i=0;item!='\0';i++);
if(i<=16)
{
fac=(16-i)/2;
if(line==1)
cmd=128;
else if(line==2)
cmd=192;
lcd_com(1);
lcd_com(cmd+fac);
for(j=0;item[j]!='\0';j++)
{
lcd_data(item[j]);
delay(5);
}
}

else
{
lcd_com(1);
lcd_on=1;
buzz(2);
}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing data to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void lcd_data(unsigned char a)
{
status=1;
rs=1;
lcd_com(a);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing 8-bit Value to LCD
e.g. writing 63 on LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_value(bit_8 x)
{
bit_8 temp;
temp = x/0x10;
if(temp>9)
return;
else
lcd_data(temp+0x30);
temp = x%0x10;
if(temp>9)
return;
else
lcd_data(temp+0x30);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bringing Cursor to (X,Y) location of LCD
X -> 1,2
Y -> 1,16
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void cursor(bit_8 x, bit_8 y)
{
if((x<1||x>2)&&(y<1||y>16))
{
x=1;
y=1;
}
if(x == 1)
lcd_com(0x7F+y);
else
lcd_com(0xBF+y);
}

/* -------------------------------------------------------------*/


void lcd_init_write(unsigned char a)
{
rs=0;
LCD=a;
en=1;
delay(lcd_delay);
en=0;
}


#endif


this is on actual hardware, hardware connections are all good. I am using JHD162A,

lcd port ====> P2
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top