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

8051 & I2C

Does someone have ever develop a program defining an i2c communication protocol for a 8051 ? It would help me make my AT89C51ED2 communicating with another board via i2c bus.

I would be interested if someone have some examples for that.
jeab@free.fr

Regards,

Parents
  • In my case I wrote next:


    #define SDATA P1_6
    #define SCLK P1_7


    void i2c_start (void)
    {
    SDATA = 1;
    SCLK = 1;
    SDATA = 0;
    delay(1);
    SCLK = 0;
    }

    void i2c_stop (void)
    {
    SCLK = 0;
    SDATA = 0;
    delay (1);
    SCLK = 1;
    delay (1);
    SDATA = 1;
    }

    void i2c_put (unsigned char put_data)
    {
    unsigned char i;
    for (i=0;i<8;i++)
    {
    SDATA = ((put_data & 0x80)?1:0);
    put_data <<=1;
    SCLK = 1;
    delay (1);
    SCLK = 0;
    }

    i = SDATA;
    SCLK = 1;
    delay (1);
    SCLK = 0;
    }

    unsigned char i2c_get (void)
    {
    unsigned char i,input_data;

    i = SDATA;
    input_data = 0;

    for (i=0; i<8; i++)
    {
    input_data <<= 1;
    SCLK = 1;
    input_data |= SDATA;
    SCLK = 0;
    }
    return input_data;
    }


    Read the description as mentioned above and everthing should be OK.

Reply
  • In my case I wrote next:


    #define SDATA P1_6
    #define SCLK P1_7


    void i2c_start (void)
    {
    SDATA = 1;
    SCLK = 1;
    SDATA = 0;
    delay(1);
    SCLK = 0;
    }

    void i2c_stop (void)
    {
    SCLK = 0;
    SDATA = 0;
    delay (1);
    SCLK = 1;
    delay (1);
    SDATA = 1;
    }

    void i2c_put (unsigned char put_data)
    {
    unsigned char i;
    for (i=0;i<8;i++)
    {
    SDATA = ((put_data & 0x80)?1:0);
    put_data <<=1;
    SCLK = 1;
    delay (1);
    SCLK = 0;
    }

    i = SDATA;
    SCLK = 1;
    delay (1);
    SCLK = 0;
    }

    unsigned char i2c_get (void)
    {
    unsigned char i,input_data;

    i = SDATA;
    input_data = 0;

    for (i=0; i<8; i++)
    {
    input_data <<= 1;
    SCLK = 1;
    input_data |= SDATA;
    SCLK = 0;
    }
    return input_data;
    }


    Read the description as mentioned above and everthing should be OK.

Children
No data