0
\$\begingroup\$

enter image description here

Source

I have understood the fundamental theory of open collector and open drain configuration. But the problem is how to define/make a micro controller's pin as open drain? Can any pin of a micro controller can be used in open drain configuration??

  1. If I define a GPIO pin as an input pin, can I use this pin in open drain configuration?
  2. Is it mandatory to define a pin as output for using it in open drain configuration?
\$\endgroup\$
  • 2
    \$\begingroup\$ Your question is much too broad, it depends a lot with the MCU model. \$\endgroup\$ – user2233709 Jan 6 at 19:52
  • 2
    \$\begingroup\$ An open drain output behavior can be approached very closely with switching between input and output-driven-low mode. This is possible with (almost?) any CPU. \$\endgroup\$ – Oldfart Jan 6 at 19:57
2
\$\begingroup\$

Whether an output is an open drain or open collector or something else is a question of hardware design.

An open collector output has to be built so that the output pin is connected to the collector of a transistor of the chip - and the collector is connected to nothing else.

If a chip isn't designed with an open collector output, the no amount of configuration will get you one.

That said, many microcontrollers are designed to have switchable GPIO pins. You can configure a pin to be input or output, and you can configure what type of output.

What combinations are possible depends on what the hardware manufacturer decided to build into the hardware.

\$\endgroup\$
  • \$\begingroup\$ So it does not matter whether a pin is defined as input or output(by setting the appropriate bits). Structurally If the pin is an open drain pin, then it must require an external pull up resistor to operate?? \$\endgroup\$ – Srikrishna Jana Jan 6 at 20:45
  • \$\begingroup\$ "If a chip isn't designed with an open collector output, the no amount of configuration will get you one." is not pragmatically true, because as mentioned in comments one can typically use the port direction control rather than the data register. \$\endgroup\$ – Chris Stratton Jan 6 at 20:51
  • \$\begingroup\$ If it is open drain, then you will need a pull up to see the output change state. \$\endgroup\$ – JRE Jan 6 at 20:55
  • \$\begingroup\$ On many chips you can get the effect of an open collector output by toggling the pin mode instead of the output value. \$\endgroup\$ – immibis Jan 8 at 4:48
2
\$\begingroup\$

If I define a GPIO pin as an input pin, can I use this pin in open drain configuration?

Maybe. if the pin sees a voltage higher than the supply voltage of the microcrontroller bad things will happen. if it never does you can fake an open drain.

Is it mandatory to define a pin as output for using it in open drain configuration?

If you're required to define the direction of the pin at compile time you can't use it as open drain.`

The trick to faking open drain is to set the output bit to low, and send the signals by flipping the data-direction bit.on and off. with the direction set to input the pin looks to the external world much like an inactive open drain, with it set to output it looks like an active open drain.

\$\endgroup\$
1
\$\begingroup\$

How to define/make a micro controller's pin as open drain? Does any pin of a micro controller can be used in open drain configuration?

In the datasheet of a MCU, you can see if an GPIO pin is capable to be push-pull, open drain output. If so, you can configure the behaviour through registers. Most MCU manufacturers offer macros or functions to set the inputs and outputs correctly.

If i define a GPIO pin as an input pin, Can I use this pin in open drain configuration?

The open drain configuration itself does not influence the input. Furthermore, you can emulate an open drain (open collector) output: Set the (push-pull) output to low for OUTPUT LOW. and switch the data direction from output to input direction for an emulated OUTPUT HIGH.

Does it mandatory to define a pin as output for using it in open drain configuration?

You may define a GPIO to open-drain, e.g. use it as normal input, and switch to open-drain when changing the data direction.

\$\endgroup\$
  • \$\begingroup\$ What you said is mostly not correct. For example, early PIC microcontrollers do not have an explicit "open drain" configuration. However, one can easily emulate an open-drain output by setting the port pin as output and low, then flipping the DDR bit between input and output. That's why I don't like your statement "An open drain has nothing to do with the input". I'm not going to downvote but please consider rephrasing your answer. \$\endgroup\$ – Dwayne Reid Jan 8 at 1:01
  • \$\begingroup\$ @Dwayne Reid: thanks, I remember, I did it also on an PIC or AVR in the past. Edited my answer. \$\endgroup\$ – Tom Kuschel Jan 8 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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