Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to interface LCD to 89s52/89v51 using I2C protocol

Status
Not open for further replies.

shreyas.p

Newbie level 3
Joined
Oct 18, 2010
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,350
I'm doing a project named "Implementation of I2C Protocol"

I'm required to show the working of I2C protocol using a micro controller (say AT89s52) as a master and two slave-devices.

Slave Device #1: LCD
Slave Device #2: Not decided yet

I did some research on the internet and learned that 89s52 doesn't have hardware I2C interface. So either I will have to go for a microcontroller that does support I2C interface or implement bit-banging to implement I2C.

I have never implement bit-banging. So anyone of you have knowledge about it, please share it with me.
I have searched on the internet and found some bit-banging examples. I have attached it alongwith this thread. Please see.


I'm trying to interface LCD via any general purpose I/O expander
eg. PCF8574

As I'll be having more than 1 device on the I2C BUS, I'm confused as to how to address a specific device.
For eg. I want to display "Hello" on LCD (slave device #1), how do I address it and then write the data to it?? :roll:

Also if someone has any suggestions for the second slave device, please comment.


Some info on I2C:
I2C Bus Technical Overview and FAQ - Embedded Systems Academy

Thanks in advance.
 

Attachments

  • I2C.doc
    86.5 KB · Views: 108

Hi,

1. The document you shared contains code for PIC microcontroller as (TRISB TRISA all happens in PIC).
2. Bit banging is easy, follow rickeys world website on 8051 , so many examples for the same, use keil uvision or RIDE to compile those.

3.To interface LCD over I2C you either need a another MCU as I2C slave or I2C IO expander, another MCU would be cheaper :)

4.While coding the same, just in slave define control registers and data registers. anything you write in data register needed to be displayed on LCD, you may put a looped routine to paste everything on LCD .

5. Or you may simply configure another 8051 MCU as slave IO expander. Depends on choices you have.


happy coding :)
 

Will the following code work, if I implement it on 89s52?



/* Hardware-Specific Support Functions That MUST Be Customized */
#define I2CSPEED 100
void I2CDELAY(int delay) {volatile int v; int i; for(i=0;i<delay;i++) v;}
unsigned READSCL(void) {return(1);} /* Return current level of SCL line, 0 or 1 */
unsigned READSDA(void) {return(1);} /* Return current level of SDA line, 0 or 1 */
void CLRSCL(void) {}
void CLRSDA(void) {}
void ARBITRATION_LOST(void) {}

/* Global Data */
unsigned start = 0;

void i2c_start_cond(void)
{
/* if start == 1, do a restart cond */
if (start)
{
/* set SDA to 1 */
READSDA();
I2CDELAY(I2CSPEED/2);
/* Clock stretching */
while (READSCL() == 0); /* You should add timeout to this loop */
}
if (READSDA() == 0)
{
ARBITRATION_LOST();
}
/* SCL is high, set SDA from 1 to 0 */
CLRSDA();
I2CDELAY(I2CSPEED/2);
CLRSCL();
start = 1;
}

void i2c_stop_cond(void)
{
/* set SDA to 0 */
CLRSDA();
I2CDELAY(I2CSPEED/2);
/* Clock stretching */
while (READSCL() == 0); /* You should add timeout to this loop */
/* SCL is high, set SDA from 0 to 1 */
if (READSDA() == 0)
{
ARBITRATION_LOST();
}
I2CDELAY(I2CSPEED/2);
start = 0;
}

/* Write a bit to I2C bus */
void i2c_write_bit(unsigned bit)
{
if (bit)
{
READSDA();
}
else
{
CLRSDA();
}
I2CDELAY(I2CSPEED/2);
/* Clock stretching */
while (READSCL() == 0); /* You should add timeout to this loop */
/* SCL is high, now data is valid */
/* If SDA is high, check that nobody else is driving SDA */
if (bit)
{
if (READSDA() == 0)
{
ARBITRATION_LOST();
}
}
I2CDELAY(I2CSPEED/2);
CLRSCL();
}

/* Read a bit from I2C bus */
unsigned i2c_read_bit(void)
{
unsigned bit;
/* Let the slave drive data */
READSDA();
I2CDELAY(I2CSPEED/2);
/* Clock stretching */
while (READSCL() == 0); /* You should add timeout to this loop */
/* SCL is high, now data is valid */
bit = READSDA();
I2CDELAY(I2CSPEED/2);
CLRSCL();
return bit;
}

/* Write a byte to I2C bus. Return 0 if ack by the slave */
unsigned i2c_write_byte(int send_start, int send_stop, unsigned char byte)
{
unsigned bit;
unsigned nack;
if (send_start)
{
i2c_start_cond();
}
for (bit = 0; bit < 8; bit++)
{
i2c_write_bit(byte & 0x80);
byte <<= 1;
}
nack = i2c_read_bit();
if (send_stop)
{
i2c_stop_cond();
}
return nack;
}

/* Read a byte from I2C bus */
unsigned char i2c_read_byte(int nak, int send_stop)
{
unsigned char byte = 0;
unsigned bit;
for (bit = 0; bit < 8; bit++)
{
byte <<= 1;
byte |= i2c_read_bit();
}
i2c_write_bit(nak);
if (send_stop)
{
i2c_stop_cond();
}
return byte;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top