wire.h library questions

I am experimenting with getting two arduinos to talk to each other using the Wire.h library and I2C.

Has anyone created a tutorial yet that demonstrates code for a master and slave? From reading the forums it seems that some people have encountered problems using some of the functions if they did not insert adequate delays.

Will the library work only on analog pins? Can I change them from pins 4 and 5 to something else?

How come it does not use the internal pullups and I have to use external ones on the I2C bus lines?

just realised there are some great demos of code already in the library. Which negates my need for a tutorial.

I am still curious abut changing the pins used and the need for pullups though.

thanks!

Ok now I found in the twi.c code that the internal pullups are activated on pins 4 and 5 of port c.

  #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__)
    // activate internal pull-ups for twi
    // as per note from atmega8 manual pg167
    sbi(PORTC, 4);
    sbi(PORTC, 5);
  #else
    // activate internal pull-ups for twi
    // as per note from atmega128 manual pg204
    sbi(PORTD, 0);
    sbi(PORTD, 1);
  #endif

I assume I could change the pins here to other analog pins but do they have to be analog? Do I still need the external pullups as well?

I think the TWI pins are hardwired in the Arduino chip so can’t be changed.

What would those two pins be doing that is unique to I2C communication that other pins could not do? i.e. What special hardware is built into that port/pins for I2C?

Intellijel,

This should help answer why you can not change the I2C pins: http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf (see Ch. 21)

That answers all my questions :) thanks

That doc also explains why I need external pullups as well.

If you look here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1205243372

Its my Dual Core/IC project that relies on i2c for chip to chip communications. There might be some info there that you'll find useful, particularly the value for the pull ups.

I wasnt able to download that pdf document intellijel, what was the explanation given about why external pull ups are needed?

I already found that out for myself a long time ago but hadnt read anything about it, so Id be interested to know what you found.

To quote the pdf:

Electrical Interconnection As depicted in Figure 21-1, both bus lines are connected to the positive supply voltage through pull-up resistors. The bus drivers of all TWI-compliant devices are open-drain or open-collector. This implements a wired-AND function which is essential to the operation of the interface. A low level on a TWI bus line is generated when one or more TWI devices output a zero. A high level is output when all TWI devices tri-state their outputs, allowing the pull-up resistors to pull the line high. Note that all AVR devices connected to the TWI bus must be powered in order to allow any bus operation. The number of devices that can be connected to the bus is only limited by the bus capacitance limit of 400 pF and the 7-bit slave address space. A detailed specification of the electrical char- acteristics of the TWI is given in “2-wire Serial Interface Characteristics” on page 309. Two different sets of specifications are presented there, one relevant for bus speeds below 100 kHz, and one valid for bus speeds up to 400 kHz.

John, The pdf link I gave to intellijet is just the datasheet for the ATMega168. You likely have it already. :)

It linked from here - http://arduino.cc/en/Main/Hardware

This reads well with the 21-1 diagram :)

Electrical Interconnection As depicted in Figure 21-1, both bus lines are connected to the positive supply voltage through pull-up resistors. The bus drivers of all TWI-compliant devices are open-drain or open-collector. This implements a wired-AND function which is essential to the operation of the interface. A low level on a TWI bus line is generated when one or more TWI devices output a zero. A high level is output when all TWI devices tri-state their outputs, allowing the pull-up resistors to pull the line high. Note that all AVR devices connected to the TWI bus must be powered in order to allow any bus operation. The number of devices that can be connected to the bus is only limited by the bus capacitance limit of 400 pF and the 7-bit slave address space. A detailed specification of the electrical char- acteristics of the TWI is given in “2-wire Serial Interface Characteristics” on page 309. Two different sets of specifications are presented there, one relevant for bus speeds below 100 kHz, and one valid for bus speeds up to 400 kHz.