Maker Pro
Maker Pro

Problem With 24C1024

Dear All,

I am trying to figure out the problem that I am facing while programming 24C1024.

I am doing a byte write and byte read operation, however everytime I am exceeding over 255 my data is rolled over from intial (zeroth location).

As per datasheet, the roll over should happen only with page write operation.

please help!
the code is as follows:

WRITE OPERATION

void EEPROM24C_Write(unsigned char data,unsigned int address) //(unsigned char data,unsigned char address)
{
I2C0_Init();
I2C0_Start();
if ( 0 != I2C0_Write(0xA0,0x18))
{
I2C0_Stop();
I2C0_Start();
I2C0_Write(0xA0,0x18);
}
I2C0_Write((address>>8),0x28); //(0,0x28)
I2C0_Write(address,0x28); //(address,0x28)

I2C0_Write(data,0x28);
I2C0_Stop();
}

READ OPERATION


void EEPROM24C_Read(unsigned int address, unsigned char no_of_bytes, unsigned char *read_buff_ptr)
{
unsigned int last_addr = address + no_of_bytes;
unsigned char rem = 0;
// I2C0_Init();
I2C0_Start();
if ( 0 != I2C0_Write(0xA0,0x18))
{
I2C0_Stop();
I2C0_Start();
I2C0_Write(0xA0,0x18);
}
I2C0_Write(address>>8,0x28);
I2C0_Write(address,0x28);
I2C0_Stop();
I2C0_Start();
I2C0_Write(0xA1,0x40);

if (address > 255)
{
rem = (last_addr + 1) % 256;

if (rem != no_of_bytes)
{
no_of_bytes -= rem;
}
else
{
rem = 0 ;
}
}

for ( ; no_of_bytes > 0; no_of_bytes--)
{
if (1 == no_of_bytes)
{
*read_buff_ptr = I2C0_Read_Single_Byte();
}
else
{
*read_buff_ptr++ = I2C0_Read();
}
}
I2C0_Stop();

if (rem)
{
I2C0_Start();
if ( 0 != I2C0_Write(0xA0,0x18))
{
I2C0_Stop();
I2C0_Start();
I2C0_Write(0xA0,0x18);
}
I2C0_Write(address>>8,0x28);
I2C0_Write(address,0x28);
I2C0_Stop();
I2C0_Start();
I2C0_Write(0xA1,0x40);

for ( ; rem > 0; rem--)
{
if (1 == rem)
{
*read_buff_ptr = I2C0_Read_Single_Byte();
}
else
{
*read_buff_ptr++ = I2C0_Read();
}
}
I2C0_Stop();
}
}
/**************Function Prototypes************/
void I2C0_Init(void);
void I2C0_Start(void);
void I2C0_Stop(void);
unsigned char I2C0_Write(unsigned char data, unsigned char status);
unsigned char I2C0_Read(void);
unsigned char I2C0_Read_Single_Byte(void);
/*********************************************/


void I2C0_Init(void)
{
PINSEL0 |= 0x50; /*Initialize Pin Connect Block P0.2 as SCL0 P0.3 as SDA0*/
I2C0CONCLR = 0x2C; /*Clear AA, I2C Interrupt Flag, Start Bit*/
I2C0CONSET = 0x44; /*Enable I2C0*/
/*
PCLK = 15MHz
I2C Frequency = 100KHz
Formula:
I2C Frequency = PCLK/((I2CSCLH + I2CSCLL)
(I2CSCLH + I2CSCLL) = 15000/100 = 150
*/
I2C0SCLH = 75;
I2C0SCLL = 75;
/*Bit Rate 100Khz*/
}
void I2C0_Start(void)
{
I2C0CONSET |= 0x20; /*Set the Start Bit*/
while(I2C0STAT!=0x08); /*Wait for the Status Set*/
}
void I2C0_Stop(void)
{
I2C0CONSET |= 0x14; /*Stop I2C*/
I2C0CONCLR = 0x08;
}
unsigned char I2C0_Write(unsigned char data,unsigned char status)
{
unsigned int i = 0xFFFF;
unsigned char err_status = 0;
I2C0DAT = data;
I2C0CONSET = 0X04;
I2C0CONCLR = 0X28;
while((I2C0STAT!=status) && (0 != i)) /* Wait for Status Set */
{
i--;
}

if (0 == i)
{
err_status = 1;
}

return err_status;
}
unsigned char I2C0_Read_Single_Byte(void)
{
I2C0CONSET = 0X04;
I2C0CONCLR = 0X2C;
while (I2C0STAT!=0x00000058); /* Wait for Status Set - 0x58 */
return(I2C0DAT);
}


unsigned char I2C0_Read(void)
{
I2C0CONSET = 0X04;
I2C0CONCLR = 0X28;
while (I2C0STAT!=0x00000050); /* Wait for Status Set - 0x50 */
return(I2C0DAT);
}


Please Help!

thanks,
Sid
 
Top