Most microcontrollers don't have dedicated open-drain outputs. On some PICs the PA4 pin (also known as RTCC) may be used as an open-drain output. Most of the other port pins are bidirectional, and have separate output data registers and direction registers.
On the PIC, the direction registers are called TRIS registers, an abbreviation for Tri-state, which is actually a trademark of National Semiconductor. The TRIS register bits should be set to logic 0 for lines that should be active outputs, and logic 1 for lines that should be high-impedance inputs. Note that this is the reverse of the conventions used by almost all other microcontrollers and peripheral devices.
In order to obtain the effect of open-drain output, it is necessary to use the TRIS register bit to control the output rather than the more usual use of the data output register. This is most easily accomplished by leaving the appropriate bit of the data output register set to 0, and setting the corresponding TRIS bit to 0 when the output should sink current, and 1 when the output should be in the open-drain (high-impedance) state.
On the low-end PIC parts (16C5x family) there is no way to access the TRIS register other than writing the whole register with the TRIS instruction. That is OK if you only have one output for which the TRIS bit ever needs to change, but if more than one TRIS bit is dynamic you will have to either keep a shadow copy of the TRIS register in RAM, or recompute the correct values to be used for all the bits.
On the mid-range PIC parts (all other 16C parts), the TRIS register can be read, but it is in the second register bank. This would typically require setting the RP0 bit in the status register to switch banks, changing the TRIS bit, and clearing the RP0 bit again. However, if you are going to be changing TRIS bits for a particular port frequently, and you don't otherwise need to use indirect addressing, you can put the address of the TRIS register into FSR and access the TRIS register indirectly through IND.
As a consequence of this, if you use a BSF or BCF instruction to set or clear bits other than your open-drain output, it may change the output register bit for the open drain output pin. The safest course of action is to avoid using RMW instructions on ports entirely, by doing the RMW on a file register then copying the file register to the port.
This problem is described in the Microchip data sheets.
Last updated September 9, 1995
Copyright 1995 Eric Smith