16F877a MCU and HD44870 LCD

Status
Not open for further replies.

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Visit site
Activity points
6,936
Dear All
This is related to PIC 16F877a MCU and HD44870 LCD ( 4bit Mode)

My LCD and MCU connections are as follows.

RD4 to LCD data pin 4
RD5 to LCD data pin 5
RD6 to LCD data pin 6
RD7 to LCD data pin 7

LCD 0,1,2,3 pins are connected to GND

Code:
void lcddata(unsigned char value)
	{
LCD_RS = 1; 
LCD_DATA= (value&0xf0);
LCD_STROBE;
LCD_DATA = ((value<<4)&0xf0);
LCD_STROBE;
__delay_ms(10);
	}


Above codes data write for 4bit mode and it is working

However my new hardware design was changed as follows

RD0 to LCD data pin 4
RD1 to LCD data pin 5
RD2 to LCD data pin 6
RD3 to LCD data pin 7

Could you please advice me how may I change my above lcddata program accordant to new hardware design
Thanks in advance
 

LCD_DATA MSB LSB interchanged

- - - Updated - - -




Code:
void lcddata(unsigned char value)
	{
LCD_RS = 1; 

LCD_DATA= ((val>>4)&0x0F);
LCD_STROBE;
LCD_DATA =(val&0x0F); 
LCD_STROBE;

__delay_ms(10);
	}
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Jestin thanks for the reply

I changed as follows but still problem remaining unchanged
Please advice

Code:
void lcddata(unsigned char value)
	{
LCD_RS = 1; 
LCD_DATA= ((value>>4)&0x0f);
LCD_STROBE;
LCD_DATA = (value&0x0f);
LCD_STROBE;
__delay_ms(10);
	}

void lcdcmd(unsigned char value)
	{
LCD_RS = 0;
LCD_DATA= ((value>>4)&0x0f);
LCD_STROBE;
LCD_DATA = (value&0x0f);
LCD_STROBE;
__delay_ms(10);
	}

Dear can you please check this program is correct

Code:
LCD_EN =0;
 __delay_us(10);  
	LCD_DATA =0X30;
     	LCD_STROBE;
       	__delay_ms(2);

       LCD_RS=0;
       __delay_us(10);  
	LCD_DATA =0X30;
     	LCD_STROBE;
	__delay_ms(2);

        LCD_RS=0;
       __delay_us(10);  
	LCD_DATA =0X30;
       	LCD_STROBE;
	__delay_ms(2);

       LCD_RS=0;
       __delay_us(10);  
	LCD_DATA =0X20;
     	LCD_STROBE;
	__delay_ms(2);

lcdcmd(0x28);
__delay_ms(15);
lcdcmd(0x01);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x80);
__delay_ms(10);
lcdcmd(0x0c);
__delay_ms(10);

Please advice
 
Last edited:

try this lcd init

Code:
LCD_EN =0;
 __delay_us(10);  
	LCD_DATA =0X3;
     	LCD_STROBE;
       	__delay_ms(2);

       LCD_RS=0;
       __delay_us(10);  
	LCD_DATA =0X3;
     	LCD_STROBE;
	__delay_ms(2);

        LCD_RS=0;
       __delay_us(10);  
	LCD_DATA =0X3;
       	LCD_STROBE;
	__delay_ms(2);

       LCD_RS=0;
       __delay_us(10);  
	LCD_DATA =0X2;
     	LCD_STROBE;
	__delay_ms(2);
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear jestin thanks for the reply

Please accept my apologize for my fault

Actually Connection could be as follows

RD0 to LCD pin 7
RD1 to LCD pin 6
RD2 to LCD pin 5
RD3 to LCD pin 4

Can you Please modify the flowing codes for above connection

Code:
void lcddata(unsigned char value)
	{
LCD_RS = 1; 
LCD_DATA= ((value>>4)&0x0f);
LCD_STROBE;
LCD_DATA = (value&0x0f);
LCD_STROBE;
__delay_ms(10);
	}

void lcdcmd(unsigned char value)
	{
LCD_RS = 0;
LCD_DATA= ((value>>4)&0x0f);
LCD_STROBE;
LCD_DATA = (value&0x0f);
LCD_STROBE;
__delay_ms(10);
	}
Please advice
Thanks in advance
 




Code:
void lcd_init() 
{
       EN=0;
       LCD_RS = 0; 
       __delay_us(10);  
	LCD_DATA=0X0c;
     	ENBL;
       	__delay_ms(2);

       LCD_RS = 0; 
       __delay_us(10);  
	LCD_DATA=0X0c;
     	LCD_STROBE;
	__delay_ms(2);

         LCD_RS = 0; 
       __delay_us(10);  
	LCD_DATA=0X0c;
       	LCD_STROBE;
	__delay_ms(2);

        LCD_RS = 0; 
       __delay_us(10);  
	LCD_DATA=0X04;
     	LCD_STROBE;
	__delay_ms(2);

	lcdcmd(0X28);
	__delay_ms(5);
	lcdcmd(0X0);
	__delay_ms(5);
	lcdcmd(0X01);
	__delay_ms(5);
	lcdcmd(0X06);
	__delay_ms(5);






void lcddata(unsigned char val)
	{
//8bit reversing 

val = (val & 0x0F) << 4 | (val & 0xF0) >> 4;
val = (val & 0x33) << 2 | (val & 0xCC) >> 2;
val = (val & 0x55) << 1 | (val & 0xAA) >> 1;


LCD_RS = 1; 
LCD_DATA=(val&0x0f);
LCD_STROBE;
LCD_DATA =((val>>4)&0x0f);
LCD_STROBE;
__delay_ms(10);
	}


void lcdcmd(unsigned char val)
	{

//8bit reversing 
val = (val & 0x0F) << 4 | (val & 0xF0) >> 4;
val = (val & 0x33) << 2 | (val & 0xCC) >> 2;
val = (val & 0x55) << 1 | (val & 0xAA) >> 1;

LCD_RS = 0; 
LCD_DATA=(val&0x0f);
LCD_STROBE;
LCD_DATA =((val>>4)&0x0f);
LCD_STROBE;
__delay_ms(10);
	}
 

Dear justing
Thanks for the reply

please see the codes and advice with wrong
Code:
#include <htc.h>
#define _XTAL_FREQ 4000000 // 4 MHz clock 

__CONFIG(0X3F39);
#define LCD_EN RC6
#define	LCD_RS RC7
#define LCD_DATA	PORTD
#define	LCD_STROBE  LCD_EN = 1; __delay_ms(1); LCD_EN=0;

unsigned char time [6] = {"Time:"};

void lcddata(unsigned char value)
	{
LCD_RS = 1; 
LCD_DATA= (value&0x0f);
LCD_STROBE;
LCD_DATA = ((value>>4)&0x0f);
LCD_STROBE;
__delay_ms(10);
	}

void lcdcmd(unsigned char value)
	{
LCD_RS = 0;
LCD_DATA= (value&0x0f);
LCD_STROBE;
LCD_DATA = ((value>>4)&0x0f);
LCD_STROBE;
__delay_ms(10);
	}

void display (){
	lcdcmd(0x80);
		for (char i=0;i<5;i++){
			lcddata(time[i]);
			
					}
		}

void lcd_init(){

TRISD	= 0;
TRISB	= 0;
TRISC   = 0;

__delay_us(10);  
lcdcmd(0x03);
__delay_us(10);  
lcdcmd(0x03);
__delay_us(10);  
lcdcmd(0x03);
__delay_us(10);
lcdcmd(0x2);
__delay_us(10);
LCD_STROBE;
__delay_ms(2);

lcdcmd(0x28);
__delay_ms(15);
lcdcmd(0x01);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x80);
__delay_ms(10);
lcdcmd(0x0c);
__delay_ms(10);
				}

void main(){        
        lcd_init();       
			while(1){	
				display();
		
			}
}

Thanks in advance

- - - Updated - - -

Dear justing

Thanks for reply
I got the point
It was great lesson and i studied lot from you
Thanks again and again for help
PA3040
 
Last edited:

the above program is not working in proteus why?
 

This program is working'
 

Attachments

  • PIC16F877 EEPROM 24LC64 Code.7z
    81 KB · Views: 137
Last edited:
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
sorry for the late reply sir i got the output with the help of saeed solutions could u guide me how to get more exposure in embedded programming and guide me to use this lcd program more efficient and easier thanks in advance.

//LCD DISPLAY INTERFACING IN PIC
#include<htc.h>
#include<pic16f877a.h>

#include<string.h>
#include<stdio.h>
#define _XTAL_FREQ 20e6
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);


#define load PORTB

#define rs RC0
#define rw RC1
#define en RC2
#define gas_sensor RC3
void delayms(unsigned int);
void lcd_cmnd(unsigned char);
void lcd_data( char);
void lcd_string(const char*);
void lcd_init(void);
void main(void)
{

TRISB=0;
TRISC0=0;
TRISC1=0;
TRISC2=0;
TRISC3=0X00;
load=0X00;

TRISC3=1;
lcd_init();
lcd_cmnd(0X80);


lcd_cmnd(0X01);
lcd_cmnd(0X80);
delayms(10);
lcd_string("RAJESH");



while(1)
{

if(gas_sensor==1)
{
lcd_init();
lcd_cmnd(0X01);
lcd_string("GAS LEAKAGE IN YOUR HOME");
delayms(10);
TRISC3=1;
}


}
}
void lcd_init()
{

lcd_cmnd(0X38); // configuring LCD as 2 line 5x7 matrix
lcd_cmnd(0X0C); // Display on, Cursor blinking
lcd_cmnd(0X01); // Clear Display Screen
lcd_cmnd(0X06); // Increment Cursor (Right side)
}


void lcd_cmnd(unsigned char command)
{
load=command;
rs=0;
rw=0;
en=1;
delayms(1);
en=0;
}

void lcd_string(const char *string)
{
while(*string)
{
lcd_data(*string++);

}
}

void lcd_data(char data)
{

load=data;
rs=1;
rw=0;
en=1;
delayms(1);
en=0;

}

void delayms(unsigned int delayms)
{
unsigned int i,j;
for(i=0;i<=delayms;i++)
{
for(j=0;j<=500;j++);
}
}
 

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…