programming eeprom

Discussion in 'Embedded Systems and Microcontrollers' started by akp007, Jan 29, 2013.

  1. akp007

    Thread Starter New Member

    Jan 28, 2013
    3
    0
    Dear all,
    i need to write a code(c ) to perform the following task(external serial eeprom interfaced with 89c51 microcontroller)

    1. to store a bulk data in the external serial eeprom (12345678965879486250,489657927800205087,.....)
    2.to update eeprom periodically automatically
    3.to compare incoming data(input to the microcontroller) with the data stored in the eeprom.
    4.if the data matches,form a string of the matched data
    4.send the string to the output ports
    .............pls suggest some sample programs............please help
     
  2. RamaD

    Active Member

    Dec 4, 2009
    254
    33
    What is the size of the bulk data? Select an EEPROM which has a larger size than your requirement. Search for serial eeprom 8051 code. There are lots of code available.
    You need to check the datasheet of the eeprom that you decide to use, and use the instruction codes in the datasheet.
     
  3. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    I've done software I2C for the 51 chipset....

    It was done on SDCC!! Works though.
    Code ( (Unknown Language)):
    1.  
    2. #include<8052.h>
    3. #include<stdio.h>
    4.  
    5. __sbit __at (0xB2) rs;  // Register select pin
    6. __sbit __at (0xB3) rw;  // Read write pin
    7. __sbit __at (0xB4) en;  // Enable pin
    8. __sbit __at (0x91) scl;
    9. __sbit __at (0x90) sda;
    10.  
    11. void I2Cinit()
    12.     {
    13.     P1 = 0xc0; 
    14.     }
    15.  
    16. void aknowledge()    //acknowledge condition
    17.     {
    18.     sda=0;
    19.     scl=1;
    20.     scl=0;
    21.     sda=1;
    22.     }
    23.    
    24. void nack()     // not acknowledge condition
    25.     {
    26.     sda=1;
    27.     scl=1;
    28.     scl=0;
    29.     scl=1;
    30.     }
    31.    
    32. void start()     //start condition
    33.     {
    34.     sda=1;
    35.     scl=1;
    36.     sda=0;
    37.     scl=0;
    38.     }
    39.    
    40. void rstart()    //re-start condition
    41.     {
    42.     scl=0;
    43.     sda=1;
    44.     scl=1;
    45.     sda=0;
    46.     }
    47.    
    48. void stop()  //stop condition
    49.     {
    50.     scl=0;
    51.     sda=0;
    52.     scl=1;
    53.     sda=1;
    54.     }
    55.    
    56. unsigned char read_byte()   //reading from EEPROM serially
    57.     {
    58.     unsigned int i;
    59.     unsigned char reead=0;
    60.    
    61.     for(i=0;i<8;i++)
    62.         {
    63.         reead=reead<<1;
    64.         scl=0;
    65.         if(sda==1)
    66.             reead++;
    67.         scl=1;
    68.         }
    69.     scl=0;
    70.     sda=1;
    71.     return reead;    //Returns 8 bit data here
    72.     }
    73.    
    74. void send_byte(unsigned char value) //send byte serially
    75.     {
    76.     unsigned int i;
    77.     unsigned char send;
    78.     send=value;
    79.        
    80.     for(i=0;i<8;i++)
    81.         {
    82.         scl=0;
    83.         sda=send/128;    //extracting MSB
    84.         send=send<<1;    //shiftng left
    85.         scl=1;
    86.         }
    87.     scl=0;
    88.     sda=1;
    89.     }
    90.    
    91. void Write(unsigned char ch, int addr)
    92.     {
    93.     start();
    94.     send_byte(0xA0);
    95.     aknowledge();  
    96.     send_byte((unsigned char)(addr>>8));
    97.     aknowledge();
    98.     send_byte((unsigned char)addr);
    99.     aknowledge();  
    100.     send_byte(ch);  //device address
    101.     aknowledge();
    102.     stop();
    103.     }
    104.        
    105. int Read(int addr)
    106.     {
    107.     unsigned char ret;
    108.     start();
    109.     send_byte(0xA0);
    110.     aknowledge();
    111.     send_byte((unsigned char)(addr>>8));  // High addr
    112.     aknowledge();
    113.     send_byte((unsigned char)addr);  // low addr
    114.     aknowledge();
    115.     rstart();
    116.     send_byte(0xA1);    //device read address
    117.     aknowledge();
    118.     ret=read_byte();
    119.     nack();             // stop reading
    120.     stop();
    121.     return ret;
    122.     }
    123. void delayus(int x)
    124.     {
    125.     while(x--);    
    126.     }
    127. void delayms(int x)
    128.     {
    129.     while(x--)
    130.         delayus(97);
    131.     }
    132.  
    133. void lcd_command(unsigned char comm) // function to send command to LCD
    134.     {
    135.     P2=comm;
    136.     en=1;
    137.     rs=0;
    138.     rw=0;
    139.     delayms(1);
    140.     en=0;
    141.     }
    142.  
    143. void lcd_ini() //Function to inisialize the LCD
    144.     {
    145.     lcd_command(0x38);  //8-bit 2 row LCD
    146.     lcd_command(0x0C);  //Display on, cursor off
    147.     lcd_command(0x80); //force cursor to the beginning of 1st line
    148.     }
    149.  
    150. void lcd_data(unsigned char disp) // function to send only single data on LCD
    151.     {
    152.     P2=disp;
    153.     en=1;
    154.     rs=1;
    155.     rw=0;
    156.     delayms(1);
    157.     en=0;
    158.     }
    159.  
    160. void lcd_dataa(unsigned char *disp) // function to send strings to LCD
    161.     {
    162.     int x;
    163.     for(x=0;disp[x]!=0;x++)
    164.         {
    165.         lcd_data(disp[x]);
    166.         }
    167.     return;
    168.     }
    169.    
    170.    
    171. void main()
    172.     {
    173.     int x,j;
    174.     I2Cinit();
    175.     lcd_ini(); 
    176.     lcd_command(0x80);
    177.     lcd_dataa("WRITE=");
    178.     for(x=0;x<10;x++)
    179.         {
    180.         Write(x+48,x);
    181.         lcd_data(x+48);
    182.         delayms(5);
    183.         }
    184.     lcd_command(0xC0);
    185.     lcd_dataa(" READ=");
    186.     for(x=0;x<10;x++)
    187.         {
    188.         j = Read(x);
    189.         lcd_data(j);
    190.         }
    191.     for(;;){}
    192.     }
     
Loading...