1
\$\begingroup\$

I'm a software engineer playing with PICs and have an old PS/2 mouse that I am using (successfully), which has CLK and DATA connections as open collectors.

So I have configured the internal pullups on the relevant PIC pins and am setting TRIS to 1 to let the lines go high and setting TRIS to 0 and writing 0 to the relevant PORT bit to force it low.

However, I've seen several similar PIC-to-PS/2 mouse implementations that, when writing to the mouse, set TRIS to 0 and write 1 to the PORT bit instead of setting TRIS to 1.

As I understand it, either of these will result in the line going high (is that correct?), but I'm not clear if one is somehow "better/safer" than the other.

Should I just follow the examples of the (presumably) more experienced implementations and write 1's to the PORT when the PIC is in control of the bus, or should I always make the line high by tri-stating to high impedance and letting the pullup do it?

I hope that makes sense and I haven't mixed any terminology - any general advice about best practices for open collector connections would be welcome.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

It's not really wrong to set the pin HIGH, but doing so may cause output contention. That is, you may get into a situation where your code is driving the pin HIGH, but the device at the other end is trying to drive the same pin LOW. The PS/2 mouse (and keyboard) protocol uses open-collector signalling to avoid contention. In an open-collector system, signals are assumed to float HIGH when they are not driven by a pin, i.e. when the pin(s) are high impedance. A pin can safely be pulled LOW by the device at either end of the wire.

So, it's probably best to use TRIS in this case.

\$\endgroup\$
5
  • \$\begingroup\$ Ok thanks, that makes sense. Does that mean if I drive a pin high and the mouse drives it low that I could blow a pin? \$\endgroup\$ Nov 27, 2014 at 14:08
  • \$\begingroup\$ @RogerRowland Possibly, but it's unlikely if the short is brief. Still, better to avoid that situation. \$\endgroup\$ Nov 27, 2014 at 14:12
  • \$\begingroup\$ Ok, I'll avoid it then. Sounds safer to me! One other point - would there be a significant difference in the time it takes the line to go high? Sorry if that's a stupid question. Still on a learning curve here. \$\endgroup\$ Nov 27, 2014 at 14:20
  • \$\begingroup\$ @RogerRowland In principle, the line will go HIGH faster if you drive it HIGH. The pull-up resistor must charge the capacitance of the wiring. Driving it HIGH will supply a larger current and charge the capacitance faster. But the choice of pull-up resistor value, data rate and protocol should avoid any problems. \$\endgroup\$ Nov 27, 2014 at 14:31
  • \$\begingroup\$ Thanks again John. My PS/2 mouse seems to provide ~12.5KHz clock, so I guess that's not "fast"? I've no idea what the built-in pullups are on my PIC though (I'm using PIC16F648A). I expect I'll suck it and see .... \$\endgroup\$ Nov 27, 2014 at 14:37
2
\$\begingroup\$

There is one case where it is useful to set the pin HIGH.

Where the open-collector logic is relatively slow, either because the pull-up resistor must be high (to save power) or because it's connected to a long wire or PCB trace, an output can be driven HIGH for one clock cycle and tri-stated in the next. (These must happen as an atomic operation, e.g. with interrupts disabled)

This gives a relatively fast rise time, and limits the duration of any contention to be short enough not to cause damage. (and of course that must be verified via the datasheets)

\$\endgroup\$
2
  • \$\begingroup\$ Ahh! The best of both worlds then? Fantastic! I'm running the PIC at 20MHz, so maybe only exposed for ~400ns if I do this in two consecutive instructions. I suppose a PS/2 mouse cable counts as "a long wire" so this would be a good approach? \$\endgroup\$ Nov 27, 2014 at 14:41
  • 1
    \$\begingroup\$ You could do that, but it might be better just to add external pull-up resistors. Microcontroller pull-ups are very weak; they're mainly intended to keep undriven inputs from floating. You shouldn't need to drive the pin high for such a low speed signal. If it's really 12.5kHz, even the internal pull-ups are probably fast enough. You'd need an oscilloscope to be sure. \$\endgroup\$
    – Adam Haun
    Nov 27, 2014 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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