Lcd

Thread Starter

musica07

Joined May 28, 2008
13
Hello. I have some problem with my LCD c codes. It is an 8x2 character display. The codes can be builded successfully in the MP lab program but the characters do not show on the LCD. I tried troubleshooting but could not find the problem. Below is my LCD codes. Any help is greatly appreciated:)

#include<p18f2620.h>
#include<delays.h>
#include<string.h>

void Init_LCD(void);
void W_LCD(char,char);
void Out_LCD(char rom near*);
void Out_Dec_LCD(unsigned int i);
void Out_Hex_LCD(unsigned int i);
void Puts_LCD(char *);
char Hex2ascii(char);
void LCD_Intro(void);
void disp_val(void);
void Delay_msec(unsigned int);
void Delay_sec(unsigned int);

#define LCD_DATA PORTB
#define LCD_E PORTBbits.RB1
#define LCD_RS PORTBbits.RB0

#define PB1 PORTAbits.RA4

#define ROM_TYPE (char rom near*)
#define OSC_FREQ 32000000
#define DELAY1MS 8 // 1ms delay = OSC_FREQ/(4*1000*1000)
#define DELAY1S 200 // 1s delay = OSC_FREQ/(4*10000*4)

#define LCD_Line1 W_LCD(0x80,0)
#define LCD_Line2 W_LCD(0xC0,0)
#define CLEAR_LCD W_LCD(0x01,0)
#define HOME_LCD W_LCD(0x02,0)

unsigned char LCD_TEMP;
unsigned int i,x,y;

void main()
{
TRISA = 0b11111111;
TRISB = 0b00001100; // PORTB=>LCD_DATA, RB0=>LCD_E, RB1=>LCD_RS;

while(1)
{
if(PB1==1)
{
Init_LCD();
LCD_Intro();

// Prompt user to put on cuff
W_LCD(0x81,0);
Out_LCD(ROM_TYPE "Put on");
W_LCD(0xC2,0);
Out_LCD(ROM_TYPE "cuff");
Delay_sec(5); // Wait for 5 seconds
W_LCD(0b00000001,0);

for(x=10; x>0; x--)
{
W_LCD(0x81,0);
Out_LCD(ROM_TYPE "s");
Delay_sec(1);
W_LCD(0b00000001,0);
}

for(y=60; y>0; y--)
{
W_LCD(0x81,0);
Out_LCD(ROM_TYPE "Taking");
W_LCD(0xC1,0);
Out_LCD(ROM_TYPE "your BP");
Delay_msec(1000); // Wait for 60 seconds
W_LCD(0b00000001,0);
W_LCD(0x81,0);
Out_LCD(ROM_TYPE " ");
W_LCD(0x81,0);
Out_LCD(ROM_TYPE " ");
Delay_msec(1000);
}
W_LCD(0x83,0);
Out_LCD(ROM_TYPE "BP");
W_LCD(0xC2,0);
Out_LCD(ROM_TYPE "Taken");
Delay_sec(3); // Wait for 3 seconds
W_LCD(0b00000001,0);
disp_val(); // Display BP measurements
}
}
}
// Initialise LCD
void Init_LCD() // LCD display Initialization
{
W_LCD(0x03 ,0);
Delay_msec(15);
W_LCD(0x02 ,0);
Delay_msec(5);
W_LCD(0x28 ,0);
Delay_msec(1);
W_LCD(0x0C ,0);
Delay_msec(1);
W_LCD(0x06 ,0);
Delay_msec(1);
W_LCD(0x01 ,0);
Delay_msec(5);
}
void W_LCD(char x, char i)
{
LCD_RS = i;
LCD_TEMP = x;
LCD_TEMP &= 0xF0;
LCD_E = 1;
LCD_DATA = LCD_TEMP;
Delay_msec(1);
LCD_E = 0;
Delay_msec(1);
LCD_TEMP = x;
LCD_TEMP <<= 4;
LCD_E = 1;
LCD_DATA = LCD_TEMP;
Delay_msec(1);
LCD_E = 0;
Delay_msec(1);
}
// Display const message on LCD
void Out_LCD(char rom near *message)
{
while (*message)
{
W_LCD(*message,1);
message++;
}
}

// Print decimal number to LCD
void Out_Dec_LCD(unsigned int i)
{
char digit, zero, value;
long denom;
denom = 1000000000;
zero = 1;
if (!value)
W_LCD('0',1);
else
{
while (denom)
{
digit = value/denom;
if (digit)
{
W_LCD(digit + '0',1);
zero = 0;
}
else if (!zero)
W_LCD('0',1);
value -= digit*denom;
denom /= 10;
}
}
}

// Print hexadecimal number to LCD
void Out_Hex_LCD(unsigned int i)
{
unsigned char ctmp0, ctmp1, ctmp2, ctmp3;
ctmp0 = 0x000f & i;
i >>= 4;
ctmp1 = 0x000f & i;
i >>= 4;
ctmp2 = 0x000f & i;
i >>= 4;
ctmp3 = 0x000f & i;
W_LCD(Hex2ascii(ctmp3),1);
W_LCD(Hex2ascii(ctmp2),1);
W_LCD(Hex2ascii(ctmp1),1);
W_LCD(Hex2ascii(ctmp0),1);
}

// Convert hexadecimal number to ASCII character
char Hex2ascii(char c)
{
c &= 0x07;
if(c < 0x0a)
return (c + 0x30);
else
return (c + 0x37);
}

// Output variable string to LCD
void Puts_LCD(char *message)
{
while (*message)
{
W_LCD(*message,1);
message++;
}
}

// Output initialisation messages
void LCD_Intro(void)
{
CLEAR_LCD;
W_LCD(0x80,0);
Out_LCD(ROM_TYPE "TEMASEK");
W_LCD(0xC2,0);
Out_LCD(ROM_TYPE "POLY");
Delay_sec(2);
W_LCD(0b00000001,0);

W_LCD(0x80,0);
Out_LCD(ROM_TYPE "PORTABLE");
W_LCD(0x80,0);
Out_LCD(ROM_TYPE "BP METER");
Delay_sec(2);
W_LCD(0b00000001,0);
}

// Display user's bp measurement
void disp_val()
{
while(1)
{
W_LCD(0x82,0);
Out_LCD(ROM_TYPE "SYS");
W_LCD(0xC0,0);
Out_LCD(ROM_TYPE "mmHg");
Delay_sec(4);
W_LCD(0b00000001,0);
W_LCD(0x82,0);
Out_LCD(ROM_TYPE "DIAS");
W_LCD(0xC0,0);
Out_LCD(ROM_TYPE "mmHg");
Delay_sec(4);
W_LCD(0b00000001,0);
W_LCD(0x81,0);
Out_LCD(ROM_TYPE "PULSE");
W_LCD(0xC0,0);
Out_LCD(ROM_TYPE "bpm");
Delay_sec(4);
W_LCD(0b00000001,0);
}
}

// Delay multiple number of msec
void Delay_msec(unsigned int delayInMSEC)
{
while (delayInMSEC--)
Delay1KTCYx(DELAY1MS);
}

// Delay multiple number of sec
void Delay_sec(unsigned int delayInSEC)
{
while (delayInSEC--)
{
Delay10KTCYx(DELAY1S);
Delay10KTCYx(DELAY1S);
Delay10KTCYx(DELAY1S);
Delay10KTCYx(DELAY1S);
}
}
 

SgtWookie

Joined Jul 17, 2007
22,230
I don't see where you are initializing the LCD display?

This must be done before you can display anything on it.

Have a look at this sample code:
Rich (BB code):
/*
 *	LCD interface example
 *	Uses routines from delay.c
 *	This code will interface to a standard LCD controller
 *	like the Hitachi HD44780. It uses it in 4 bit mode, with
 *	the hardware connected as follows (the standard 14 pin 
 *	LCD connector is used):
 *	
 *	PORTD bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
 *	PORTA bit 3 is connected to the LCD RS input (register select)
 *	PORTA bit 1 is connected to the LCD EN bit (enable)
 *	
 *	To use these routines, set up the port I/O (TRISA, TRISD) then
 *	call lcd_init(), then other routines as required.
 *	
 */

#include	<pic.h>
#include	"lcd.h"
#include	"delay.h"

#define	LCD_RS RA3
#define	LCD_RW RA2
#define LCD_EN RA1

#define LCD_DATA	PORTD

#define	LCD_STROBE()	((LCD_EN = 1),(LCD_EN=0))

/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)
{
	DelayUs(40);
	LCD_DATA = ( ( c >> 4 ) & 0x0F );
	LCD_STROBE();
	LCD_DATA = ( c & 0x0F );
	LCD_STROBE();
}

/*
 * 	Clear and home the LCD
 */

void
lcd_clear(void)
{
	LCD_RS = 0;
	lcd_write(0x1);
	DelayMs(2);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
	LCD_RS = 1;	// write characters
	while(*s)
		lcd_write(*s++);
}

/* write one character to the LCD */

void
lcd_putch(char c)
{
	LCD_RS = 1;	// write characters
	lcd_write( c );
}


/*
 * Go to the specified position
 */

void
lcd_goto(unsigned char pos)
{
	LCD_RS = 0;
	lcd_write(0x80+pos);
}
	
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
	char init_value;

	ADCON1 = 0x06;	// Disable analog pins on PORTA

	init_value = 0x3;
	TRISA=0;
	TRISD=0;
	LCD_RS = 0;
	LCD_EN = 0;
	LCD_RW = 0;
	
	DelayMs(15);	// wait 15mSec after power applied,
	LCD_DATA	 = init_value;
	LCD_STROBE();
	DelayMs(5);
	LCD_STROBE();
	DelayUs(200);
	LCD_STROBE();
	DelayUs(200);
	LCD_DATA = 2;	// Four bit mode
	LCD_STROBE();

	lcd_write(0x28); // Set interface length
	lcd_write(0xF); // Display On, Cursor On, Cursor Blink
	lcd_clear();	// Clear screen
	lcd_write(0x6); // Set entry Mode
}
 
Last edited:
Top