I have a relay with 5V on one side of the relay's coil and the collector of a darlington transistor on the other side. I have a diode reverse-biased across the coil. I am using a 16F877A with PORTB as inputs (active low) with weak pull-ups enabled. On B0 I monitor whether a 12kHz square wave is present. The square wave is a good 5V-0V. If B0 has a 12kHz signal, I make A2 high, which is connected to the base of the darlington. This allows the relay's coil to find ground through the transistor and turn on.

However, although the relay turns on with the 12kHz signal present, every second for a split second the relay turns off and then back on and so on and so on. When a put a digital probe on A2 during this on-off cycle, the pin mimics the on-off cycle of the relay with a high-low cycle.

Is the problem with my code? If not, any suggestions?

MAIN:
COUNT PORTB.0, 1, Counter
IF (Counter >= 11) AND (Counter <=13) THEN
PORTA.2 = 1
ELSE
PORTA.2 = 0
GOTO MAIN
ENDIF
. . .