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.

lcd in 4 bit mode problem

Status
Not open for further replies.

GURMEET singh

Junior Member level 2
Joined
Oct 10, 2012
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,402
Please correct me,if i am doing wrong..its not working:sad:


// Config word
#define _LEGACY_HEADERS
// Define pins
#define _XTAL_FREQ 40000000

#include <htc.h>
// Main function
#define lcd_port PORTB
#define LCD_EN RA1
#define LCD_RS RA0


////////////////////////////////////////////////////////////////////
//void delay();
//void lcd_cmd ();
//void lcd_data (unsigned char dat);
//void lcd_init ();

/////////////////////delay routine///////////////////////////////////


/////////////////////main program///////////////////////////////////////
void delay(unsigned int temp)
{
while(temp!=0)
{
temp--;
}
}

/////////////////////main program///////////////////////////////////////
void lcd_cmd (unsigned char cmd)
{
unsigned char a;
a=cmd;
lcd_port = (cmd & 0xf0);
LCD_RS=0;
LCD_EN=1;
delay(300);
LCD_EN=0;
a=cmd;
lcd_port = ((cmd<<4) & 0xf0);
LCD_RS=0;
LCD_EN=1;
delay(300);
LCD_EN=0;
delay(500);
}

void lcd_data (unsigned char dat)
{
unsigned char a;
a=dat;
lcd_port = (dat & 0xf0);
LCD_RS=1;
LCD_EN=1;
delay(300);
LCD_EN=0;
a=dat;
lcd_port = ((dat<<4) & 0xf0);
LCD_RS=1;
LCD_EN=1;
delay(300);
LCD_EN=0;
delay(500);
delay(500);
}
void lcd_string(unsigned char *str)
{
while(*str)
lcd_data(*str++);
delay(300);
}
void lcd_init ()
{
lcd_cmd (0x28); // 4-bit mode - 2 line - 5x7 font.
lcd_cmd (0x0C); // Display no cursor - no blink.
lcd_cmd (0x06); // Automatic Increment - No Display shift.
lcd_cmd (0x80); // Address DDRAM with 0 offset 80h.
}
void main(void)
{
TRISA=0x00;
TRISB=0x00;
delay(200);
lcd_init ();
lcd_cmd (0x81);
lcd_string("HELLO");
lcd_cmd (0xc1);
lcd_string("NITISH");
// while(1);


}
 

Without even looking into code flow, your delays are probably wrong. LCDs are quite slow devices and especially during initialization it is very important they have long enough to digest the bytes you feed them. A simple count down of 200 or 300 to zero is likely to take far shorter than the several mS delays needed. In particular, note how long the LCD needs from powering up to being ready for the start of initializing data, most need around 100mS. It looks like you are using a PIC although you haven't said which type, your present 'delay(200)' probably only lasts around 200uS when you really need several mS.

Why not use a timer to generate interrupts at say 1mS intervals and use those as the basis of delays, that lets you set precise timings from 1mS up to 65.5 seconds if you pass an integer value to a 1mS count down routine.

Brian.
 

Without even looking into code flow, your delays are probably wrong. LCDs are quite slow devices and especially during initialization it is very important they have long enough to digest the bytes you feed them. A simple count down of 200 or 300 to zero is likely to take far shorter than the several mS delays needed. In particular, note how long the LCD needs from powering up to being ready for the start of initializing data, most need around 100mS. It looks like you are using a PIC although you haven't said which type, your present 'delay(200)' probably only lasts around 200uS when you really need several mS.

I have corrected all the delays ...now its working fine,Thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top