Hi,
Need your help
I am using atmega128 microcontoller interfacing it with at24c512(i2c
bus).
The code is running and i am getting data .
butt i am not able to write/read more then 2 pages(128 bytes per page)
at a time. i amnot getting where i m going wrong.
// 2 Wire Bus initialization
// Generate Acknowledge Pulse: On
// 2 Wire Bus Slave Address: 00h
// General Call Recognition: Off
// Bit Rate: 400kHz // 307.692 kHz// 200.000 kHz
TWBR=0x0C;//0x05;//0x0C;
TWAR=0x00;
TWCR=0x44;
ptrom=0x0000;
delay_ms(1);
////wanted to write 8 page or the 512 page all together
///////////////// writing 2 page
twi2ctransmitdata(0xA0,0,0);
twi2ctransmitdata(0xA0,128,1);
////wanted to read 8 page or the 512 page all together
///////////////// reading 2 page
twi2crecievedata(0xA0,0);
for(j=1;j<128*2+1;j++)
{
*ptrom=data1[j];
ptrom++;
delay_us(500);
}
twi2ctransmitdata(0xA0,256,2);
twi2ctransmitdata(0xA0,384,3);
twi2crecievedata(0xA0,0);
for(j=1;j<128*2+1;j++)
{
*ptrom=data1[j];
ptrom++;
delay_us(500);
}
twi2ctransmitdata(0xA0,512,4);
twi2ctransmitdata(0xA0,640,5);
twi2crecievedata(0xA0,0);
for(j=1;j<128*2+1;j++)
{
*ptrom=data1[j];
ptrom++;
delay_us(500);
}
twi2ctransmitdata(0xA0,768,6);
twi2ctransmitdata(0xA0,896,7);
twi2crecievedata(0xA0,0);
for(j=1;j<128*2+1;j++)
{
*ptrom=data1[j];
ptrom++;
delay_us(500);
}
PORTA=0xFF;
delay_us(1000);
while (1)
{
};
}
void twi2ctransmitdata(unsigned char add,unsigned int eadd01,int i)
{
int id;
start:TWCR = TWCR | 0x24;
wait();
if((TWSR & 0xF8) == 0x38)
goto start;
else
if((TWSR & 0xF8) == 0x08)
{
TWDR = add;
TWCR=TWCR & 0xDF;
TWCR = TWCR | 0x04;
wait();
}
if((TWSR & 0xF8) == 0x20)
TWCR=(TWCR | 0x14);
else
if((TWSR & 0xF8) == 0x38)
goto start;
else
if((TWSR & 0xF8) == 0x18)
{
TWDR=eadd01<<8;
TWCR=(TWCR | 0x04);
wait();
}
if((TWSR & 0xF8) == 0x30)
TWCR=(TWCR | 0x14);
else
if((TWSR & 0xF8) == 0x38)
goto start;
else
if((TWSR & 0xF8) == 0x28)
{
TWDR = eadd01 & 0xFF;
TWCR =(TWCR | 0x04) ;
wait();
}
if((TWSR & 0xF8) == 0x30)
TWCR=(TWCR | 0x14);
else
if((TWSR & 0xF8) == 0x38)
goto start;
else
if((TWSR & 0xF8) == 0x28)
{
for(id=0;id<128;id++)
{
TWDR = data[128*i+id];
// TWCR=TWCR & 0xDF;
TWCR =(TWCR | 0x04) ;
wait();
if((TWSR & 0xF8) != 0x28)
break;
}
}
if((TWSR & 0xF8) == 0x30)
TWCR=(TWCR | 0x14);
else
if((TWSR & 0xF8) == 0x38)
goto start;
else
if((TWSR & 0xF8) == 0x28)
{
TWCR=(TWCR | 0x14);
}
delay_ms(5);
}
void twi2crecievedata(unsigned char add1,unsigned int *eadd11)
{
int ir;
start1:TWCR =TWCR | 0x24;
wait();
if((TWSR & 0xF8) == 0x38)
goto start1;
else
if((TWSR & 0xF8) == 0x08)
{
TWDR = add1;
TWCR=TWCR & 0xDF;
TWCR=(TWCR | 0x04);
wait();
}
if((TWSR & 0xF8) == 0x20)
TWCR=(TWCR | 0x14);
else
if((TWSR & 0xF8) == 0x38)
goto start1;
else
if((TWSR & 0xF8) == 0x18)
{
TWDR=eadd11<<8;
TWCR=(TWCR | 0x04);
wait();
}
if((TWSR & 0xF8) == 0x30)
TWCR=(TWCR | 0x14);
else
if((TWSR & 0xF8) == 0x38)
goto start1;
else
if((TWSR & 0xF8) == 0x28)
{
TWDR = eadd11 & 0xFF;
TWCR =(TWCR | 0x04);
wait();
}
if((TWSR & 0xF8) == 0x30)
TWCR=(TWCR | 0x14);
else
if((TWSR & 0xF8) == 0x38)
goto start1;
else
if((TWSR & 0xF8) == 0x28)
{
TWCR=TWCR | 0x24;
wait();
}
if((TWSR & 0xF8) == 0x38)
goto start1;
else
if((TWSR & 0xF8) == 0x10)
{
TWDR = add1 | 1;
TWCR=TWCR & 0xDF;
TWCR=(TWCR | 0x04);
wait();
}
if((TWSR & 0xF8) == 0x48)
TWCR=(TWCR | 0x14);
else
if((TWSR & 0xF8) == 0x38)
goto start1;
else
if((TWSR & 0xF8) == 0x40)
{
delay_ms(1);
for(ir=0;ir<128*2+1;ir++)
{
data1[ir]=TWDR;
TWCR=(TWCR | 0x44);
wait();
if(ir==128*2)
{
TWCR=TWCR & 0xBF;
TWCR=(TWCR | 0x04);
}
}
wait();
}
if((TWSR & 0xF8) == 0x38)
goto start1;
else
if((TWSR & 0xF8) == 0x58)
TWCR=(TWCR | 0x14);
delay_ms(5);
}
void wait()
{
while((TWCR & 0x80) !=0x80)
;
}