Hi all... I am trying to Interface AT24C16 Eeprom with AT89S52 microcontroller. My concept is to save the data's from string Eprom_data in which user input's using keypad. and then again read the data and then store it to string chanuser and then display it on the LCD. but what the problem is the code only works perfect with AT24C32 and AT24C64 but does not work properly with AT24C16. I have tried stimulating in Proteus. if i use AT24C16 and try writing data to any page address or any other block, it wont write to that specific address.. it will write only to 0x0001. but if i work with the same code using 24C32 and 24C64. the things work prefect.
/* sending commands to LCD display to act in command mode */ void lcdinit() { lcdcmd(0x38); lcdcmd(0x0F); lcdcmd(0x06); lcdcmd(0x01); } void lcdcmd(unsigned char value) { lcdready(); ldata=value; rs=0; rw=0; en=1; msdelay(10); en=0; } /* sending commnad to LCD to display characters*/ void lcddata(unsigned char value) { lcdready(); ldata=value; rs=1; rw=0; en=1; msdelay(20); en=0; } /* checking LCD buffer for free */ void lcdready(void) { busy=1; rs=0; rw=1; if(busy==1) { en=0; msdelay(1); en=1; } } void printstring(unsigned char ch[]) { unsigned int i; for(i=0;ch[i]!='\0';i++) lcddata(ch[i]); } /* generating delay*/ void msdelay(unsigned int value) { unsigned int i,j; for(i=0;i<value;i++) for(j=0;j<100;j++); } void LCDclear(void) { lcdcmd(0x01); } int keypad() { unsigned char dat[4][4]={'1','2','3','A', // assigning key matrix '4','5','6','B', '7','8','9','-', '*','0','#','+', }; unsigned char colloc,rowloc; COL=0xFF; row0=0; row1=0; row2=0; row3=0; /* setting LCD screen*/ while(1) { /* reading character from keyboard */ do { row0=0; row1=0; row2=0; row3=0; colloc=COL; colloc&=0x0F; }while(colloc!=0x0F); do { do { msdelay(10); colloc=COL; colloc&=0x0F; } while(colloc==0x0F); msdelay(10); colloc=COL; colloc&=0x0F; } while(colloc==0x0F); while(1) { row0=0; row1=1; row2=1; row3=1; msdelay(5); colloc=COL; colloc&=0x0F; if(colloc!=0x0F) { rowloc=0; break; } row0=1; row1=0; row2=1; row3=1; msdelay(5); colloc=COL; colloc&=0x0F; if(colloc!=0x0F) { rowloc=1; break; } row0=1; row1=1; row2=0; row3=1; msdelay(5); colloc=COL; colloc&=0x0F; if(colloc!=0x0F) { rowloc=2; break; } row0=1; row1=1; row2=1; row3=0; msdelay(5); colloc=COL; colloc&=0x0F; if(colloc!=0x0F) { rowloc=3; break; } } if(colloc==0x0E) return(dat[rowloc][0]); else if(colloc==0x0D) return(dat[rowloc][1]); else if(colloc==0x0B) return(dat[rowloc][2]); else return(dat[rowloc][3]); } } void Eprom_Read(unsigned int Page_Add) { unsigned char i; I2CStart(); /* Slave address */ I2CSend(0xA0); /* 2-Byte Sub Address */ I2CSend(Page_Add>>8); I2CSend(Page_Add); I2CStop(); //restart I2CStart(); /* Slave address writh Read bit */ I2CSend(0xA1); /* Read 8 Byte from EEPROM */ for (i = 0; i < 10; i++) { Eprom_data[i] = I2CRead(); I2CAck(); } Eprom_data[i] = I2CRead(); I2CNak(); I2CStop(); } void Eprom_Write(unsigned int Page_Add) { unsigned int i; I2CStart(); /* Slave address */ I2CSend(0xA0); /* 2-Byte Sub Address */ I2CSend(Page_Add>>8); I2CSend(Page_Add); /* Data 8 bytes */ for (i = 0; i < 11; i++) I2CSend(Eprom_data[i]); I2CStop(); while (eeprom_not_ready()); } unsigned char eeprom_not_ready() { unsigned char ack; I2CStart(); /* Slave address */ ack = I2CSend(0xA0); I2CStop(); return ack; } unsigned char I2CSend(unsigned char Data) //Provision to recieve ack/nack after sending data ;clock cycle included ; for EEPROM { unsigned char i, ack_bit; for (i = 0; i < 8; i++) { if ((Data & 0x80) == 0) SDA = 0; else SDA = 1; delay_us(1); SCL = 1; delay_us(1); SCL = 0; Data<<=1; } delay_us(1); SDA = 1; delay_us(1); SCL = 1; delay_us(1); ack_bit = SDA; SCL = 0; return ack_bit; } void I2CStart() { SDA = 0; delay_us(1); SCL = 0; } void I2CStop() { SCL = 0; delay_us(1); SDA = 0; delay_us(1); SCL = 1; delay_us(1); SDA = 1; } unsigned char I2CRead() { unsigned char i, Data=0; for (i = 0; i < 8; i++) { SCL = 1; if(SDA) Data |=1; if(i<7) Data<<=1; SCL = 0; } return Data; } void I2CAck() { SDA = 0; delay_us(1); SCL = 1; delay_us(1); SCL = 0; delay_us(1); SDA = 1; } void I2CNak() { SDA = 1; delay_us(1); SCL = 1; delay_us(1); SCL = 0; delay_us(1); SDA = 1; } void delay_us(unsigned int us_count) { while(us_count!=0) { us_count--; } </pre?