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.

[SOLVED] PIC -> LCD Communication problem

Status
Not open for further replies.

Mutad0r

Junior Member level 3
Junior Member level 3
Joined
Feb 13, 2013
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,608
So, I have searched the forum, and asked my teacher, and everything seems to be correct,but it doesn¨t work!

I have a 3,3v PIC Communicating with a 5v LCD in 4-bit mode, I'm using 5v tolerant pins and the lcd logic has minimum of 2.2V.

I have measured all the pins and they all rise and fall as they should, but all I get from the LCD is all the pixels in the top row lit. I'm at a loss of what to do at this point, and hope someone spots whatever I have done wrong.

Code:
/*CONFIGURATION*/
#pragma config OSC=INTOSCPLL
#pragma config WDTEN = OFF
#pragma config XINST = OFF


#define _XTAL_FREQ 8000000





/*INCLUDES*/
#include <stdio.h>
#include <stdlib.h>
#include <p18cxxx.h>
#include <pconfig.h>
#include <xc.h>
#include <I2C.h>
#include <plib.h>

#define RW_PIN   LATDbits.LATD1		/* PORT for RW */
#define TRIS_RW  TRISDbits.TRISD1  	/* TRIS for RW */

#define RS_PIN   LATDbits.LATD0		/* PORT for RS */
#define TRIS_RS  TRISDbits.TRISD0   	/* TRIS for RS */

#define E_PIN    LATDbits.LATD2 		/* PORT for E  */
#define TRIS_E   TRISDbits.TRISD2   	/* TRIS for E  */

void Onetime_Setup(void);
void I2COMM(void);
void init_LCD(void);

void LCD_DATA(unsigned  char DB7,unsigned char DB6,unsigned char DB5,unsigned char DB4)
{
    LATDbits.LATD7=DB7;
    LATDbits.LATD6=DB6;
    LATDbits.LATD5=DB5;
    LATDbits.LATD4=DB4;


	RW_PIN=0;
        E_PIN=1;
        __delay_us(500);
	E_PIN=0;
}

int main()
{
 Onetime_Setup();
    init_LCD();


 while(1)
 {


     ///////////////////
	RS_PIN=1;
        LCD_DATA(0,1,0,0);
	__delay_ms(1);

        RS_PIN=1;
        LCD_DATA(1,0,0,0);
        __delay_ms(1);

        //////////////

             ///////////////////
        RS_PIN=1;
        LCD_DATA(0,1,1,0);
	__delay_ms(1);

	RS_PIN=1;
        LCD_DATA(0,1,0,1);
	__delay_ms(1);

        //////////////
             ///////////////////
	RS_PIN=1;
        LCD_DATA(0,1,1,0);
	__delay_ms(1);

	RS_PIN=1;
        LCD_DATA(1,1,0,0);
        __delay_ms(1);

        //////////////
                     ///////////////////
	RS_PIN=1;
        LCD_DATA(0,1,1,0);
	__delay_ms(30);

	RS_PIN=1;
        LCD_DATA(1,1,0,0);
        __delay_ms(1);

                     ///////////////////

	RS_PIN=1;
        LCD_DATA(0,1,1,0);
	__delay_ms(1);

	RS_PIN=1;
        LCD_DATA(1,1,1,1);
        __delay_ms(1);

        //////////////
        //////////////

 }  //WHILE(1) END

 }   //MAIN() END




//--------------------------PERSONAL FUNCTIONS------------------------------

void Onetime_Setup()    //Setup at start of main()
{
    //SETUP PORTS//
            TRISA=0b11111111;
            TRISB=0b11111111;
            TRISC=0b11111111;
            TRISD=0b00001000;
    //PORT SETUP END//
            RW_PIN=0;
                    E_PIN=0;
                    RS_PIN=0;
}




void init_LCD(void){


	__delay_ms(30);
    	__delay_ms(30);
    //INITIALIZE
	RS_PIN=0;
        LCD_DATA(0,0,1,1);
	__delay_ms(30);

	RS_PIN=0;
        LCD_DATA(0,0,1,1);
	__delay_ms(10);

	RS_PIN=0;
        LCD_DATA(0,0,1,1);
	__delay_ms(5);

        //SET 4BIT, LINES, FONT

	RS_PIN=0;
        LCD_DATA(0,0,1,0);
	__delay_ms(2);

	RS_PIN=0;
        LCD_DATA(1,0,0,0);
	__delay_ms(2);

        //wHATEVAS

	RS_PIN=0;
        LCD_DATA(0,0,0,0);
	__delay_ms(2);

	RS_PIN=0;
        LCD_DATA(1,1,0,0);
	__delay_ms(2);

	RS_PIN=0;
        LCD_DATA(0,0,0,0);
	__delay_ms(2);

	RS_PIN=0;
        LCD_DATA(0,0,0,1);
	__delay_ms(2);

	RS_PIN=0;
        LCD_DATA(0,0,0,0);
	__delay_ms(2);
        ///final

	RS_PIN=0;
        LCD_DATA(0,1,1,1);
	__delay_ms(2);

	}
 

which controller you are using?
is it one time programmable? i think it is one time programmable bcoz it is 18cxx and c is shows that.
so if you burn for many times then previous programs remain in as it is. so it will not worked properly.
 

What do you mean one time programmable? I'm using PIC18F47J53 and I have other projects that work on this mcu ( even I2C communication ) and I've changed programs on them multiple times.
 

Re: PIC -&gt; LCD Communication problem

yes but you didn't mention the controller you are using and as you use header file of controller that is

Code:
#include <p18cxxx.h>

so i conclude that it is one time programmable.
and in your lcd program you can use a byte instead of bit presentation.

- - - Updated - - -

you can use code like this i did't check as i don't have this compiler.

Code:
/*CONFIGURATION*/
#pragma config OSC=INTOSCPLL
#pragma config WDTEN = OFF
#pragma config XINST = OFF


#define _XTAL_FREQ 8000000





/*INCLUDES*/
#include <stdio.h>
#include <stdlib.h>
#include <p18cxxx.h>
#include <pconfig.h>
#include <xc.h>
#include <I2C.h>
#include <plib.h>

#define RW_PIN   LATDbits.LATD1		/* PORT for RW */
#define TRIS_RW  TRISDbits.TRISD1  	/* TRIS for RW */

#define RS_PIN   LATDbits.LATD0		/* PORT for RS */
#define TRIS_RS  TRISDbits.TRISD0   	/* TRIS for RS */

#define E_PIN    LATDbits.LATD2 		/* PORT for E  */
#define TRIS_E   TRISDbits.TRISD2   	/* TRIS for E  */

void Onetime_Setup(void);
void I2COMM(void);
void init_LCD(void);

void LCD_DATA(unsigned  char dat)		// function to send data
{
	RW_PIN=0;		
	RS_PIN = 1;
	dat = dat & 0xF0;		// send higher nibble first
	PORTD |=dat;				// ORing with control lines and data
        E_PIN=1;
        __delay_us(500);
	E_PIN=0;
	
	dat = dat << 4;
	dat = dat & 0xF0;		// send lower nibble second
	PORTD |=dat;				// ORing with control lines and data
        E_PIN=1;
        __delay_us(500);
	E_PIN=0;	
}

void LCD_COMMAND(unsigned  char dat)
{
	RW_PIN=0;		
	RS_PIN = 0;
	dat = dat & 0xF0;		// send higher nibble first
	PORTD |=dat;				// ORing with control lines and data
        E_PIN=1;
        __delay_us(500);
	E_PIN=0;
	
	dat = dat << 4;
	dat = dat & 0xF0;		// send lower nibble second
	PORTD |=dat;				// ORing with control lines and data
        E_PIN=1;
        __delay_us(500);
	E_PIN=0;	
}

int main()
{
 Onetime_Setup();
    init_LCD();


 while(1)
 {

 }  //WHILE(1) END

 }   //MAIN() END




//--------------------------PERSONAL FUNCTIONS------------------------------

void Onetime_Setup()    //Setup at start of main()
{
    //SETUP PORTS//
            TRISA=0b11111111;
            TRISB=0b11111111;
            TRISC=0b11111111;
            TRISD=0b00001000;
    //PORT SETUP END//
            RW_PIN=0;
                    E_PIN=0;
                    RS_PIN=0;
}




void init_LCD(void){


	__delay_ms(30);
    //INITIALIZE
	__delay_ms(30);
	LCD_COMMAND(0x03);
	__delay_ms(30);
	LCD_COMMAND(0x03);
	__delay_ms(30);
	LCD_COMMAND(0x03);		// use such function to send coomands
	__delay_ms(30);
	
 LCD_COMMAND(0x02);
	__delay_ms(30);
 LCD_COMMAND(0x28);
	__delay_ms(30);
 LCD_COMMAND(0x01);
	__delay_ms(30);
 LCD_COMMAND(0x0c);
	__delay_ms(30);
	
	}

complete this code and use this code then after.
 

Re: PIC -&gt; LCD Communication problem

That DOES seem to be a better way to do it... I'll try it tomorrow, as school is over for today. I'm not so sure if there's a difference though, but I'll try anything :)
 

but all I get from the LCD is all the pixels in the top row lit.

Seems to be a common error, improper initialization / improper timing.
 

The code provided by embpic does seem to work, as far as compiling and programming goes. The LCD just sits there with all the pixels on the top row lit though, I'd at LEAST would expect it to be empty..I Got problems with the E pin connection and changed the connector I use to a more reliable one. Now a single square on top was blinking slightly, but all the pixels on the top row were still lit... When I turned it off, I saw the shadow of the cursor as the screen turned off, but as I turned it all on again, it didn¨t work anymore....

 

Ground RW pin as u don't need RW pin for writing operation. Also try with D0-D4 instead of D4-D7 initially then change hardware pin after ur LCD work,
 

Ground RW pin as u don't need RW pin for writing operation. Also try with D0-D4 instead of D4-D7 initially then change hardware pin after ur LCD work,

Grounding the RW did squat..

About the code from embpic: does it seem ok? The initialization seems a bit strange, considering it tries to send the byte in 2 parts before the lcd is set in 4-bit mode...
 

SOLVED: Here's the code that finally worked. The example I used had it initiated with 8 bit communication, even though it was a 4-bit example. The Lit pixels on all the squares was solved with lowering the contrast (forsome reason)

Code:
/*
 * File:   newmain.c
 * Author: Maarek Aabmets
 *
 * Created on 4. maaliskuuta 2013, 9:49
 */

/*CONFIGURATION*/
#pragma config OSC=INTOSCPLL
#pragma config WDTEN = OFF
#pragma config XINST = OFF


#define _XTAL_FREQ 8000000





/*INCLUDES*/
//#include <inputread.h>      //Personal Header
#include <stdio.h>
#include <stdlib.h>
#include <p18cxxx.h>
#include <pconfig.h>
#include <xc.h>
#include <I2C.h>
#include <plib.h>

#define RW_P   LATDbits.LATD1		/* PORT for RW */

#define RS_P   LATDbits.LATD0		/* PORT for RS */

#define E_P    LATDbits.LATD2 		/* PORT for E  */

void Onetime_Setup(void);
void I2COMM(void);
void init_LCD(void);
void LCD_DATA(unsigned  char);
void LCD_COMM(unsigned  char);
unsigned char TEMP, REPEAT;


int main()
{
    REPEAT=0;
    while (REPEAT<100)
    {
        __delay_ms(30);
        REPEAT++;
    }
REPEAT=0;
 Onetime_Setup();
    init_LCD();

LCD_DATA(0b00110001);
LCD_DATA(0b00110010);
LCD_DATA(0b00110011);
LCD_DATA(0b00110100);
LCD_DATA(0b00110101);
LCD_DATA(0b00110110);
LCD_DATA(0b00110111);

while(1)
 {

 }  //WHILE(1) END

 }   //MAIN() END




//--------------------------PERSONAL FUNCTIONS------------------------------

void Onetime_Setup()    //Setup at start of main()
{
    //SETUP PORTS//
            TRISA=0b11111111;
            TRISB=0b11111111;
            TRISC=0b11111111;
            TRISD=0b00001000;
    //PORT SETUP END//
                    RW_P=0;
                    E_P=0;
                    RS_P=0;
}




void init_LCD(void)
{
    /////////INITIALS
        PORTD= 0b00110000;
        RS_P=0;
	RW_P=0;
        E_P=1;
        __delay_us(500);
	E_P=0;
        __delay_ms(30);


        RS_P=0;
	RW_P=0;
        E_P=1;
        __delay_us(500);
	E_P=0;
        __delay_ms(30);

        RS_P=0;
	RW_P=0;
        E_P=1;
        __delay_us(500);
	E_P=0;
        __delay_ms(30);

        /////////INITIALSEND NOW S ET 4 BIT MODE
        PORTD= 0b00100000;
        RS_P=0;
	RW_P=0;
        E_P=1;
        __delay_us(500);
	E_P=0;
        __delay_ms(30);
        //////NOW IS 4 BIT MODE
        LCD_COMM(0b00101000);
                __delay_ms(30);
        LCD_COMM(0b00001000);
                __delay_ms(30);
        LCD_COMM(0b00000001);
                __delay_ms(30);
        LCD_COMM(0b00000110);
                __delay_ms(30);
        LCD_COMM(0b00001111);
                __delay_ms(30);
}

void LCD_DATA(unsigned  char BYTE)
{
        TEMP= BYTE & 0b11110000;
        PORTD= TEMP;
        RS_P=1;
	RW_P=0;
        E_P=1;
        __delay_us(500);
	E_P=0;
        __delay_us(500);

        BYTE = BYTE << 4;
        TEMP= BYTE & 0b11110000;
        PORTD= TEMP;
        RS_P=1;
	RW_P=0;
        E_P=1;
        __delay_us(500);
	E_P=0;
        __delay_us(500);
}

void LCD_COMM(unsigned  char BYTE)
{
        TEMP= BYTE & 0b11110000;
        PORTD= TEMP;
        RS_P=0;
	RW_P=0;
        E_P=1;
        __delay_us(500);
	E_P=0;
        __delay_us(500);


        BYTE = BYTE << 4;
        TEMP= BYTE & 0b11110000;
        PORTD= TEMP;
        RS_P=0;
	RW_P=0;
        E_P=1;
        __delay_us(500);
	E_P=0;
        __delay_us(500);

}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top