This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I2C Interfacing EEPROM

I have studied I2C in a number of sites but I can't find the error in my program. I'm trying to write 'A' and read it from the eeprom.

#include<at89x51.h>
#include<lcd.h>
#include<delay.h>
#include<intrins.h>   //For using [_nop_()]
#define ack 1
#define nack 0
sbit sda=P1^0;
sbit scl=P1^1;

void I2C_start() // Start I2C
{
        sda = 1;
        _nop_();_nop_();
        scl = 1;
        _nop_();_nop_();
    sda = 0;
        _nop_();_nop_();
    scl = 0;
        _nop_();_nop_();
}
void I2C_stop()
{
        sda = 0;
        _nop_();_nop_();
        scl = 1;
        _nop_();_nop_();
        sda = 1;
        _nop_();_nop_();
}
void I2C_writebit(unsigned char Data)
{
        unsigned char i;
        for(i=0;i<8;i++)
        {
                sda = (Data & 0x80) ? 1:0;
                scl = 1;
                _nop_();
                scl = 0;
                Data<<=1;
        }
        scl= 1;
        _nop_();_nop_();
        scl = 0;
}

unsigned char I2C_readbit(char ackbit)
{
        unsigned char i, Data=0;
                sda = 1;// not clear Y SDA is out here
        for(i=0;i<8;i++)
                {
                        scl = 1;
                        Data<<=1;
                        Data  = (Data | sda);
                scl = 0;
                        _nop_();
        }
        if(ackbit)
                sda = 0;
                else
                        sda = 1;
        _nop_();_nop_();
                scl = 1;
                _nop_();_nop_();
                scl = 0;
                return Data;
}
void I2C_writebyte()                                    //save in EEPROM
{
        I2C_start();
        I2C_writebit(0xA0);                                             //device address
        I2C_writebit(0x00);                                             //word address
        I2C_writebit(0x41);                                             //send data     A
        I2C_stop();
}
void I2C_readbyte()
{
        unsigned char i;
        I2C_start();
        I2C_writebit(0xA0);
        I2C_writebit(0x00);
        I2C_start();
        I2C_writebit(0xA1);                                      //device address
        i=I2C_readbit(nack);
        I2C_stop();
        lcd_data(i); // I have written a separate LCD.h file for this function
}
void main()
{

        unsigned short int LCDL1=0x80;
        unsigned short int LCDL2=0xc0;
        lcd_ini();
        I2C_writebyte();
        I2C_readbyte();
        while(1)
        {
                delay_msec(1);
        }
}

0