# Understanding PIC32 digital output with open-drain

I'm trying to understand how to operate digital pins configured as open-drain outputs on a pic32. As I've read on other forums, setting TRISx to 0 and ODCx to 1 configures the pin as open-drain output, and writing 1 to LATx will drive the output high-Z (w/pullup resistor) and writing 0 will drive it low.

If you have an actual open drain output, you can keep TRIS low and control the transistor with LAT. LAT=0 => transistor closed to ground and the output will be low, LAT=1 => transistor open and output will be floating or clamped high with a resistor (or pulled low by another signal).

What I'm caught up on is this part of the diagram for the pins:

From the PIC32 IO Port Reference Manual

What does ODCx do if TRISx is set 0? The output of the multiplexer controlled by ODCx will be low no matter what, as its either TRISx or (TRISx AND 'LATx), which will always be 0 when TRISx is 0.

p12-3 from I/O Ports under 12.2.4 Registers for Open-Drain Configuration (ODCx):

n. If the ODCx bit for an I/O pin is a ‘1’, the pin acts as an open-drain output.

and:

The ODCx register setting takes effect in all the I/O modes, allowing the output to behave as an open-drain even if a peripheral is controlling the pin. Although the user could achieve the same effect by manipulating the corresponding LATx and TRISx bits, this procedure will not allow the peripheral to operate in Open-Drain mode

To enable Open-Drain mode, you have to use ODCx.

p12-3 under 12.2:1 Registers for Configuring Tri-state Functions (TRISx)

If data direction bit is ‘0’, the corresponding I/O port pin is an output

and under 12.2.3 Registers for Configuring Latch Functions (LATx)

A write to a LATx register latches data to corresponding port I/O pins. Those I/O port pin(s) configured as outputs are updated.

So ODCx = 1, TRISx = 0. Then output a 0 to LATx and output is driven low. A 1 and output is open-drain with pull-up to a max of $V_{IH}$.

Which agrees with your thoughts, web. But it is always better to go to the data sheet.

• "So ODCx = 1, TRISx = 0. Then output a 0 to LATx and output is driven low. A 1 and output is open-drain with pull-up to a max of VIH. " - That's how I understood it, I just couldn't rectify that with what I was seeing in the diagram. Thanks. – tylero Feb 28 '17 at 19:40
• Well sometimes a block diagram does not really represent what is being done. 8) – StainlessSteelRat Feb 28 '17 at 20:55

TRISx basically defines whether the pin is an output pin or not. ODCx determines whether the output is active both ways or ONLY pulls low.

So you are correct, if TRISx is not set, ODCx does nothing.

Note it's not truly open-drain... it's more Low or Disabled. Because of that it is prudent, when changing the pin mode on the fly, to turn off TRISx BEFORE or while changing OCDx.

BTW: From the shown circuitry, I have always thought that implementation presents a bit of a race hazard between the latch value propagation and the enabling mechanism path. However, I never had any issues with it, perhaps others have encountered some strange behaviours.