1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

eeprom 24C64 read problem

Discussion in '8051/8951' started by ash20, Apr 22, 2012.

  1. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    Can someone tell how to read data serially from memory??
    i need the code in C not in assembly language..

    i have successfully saved my name in memory but am not able to read it from memory.

    please help... (need the C code)
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    I thought I posted it in your last postings...

    Did it not work...

    I had tested it, so it should have..

    Oh sorry... in C. I have I2C routines in my tutorials in my signature..... They are software I2C... they are for the pic16f628... however only the port definitions need to be changed

    Tutorial 6... The last one is a little large so you might try the first.. tutorial 6.1
    http://www.electro-tech-online.com/threads/directorial-antenna.467/
     
    Last edited: Apr 23, 2012
  3. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    :) yeah you helped me a lot but i needed the c code and when you gave me the assembly code, i was feeling shy to ask for the c code :p now i am reading the link u gave.. thanks again sir :)
     
    Last edited: Apr 23, 2012
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK

    Don't be shy... I can do both..... If you want us to know in future, just add it to your posts.. no-one will bother!!
     
  6. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    sir, actually, i have a very similar code which you gave and it is as follows:
    -->

    unsigned char read_byte() //reading from EEPROM serially
    {
    unsigned int i;
    sda=1;
    reead=0;
    for(i=0;i<8;i++)
    {
    reead=reead<<1;
    scl=1;
    _nop_();
    _nop_();
    if(sda==1)
    reead++;
    scl=0;
    }
    sda=0;
    return reead; //Returns 8 bit data here
    }

    void Read()
    {
    start();
    send_byte(0xA0);
    aknowledge();
    send_byte(0x00);
    aknowledge();
    start();
    send_byte(0xA1); //device address
    aknowledge();
    i=read_byte();
    aknowledge();
    j=read_byte();
    aknowledge();
    stop();
    if(i==5)
    {
    delay(100);
    write=i+48;
    lcd_command(0xC6);
    lcd_data(write);
    }

    if(j==65)
    {
    lcd_command(0xC7);
    lcd_data(j);
    }
    aknowledge();
    }

    they saved 5A at the starting and what i am not able to understand is that:
    1. from i=0 to 8, they are doing reead=reead<<1; and then incrementing read. but wont it give the same result every time?? since reead is being initialised with 0 every time they call the function read_byte
    2. firstly they gave 0xA0 as address, then 0xA1 as address. how do we know where is the character? and why have they not used 0xA2 for reading the still next char??
    3. even if this code is similar to yours, it is not working. my eeprom is 24C64.

    please solve my these 3 problems.
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    The address 0xA0 is the DEVICE address.. 0xA0 = write 0xA1 = read

    I2C requires the device address first then the 16 bit internal address, followed by the 8 bit data
    You need a 16 bit address as the 24c64 is an 8x8kbyte eeprom 8k requires 13 address bits

    The code you have here is for a 8 bit addressed device (same as the RTC) they have up to 2kilobits 256x8bytes...

    The code in my C tutorials allow for 8 or 16 bit and random or sequential reads / writes.

    Notice the two address writes
    Code (text):

    unsigned char E_Read(int addr, unsigned char *ch, char len)
        {
        unsigned char byte;
        ErrFlags = 0;                                   // Clear error
        I2C_Start();
        I2C_Write(CHIP_Write);
        if(!I2C_nack()) return  1;                      //
        I2C_Write((unsigned char) (addr >> 8 & 0xff));  //  << here is high byte of address
        if(!I2C_nack()) return  1;
        I2C_Write((unsigned char) addr & 0xff);         // << here is low byte of address
        if(!I2C_nack()) return 1;
        I2C_Start();
        I2C_Write(CHIP_Read);                           //
        if(!I2C_nack()) return  1;
        if(len == 1)                                    // Read a single char.
            {
            *ch = I2C_Read();
            }
        else if(len > 1)                                // Read a sequential data steam
            {
            while(len-- > 1)
                {
                *ch++ = I2C_Read();                     // read character
                I2C_ack();                              // Send ack
                }
            *ch = I2C_Read();
            }
        else                                            // Read s NULL string
            {
            do{
                *ch = I2C_Read();
                I2C_ack();
                }while(*ch++ != 0);
            *ch = I2C_Read();
            }
        I2C_Sendnack();                                 // send nack
        I2C_Stop();                                     // send stop
        return 0;
        }
     
     
  8. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    ok sir. now i understand.. i will write my code here and u tell that is right or not..
    and one more thing sir,

    for(i=1;i<11;i++)
    {
    lcd_command(0x01);
    lcd_start();
    lcd_command(0x80);
    lcd_show("ROLL NUMBER ");
    lcd_command(0x8C);
    lcd_data(i);
    keypad(); //call the keypad
    }

    here it is not showing what i thought it might be. like roll number 1, roll number 2 etc. it is showing some different symbols in place of 1 2 3 ... so what should i do to show 1,2,3... ??
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    Code (text):

    for( i = 0 ; i < 10 ; i++ )
       {
       lcd_command( 0x01 );
       lcd_start();
       lcd_command( 0x80 );
       lcd_show( "ROLL NUMBER " );
       lcd_command( 0x8C );
       lcd_data( i + 0x30 );      // you have to add in the ASCII... numbers start at 0x30.
       keypad();       //call the keypad
       }
    0 to 9 (if you try to send 10+ 0x30 you get ';' not '0' as expected.
     
    Last edited: Apr 25, 2012
  10. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    i solved the number problem by following method:
    char j=48;
    for(i=1;i<10;i++) //from 1 to 9
    {
    lcd_command(0x80);
    lcd_dataa("ROLL NUMBER ");
    lcd_command(0x8C);
    lcd_data(j+i);
    keypad();
    }

    lcd_command(0x80);
    lcd_dataa("ROLL NUMBER 10");

    now this is my probgram for writing data to memory. tell me is this correct?? i have modified it according to your link (prog 6_4)

    void send_byte(unsigned char value) //send byte serially
    {
    unsigned int i;
    unsigned char send;
    send=value;
    for(i=0;i<8;i++)
    {
    sda=send/128; //extracting MSB
    send=send<<1; //shiftng left
    scl=1;
    _nop_();
    _nop_();
    scl=0;
    }
    ack=sda; //reading acknowledge
    sda=0;
    }

    //inside main() after some codes

    start();
    send_byte(0xA0); //device address
    aknowledge();
    send_byte((unsigned char)(memadd>>8 & 0xff)); //word address (upper half)
    aknowledge();
    send_byte((unsigned char)(memadd & 0xff)); //word address (lower half)
    aknowledge();
    for(i=0;i<6;i++)
    {
    send_byte(date); //send data
    aknowledge();
    memadd=memadd+i+1;
    }
    stop();
    if(ack==0)
    {led1=1;}
    aknowledge();

    //then again some codes
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    The only thing I can see is:.. In your send_byte routine there is some sort of acknowledge "ack = sda".... However you immediately call the function acknowledge!!

    Can I see your acknowledge routine?
     
  12. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    void aknowledge() //acknowledge condition
    {
    scl=1;
    _nop_();
    _nop_();
    scl=0;
    }

    void start() //start condition
    {
    sda=1;
    scl=1;
    _nop_(); //No operation
    _nop_();
    sda=0;
    scl=0;
    }

    void stop() //stop condition
    {
    sda=0;
    scl=1;
    _nop_();
    _nop_();
    sda=1;
    scl=0;
    }
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    Looks ok.. Doe's it work?
     
  14. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    huff.. i was so worried it might be wrong. your "looks ok" is like a certificate of pass. :p

    actually i have written this code in my main program only for saving data. now i am not writing the code for reading data and instead of reading, i am showing the saved data on hyper terminal... so i assume, data is correctly saved in the memory. only problem is that when i connect via hyperterminal, it is not showing the actual content of date and is showing all those smiley and special symbols upto count of 6. thats is my code till i=6 shows these special characters. so it means these characters are saved in the memory or there is some other problem in hyperterminal and due to that, these characters come up?
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    Can you simulate before you build?..... I can, so if you want the code tested first, shout and I'll do it...

    remember to convert to ascii terminals won't display anything below 32 decimal 48 decimal = 0.
     
    Last edited: Apr 27, 2012
  16. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    1. which software do you use to simulate?? (so that i can do it in future)
    2. i will do the hyprterminal thing. could you please simulate the code part for saving in eeprom and display the result here???
     
    Last edited: Apr 27, 2012
  17. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    I use protues.... The only compiler I have is SDCC The IDE is VSM studio (works with ISIS).

    ISIS is real time simulation... So the chip behaves exactly as it does in the real world.

    I'll convert the parts of the code posted here... get it working.... and show the results in screenshots.
     
  18. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    ok. waiting. till then i will try to work on protues and tell you my experience with it. :)
     
  19. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    It's quite expensive..
     
  20. ash20

    ash20 New Member

    Joined:
    Dec 14, 2010
    Messages:
    113
    Likes:
    0
    Location:
    India
    ohh.. then you can only show how my program works..

    so i should paste whole program ?? or should i post my program for reading from eeprom ?? because that is my final step...
     
    Last edited: Apr 28, 2012
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    There are quite a few of us that use ISIS But the full version WITH several micro modules is very expensive I pay about £500 a year in renewals.

    Pasting your program may be difficult as we probably use different compilers... As I have said, I use SDCC... You are probably using keil or something?

    I would have to convert it...... I have converted about....Half already, but if the conversion is too drastic.. you'll have to convert it back when I'm done...

    Are you using a free version of C.... I could possibly download and use that!!
     
    Last edited: Apr 29, 2012

Share This Page