Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

simulation is ok but in real device no display??

Status
Not open for further replies.

haowhaow

New Member
im newbie in programming.....
im using hi tech compiler, pic18f4520 and 16 x 2 lcd.....
after simulation is ok....but in real device, my lcd is blank .....
i dunno how to configure my code.....can anyone here helps me to solve it??


here is my code

#include <htc.h>
#include <math.h>


#include "lcd.h"
__CONFIG(1, FCMDIS & IESODIS & HS);
__CONFIG(2, BORDIS & BORV45 & PWRTEN & WDTDIS & WDTPS1);
__CONFIG(3, CCP2RB3 & LPT1DIS & MCLRDIS & 0xFDFF);
__CONFIG(4, DEBUGDIS & XINSTDIS & LVPDIS & STVRDIS);


//Simple Delay Routine
void Wait(unsigned int delay)
{
for(;delay;delay--)
__delay_us(100);
}
void ADCInit(void){
ADCON0=0b00000001; // select Fosc/2
ADCON1=0b00001110; // select left justify result. A/D port configuration 0
ADCON2=0b00001010;
}
unsigned char read_adc(unsigned char ){
ADON = 1; // initiate conversion on the selected channel
GODONE=1 ;//Start conversion
while(GODONE= 1)continue;
return(ADRESH); // return 8 MSB of the result
}
void main()
{
//Let the LCD Module start up
Wait(100);

//Initialize the LCD Module
LCDInit(LS_BLINK);

//Initialize the ADC Module
ADCInit();

//Clear the Module
LCDClear();

//Write a string at current cursor pos
LCDWriteString("Temperature:");
LCDWriteStringXY(5,1,"Degree C");


while(1)
{
unsigned int val;
unsigned int t; //Temperature

val=read_adc(); //Read Channel 0

t=round(((val*5000)/1024)/10);

LCDWriteIntXY(0,1,t,3);//Prit IT!


Wait(1000);

}
}
 
Do you have a contrast control pot connected to the Vee (contrast) pin. The contrast control can be touchy and if it's not adjusted properly, you won't see anything on the LCD screen.

I'd post a link showing the connections but I have been forbidden to do so here.
 
The H-tech library Is for 4 bit or 8 bit mode it uses the lower 4 bits of the the PORTD And PORTA 1-3 for control lines You have to init one eight bits or four mode.
You have not set any mode you don't set the fosc 4 mhz 20mhz etc which errors the delay?

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 or 8 bit mode
 *	
 */

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

static bit fourbit;		// four or eight bit mode?

#ifdef CHECKBUSY

unsigned char 
lcd_read_cmd_nowait(void)
{
	unsigned char c, readc;

	LCD_DATA_TRIS	 = INPUT_DATA;

	LCD_RW = 1; // Read LCD
	asm("nop"); // short propagation delay
	asm("nop"); // short propagation delay

	if (fourbit)
	{
		LCD_STROBE_READ(readc); // Read high nibble
		// Move 4 bits to high nibble while zeroing low nibble
		c = ( ( readc << 4 ) & 0xF0 ); 
		LCD_STROBE_READ(readc); // Read low nibble
    		c |= ( readc & 0x0F ); // Or in 4 more bits to low nibble
	}
	else
	{
		LCD_STROBE_READ(readc); 
		c = readc;
	}
	LCD_RW = 0; // Return to default mode of writing LCD
	LCD_DATA_TRIS	 = OUTPUT_DATA; // Return to default mode of writing LCD

	return(c);
}

void
lcd_check_busy(void) // Return when the LCD is no longer busy, or we've waiting long enough!
{
	// To avoid hanging forever in event there's a bad or 
	// missing LCD on hardware.  Will just run SLOW, but still run.
	unsigned int retry; 
	unsigned char c;

	for (retry=1000; retry-- > 0; ) {
		c = lcd_read_cmd_nowait();
		if (0==(c&0x80)) break; // Check busy bit.  If zero, no longer busy
	}
}

#endif

/* send a command to the LCD */
void
lcd_cmd(unsigned char c)
{
	LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h

	if (fourbit)
	{
		LCD_DATA = ( ( c >> 4 ) & 0x0F );
		LCD_STROBE();
    		LCD_DATA = ( c & 0x0F );
		LCD_STROBE();
	}
	else
	{
		LCD_DATA = c;
		LCD_STROBE();
	}
}

/* send data to the LCD */
void
lcd_data(unsigned char c)
{
	LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h

	LCD_DATA = 0;
	LCD_RS = 1;
	if (fourbit)
	{
    		LCD_DATA |= ( ( c >> 4 ) & 0x0F );      
		LCD_STROBE();
		LCD_DATA &= 0xF0;
		LCD_DATA |= ( c & 0x0F ); 
		LCD_STROBE();
	}
	else
	{
		LCD_DATA = c;
		LCD_STROBE();
	}
	LCD_RS = 0;
}

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

void
lcd_puts(const char * s)
{
	while(*s)
		lcd_data(*s++);
}

/* initialize the LCD */
void
lcd_init(unsigned char mode)
{
	char init_value;

	fourbit		= 0;
	if (mode == FOURBIT_MODE){
		fourbit = 1;
		init_value = 0x3;
	}else{
		init_value = 0x3F;
	}
	LCD_RS = 0;
	LCD_EN = 0;
	LCD_RW = 0;
	LCD_RS_TRIS	 = OUTPUT_PIN;
	LCD_EN_TRIS	 = OUTPUT_PIN;
	LCD_RW_TRIS	 = OUTPUT_PIN;
	LCD_DATA_TRIS	 = OUTPUT_DATA;
	DelayMs(15);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	DelayMs(5);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	DelayUs(200);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	
	if (fourbit){
		LCD_WAIT; //may check LCD busy flag, or just delay a little, depending on lcd.h
		LCD_DATA = 0x2; // Set 4-bit mode
		LCD_STROBE();

		lcd_cmd(0x28); // Function Set
	}else{
		lcd_cmd(0x38);
	}
	lcd_cmd(0xF); //Display On, Cursor On, Cursor Blink
	lcd_cmd(0x1); //Display Clear
	lcd_cmd(0x6); //Entry Mode
	lcd_cmd(0x80); //Initialize DDRAM address to zero
}

The LCD. H
Code:
#ifndef _LCD_H_
#define _LCD_H_

/*
 *	LCD interface header file
 */

/* 	Defining CHECKBUSY will check if the LCD is busy. The RW bit of the 
 * 	LCD must connected to a port of the processor for the check busy
 * 	process to work.
 * 
 * 	If CHECKBUSY is not defined it will instead use a delay loop.
 * 	The RW bit of the LCD does not need to connected in this case.
 */

// #define CHECKBUSY	1

#ifdef CHECKBUSY
	#define	LCD_WAIT lcd_check_busy()
#else
	#define LCD_WAIT DelayMs(5)

#endif

#define MESSAGE_LINE		0x0

#define LCD_RS	LA3
#define LCD_EN	LA1
#define LCD_RW	LA2 
#define LCD_DATA	LATD
#define LCD_DATA_PORT	PORTD 
#define LCD_RS_TRIS	TRISA3
#define LCD_EN_TRIS	TRISA1
#define LCD_RW_TRIS	TRISA2 
#define LCD_DATA_TRIS	TRISD

#define FOURBIT_MODE	0x0
#define EIGHTBIT_MODE	0x1
#define OUTPUT_PIN      0x0	
#define INPUT_PIN       0x1	
#define OUTPUT_DATA     0x0	
#define INPUT_DATA      0x0F	

#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

#define LCD_STROBE_READ(value)	LCD_EN = 1; \
				asm("nop"); asm("nop"); \
				value=LCD_DATA_PORT; \
				LCD_EN = 0; 

#define	lcd_cursor(x)			lcd_cmd(((x)&0x7F)|0x80)
#define lcd_clear()			lcd_cmd(0x1)
#define lcd_putch(x)			lcd_data(x)
#define lcd_goto(x)			lcd_cmd(0x80+(x));
#define lcd_cursor_right()		lcd_cmd(0x14)
#define lcd_cursor_left()		lcd_cmd(0x10)
#define lcd_display_shift()		lcd_cmd(0x1C)
#define lcd_home()			lcd_cmd(0x2)

extern void lcd_cmd(unsigned char);
extern void lcd_data(unsigned char);
extern void lcd_puts(const char * s);
extern void lcd_init(unsigned char);

#endif
 
Last edited:

Attachments

  • ad595.JPG
    ad595.JPG
    332.5 KB · Views: 525
Status
Not open for further replies.

Latest threads

Back
Top