On Wed, 7 Oct 1998, Quentin wrote: > Thanks to all that replied. > Using another pin to set high and read and then set low to read to > determine sink or source is a good idea, but I see some problems as you > don't know at that stage if the sensor is sensing or not, or if it is in > transition. Write a software filter to debounce transitions. > I try to stay away from any kind of switches as that is what confuse the > people that install it. Ok, no jumpers. > I've tinkered a bit with electronic workbench and came up with this: > As I want to use a 12C508 the pins already give me part of the solution. I > can use the Shmidt Trigger (sp) and do this: > 0 +5V > | > > > < 10K > > > 2K | ||20nF > 0------VVVV---0-----||----0 PIC Pin > | || > > > < 10K > > > | > --- > - 0V This won't work. It makes pulses that are 1/2 Vcc 'tall' due to clamping in the PIC input, AND the PIC input is d.c. floating meanwhile. Now, my second proposal: R1 R2 2K 220R Input O---/\/\/\---*---*---/\/\/\---O PIC pin | | K - - C1 A - A - | | | === === Z5V1 0.01 uF For every measuring cycle, you do the following: 1. Test the state on the PIC pin and store it as S0 2. Turn the pin to output, and output ~S0 for T = 3 * R2 * C1 3. Turn the pin to input, and start a counter. If after T = 3 * R1 * C1 the input is still ~S0: fail, switch not connected or open collector (?) If the pin returns to S0 (i.e. != ~S0) after a period shorter than this, then the state to be read is S0. Now, to detect changes only, for each measurement, have a 'last state' register, and a 'new states' register. After running the subroutine described above, XOR the byte containing S0 (if it succeeded) with OS0 (where OS0 is the stored 'old' state). This will set the corresponding bit IFF there was a change. Store S0 in OS0 for the next pass. For 2 or more bits to sense on a chip, run the #1 subroutine for each bit, then run the XOR. Its result may serve to trigger a network transmission etc (only if the result of the XOR is not all 0s). Bonuses: The input switch is debounced free of charge, and the PIC input protected from transients. There is a switch failure detection, that will also detect a very noisy switch as a failure sometimes, and will also detect open collector or momentary switches as failures - it's up to you ;) Assumptions: The circuit assumes that the source can supply or sink current at least sometimes ;) and that it is relatively slow changing. Peter