PIC Tricks
and Tips

Touch Switch

After several experiments with touch sensors, this one works really well.  One pin on the PIC is used to create a short pulse.  Any number of other pins act as touch sensitive inputs.  Here is what a snipet of the code for 4 inputs looks like...
bsf	rb,4    ;start pulse
movf rb,w ;read port b
bcf rb,4 ;end pulse
andlw 0Fh ;select input bits

The 10k resistors shown seem to work well with a 20mhz clock.

The circuit works like this...

With no additional capacitance (body contact) with the touch sensor, the input goes high by the time the port is read by the instruction after the one setting RB4 high.  If just a bit of capacitance is added (by touching the contact), the input is still low when it is read.

Because there is no tacktile feedback or positive 'action' on this type of switch, it is a good idea to do a software 'debounce' of the signal - read it several times in a row to be sure of the state of the switches.

For the sensors themselves, I like to use silver colored thumb tacks ($1 for box of 100) from the local office supply.  These work well on plastic panels and can be easily solderd.

Better, faster, random numbers

Many random number generators are software versions of the hardware shift register feedback circuit.   Two problems with this approach are that this operation is usually clumsy in software and the results have some noticeable patterns.

Some time ago, I don't remember just when, I came up with the Chop Suey Machine which works as follows:

Start with a set of memory locations (any size will do).  Add the first value to the second one and store it in the second one.   Now take the result and add it to the third one and store it there.   Continue for as many locations as you like. Finally, add to the 1st byte and swap nibbles.

A three byte implementation in PIC code looks like this....

chop    movf    val1,w     ;get the 1st
        addwf   val2,w     ;add the 2nd
        movwf   val2       ;store the 2nd
        addwf   val3,w     ;add the 3rd & store
        movwf   val3       ;back in 3rd
        addwf   val1       ;add back to 1st
        swapf   val1       ;swap nibbles
        movf    val1,w     ;return one byte

An initial state of zero's in all locations results in zero's being output.  Seed at least one location with a non zero value to start.   Adding additional locations greatly increases the length of the series while adding very little overhead (2 instructions each).  There is no guarantee of producing a maximum length sequence with this little hack.   You can't even guarantee the sequence length.  Like may random generators, sequence length may depend on the initial values.


Slow Motion Clock

Sometimes the sleep mode will not work for some application that needs to keep an eye on some things while radically conserving power.   In that case, you can just slow down the (RC) clock speed by switching a second resistor in or out of the circuit. 

In this example, output RA is either driven high (for high speed mode) or driven tri-state (for low speed mode).   The values shown produce clock speeds of 2 mhz and 60 khz respectively.

10 Pulse Coding
Ten-pulse coding was developed to save power, increase range, and be very speed tolerant.  It consists of ten pulses with nine timed gaps between them.  The first gap is the reference time.  The eight remaining gaps are either longer (one) or shorter (zero) than the reference.  My conventions is to transmit data LSB first as in standard serial (async) data. 

Depending on how the data is to be transmitted (wire, RF, IR, etc.), the width of the pulses, the timing between them and the 'spread' between zero and one values can be selected.   For instance, if you want to increase range of an IR data link, you can run the IR emitter well beyond its designed continuous power limits due to the short pulses.  This increases range without increasing average power consumption.

RC clocking can be used at both ends, if necessary.  Each data byte contains its own reference time so even timing drift between successive bytes can be tolerated.


Anti-crosstalk for Network I/O

This example is using a PIC16F628 - a real favorite of mine recently.   Among its other features is a full function hardware UART.  This circuit was part of a servo motor controller circuit using a 74LS07 in the design.  One (left over) section of this chip is used to provide the open collector output drive.   Input in normally routed thru the 10k resistor to the RX input.  In order to provide anti-crosstalk (keeping the the output data from comming back into the input), a small pnp transistor keeps the input line from going low durring the low levels of the output line.  Beats the heck out of doing it in software!

Video Sync (genlock) for PICs

In many cases, especially for video overlay operations, it is necessary to synchronize a PIC to some external event (such as horizontal sync pulses).

This circuit uses only one extra part - a D-type flip-flop (such as a 74hc74).  The pic is configured to run an external RC oscillator but the normal pull-up resistor is connected to the output of the 74hc74.  The external sync pulse sets Q high allowing the oscillator to run until the pic sets RA,0.  This stops the processor until the next sync pulse comes along.  This not only locks the clock to the external event but locks the processor cycle as well.  Any video generated with the pic in this way will be 'genlocked' to the original video signal.