This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

code for interfacing 24C16 I2C EEPROM with 8051

Hello,

I am unable to interface the 24C16 in my 8051 board.
Can anyone give me the code (in 'C')required for interfacing the 24C16 using any two port lines.

Thanks,

Mohit

Parents
  • This is all the code for C167 to 24C08.

    You will need to sort out your own 'printf' statement.

    #define _MAIN_
    /*********************************************************************/

    /*********************************************************************/

    #include <reg167.h>
    #include <stdio.h>
    #include <string.h>

    //I2C values
    #define INPUT DP2 = DP2 & 0xBFFF; //bit 14 of P2 used for I2C data line
    #define OUTPUT DP2 = DP2 | 0xC000; // bit 14 is ourput , lets do bit 15 here as well!
    #define SETCLOCK P2 = P2 | 0x8000; //bit 15 high
    #define CLRCLOCK P2 = P2 & 0x7FFF; //bit 15 low
    #define SETDATA P2 = P2 | 0x4000; //bit 14 high
    #define CLRDATA P2 = P2 & 0xBFFF; //bit 14 low
    #define BOTHHIGH P2 = P2 | 0xC000; //bits 14 and 15 high
    #define BOTHLOW P2 = P2 & 0x3FFF;
    #define RD 0x01;
    #define WR 0x00;






    /* global */
    unsigned char delayfull;
    unsigned char delayhalf;
    unsigned char onebit[16];
    unsigned char rx;


    /*** Executable Functions ***/
    void fnDelay(unsigned char mydelay)
    {
    unsigned char uchCounter;
    for(uchCounter = 0; uchCounter < mydelay; uchCounter ++); //need to play with this
    }



    void fnI2C_SendStart()
    {
    OUTPUT
    SETDATA
    SETCLOCK
    fnDelay(delayhalf);
    CLRDATA
    fnDelay(delayhalf);
    CLRCLOCK
    fnDelay(delayhalf);
    }


    void fnI2C_SendStop()
    {
    OUTPUT
    BOTHLOW
    SETCLOCK
    fnDelay(delayhalf);
    SETDATA
    fnDelay(delayhalf);
    CLRCLOCK
    fnDelay(delayhalf);
    }

    //receive byte function

    /* Read Byte */

    unsigned char fnI2C_ReceiveByte()
    {
    unsigned char uchBitCounter;
    unsigned char uchBitSelection;

    INPUT

    uchBitSelection=0x00;

    for(uchBitCounter=0;uchBitCounter<8;uchBitCounter++)
    {
    SETCLOCK
    fnDelay(delayfull);

    if((P2 & 0x4000) > 0) //looking for data on bit 14

    uchBitSelection|=0x01;

    if(uchBitCounter<7)
    uchBitSelection<<=1;

    fnDelay(delayfull);
    CLRCLOCK
    fnDelay(delayfull);
    }

    OUTPUT
    CLRDATA
    fnDelay(delayfull);
    SETCLOCK
    fnDelay(delayfull);
    CLRCLOCK
    INPUT

    return uchBitSelection;
    }

    unsigned char fnI2C_ReceiveByteNA()
    {
    unsigned char uchBitCounter;
    unsigned char uchBitSelection;

    INPUT

    uchBitSelection=0x00;

    for(uchBitCounter=0;uchBitCounter<8;uchBitCounter++)
    {
    SETCLOCK
    fnDelay(delayfull);
    if((P2 & 0x4000) > 0) // is bit 14 held high then the data is a 1
    uchBitSelection|=0x01;

    if(uchBitCounter<7)
    uchBitSelection<<=1;

    fnDelay(delayfull);
    CLRCLOCK
    fnDelay(delayfull);
    }

    SETCLOCK
    fnDelay(delayfull);
    CLRCLOCK

    return uchBitSelection;
    }




    /* Send Byte */

    unsigned char fnI2C_SendByte(unsigned char uchByte)
    {
    unsigned char uchBitCounter;
    unsigned char uchBitSelection;

    OUTPUT

    uchBitSelection = 0x80;

    for(uchBitCounter = 0; uchBitCounter <8; uchBitCounter ++)
    {
    if(uchByte & uchBitSelection)
    SETDATA
    else
    CLRDATA

    uchBitSelection >>= 1;

    SETCLOCK
    fnDelay(delayfull);
    CLRCLOCK
    fnDelay(delayfull);
    CLRDATA
    }
    CLRDATA //august
    INPUT
    SETCLOCK
    fnDelay(delayfull);
    // should check if acknowledge is received uchBitSelection = _PTA.Byte;
    CLRCLOCK

    uchBitSelection ^= 0xFF;
    uchBitSelection &= 0x08; //20 SHOULD BE 08
    fnDelay(delayfull);
    //return uchBitSelection;
    }




    /* Function Definitions */






    // Main Function Entered From Reset

    void main (void) {

    unsigned char i;
    long x;




    delayfull = 8;//play with these to get the i2c repetition rate
    delayhalf = 4;







    DP2 = 0xFF00;

    P2 = P2 & 0x00FF; //clear top 8 bits





    IEN = 1; /* Enable Interrupts (for monitor) */


    //write one byte to flash
    fnI2C_SendStart();
    fnI2C_SendByte(0xA0); //device address write mode
    fnI2C_SendByte(0); //lo address
    fnI2C_SendByte(45); //a number
    fnI2C_SendStop();

    //nasty - should check for something in the chip
    //wait for data line to go low

    for (i=0;i<80;i++) //worked at 100 and 80 and 60, failed at 40
    {
    fnDelay(127);
    }

    //read it from flash
    //random access read according to st
    fnI2C_SendStart();
    fnI2C_SendByte(0xA0); //device address write mode
    fnI2C_SendByte(0);
    fnI2C_SendStart();
    fnI2C_SendByte(0xA1); // read

    i = fnI2C_ReceiveByteNA(); //NA??

    fnI2C_SendStop();

    printf("%d",i);





    /* Loop forever */



    while(1)
    {


    } // end of while forever loop



    } //end of main


    ....

    good luck

    steve robinson

Reply
  • This is all the code for C167 to 24C08.

    You will need to sort out your own 'printf' statement.

    #define _MAIN_
    /*********************************************************************/

    /*********************************************************************/

    #include <reg167.h>
    #include <stdio.h>
    #include <string.h>

    //I2C values
    #define INPUT DP2 = DP2 & 0xBFFF; //bit 14 of P2 used for I2C data line
    #define OUTPUT DP2 = DP2 | 0xC000; // bit 14 is ourput , lets do bit 15 here as well!
    #define SETCLOCK P2 = P2 | 0x8000; //bit 15 high
    #define CLRCLOCK P2 = P2 & 0x7FFF; //bit 15 low
    #define SETDATA P2 = P2 | 0x4000; //bit 14 high
    #define CLRDATA P2 = P2 & 0xBFFF; //bit 14 low
    #define BOTHHIGH P2 = P2 | 0xC000; //bits 14 and 15 high
    #define BOTHLOW P2 = P2 & 0x3FFF;
    #define RD 0x01;
    #define WR 0x00;






    /* global */
    unsigned char delayfull;
    unsigned char delayhalf;
    unsigned char onebit[16];
    unsigned char rx;


    /*** Executable Functions ***/
    void fnDelay(unsigned char mydelay)
    {
    unsigned char uchCounter;
    for(uchCounter = 0; uchCounter < mydelay; uchCounter ++); //need to play with this
    }



    void fnI2C_SendStart()
    {
    OUTPUT
    SETDATA
    SETCLOCK
    fnDelay(delayhalf);
    CLRDATA
    fnDelay(delayhalf);
    CLRCLOCK
    fnDelay(delayhalf);
    }


    void fnI2C_SendStop()
    {
    OUTPUT
    BOTHLOW
    SETCLOCK
    fnDelay(delayhalf);
    SETDATA
    fnDelay(delayhalf);
    CLRCLOCK
    fnDelay(delayhalf);
    }

    //receive byte function

    /* Read Byte */

    unsigned char fnI2C_ReceiveByte()
    {
    unsigned char uchBitCounter;
    unsigned char uchBitSelection;

    INPUT

    uchBitSelection=0x00;

    for(uchBitCounter=0;uchBitCounter<8;uchBitCounter++)
    {
    SETCLOCK
    fnDelay(delayfull);

    if((P2 & 0x4000) > 0) //looking for data on bit 14

    uchBitSelection|=0x01;

    if(uchBitCounter<7)
    uchBitSelection<<=1;

    fnDelay(delayfull);
    CLRCLOCK
    fnDelay(delayfull);
    }

    OUTPUT
    CLRDATA
    fnDelay(delayfull);
    SETCLOCK
    fnDelay(delayfull);
    CLRCLOCK
    INPUT

    return uchBitSelection;
    }

    unsigned char fnI2C_ReceiveByteNA()
    {
    unsigned char uchBitCounter;
    unsigned char uchBitSelection;

    INPUT

    uchBitSelection=0x00;

    for(uchBitCounter=0;uchBitCounter<8;uchBitCounter++)
    {
    SETCLOCK
    fnDelay(delayfull);
    if((P2 & 0x4000) > 0) // is bit 14 held high then the data is a 1
    uchBitSelection|=0x01;

    if(uchBitCounter<7)
    uchBitSelection<<=1;

    fnDelay(delayfull);
    CLRCLOCK
    fnDelay(delayfull);
    }

    SETCLOCK
    fnDelay(delayfull);
    CLRCLOCK

    return uchBitSelection;
    }




    /* Send Byte */

    unsigned char fnI2C_SendByte(unsigned char uchByte)
    {
    unsigned char uchBitCounter;
    unsigned char uchBitSelection;

    OUTPUT

    uchBitSelection = 0x80;

    for(uchBitCounter = 0; uchBitCounter <8; uchBitCounter ++)
    {
    if(uchByte & uchBitSelection)
    SETDATA
    else
    CLRDATA

    uchBitSelection >>= 1;

    SETCLOCK
    fnDelay(delayfull);
    CLRCLOCK
    fnDelay(delayfull);
    CLRDATA
    }
    CLRDATA //august
    INPUT
    SETCLOCK
    fnDelay(delayfull);
    // should check if acknowledge is received uchBitSelection = _PTA.Byte;
    CLRCLOCK

    uchBitSelection ^= 0xFF;
    uchBitSelection &= 0x08; //20 SHOULD BE 08
    fnDelay(delayfull);
    //return uchBitSelection;
    }




    /* Function Definitions */






    // Main Function Entered From Reset

    void main (void) {

    unsigned char i;
    long x;




    delayfull = 8;//play with these to get the i2c repetition rate
    delayhalf = 4;







    DP2 = 0xFF00;

    P2 = P2 & 0x00FF; //clear top 8 bits





    IEN = 1; /* Enable Interrupts (for monitor) */


    //write one byte to flash
    fnI2C_SendStart();
    fnI2C_SendByte(0xA0); //device address write mode
    fnI2C_SendByte(0); //lo address
    fnI2C_SendByte(45); //a number
    fnI2C_SendStop();

    //nasty - should check for something in the chip
    //wait for data line to go low

    for (i=0;i<80;i++) //worked at 100 and 80 and 60, failed at 40
    {
    fnDelay(127);
    }

    //read it from flash
    //random access read according to st
    fnI2C_SendStart();
    fnI2C_SendByte(0xA0); //device address write mode
    fnI2C_SendByte(0);
    fnI2C_SendStart();
    fnI2C_SendByte(0xA1); // read

    i = fnI2C_ReceiveByteNA(); //NA??

    fnI2C_SendStop();

    printf("%d",i);





    /* Loop forever */



    while(1)
    {


    } // end of while forever loop



    } //end of main


    ....

    good luck

    steve robinson

Children
No data