Electronic – PIC18F2550 I2C Open Drain

i2copen-collectoropen-drainpic

As far as I have read, the I2C pins are Open Drain or Open collector, but in the PIC18F2550 the datasheet doesnt say anything about those pins, and even says they are a digital output if you select them to be one.
Do I need to add a pullup resistor to the pins so to use them as a digitial output?
If I dont, how do they work in the I2C?
Thanks!

Best Answer

You can do open drain on any digital pin on the PIC yourself, it's easy! I'll show through a quick chunk of example code using RB0.

_TRISB0 = 1; // Set the pin to high impedance
_LATB0 = 0;  // Set the output low (this would be _PORTB0 on some pics)
// As long as you're using pin RB0 as a open-drain, don't touch _LATB0

// To output a "low" (drain) do this:
_TRISB0 = 0; // Set the pin to output, it's already low, so it will "drain"

// To output a "high" (open) do this:
_TRISB0 = 1; // Set the pin to high impedance, the pullup resistor will pull high

The I2C module will do something similar to this internally, without you doing anything. In other words: yes! You'll need a pullup resistor!


EDIT: To answer your second question which I did not directly address, the pin does not need a pullup resistor during normal digital output operation. My example was to show you how a normal, fully functioning digital I/O pin could operate as a open-drain without any additional behavior or behaving unexpectedly. I should also note that I have done this exactly what I showed here to do open-drain in real programs, so this isn't a toy example, it's really how you do it!

Related Topic