about EEPROM 24c64 communication with 89s52

Discussion in 'Embedded Systems and Microcontrollers' started by visva, Sep 1, 2012.

  1. visva

    Thread Starter New Member

    Sep 1, 2012
    3
    0
    help me for the following doubts:

    i have interfaced EEPROM(24c64) with 89s52). but i can't understand what is the process in it. i know something about it. its having sda pin(pin 5) for serial data communication and scl pin (pin 6) for control signal.
    question:
    1. how to do the read and write operation on it.
    2. In the following program doing some serial communication which is taken from engineersgarage.com http://www.engineersgarage.com/micro...T89C51-circuit

    3. In that program what is device address
    4. what is calculation in extracting MSB.
    5. tell me simple procedure for do a own program.



    Code ( (Unknown Language)):
    1. //Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)
    2. #include<reg51.h>
    3. #include<intrins.h>   //For using [_nop_()]
    4. sbit sda=P1^0;
    5. sbit scl=P1^1;
    6. sbit led=P0^3;
    7. bit ack;
    8. sbit led1=P0^1;
    9. sfr lcd_data_pin=0xA0;//p2 port
    10. sfr output=0x80;//p0 port
    11. sbit rs=P3^0;
    12. sbit rw=P3^1;
    13. sbit en=P3^6;
    14. unsigned char reead,write,write2,i,j;
    15. unsigned int temp;
    16. void delay(unsigned int count)
    17. {
    18.     int i,j;
    19.     for(i=0;i<count;i++)
    20.         for(j=0;j<1275;j++);
    21. }
    22. void lcd_command(unsigned char comm)
    23. {
    24.     lcd_data_pin=comm;
    25.     en=1;
    26.     rs=0;
    27.     rw=0;
    28.     delay(1);
    29.     en=0;
    30. }
    31. void lcd_data(unsigned char disp)
    32. {
    33.     lcd_data_pin=disp;
    34.     en=1;
    35.     rs=1;
    36.     rw=0;
    37.     delay(1);
    38.     en=0;
    39. }
    40. void lcd_dataa(unsigned char *disp)
    41. {
    42.     int x;
    43.     for(x=0;disp[x]!=0;x++)
    44.     {
    45.         lcd_data(disp[x]);
    46.     }
    47. }
    48. void lcd_ini()
    49. {
    50.     lcd_command(0x38);        // for using 8-bit 2 row LCD
    51.     delay(5);
    52.     lcd_command(0x0F);        // for display on, cursor blinking
    53.     delay(5);
    54.     lcd_command(0x80);
    55.     delay(5);
    56. }
    57. void aknowledge()     //acknowledge condition
    58. {
    59.     scl=1;
    60.     _nop_();
    61.     _nop_();
    62.     scl=0;
    63. }
    64. void start()        //start condition
    65. {
    66.     sda=1;
    67.     scl=1;
    68.     _nop_();         //No operation
    69.     _nop_();
    70.     sda=0;
    71.     scl=0;
    72. }
    73. void stop()         //stop condition
    74. {
    75.     sda=0;
    76.     scl=1;
    77.     _nop_();
    78.     _nop_();
    79.     sda=1;
    80.     scl=0;
    81. }
    82. [B]void send_byte(unsigned char value)  //send byte serially
    83. {
    84.     unsigned int i;
    85.     unsigned char send;
    86.     send=value;
    87.     for(i=0;i<8;i++)
    88.     {
    89.         sda=send/128;           //extracting MSB
    90.         send=send<<1;           //shiftng left
    91.         scl=1;
    92.         _nop_();
    93.         _nop_();
    94.         scl=0;
    95.     }
    96.    ack=sda;                 //reading acknowledge
    97.    sda=0;
    98. }[/B]
    99. [B]unsigned char read_byte()            //reading from EEPROM serially
    100. {
    101.     unsigned int i;
    102.     sda=1;
    103.     reead=0;
    104.     for(i=0;i<8;i++)
    105.     {
    106.         reead=reead<<1;
    107.         scl=1;
    108.         _nop_();
    109.         _nop_();
    110.         if(sda==1)
    111.             reead++;
    112.         scl=0;
    113.     }
    114.     sda=0;
    115.     return reead;               //Returns 8 bit data here
    116. }       [/B]
    117. void save()                 //save in EEPROM
    118. {
    119.     [B]start();
    120.     send_byte(0xA0);                        //device address
    121.     aknowledge();
    122.     send_byte(0x00);                        //word address
    123.     aknowledge();
    124.     send_byte(5);                           //send data
    125.     aknowledge();
    126.     send_byte(65);
    127.     aknowledge();
    128.     stop();     [/B]                       
    129.     if(ack==0)
    130.     {
    131.         led1=1;
    132.         delay(100);
    133.         led1=0;
    134.         delay(100);
    135.         lcd_command(0x86);
    136.         lcd_data('5');
    137.         lcd_command(0x87);
    138.         lcd_data('A');
    139.     }
    140.     else
    141.         led1=1;
    142.     aknowledge();
    143. }
    144. void Read()
    145. {
    146.     start();
    147.     send_byte(0xA0);
    148.     aknowledge();
    149.     send_byte(0x00);
    150.     aknowledge();
    151.     start();
    152.     send_byte(0xA1);                     //device address
    153.     aknowledge();
    154.     i=read_byte();
    155.     aknowledge();
    156.     j=read_byte();
    157.     aknowledge();
    158.     stop();
    159.     if(i==5)
    160.     {
    161.         led=0;
    162.         delay(100);
    163.         led=1;
    164.         delay(100);
    165.         write=i+48;
    166.         lcd_command(0xC6);
    167.         lcd_data(write);   
    168.     }
    169.     else
    170.      led=1;
    171.     if(j==65)
    172.     {
    173.         lcd_command(0xC7);
    174.         lcd_data(j);
    175.     }
    176.     aknowledge();
    177. }
    178.  
    179. int main()
    180. {
    181.     lcd_ini();
    182.     lcd_dataa("Sent :");
    183.     lcd_command(0xC0);
    184.     lcd_dataa("Read :");
    185.     temp=0;
    186.     while(1)
    187.     {
    188.         save();
    189.         Read();
    190.     }
    191.     return 0;
    192. }
     
Loading...