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.

Please correct this LCD code

Status
Not open for further replies.

Naumanpak

Member level 2
Joined
Nov 19, 2009
Messages
51
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
Pakistan
Activity points
1,635
Hi experts,

I have had a great experience with edaboard, you people have helped me a lot.

I have this LCD code for 16x4 module. But the problem is that every time I run it, i only get black boxes on row 1 and 3. Any suggestions??


thanks


Code:
#include <regx51.h>


#define rs       P2_5
#define rw       P2_6
#define enable   P2_7
#define lcd_port P0
#define XTAL     11.059200  
#define XDIVIDER 12.0       

#define delay_1ms 164 * (XTAL/XDIVIDER)

void delay(unsigned char);
void delay_ms(unsigned int);
void lcd_display(unsigned char);
void Lcd_Ini();
void lcd_command(unsigned char);
main()
{
                unsigned int i;
		lcd_command(0x01);
		Lcd_Ini();
                lcd_command(0x80);
                lcd_display('L');
                lcd_display('I');
                lcd_display('N');
		lcd_display('E');
		lcd_display('1');
                lcd_command(0xC0);
                lcd_display('L');
                lcd_display('I');
                lcd_display('N');
		lcd_display('E');
		lcd_display('2');
		lcd_command(0x94);
                lcd_display('L');
                lcd_display('I');
                lcd_display('N');
		lcd_display('E');
		lcd_display('3');
	        lcd_command(0xd4);
                lcd_display('L');
                lcd_display('I');
                lcd_display('N');
		lcd_display('E');
		lcd_display('4');

				while(1){;}     
}
   
void Lcd_Ini()
{
                lcd_command(0x48);
                delay_ms(30);
                lcd_command(0x0c);
                delay_ms(10);
                lcd_command(0x06);
                delay_ms(10);
                lcd_command(0x01);                    
                delay_ms(10);
}
  
void lcd_command(unsigned char command)
{
                rw=0;
                rs=0;
                lcd_port=command;
                enable=1;
                enable=0;
                delay_ms(5);
}
  
void lcd_display(unsigned char display)
{
                rw=0;
                rs=1;
                lcd_port=display;
				enable=1;
                enable=0;
                delay_ms(1);
}

void delay_ms(unsigned int i)
{
 
       			for(;i!=0x00;i--)
               			{
               			delay(delay_1ms);
                        }
}

void delay(unsigned char j)
{
                for (;j!=0x00;j--)
                        {
                        }
}
 

Some LCDs need software initialization when the hardware initialization does not work.

If you don't initialize correct, they will do weird things.

Google for LCD software initialization and give it a try.

Also insert a NOP between RW and RS setting, this depends on the speed of the system. So try:

RW = 0;
NOP();
RS=0;
NOP();

this is to let the signal to settle.
 

    Naumanpak

    Points: 2
    Helpful Answer Positive Rating
Can you post a picture of what's happening? Do your rows start on row 0, or row 1?

If our rows are laid out as:

Row1
Row2
Row3
Row4

Usually row3 is an extension of row1. There for to type:

edaboard.com is a friendly place to get advice.

It would look like this

Row1: edaboard.com is
Row2:
Row3: a friendly place t
Row4:

Matt
 

    Naumanpak

    Points: 2
    Helpful Answer Positive Rating
thanks Matt & daviddlc for suggestions. I have tried NOP(); but same problem yet.

this is what I get on LCD
 

This is initialisation problem. You have to initialize the lcd before sending data

Nandhu
 

    Naumanpak

    Points: 2
    Helpful Answer Positive Rating
Thanks Nandhu, I did the initialization with Lcd_Ini(); right at the top in main()

Can you please tell me exactly what to do now..?


cheers
 

put in some delay between initialization codes
 

    Naumanpak

    Points: 2
    Helpful Answer Positive Rating
In my previous post, I asked you to Google for LCD software initialization, you will see some examples there.

Added after 5 minutes:

This is what I have for a 2 x 16, you will need to adapt to your needs:

void LCD_InitDisplay()
{
RS = 0; //RS = 0
RW = 0; //R/W = 0
Delay10KTCYx(36); //Wait aprox. 30 mSec.
PORTB = 0x30; //Function Set: 8 bits
Delay10KTCYx(6); //Wait aprox. 5 mSec.
PORTB = 0x30; //Function Set: 8 bits
Delay1KTCYx(2); //Wait aprox 100uSec. (166 uSec)
LCD_WriteCommand(0x38); //Function Set: 8 bits, 2 lines, 5x7 character font
LCD_WriteCommand(0x0C); //ON/OFF Control: Display on,
LCD_WriteCommand(0x01); //Clear display
LCD_WriteCommand(0x06); //Increment, no shift
}//LCD_InitDisplay

Again remember the NOP() after RS and RW settings depends on the system speed.
 

    Naumanpak

    Points: 2
    Helpful Answer Positive Rating
I have made a C18 library to use my LCD. It is not optimized but it works with no problems. Hope it helps.

Code:
/*
Biblioteca para usar LCD com PIC18F4431
Autor: Diego Sá
Data: 07/03/2010
*/

//--Evitar que este header file seja incluído várias vezes no programa
#ifndef __LCD_CONFIG_H //Verifica se a expressão __LCD_CONFIG_H ainda não foi definida
#define __LCD_CONFIG_H //Define a expressão __LCD_CONFIG_H
//--

#define RS PORTBbits.RB2
#define RW PORTBbits.RB3
#define EN PORTBbits.RB4
#define DATA PORTD
#define TRIS_DATA TRISD
#define NEW_LINE 0x0A
#define BUSY_FLAG PORTDbits.RD7

void configLCD(void);
void SendCmdLCD(unsigned char cmd_lcd);
void WriteCharLCD(char e);
void WriteTextLCD(char *text);
void BusyFlagCheck(void);

char j=0;
char d;

void configLCD(void)
{
	TRISBbits.TRISB2=0;  //Pino RS como output
	TRISBbits.TRISB3=0;  //Pino RW como output
	TRISBbits.TRISB4=0;  //Pino EN como output

	EN=0;
	RS=0;
	RW=0;

	BusyFlagCheck();
	
	SendCmdLCD(0b00111000);//Function Set: 8-bit bus mode; 2-line display mode; 5X8 dots format display
	Delay1KTCYx(10); //Delay 5ms
	SendCmdLCD(0b00001111);//Display Control: Display ON; Cursor ON; Cursor blink ON
	Delay1KTCYx(10); //Delay 5ms
	SendCmdLCD(0b00000001);//Clear Display
	Delay1KTCYx(10); //Delay 5ms
	SendCmdLCD(0b00000110);//cursor moves to right; DDRAM adress increased by 1; disply shifting is not performed
}

void SendCmdLCD(unsigned char cmd_lcd)
{
	TRIS_DATA=0b00000000;
	EN=0;
	RW=0;
	RS=0;
	
	DATA=cmd_lcd;

	Delay10TCYx(2); //Aguarda 20 ciclos (10 uSeg)

	EN=1;

	Delay1KTCYx(10);// Aguarda 5ms
			// Ciclos = (TimeDelay * Fosc) / 4
			// Ciclos = (5ms * 8MHz) / 4
			// Ciclos = 10,000
	EN=0;

	Delay10TCYx(2); //Aguarda 20 ciclos (10 uSeg)

	DATA=0b00000000;

	Delay10TCYx(2); //Aguarda 20 ciclos (10 uSeg)

	BusyFlagCheck();
}

void WriteCharLCD(char e)
{
	TRIS_DATA=0b00000000;

	EN=0;
	RW=0;
	RS=0;

	Delay10TCYx(2); //Aguarda 20 ciclos (10 uSeg)

	RS=1;
	DATA=e;

	Delay10TCYx(2); //Aguarda 20 ciclos (10 uSeg)

	EN=1;

	Delay1KTCYx(10);// Aguarda 5ms

	EN=0;

	Delay10TCYx(2); //Aguarda 20 ciclos (10 uSeg)

	DATA=0b00000000;
	RS=0;

	Delay10TCYx(2); //Aguarda 20 ciclos (10 uSeg)

	BusyFlagCheck();
}

void WriteTextLCD(char *text)
{
	j=0;

	while(*text)
	{
		d=*text++;

		if (d==NEW_LINE)
		{
			break;
		}
		else
		{
			WriteCharLCD(d);
			j++;
			if (j>16) break;
		}
	}
}

void BusyFlagCheck(void)
{
	TRIS_DATA=0b11111111;
	
	EN=0;
	RS=0;
	RW=1;
	Delay1KTCYx(10); //Delay 5ms
	
	while (1)
	{
		EN=1;
		Delay10TCYx(1);

		if (BUSY_FLAG==0)
		{
			break;
		}
		else
		{
			EN=0;
			Delay1KTCYx(10); //Delay 5ms
		}
	}

	EN=0;
	RW=0;
	TRIS_DATA=0b00000000;
}

#endif
 

    Naumanpak

    Points: 2
    Helpful Answer Positive Rating
O thank you all! another solution within 24hrs!

I finally got it working with a bit of more delays and setting rs and rw to 0 in initialization.

thank you all once again!!

Added after 3 minutes:

I have another problem but I will post it as an another topic.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top