On Wed 14 Jan, "Stevenson, Brad" wrote: > I recently wrote some 'master' i2c code to access a serial eeprom. For > the two pins (sda and scl) needed, I set the corresponding bits in the > port data registers to zero, and set the corresponding tristate [snip] > > One 'gotcha' to be careful of if you're doing it this way: avoid using > read-modify-write commands (such as bsf, bcf) on any of the other pins > on the same port as your i2c pins. Since your i2c pins are set as inputs > and pulled high (with the data bits set to zero), a read modify write > command will 'see' the logic one's on the inputs during the read, and > change your data bits from 0 to 1 on the write portion of the opcode. > The sympton you'll see when trouble shooting is: absolutely nothing will > happen on your port pins when you call your i2c routines. When the I2C bus is idle, the two pins float, which means that it is then safe to use BSF/BCF on other pins of the port. Before you send the next I2C message you have to clear the two I2C data bits agin -- I usually have that code in the subroutine which generates the I2C start condition. One thing NOT to do there is this: SendI2CStart bcf SDA ; clear both data bits bcf SCL ; ** WRONG ** . . etc. The second instruction will of course set the SDA data bit to high again . Instead, my I2C file goes like this: SendI2CStart movlw ~(SDA_MASK|SCL_MASK) ; make sure output lines are low andwf I2CPORT ; when outputs are enabled <20us delay here for benefit of slow PIC slaves> bsf status_rp bcf SDA ; pull SDA low while SCL is high bcf status_rp <20us delay here for benefit of slow PIC slaves> retlw 0 The delays in the start condition allow PICs running at 4MHz to detect start conditions more easily; when the PIC sees one, it then can read the I2C message at high speed. I'm using this code in all my PICs now so that in the future I won't have to replace any I2C code when I want to add a PIC slave to a device. Frank ------------------------------------------------------------------------ Frank A. Vorstenbosch Phone: +44-181-636 3391 Electronics & Software Engineer Home: +44-181-544 1865 Eidos Technologies Ltd., Wimbledon, London Mobile: +44-976-430 569