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.

can not get a consistent result each time the PIC is powered

Status
Not open for further replies.

hhhsssmmm

Member level 1
Joined
May 14, 2009
Messages
37
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,732
Hello

Im using a PIC18F4220 @ 20Mhz ceramic with C18 compiler.

Im trying to do a simple test program where the PIC sends out the letter 'a' to an LCD connected to PORTD (8bit mode).....no nibbling!

However I can not get a consistent result each time the PIC is powered up. The behaviour is that sometimes the letter 'a' is displayed perfectly and sometimes it is not displayed at all. Sometimes the cursor simply blinks or sometimes the display remains completely blank.

Im using the following LCD module from Palm Technology Co., LTD

Display: LCD 20x2

Model: PMC2002D9-SBLW-1


Below is my test program. Please can some one help me to get the stable letter 'a' result each time the PIC is powered up.

Thank you

Haseeb

Code:
#include <p18f4220.h>
#include <delays.h>
     
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
  
void SendLCD(unsigned char Byte, unsigned char type);

#define LCD LATD //LCD Latch PORT
#define LCD_RS LATEbits.LATE0 //Register Select (0: Instruction/ 1: Data)
#define LCD_E LATEbits.LATE2 //Enable (Starts data read/write)

void main (void)
{   
   
    ADCON1 = 0x0F; //SET ALL PORTS as DIGITAL I/O
   
    PORTD = 0; //initiallize PORTD  
    TRISD = 0x00; //LCD output
   
    PORTE = 0; //initiallize PORTE
    TRISEbits.TRISE0 = 0;  //LCD Register Select (to LCD Pin 4)
    TRISEbits.TRISE2 = 0;  //LCD Enable (to LCD Pin 6)       





    //Initialize the LCD

    LCD = 0x00; //clear LCD PORTD pins

    Delay1KTCYx(100); //delay 20mS

    SendLCD(0x03,0); //Initialization command

    Delay1KTCYx(25); //delay 5mS

    SendLCD(0x03,0); //Initialization command

    Delay10TCYx(75); //delay 150uS

    SendLCD(0x03,0); //Initialization command

    Delay10TCYx(75); //delay 150uS

    SendLCD(0x3C,0); //Interface lenght is 8 bits long, 2-lines, 5x10 dots

    Delay10TCYx(25); //delay 50uS   

    SendLCD(0x10,0); //Turn off LCD

    Delay10TCYx(25); //delay 50uS

    SendLCD(0x01,0); //Clear LCD

    Delay10TCYx(25); //delay 50uS

    SendLCD(0x06,0); //Increment the cursor after each byte written

    Delay10TCYx(25); //delay 50uS

    SendLCD(0x0F,0); //Turn on LCD, cursor on, cursor blinking on

    Delay10TCYx(25); //delay 50uS
   
   




    SendLCD(0x80,0); //Send 0x80 to set line to 1

    Delay10TCYx(25); //delay 50uS   






    SendLCD('a',1); //print letter 'a' on LCD

    while(1); //loop forever



       

} //end of main()
                                                                                               




void SendLCD(unsigned char Byte, unsigned char type)
{
   
    LCD_RS = type; //Set whether it is a Command (0) or Data/Char (1)   

    LCD = 0x00; //Clear the LCD PORTD

    LCD = Byte; //assign the new data to the LCD PORTD

    //Toggle the E(enable)   
    LCD_E = 1; //Set Enable High
   
    //Delay 5uS
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();   
   
    LCD_E = 0; //Set Enable Low

}
 

LCD letter 'a' problem

Hi,
Try this and see what happens:
Code:
#include <p18f4220.h>

#pragma config OSC = HS
#pragma config WDT = OFF //Watchdog Timer off
#pragma config LVP = OFF //LVP off

void main(void);
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void MSDelay(unsigned int time);

#define rs PORTEbits.RE0
#define en PORTEbits.RE2
#define port PORTE

void main(void){
	TRISD = 0;
	TRISB = 0;
	PORTD = 0;
	PORTB = 0;
	en = 0;
	MSDelay(250);
	lcdcmd(0x0C); //display on, cursor off
	MSDelay(15);
	lcdcmd(0x01); //clear LCD
	MSDelay(15);
	lcdcmd(0x06); //shift cursor right
	MSDelay(15);
	lcdcmd(0x80); //first row, first column
	MSDelay(15);
        lcddata('a');
	while(1);
}

void lcdcmd(unsigned char value){
	port = value;
	rs = 0;
	en = 1;
	MSDelay(1);
	en = 0;
}

void lcddata(unsigned char value){
	port = value;
	rs = 1;
	en = 1;
	MSDelay(1);
	en = 0;
}

void MSDelay(unsigned int itime){
	unsigned int i, j;
	for(i=0;i<itime;i++)
		for(j=0;j<135;j++);
}
Hope this helps.
Tahmid.
 

Re: LCD letter 'a' problem

I think the original problem is it doesn't check the LCD status before writing to it. The initialization works because it has fixed delays in it but the as soon as the device mode is set, it is best to poll the status to see if it is safe to write new data.

Brian.
 

Re: LCD letter 'a' problem

Hi hhhsssmmm,

I think you are trying to initialise the lcd before it gets powered up. Use a delay in the code befor you start sending anything to the lcd.
 

LCD letter 'a' problem

I think you are trying to initialise the lcd before it gets powered up. Use a delay in the code befor you start sending anything to the lcd.
Exactly. Try putting a delay of about 250ms like I did in the code.
Tahmid.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top