Interfacing EEPROM with 24c04

Thread Starter

bubblysush

Joined May 14, 2012
3
Hello All
I m trying to interface EEPROM with 24c04. When i send 2 bytes of data to the EEPROM, the 1st byte gets written to the loaction followed by the 2nd byte overwriting the same loaction. this should not happen . please help me... i have attached my code. plz help

Rich (BB code):
#include <reg51.h>
#include <absacc.h>
#include<string.h>
#include <intrins.h>     // for nop operation

void lcdstr(unsigned char *s);
void lcdcmd(unsigned char val);
void lcddata(unsigned char val);
void lcdinit();

unsigned char bdata busyflag;
unsigned char dat,datn;

sbit rs = P2^0;
sbit rw = P2^1;
sbit e = P2^2;
sbit Scl=P3^4;    //Serial Clock
sbit Sda=P3^3;    //Serial Data
bit aa;  // ack 

void delay(unsigned int t)         // delay function
{
    unsigned int i,j;
    for(i=0;i<t;i++)
        for(j=0;j<1275;j++);
}
code unsigned char word1[16]={"  program "};    //Line 1 shows the cache
code unsigned char word2[16]={"  by Sush"};        //Line 2 shows the cache
              
code unsigned char arr1[100]={"123456789"};    

unsigned char d;
unsigned char a[50];

//////////////////////////////////////start of I2C ////////////////////////////
#define uchar unsigned char
#define uint  unsigned int

/*Send start condition*/
void Start(void)        /*Initial conditions*/
{
        Scl=1;
        Sda=1;
        _nop_ ();
        _nop_ ();
        _nop_ ();
        _nop_ ();
        Sda=0;
        Scl=0;
       
}
void Stop(void) /*Stop condition*/
{
        Scl=1;
        Sda=0;
        _nop_ ();
        _nop_ ();
        _nop_ ();
        _nop_ ();
        Sda=1;
        Scl=0;
}
void Ack(void)  /*Acknowledge bit*/
{
        
        Scl=1;
        _nop_ ();
        _nop_ ();
     
        Scl=0;
}

void Send(uchar Data)   //Subroutine to send data
{
        unsigned char i;
        uchar temp;
        temp=Data;
        for(i=0;i<8;i++)
        {
            Sda=temp/128;            //extracting MSB
            temp=temp<<1;            //shiftng the bits towards left one step. the actual MSB is made the LSB
            Scl=1;
            _nop_();
            _nop_();
            Scl=0;
        } 
        aa=Sda;                    //reading acknowledge
           Sda=0;
}   
    

uchar Read(void) //Read one byte of data, and returns the byte value
{
    unsigned int i;
    uchar temp;
    Sda=1;
    temp=0;
    for(i=0;i<8;i++)
    {
        temp=temp<<1;     
        Scl=1;
        _nop_();
        _nop_();
        if(Sda==1)
            temp++;
        Scl=0;
    }
    Sda=0;
    return temp;        //Returns 8 bit data here
}

void WrToROM(uchar Data[],uchar Address,uchar Num)//Write a set of data to the AT24C02 in
{     //The first parameter is an array address, data in the AT24C02 in the beginning of the address, number of data
     uchar i=0;
     uchar *PData;
     PData=Data;
     Start();        
     Send(0xa0);    //A0, A1, A2 earth, solid AT24C02 write address 0XA0
     Ack();
     Send(Address);
     Ack();
     for(i=0;i<Num;i++)
     {
         Send(*(PData+i));
         Ack();    
               
     }
     lcdstr(PData);
     Stop();
}
//void  RdFromROM(uchar Address,uchar Num)//Read out in a set of data to the AT24C02
   
void RdFromROM(uchar Address,uchar Num)
{
     uchar i=0;
     uchar *PData;
     for(i=0;i<Num;i++)
     {
        Start();
        Send(0xa0); //A0, A1, A2 earth, solid AT24C02 write address 0XA0
        Ack();
        Send(Address+i);
        Ack();
        Send(0xa1); //A0, A1, A2 earth, solid AT24C02 read address 0XA1
        Ack();
        *(PData+i)=Read();
        a=*PData;
        Scl=0;
        Stop();
   }
}

/////////////////////////////////END of I2C/////////////////////////////////

void lcdinit()    // lcd initializations 
{
    lcdcmd(0x38); 
    delay(5);
    lcdcmd(0x0c); 
    delay(5);
    lcdcmd(0x01); 
    delay(5);
    lcdcmd(0x06); 
    delay(5);
   
}
void lcdstr(unsigned char *s)  // to print a string
{
     unsigned int l,i;
     l = strlen(s);               
     for(i=1;i<=l;i++)
     {
      lcddata(*s);              
      s++;  
     }
}
void lcdcmd(unsigned char val)   // command to lcd
{      
     
        P1=val;
        rs=0;
        rw=0;
        e=1;
        delay(1);
        e=0;
        return;
}

void lcddata(unsigned char val)   // lcd data
{
             
         P1=val;  
         rs=1;
         rw=0;
         e=1;
         delay(1);
         e=0;
         return;
}

///////////////////////////////////
void main()
{
        unsigned char leng;
        lcdinit();

        while(1)
        {
            lcdcmd(0x80);
            lcdstr(word1);
            lcdcmd(0xc0);
            lcdstr(word2);
            delay(50)  ;
            lcdcmd(0x01);
            leng=strlen(arr1);
            WrToROM(arr1,0x00,leng);
            lcdcmd(0x80);
            lcdstr(arr1);
            lcdcmd(0xC0);
            RdFromROM(0x00,leng);
            delay(50);
            lcdstr(a);
            delay(50);
            lcdcmd(0x01);
            delay(50);
        }     
}
 
Last edited by a moderator:

bondkicha

Joined Dec 31, 2012
1
#include <lpc213x.h>
#include "usart0.c"
void initi(void);
void i2c0_int(void) __irq;
void address_write(void);
void data(unsigned char *da);
void stop(void);
void start(void);
void eeprom_address(void);


unsigned char flag=0x00;
unsigned char *d="+919677000854";
void main()
{
initi();
usart0_init();
start();
while(1)
{

}



}
void initi(void)
{
PINSEL0=0x00000050;
I2C0CONCLR=0x6c;
I2C0CONSET=0x40;
I2C0SCLH=80;
I2C0SCLL=70;

VICIntSelect=0x00;
VICIntEnable=(1<<9);
VICVectCntl0=0x20|9;
VICVectAddr0=(unsigned long)i2c0_int;
}
void i2c0_int(void) __irq
{
unsigned char temp=0x00;
temp=I2C0STAT;
usart0_char(temp);
switch(temp)
{
case 8:
address_write();
break;
case 24:
eeprom_address();
break;

case 40:
if(*d!='\0')
{

data(d++);
}
else
{
stop();
}
break;

}
VICVectAddr=0x00;


}

void address_write(void)
{
I2C0DAT=0xA0;
I2C0CONCLR=0x28;
}

void eeprom_address(void)
{
I2C0DAT=0x00070;
I2C0CONCLR=0x08;
}

void data(unsigned char *da)
{
I2C0DAT=*da;
I2C0CONCLR=0x08;
}

void stop(void)
{
I2C0CONSET=0x10;
// I2C0CONCLR=0x48;
}

void start(void)
{
I2C0CONSET=0x20;
}
 
Top