Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
    
With PIC as an I2C bus master, you do need to add pull-up resistors. –  Nick Alexeev Nov 15 '12 at 6:07
    
@NickAlexeev : With the PIC as any part of the system you'll need to add pull-up resistors! I2C is always an open-drain system so something has to pull the bus up to a voltage unless you have some weird, non-standard I2C implementation. For clarity though: you only need one set of pull up resistors on the bus no matter how many nodes you have. –  Kit Scuzz Nov 15 '12 at 6:54
    
@KitScuzz I agree with your first sentence about adding pullup resistors. There is a remote possibility that a bus master (not a PIC) has got built-in pull-ups. Then again, external resistors may be more reliable. –  Nick Alexeev Nov 16 '12 at 5:47
    
I know it needs it, I was just wondering how it works, since the is no open drain output in the datasheet –  mFeinstein Nov 16 '12 at 18:13

2 Answers 2

up vote 2 down vote accepted

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!

share|improve this answer

The PIC has a built-in peripheral (the MSSP) that allows hardware-based i2c operation. The open-drain behavior results from the fact that it operates the direction control (aka "TRIS") automatically for these pins. If the MSSP is disabled, the port pins become ordinary I/O pins and now they're controlled the conventional way, and if programmed as output they will drive high or low.

What was confusing you was the fact that they can operate in either mode. You'd use the pullup resistors if it were set up for i2c, but not if the port pins were being used as general-purpose I/O.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.