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 code doesnt work

Status
Not open for further replies.

meche

Junior Member level 2
Joined
Apr 12, 2017
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
252
This is my first time of interfacing an LCD module to a pic mcu (pic16f877a) but so far my code(I actually got it from the internet) doesn't work when I simulate it in proteus. My LCD is connected to port B. Below is my main c code for writing to the LCD.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#define _XTAL_FREQ 8000000
#include"newxc8_header.h"
#include"Lcd_hd44780_pic16.h"
 
 
#define RS RB5
#define EN RB4
#define D4 RB0
#define D5 RB1
#define D6 RB2
#define D7 RB3
 
void main (void)
{
    TRISB=0x00;
    //Initialize the LCD Module
  LCDInit(LS_NONE);
 
  //Clear the display
  LCDClear();
  LCDGotoXY(0x00);
 
  //Write a string
  LCDWriteString("Hello World !");
 
  while(1)
  {
     //Do nothing, just loop indefinitely
  }
 
}



And below is my config file (newxc8_header.h) for the project.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[/ PIC16F877A Configuration Bit Settings
 
// 'C' source line config statements
 
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
 
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
 
#include <xc.h>]


I compiles in MPLAB X IDE with XC8 compiler. I simulated in proteus. The LCD lights up but no message is displayed.

Please can someone help me out. I want to start using LCD for my projects but I am finding it very difficult.
 
Last edited by a moderator:

hello,


LCDGotoXY(0x00);

only one parameter ?
X and Y values ?

post also "Lcd_hd44780_pic16.h"
because
what means
LCDInit(LS_NONE); ?

 

Below is the "Lcd_hd44780_pic16.h" code:

Code:
/* 
 * File:   lcd_hd44780_pic16.h
 * Author: Avinash
 *
 * Created on 6 June, 2013, 11:51 AM
 */

#ifndef LCD_HD44780_PIC16_H
#define	LCD_HD44780_PIC16_H

#include <xc.h>
#include <stdint.h>

#ifdef	__cplusplus
extern "C" {
#endif


/************************************************
	LCD CONNECTIONS
*************************************************/

//LCD Data Port
//Port PD0-PD3 are connected to D4-D7
#define LCD_DATA        D   //Port PD0-PD3 are connected to D4-D7
#define LCD_DATA_POS    0

//Register Select (RS)
//RS is connected to Port D bit 4
#define LCD_RS_PORT     D
#define LCD_RS_POS      4

//Read/Write (RW)
//RW is connected to Port D bit 5
#define LCD_RW_PORT     D
#define LCD_RW_POS      5

//Enable signal (E)
//E is connected to Port D bit 6
#define LCD_E_PORT      D
#define LCD_E_POS       6

/***********************************************

LCD Type Selection
Uncomment Just one of them

************************************************/

//#define LCD_TYPE_202	//For 20 Chars by 2 lines

//#define LCD_TYPE_204	//For 20 Chars by 4 lines

#define LCD_TYPE_162	//For 16 Chars by 2 lines

//#define LCD_TYPE_164	//For 16 Chars by 4 lines


//************************************************




//************************************************

#define LS_BLINK 0B00000001
#define LS_ULINE 0B00000010
#define LS_NONE	 0B00000000



/***************************************************
			F U N C T I O N S
****************************************************/



void LCDInit(uint8_t style);
void LCDWriteString(const char *msg);
void LCDWriteInt(int val,int8_t field_length);
void LCDGotoXY(uint8_t x,uint8_t y);

//Low level
void LCDByte(uint8_t,uint8_t);
#define LCDCmd(c) (LCDByte(c,0))
#define LCDData(d) (LCDByte(d,1))

void LCDBusyLoop();

/***************************************************
			F U N C T I O N S     E N D
****************************************************/


/***************************************************
	M A C R O S
***************************************************/
#define LCDClear() LCDCmd(0b00000001)
#define LCDHome()  LCDCmd(0b00000010)

#define LCDWriteStringXY(x,y,msg) {\
 LCDGotoXY(x,y);\
 LCDWriteString(msg);\
}

#define LCDWriteIntXY(x,y,val,fl) {\
 LCDGotoXY(x,y);\
 LCDWriteInt(val,fl);\
}
/***************************************************/




/*_________________________________________________________________________________________*/

#ifdef	__cplusplus
}
#endif

#endif	/* LCD_HD44780_PIC16_H */

Below is also the "Lcd_hd44780_pic16.c" code:

Code:
/******************************************************************************

                          eXtreme Electronics xAPI(TM)
			  ----------------------------
xAPI is a Powerful but easy to use C library to program the xBoard(TM)
series of PIC development board.


The APIs are highly documented and easy to use even by a beginner.

 Log
 08 June 2013
    *Negative Integer printing corrected and tested.
    *Cursor style LS_NONE corrected and tested.
    *Custom chars tested.

For More Info Log On to
[url]www.eXtremeElectronics.co.in[/url]

Copyright 2008-2013 eXtreme Electronics India

                            LCD Core - ver 1.0 (2013)

This module is used for interfacing with Standard Alpha Numeric LCD Modules.
For More information please see supplied tutorials and videos.

                                     NOTICE

NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR PUBLISHED WITHOUT A
WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE LIBRARY, NOR ANY PART
OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS INTENDED TO BE USED FOR
HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT TO USE THEM IN
COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.


WRITTEN BY:
AVINASH GUPTA
[email]me@avinashgupta.com[/email]

*******************************************************************************/


#include <xc.h>
#include <stdint.h>

#include "lcd_hd44780_pic16.h"
#include "myutils.h"
#include "custom_char.h"

#define LCD_DATA_PORT 	PORT(LCD_DATA)
#define LCD_DATA_TRIS 	TRIS(LCD_DATA)

#define LCD_E           PORTBIT(LCD_E_PORT,LCD_E_POS)
#define LCD_E_TRIS      TRISBIT(LCD_E_PORT,LCD_E_POS)

#define LCD_RS          PORTBIT(LCD_RS_PORT,LCD_RS_POS)
#define LCD_RS_TRIS     TRISBIT(LCD_RS_PORT,LCD_RS_POS)

#define LCD_RW          PORTBIT(LCD_RW_PORT,LCD_RW_POS)
#define LCD_RW_TRIS     TRISBIT(LCD_RW_PORT,LCD_RW_POS)


#define SET_E() (LCD_E=1)
#define SET_RS() (LCD_RS=1)
#define SET_RW() (LCD_RW=1)

#define CLEAR_E() (LCD_E=0)
#define CLEAR_RS() (LCD_RS=0)
#define CLEAR_RW() (LCD_RW=0)

#ifdef LCD_TYPE_162
	#define LCD_TYPE_204
#endif

#ifdef LCD_TYPE_202
	#define LCD_TYPE_204
#endif


void LCDByte(uint8_t c,uint8_t isdata)
{
//Sends a byte to the LCD in 4bit mode
//cmd=0 for data
//cmd=1 for command


//NOTE: THIS FUNCTION RETURS ONLY WHEN LCD HAS PROCESSED THE COMMAND

uint8_t hn,ln;			//Nibbles
uint8_t temp;

hn=c>>4;
ln=(c & 0x0F);

if(isdata==0)
	CLEAR_RS();
else
	SET_RS();

__delay_us(0.5);		//tAS

SET_E();

//Send high nibble

temp=(LCD_DATA_PORT & (~(0X0F<<LCD_DATA_POS)))|((hn<<LCD_DATA_POS));
LCD_DATA_PORT=temp;

__delay_us(1);			//tEH

//Now data lines are stable pull E low for transmission

CLEAR_E();

__delay_us(1);

//Send the lower nibble
SET_E();

temp=(LCD_DATA_PORT & (~(0X0F<<LCD_DATA_POS)))|((ln<<LCD_DATA_POS));

LCD_DATA_PORT=temp;

__delay_us(1);			//tEH

//SEND

CLEAR_E();

__delay_us(1);			//tEL

LCDBusyLoop();
}

void LCDBusyLoop()
{
	//This function waits till lcd is BUSY

	uint8_t busy,status=0x00,temp;

	//Change Port to input type because we are reading data
	LCD_DATA_TRIS|=(0x0f<<LCD_DATA_POS);

	//change LCD mode
	SET_RW();		//Read mode
	CLEAR_RS();		//Read status

	//Let the RW/RS lines stabilize

	__delay_us(0.5);		//tAS


	do
	{

		SET_E();

		//Wait tDA for data to become available
		__delay_us(0.5);

		status=(LCD_DATA_PORT>>LCD_DATA_POS);
		status=status<<4;

		__delay_us(0.5);

		//Pull E low
		CLEAR_E();
		__delay_us(1);	//tEL

		SET_E();
		__delay_us(0.5);

		temp=(LCD_DATA_PORT>>LCD_DATA_POS);
		temp&=0x0F;

		status=status|temp;

		busy=status & 0b10000000;

		__delay_us(0.5);

                CLEAR_E();
		__delay_us(1);	//tEL
	}while(busy);

	CLEAR_RW();		//write mode

        //Change Port to output
	LCD_DATA_TRIS&=(~(0x0F<<LCD_DATA_POS));

}

void LCDInit(uint8_t style)
{
	/*****************************************************************

	This function Initializes the lcd module
	must be called before calling lcd related functions

	Arguments:
	style = LS_BLINK,LS_ULINE(can be "OR"ed for combination)
	LS_BLINK : The cursor is blinking type
	LS_ULINE : Cursor is "underline" type else "block" type
        LS_NONE : No visible cursor

	*****************************************************************/

	//After power on Wait for LCD to Initialize
	__delay_ms(30);

	//Set IO Ports
	LCD_DATA_TRIS&=(~(0x0F<<LCD_DATA_POS)); //Output

        LCD_E_TRIS=0;   //Output
        LCD_RS_TRIS=0;  //Output
        LCD_RW_TRIS=0;  //Output

	LCD_DATA_PORT&=(~(0x0F<<LCD_DATA_POS));//Clear data port

        CLEAR_E();
	CLEAR_RW();
	CLEAR_RS();

	//Set 4-bit mode
	__delay_us(0.5);	//tAS

	SET_E();
	LCD_DATA_PORT|=((0b00000010)<<LCD_DATA_POS); //[B] To transfer 0b00100000 i was using LCD_DATA_PORT|=0b00100000
	__delay_us(1);
	CLEAR_E();
	__delay_us(1);

	//Wait for LCD to execute the Functionset Command
	LCDBusyLoop();                                    //[B] Forgot this delay

	//Now the LCD is in 4-bit mode

	
	LCDCmd(0b00101000);             //function set 4-bit,2 line 5x7 dot format
        LCDCmd(0b00001100|style);	//Display On

	/* Custom Char */
        LCDCmd(0b01000000);

	uint8_t __i;
	for(__i=0;__i<sizeof(__cgram);__i++)
		LCDData(__cgram[__i]);


}
void LCDWriteString(const char *msg)
{
	/*****************************************************************

	This function Writes a given string to lcd at the current cursor
	location.

	Arguments:
	msg: a null terminated C style string to print

	Their are 8 custom char in the LCD they can be defined using
	"LCD Custom Character Builder" PC Software.

	You can print custom character using the % symbol. For example
	to print custom char number 0 (which is a degree symbol), you
	need to write

	LCDWriteString("Temp is 30%0C");
                                  ^^
                                   |----> %0 will be replaced by
                                          custom char 0.

	So it will be printed like.

		Temp is 30°C

	In the same way you can insert any symbols numbered 0-7


	*****************************************************************/
 while(*msg!='\0')
 {
 	//Custom Char Support
	if(*msg=='%')
	{
		msg++;
		int8_t cc=*msg-'0';

		if(cc>=0 && cc<=7)
		{
			LCDData(cc);
		}
		else
		{
			LCDData('%');
			LCDData(*msg);
		}
	}
	else
	{
		LCDData(*msg);
	}
	msg++;
 }
}

void LCDWriteInt(int val,int8_t field_length)
{
	/***************************************************************
	This function writes a integer type value to LCD module

	Arguments:
	1)int val	: Value to print

	2)unsigned int field_length :total length of field in which the value is printed
	must be between 1-5 if it is -1 the field length is no of digits in the val

	****************************************************************/

	char str[5]={0,0,0,0,0};
	int i=4,j=0;

        //Handle negative integers
        if(val<0)
        {
            LCDData('-');   //Write Negative sign
            val=val*-1;     //convert to positive
        }

	while(val)
	{
            str[i]=val%10;
            val=val/10;
            i--;
	}
	if(field_length==-1)
		while(str[j]==0) j++;
	else
		j=5-field_length;

	
	for(i=j;i<5;i++)
	{
	LCDData(48+str[i]);
	}
}
/********************************************************************

Position the cursor to specific part of the screen

********************************************************************/
void LCDGotoXY(uint8_t x,uint8_t y)
{
 	if(x>=20) return;

	#ifdef LCD_TYPE_204

	switch(y)
	{
		case 0:
			break;
		case 1:
			x|=0b01000000;
			break;
		case 2:
			x+=0x14;
			break;
		case 3:
			x+=0x54;
			break;
	}

	#endif

	#ifdef LCD_TYPE_164
	switch(y)
	{
		case 0:
			break;
		case 1:
			x|=0b01000000;
			break;
		case 2:
			x+=0x10;
			break;
		case 3:
			x+=0x50;
			break;
	}

	#endif

	x|=0b10000000;
  	LCDCmd(x);
}
 
Last edited by a moderator:

You can find a tutorial on adding code/syntax tags HERE go read it. Moderators are getting tired of correcting your posts.
 

At post #1 you said that LCD is connected to port B.
At post #3 you've posted a code which makes use of the port D to drive the LCD.

In addition, you have not made any mention to paulfujo's remark, which points to an issue that would generate a compilation error due to insufficient arguments for the LCDGotoXY function - for which neither of the 2 arguments could assume the value 0x00.
 
Sorry for all that. Its just that the code tag icon doesnt appear on my console when I am using the "Quick reply" . Henceforth, I will be using "Go advanced" where the code tag icon is available.

- - - Updated - - -

* The LCDGotoXY function was not part of the original code. I included it to see the effect but which I later remove.

* While trying to troubleshoot, I used different ports to try and drive the LCD.

* currently the port connection to the LCD is as follows:
RS - RA3
RW - RA2
EN - RA1

D4 - RD0
D5 - RD1
D6 - RD2
D7 - RD3

and the current main c code is as below:

Code:
#define _XTAL_FREQ 4000000
#include"newxc8_header.h"
#include"Lcd_hd44780_pic16.h"


#define RW RA2
#define RS RA3
#define E RA1
#define D4 RD0
#define D5 RD1
#define D6 RD2
#define D7 RD3

void main (void)
{
    TRISA=0x00;
    TRISD=0x00;
    //Initialize the LCD Module
  LCDInit(LS_NONE);

  //Clear the display
  LCDClear();

  //Write a string
  LCDWriteString("Hello World !");

  while(1)
  {
     //Do nothing, just loop indefinitely
  }

}
 

Unless you have edited the "Lcd_hd44780_pic16.h" file you have a conflict of pin numbers.

A ".h" file is simply slotted into line in the program by the compiler as though you had typed it there. When it defines which pins are used the program will use those until it sees code that redefines them to other uses. So there is no error in the code that the compiler would recognize but the signals sent to the pins are being changed between the header and main modules of the program.

Personally, I would remove the "Lcd_hd44780_pic16.h" line completely and paste the LCD function part of it as a subroutine in the main program.

Brian.
 

hello,


a LCD used in 4 bit data mode, don't need R/W commande.
LCD R/W pin must be connected to Gnd.
 
a LCD used in 4 bit data mode, don't need R/W command.
LCD R/W pin must be connected to Gnd.
I would say don't necessarily need read access, LCD in 8 bit mode neither needs it, by the way. But you're required to use maximal delay for all commands which slows down the interface. Polling for ready makes sense in an effective LCD driver implementation.
 

Sorry for all that. Its just that the code tag icon doesnt appear on my console when I am using the "Quick reply" . Henceforth, I will be using "Go advanced" where the code tag icon is available.

Why? adding them by hand isn't hard, just

Code Verilog - [expand]
1
2
3
[code]
...your code..
[/code]

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top