<HOME
<Electronics

PIC16F876 RB6/PGC weirdness

Here's a weird problem I encountered with a PIC16F876 device. The same may or may not occur on other PIC devices as well.

I was using the '876 as the brains of a stepper motor control thingy (as part of a custom rotateable sample jig in an EDM machine), with a four-button keypad. The four push-button switches were connected to the RB4, RB5, RB6 and RB7 pins of the device. The other ends of the switches were connected to ground. Like this:

The four PORTB pins were configured as inputs, with their internal pull-ups active. Simple, yes? The other pins of the PIC controlled the stepper motor electronics and also drove a multiplexed four-digit 7-segment LED display. Those are not shown here.

Everything worked fine on breadboard. But when I soldered up the final PCB and put the keypad at the end of a 1.5 m long cable, something strange happened. One of the keys on the keypad caused erratic and frankly quite impossible behavior in the PIC. Behavior that did not make sense at all. Not something you'd expect from switch bounce or some such effect (I accounted for switch bounce in the software), but it appeared to completely mess up program execution within the device. As if it caused an unintentional jump to some odd position in the program. The device would start doing odd things—performing bits of existing program out of sequence—and finally would crash completely.

Mind you, the other three keypad buttons worked just fine. It was only the one wired to RB6/PGC that caused the program to crash. And only when the keypad was wired through a long cable. The cable terminated in a jumper block near the PIC—if I disconnected that and simulated a keypress by shorting the appropriate pins with a paperclip, there was no problem! But the keypad at the end of its long cable caused the program to misbehave.

Apparently, the increased inductance or capacitance or whatnot of the cable caused the transition at the input pin to do something weird, perhaps a momentary voltage spike outside the supply rails—I don't know, I did not put an oscilloscope probe at the input to look. And apparently RB6 was more sensitive than the other pins, perhaps due to it being a programming pin (although the device was not in programming mode, of course). During programming, the RB6/PGC pin clocks the program counter through the program memory, so perhaps unwanted voltage spikes during operation can also cause glitches to the program counter? Who knows.

The solution, finally, was to put a 100-ohm resistor in series with the input pin, close to the PIC. Suddenly everything just worked again. I don't know whether this is specific to the PGC pin in this device, or whether an even longer keypad cable would have caused similar problems with RB7/PGD as well, perhaps also with RB4 and RB5? Just to be safe, I put similar series resistors on all four inputs. And just in case similar problems might exist in other PIC devices as well, I have made a point of using series resistors whenever there is any significant length of wire leading from a keypad or an interlock microswitch to a digital input pin.


Antti J. Niskanen <uuki@iki.fi>