[SOLVED] 16F877a with 16x2 LCD (jhd 162a) problem

Status
Not open for further replies.

varunme

Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,765
My 16F877a interfacing with JHD 162A setup shows black boxes , when I apply reset a number of times some unrecognized characters will show , what can be the reason ? btw , am using hitech c , 20Mhz crystal
 
Last edited:

its delay routine problem.. try trial and error for delay. increase the delay.
 

please post your code....and also check the voltage... use 5volts regulator because it happened to me earlier when i used cheap dc transformer....
 

check the regulator. if it is overheating place a 100 uF capacitor in the lcd.
check the delays in the initialization procedure of lcd.
check the connections to the lcd, check if RS and enable pins are interchanged
check the R/W pin connected to ground or the proper pin of microcontroller
place some delay in the lcd_strobe macro.
 

LCD.c

/*
* 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)
* PORTC bit 0 is connected to the LCD RS input (register select)
* PORTC bit 2 is connected to the LCD EN bit (enable)
*
* To use these routines, set up the port I/O (TRISC, TRISD) then
* call lcd_init(), then other routines as required.
*
*/

#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 20000000
#endif


#include <htc.h>
#include "lcd.h"


__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT);

#define LCD_RS RC0
#define LCD_RW RC1
#define LCD_EN RC2

#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)
{
__delay_us(200);
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);
__delay_ms(20);
}

/* 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()
{
TRISC=0;
TRISD=0;
PORTC=0;
PORTD=0;
char init_value;

ADCON1 = 7; // Disable analog pins on PORTE

init_value = 0x3;

LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;

__delay_ms(30); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(20);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(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
}


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

main.c

#include <htc.h>
#include "lcd.h"

void
main(void)
{

lcd_init();

lcd_goto(0); // select first line

lcd_puts("Welcome");

lcd_goto(0x40); // Select second line

lcd_puts("Hello world");


for(;;
}


//////////////////////////////////////////////////////////////////
 
Last edited:

I have added the delay , but now grey squares begin to appear
 
Last edited:

Your description about hardware and definition is software mistaches

* PORTD bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
* PORTC bit 3 is connected to the LCD RS input (register select)
* PORTC bit 1 is connected to the LCD EN bit (enable)

#define LCD_RS RC0
#define LCD_RW RC1
#define LCD_EN RC2

Check Enable pin and Register pins
 

I am using this very confusing board

 

Your description about hardware and definition is software mistaches
Check Enable pin and Register pins
Description about hardware and definition are just copied from a sample , i forgot to update with my hardware

---------- Post added at 10:37 ---------- Previous post was at 10:24 ----------

The characters are shifting from left to right
 

No I can see the characters , if I press reset a fewtimes , contrast is not an issue
 

After altering delay a number of times the characters are now recognizable and excatly what I have given , but that comes only after reseting the micro a number of times , what can be the reason ?
 

After altering delay a number of times the characters are now recognizable and excatly what I have given , but that comes only after reseting the micro a number of times , what can be the reason ?

Seems it have timing problem. Increase timings and check.
 
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
It was a combination of many mistakes , thanks for all inputs from all member for solving my problems

the mistakes are
  • The Enable pin was connected incorrectly ,
  • The delay for the initialization also was not correct
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…