I2C LCD interface with 8051

Status
Not open for further replies.

ISPN

Newbie level 6
Joined
Jul 11, 2014
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
110
Could you please explanin me how to initialize LCD under I2C bus? I used PCF8574T based I2C module to interface LCD. However, I am confusing how to write the command and data in efficient manner.
 

If you want simple program for I2C ((PCF8574T)) LCD Module (20*4) then here you go
Code:
#include <18f4520.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd20x4.h"
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use I2C (master, sda = PIN_C4, scl = PIN_C3)

#define LCDADDR        0x70 // default slave address
#define ON            0x08 
#define OFF            0x00

#define LCD_SET_BIT(x)    fetch_data(x)

byte LCD_ADDR=0x4e; //I2C slave address for Funduino LCD module 

//Transmittion data
void transceiver(unsigned char data)
   {
        
        i2c_start();
        i2c_write(LCD_ADDR); //the slave addresse
        i2c_write(data);    
        i2c_stop();
   }

//Clocking the LCD's enable pin during transmit data
void fetch_data(unsigned char data)
   {
        data=data|0b00000100;//set pin E is a 1
        transceiver(data);
        delay_ms(1);
        data=data-4;//toggle E back to 0
        transceiver(data);
        delay_ms(1);

   }

void lcd_init()
{

   //Request works on the command by set the RS = 0 R/W = 0 write
        LCD_SET_BIT(0x00);
        LCD_SET_BIT(0x10);
        LCD_SET_BIT(0x00);
        LCD_SET_BIT(0x00);
        LCD_SET_BIT(0x10);
           //First state in 8 bit mode
        LCD_SET_BIT(0x30);
        LCD_SET_BIT(0x30);
           //Then set to 4-bit mode
        LCD_SET_BIT(0x30);
        LCD_SET_BIT(0x20);
           //mode 4 bits, 2 lines, characters 5 x 7 (28 h)
        LCD_SET_BIT(0x20);
        LCD_SET_BIT(0x80);
           //no need cursor on (0Ch)
        LCD_SET_BIT(0x00);
        LCD_SET_BIT(0xC0);
           //the cursor moves to the left (06 h)
        LCD_SET_BIT(0x00);
        LCD_SET_BIT(0x60);
           //clears the display
        LCD_SET_BIT(0x00);
        LCD_SET_BIT(0x10);
}

void lcd_clear()
{
    LCD_SET_BIT(0x00);
    LCD_SET_BIT(0x10);
}

// Need the backlight lid.
void lcd_backlight(byte state)
{
  LCD_SET_BIT(0x00);
  LCD_SET_BIT(state);
}

//Display the character on LCD screen.
void display(char in_data)
{
        char data;
        data=in_data&0xF0;
        data=data+1; //set RS pin to 1
        fetch_data(data);
        data=in_data&0x0F;
        data=data<<4;
        data=data+1; //set RS pin to 1
        fetch_data(data);

}

//Make the x/y pointer 
void lcd_goto_xy(byte x, byte y)
{
  byte ptr1, ptr2;
  
  fetch_data(0x10);
  fetch_data(0x00);

  switch(y)
  {
   case 1:
          ptr1=line_1[x]&0xF0;// stamp the high bit
          ptr2=line_1[x]&0x0F;// stamp the low bit
        
     break;

   case 2:
           ptr1=line_2[x]&0xF0;
          ptr2=line_2[x]&0x0F;
         
     break;

   case 3:
          ptr1=line_3[x]&0xF0;
          ptr2=line_3[x]&0x0F;
          
     break;

   case 4:
           ptr1=line_4[x]&0xF0;
          ptr2=line_4[x]&0x0F;
         
     break;

   default:
          fetch_data(0x80);
          fetch_data(0x00);
     break;
     
  }
          ptr2=ptr2<<4;
          fetch_data(ptr1);
          fetch_data(ptr2);          
} 

void main()
{ 
  char led=0;
  delay_ms(20);
  lcd_init();
  
        lcd_goto_xy(0,1);
        display("ABCDEFGHIJKLMNOPQRST");
        lcd_goto_xy(0,2);
        display("12345678901234567890"); 
        lcd_goto_xy(0,3);
        display("AAAAAAAAAAAAAAAAAAAA");
        lcd_goto_xy(6,4);
        display("It's Work.!!!");
        lcd_backlight(ON);
     while(1);
     
}
 
Reactions: ISPN

    ISPN

    Points: 2
    Helpful Answer Positive Rating
Thank you so much. I am going to use at89c2051 and assembly. I will try to convert ot to assembly. Thanks a lot
 

Don't try. This code is useless for you. No one knows what for it was given to you.
2051 doens't have h/w I2c. You have to use s/w method.
You can try use this document to make your own library for I2c. It is more or less clear to understanding.
**broken link removed**
 
This code is useless for you.
Not necessarily if you put in software I2C routines for i2c_xxx() functions. Or replace transceiver() respectively.
 
Reactions: ISPN

    ISPN

    Points: 2
    Helpful Answer Positive Rating
Thank you so much. now i am trying.

- - - Updated - - -

Thank you so much

- - - Updated - - -

This is the code i wrote in assembly. But it does not work. I ma not sure weather i wrote device address correctly. Second i am not sure, that i have to check busy flag in i2C lcd implementation. Please go through my code and give me some suggestions.
 

Attachments

  • I2C.txt
    4.4 KB · Views: 159

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…