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

Need correction for I2c write routine

Hi ,
The following code works fine while reading the contents from the EEPROM (24C04) but does not continuously writes to EEPROM.

Please suggest the correction in this code.

#include <REG52.H>
#include <stdio.h>
#include <intrins.h>

sbit SDA = P2^0;
sbit SCL = P2^1;


#define uchar unsigned char
#define uint unsigned int
#define Byte unsigned char
#define Word unsigned int
#define bool bit
#define true 1
#define false 0


#define SomeNOP(); _nop_();_nop_();_nop_();_nop_();

void I2CStart(void);
void I2CStop(void);
void WaitAck(void);
void SendAck(void);
void SendNotAck(void);
void I2CSendByte(Byte);
Byte I2CReceiveByte(void);
void contr(uchar);

void contw(uchar,uchar);

void main(void)
{
unsigned char count=0;
PCON |= 0x80; // Set bit 7 of the PCON register (SMOD = 1)
SCON = 0x50; // 0101,0000 (Mode 1 and RxD enable)
TMOD |= 0x20; // Timer #1 in autoreload 8 bit mode
TH1 = 0xFA; // Baud rate is determined by
// Timer #1 overflow rate
TR1 = 1; // Turn on Timer 1
TI = 1;


contw(0,'P');
contw(1,'O');


while(1)
{
if(count==5)
count=0;
contr(count++);
}
}


/**--------------------------------------------------------------------------------
void I2CStart(void)
I2C
---------------------------------------------------------------------------------*/
void I2CStart(void)
{
SDA=1;
SCL=1;
SomeNOP();//INI
SDA=0;
SomeNOP(); //START
SCL=0;
SomeNOP();
}


/**--------------------------------------------------------------------------------

void I2CStop(void)
I2C

---------------------------------------------------------------------------------*/
void I2CStop(void)
{
SCL=0;
SDA=0;
SomeNOP(); //INI
SCL=1;
SomeNOP();
SDA=1; //STOP
SomeNOP();
}

/**--------------------------------------------------------------------------------
bit I2CAck(void)
I2C
----------------------------------------------------------------------------------*/

void WaitAck(void)
{
SDA=1;
SomeNOP();
SCL=1;
while(SDA)
SomeNOP();
SCL=0;
SomeNOP();
}

/**--------------------------------------------------------------------------------

void SendAck(void)
I2C

---------------------------------------------------------------------------------*/
void SendAck(void)
{
SDA=0;
SomeNOP();
SCL=1;
SomeNOP();
SCL=0;
}


/**--------------------------------------------------------------------------------

void SendNotAck(void)
I2C

**--------------------------------------------------------------------------------*/
void SendNotAck(void)
{
SDA=1;
SomeNOP();
SCL=1;
SomeNOP();
SCL=0;
}

/**--------------------------------------------------------------------------------

void I2CSend(uchar ch)
I2C

---------------------------------------------------------------------------------*/
void I2CSendByte(Byte ch)
{
uchar i=8;
while (i--)
{
SCL=0;
_nop_();
SDA=(bit)(ch&0x80);
ch<<=1;
SomeNOP();
SCL=1;
SomeNOP();
}
SCL=0;
}


/**--------------------------------------------------------------------------------

uchar I2CReceive(void)
I2C

---------------------------------------------------------------------------------*/
Byte I2CReceiveByte(void)
{
uchar i=8;
Byte ddata=0;
SDA=1;
while (i--)
{
ddata<<=1;
SCL=0;
SomeNOP();
SCL=1;
SomeNOP();
ddata|=SDA;
}
SCL=0;
return ddata;
}

/**--------------------------------------------------------------------------------

void contr(uchar)
I2C Content Read

---------------------------------------------------------------------------------*/

void contr(uchar loc)
{
char i;
I2CStart();
I2CSendByte(0xA2);
WaitAck();
I2CSendByte(loc);
WaitAck();
I2CStop();

I2CStart();
I2CSendByte(0xA3);
WaitAck();
printf("%c",I2CReceiveByte());
SendNotAck();
I2CStop();
}


/**--------------------------------------------------------------------------------

void contw(uchar,uchar)
I2C Content Write

*Error in function*
---------------------------------------------------------------------------------*/

void contw(uchar loc,uchar info)
{
I2CStart();
I2CSendByte(0xA2);
WaitAck();
I2CSendByte(loc);
WaitAck();
I2CSendByte(info);
WaitAck();
I2CStop();
}

0