LCD Interface code for pic16f877a

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
30
Here is my code for interfacing 16*2 LCD with PIc16f877a in 4 bit mode

Code:
#include<htc.h>
#include<pic.h>

#define RS RB5
#define EN RB4
#define D4 RB0
#define D5 RB1
#define D6 RB2
#define D7 RB3


//#define _XTAL_FREQ 11059200
#include "lcd.h"

__CONFIG (0xFF02);


void main()
{
int i;
  TRISB = 0x00;
  Lcd4_Init();
  while(1)
  {
//Lcd4_Clear();
  //  Lcd4_Set_Cursor(1,1);
    //Lcd4_Write_String("Hello World");
    /* for(i=0;i<15;i++)
    {
    //  __delay_ms(1000);
      Lcd4_Shift_Left();
    }
    for(i=0;i<15;i++)
    {
      __delay_ms(1000);
      Lcd4_Shift_Right();
    } */
//for(i=0;i<100;i++)
    Lcd4_Clear();
    Lcd4_Set_Cursor(2,1);
    Lcd4_Write_Char('a');
   Lcd4_Write_Char('S');
    //__delay_ms(2000);
for(i=0;i<1;i++);
  }
}
code was bulit successfully in MPLAB but there was no indication in the display of my development board

Moderator edit: added [code] ... [/code]
 

AlbertHall

Joined Jun 4, 2014
11,505
First, when you adjust the contrast pot can you show and remove a row of boxes on the top line of the display?
If not, you have a wiring problem with the LCD.
If so then adjust the pot so the boxes are juat barely visible and then run your code.
 

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
30
Yeah now I can able to see 'as' in the display

Next question is when I am sending the string of char ' Hello world' it is not showing in the display
 

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
30
The current version code is here

C:
//#include<htc.h>
#include<pic.h>

#define RS RB5
#define EN RB4
#define D4 RB0
#define D5 RB1
#define D6 RB2
#define D7 RB3


#define _XTAL_FREQ 11059200
#include "lcd.h"

__CONFIG (0xFF02);


void main()
{
int i;
  TRISB = 0x00;
  Lcd4_Init();
  while(1)
  {
   Lcd4_Set_Cursor(1,1);
    Lcd4_Write_String(" Hello World");
    /* for(i=0;i<15;i++)
    {
    //  __delay_ms(1000);
      Lcd4_Shift_Left();
    }
    for(i=0;i<15;i++)
    {
      __delay_ms(1000);
      Lcd4_Shift_Right();
    } */
//for(i=0;i<100;i++)
    Lcd4_Clear();
    Lcd4_Set_Cursor(2,1);
    Lcd4_Write_Char('a');
   Lcd4_Write_Char('s');
    //__delay_ms(2000);
for(i=0;i<10;i++);
  }
}
Moderator edit: added [code=C] ... [/code]
Mod edit: added C specifier to code tag - JohnInTX
 
Last edited by a moderator:

AlbertHall

Joined Jun 4, 2014
11,505
There are no delays in this code to give you a chance to see what is being displayed and line 38 clears the display.
Remove line 38 and uncomment line 42.
 

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
30
Here I used for loop for delay.
When I use
__delay_ms(1000);
It's showing error (499) and also header file for delay is not available here.i.e. delay.h
 

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
30
Code;

C:
//#include<htc.h>
#include<pic.h>

#define RS RB5
#define EN RB4
#define D4 RB0
#define D5 RB1
#define D6 RB2
#define D7 RB3


#define _XTAL_FREQ 11059200
#include "lcd.h"

__CONFIG (0xFF02);


void main()
{
int i;
TRISB = 0x00;
Lcd4_Init();
Lcd4_Clear();

while(1)
{
Lcd4_Set_Cursor(1,1);
   Lcd4_Write_String(" Hello World");
   /* for(i=0;i<15;i++)
    {
  //  __delay_ms(1000);
      Lcd4_Shift_Left();
    }
    for(i=0;i<15;i++)
  {
      __delay_ms(1000);
      Lcd4_Shift_Right();
    } */
//for(i=0;i<100;i++)
  //Lcd4_Clear();
  Lcd4_Set_Cursor(2,1);
  Lcd4_Write_Char('a');
Lcd4_Write_Char('s');
//__delay_ms(2000);
for(i=0;i<10;i++);
}
}
sitll i am not able to see 'Hello world' in the display!
 
Last edited by a moderator:

dendad

Joined Feb 20, 2016
3,911
Have you tried to write the code without using libraries?
That would give you a better understanding of how the LCDs work.
 

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
30
still not working so i changed the code,

anyone can verify this. as per this i need to get 'Hai' in the display, instead of that i getting three char withno defined char

C:
#include<pic.h>

#define EN RB4
#define RS RB5

__CONFIG (0xFF02);

void lcd_delay()
{
int i;
for(i=0;i<255;i++);
}
void lcd_data(unsigned char)
{
    EN=1;
    RS=0;
    lcd_delay();
    EN=0;  
}
void lcd_command(unsigned char)
{
    EN=1;
    RS=1;
    lcd_delay();
    EN=0;  
}
void display_string(const char *s)
    {
        while(*s)
        lcd_data(*s++);
        }
void lcd_Init()
{
    TRISB=0x00;
    PORTB=0x00;
    ADCON1=0x06;
    lcd_command(0X01); //Clear Dispaly
    lcd_delay();
    lcd_command(0x0C); //Display ON, Cursor OFF
    lcd_delay();
    lcd_command(0x80); // Force begin to first character
    lcd_delay();
    lcd_command(0X06); // Inc cursor, Shift cursor to right
    lcd_delay();
    lcd_command(0X28); // 4bit mode,2 lines, 5x7 matrix
    lcd_delay();
}
          
void main()
{
lcd_Init();
    while(1)
{
display_string("Hai");
lcd_delay();
}
}
 
Last edited by a moderator:

dendad

Joined Feb 20, 2016
3,911
For one thing, the RS needs to be stable 100nS before E is active. Is it?
Getting some chars to display ok, but not others, makes me thing you are driving the display too fast. They are pretty slow devices.
That is assuming your code is correct.
 
Top