Interfacing EEPROM with 24c04

Discussion in 'Embedded Systems and Microcontrollers' started by bubblysush, May 14, 2012.

  1. bubblysush

    Thread Starter New Member

    May 14, 2012
    3
    0
    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

    Code ( (Unknown Language)):
    1.  
    2. #include <reg51.h>
    3. #include <absacc.h>
    4. #include<string.h>
    5. #include <intrins.h>     // for nop operation
    6.  
    7. void lcdstr(unsigned char *s);
    8. void lcdcmd(unsigned char val);
    9. void lcddata(unsigned char val);
    10. void lcdinit();
    11.  
    12. unsigned char bdata busyflag;
    13. unsigned char dat,datn;
    14.  
    15. sbit rs = P2^0;
    16. sbit rw = P2^1;
    17. sbit e = P2^2;
    18. sbit Scl=P3^4;    //Serial Clock
    19. sbit Sda=P3^3;    //Serial Data
    20. bit aa;  // ack
    21.  
    22. void delay(unsigned int t)         // delay function
    23. {
    24.     unsigned int i,j;
    25.     for(i=0;i<t;i++)
    26.         for(j=0;j<1275;j++);
    27. }
    28. code unsigned char word1[16]={"  program "};    //Line 1 shows the cache
    29. code unsigned char word2[16]={"  by Sush"};        //Line 2 shows the cache
    30.              
    31. code unsigned char arr1[100]={"123456789"};    
    32.  
    33. unsigned char d;
    34. unsigned char a[50];
    35.  
    36. //////////////////////////////////////start of I2C ////////////////////////////
    37. #define uchar unsigned char
    38. #define uint  unsigned int
    39.  
    40. /*Send start condition*/
    41. void Start(void)        /*Initial conditions*/
    42. {
    43.         Scl=1;
    44.         Sda=1;
    45.         _nop_ ();
    46.         _nop_ ();
    47.         _nop_ ();
    48.         _nop_ ();
    49.         Sda=0;
    50.         Scl=0;
    51.        
    52. }
    53. void Stop(void) /*Stop condition*/
    54. {
    55.         Scl=1;
    56.         Sda=0;
    57.         _nop_ ();
    58.         _nop_ ();
    59.         _nop_ ();
    60.         _nop_ ();
    61.         Sda=1;
    62.         Scl=0;
    63. }
    64. void Ack(void)  /*Acknowledge bit*/
    65. {
    66.        
    67.         Scl=1;
    68.         _nop_ ();
    69.         _nop_ ();
    70.      
    71.         Scl=0;
    72. }
    73.  
    74. void Send(uchar Data)   //Subroutine to send data
    75. {
    76.         unsigned char i;
    77.         uchar temp;
    78.         temp=Data;
    79.         for(i=0;i<8;i++)
    80.         {
    81.             Sda=temp/128;            //extracting MSB
    82.             temp=temp<<1;            //shiftng the bits towards left one step. the actual MSB is made the LSB
    83.             Scl=1;
    84.             _nop_();
    85.             _nop_();
    86.             Scl=0;
    87.         }
    88.         aa=Sda;                    //reading acknowledge
    89.            Sda=0;
    90. }  
    91.    
    92.  
    93. uchar Read(void) //Read one byte of data, and returns the byte value
    94. {
    95.     unsigned int i;
    96.     uchar temp;
    97.     Sda=1;
    98.     temp=0;
    99.     for(i=0;i<8;i++)
    100.     {
    101.         temp=temp<<1;    
    102.         Scl=1;
    103.         _nop_();
    104.         _nop_();
    105.         if(Sda==1)
    106.             temp++;
    107.         Scl=0;
    108.     }
    109.     Sda=0;
    110.     return temp;        //Returns 8 bit data here
    111. }
    112.  
    113. void WrToROM(uchar Data[],uchar Address,uchar Num)//Write a set of data to the AT24C02 in
    114. {     //The first parameter is an array address, data in the AT24C02 in the beginning of the address, number of data
    115.      uchar i=0;
    116.      uchar *PData;
    117.      PData=Data;
    118.      Start();        
    119.      Send(0xa0);    //A0, A1, A2 earth, solid AT24C02 write address 0XA0
    120.      Ack();
    121.      Send(Address);
    122.      Ack();
    123.      for(i=0;i<Num;i++)
    124.      {
    125.          Send(*(PData+i));
    126.          Ack();    
    127.                
    128.      }
    129.      lcdstr(PData);
    130.      Stop();
    131. }
    132. //void  RdFromROM(uchar Address,uchar Num)//Read out in a set of data to the AT24C02
    133.    
    134. void RdFromROM(uchar Address,uchar Num)
    135. {
    136.      uchar i=0;
    137.      uchar *PData;
    138.      for(i=0;i<Num;i++)
    139.      {
    140.         Start();
    141.         Send(0xa0); //A0, A1, A2 earth, solid AT24C02 write address 0XA0
    142.         Ack();
    143.         Send(Address+i);
    144.         Ack();
    145.         Send(0xa1); //A0, A1, A2 earth, solid AT24C02 read address 0XA1
    146.         Ack();
    147.         *(PData+i)=Read();
    148.         a[i]=*PData;
    149.         Scl=0;
    150.         Stop();
    151.    }
    152. }
    153.  
    154. /////////////////////////////////END of I2C/////////////////////////////////
    155.  
    156. void lcdinit()    // lcd initializations
    157. {
    158.     lcdcmd(0x38);
    159.     delay(5);
    160.     lcdcmd(0x0c);
    161.     delay(5);
    162.     lcdcmd(0x01);
    163.     delay(5);
    164.     lcdcmd(0x06);
    165.     delay(5);
    166.    
    167. }
    168. void lcdstr(unsigned char *s)  // to print a string
    169. {
    170.      unsigned int l,i;
    171.      l = strlen(s);              
    172.      for(i=1;i<=l;i++)
    173.      {
    174.       lcddata(*s);              
    175.       s++;  
    176.      }
    177. }
    178. void lcdcmd(unsigned char val)   // command to lcd
    179. {      
    180.      
    181.         P1=val;
    182.         rs=0;
    183.         rw=0;
    184.         e=1;
    185.         delay(1);
    186.         e=0;
    187.         return;
    188. }
    189.  
    190. void lcddata(unsigned char val)   // lcd data
    191. {
    192.              
    193.          P1=val;  
    194.          rs=1;
    195.          rw=0;
    196.          e=1;
    197.          delay(1);
    198.          e=0;
    199.          return;
    200. }
    201.  
    202. ///////////////////////////////////
    203. void main()
    204. {
    205.         unsigned char leng;
    206.         lcdinit();
    207.  
    208.         while(1)
    209.         {
    210.             lcdcmd(0x80);
    211.             lcdstr(word1);
    212.             lcdcmd(0xc0);
    213.             lcdstr(word2);
    214.             delay(50)  ;
    215.             lcdcmd(0x01);
    216.             leng=strlen(arr1);
    217.             WrToROM(arr1,0x00,leng);
    218.             lcdcmd(0x80);
    219.             lcdstr(arr1);
    220.             lcdcmd(0xC0);
    221.             RdFromROM(0x00,leng);
    222.             delay(50);
    223.             lcdstr(a);
    224.             delay(50);
    225.             lcdcmd(0x01);
    226.             delay(50);
    227.         }    
    228. }
    229. [/i]
     
    Last edited by a moderator: May 14, 2012
  2. bondkicha

    New Member

    Dec 31, 2012
    1
    0
    #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;
    }
     
Loading...