logo
background
 Home and Links
 Your PC and Security
 Server NAS
 Wargames
 Astronomy
 PhotoStory
 DVD making
 Raspberry Pi
 PIC projects
 Other projects
 Next >>

Using the PIC for everything

Links to all my PIC tips, tricks and 'mini-project' notes

Whilst the mid-range PIC's can tackle many complex and otherwise almost impossible applications with ease, the challenge is to minimise cost by using the cheapest baseline PIC 'whenever possible'. Baseline PIC's can be had for less than 50p each = I purchased many 16F5x chips for between 40 and 50p each (mainly from CPC as 'remaindered' stock in their 'Bargain bin' section).

The even cheaper to use 12F675 (it has an internal OSC) can be found for as little as 20p (in Qty 10pcs, eBay), as can many other PIC's for less than £1 each. These PIC's are so cheap that you will soon start using them 'for everything' (especially as the PIC can often be used in place of a higher cost 'single function' digital chip - such as divider, ADC, PWM generator etc.) !

Buying the PIC in a 'TSOP' package is (sometimes) cheaper than the DIL/DIP package version = and whilst this costs you 10-20p extra for a mini-PCB TSOP-DIP 'converter', if you use a 'bigger' PCB than the PIC TSOP really needs you can mount other devices (resistors, caps, even osc. crystals) on the same board - and make use of the extra 'pin holes' to wire this up to the rest of your circuit

Below is a mix of programming tips and tricks, common circuit tricks and all the 'mini-projects' I've used the PIC for

I hope these details proves as useful to you as it does to me !

Below, click on the '+' to expand 'in place' (includes diagrams/images) or click the title URL (to view/download the text only version).

(+) 0004 Multi byte ADD - (24bit)

(+) 0005 new PIC 33 instruction set - (macros)

(+) 0006 Binary multiply methods

(+) 0007 8x8 - (multiply)

(+) 0008 8x16 - (multiply)

(+) 0011 Bi color LED driving

(+) 0012 One pin dual LED and button detect



(-) 0013 Input only multi button detect


Multiple-button detect with an 'input only' pin

The 'standard' method of detecting any of multiple push-buttons is to use the 'variable resistance measurement' approach.

This relies on using a single pin first as an output - to set a capacitor to a 'known' start voltage (logic 'hi') - and then as an input (to sense when the variable resistance has discharged the capacitor to the 'low' level). The Hi-to-Lo time then reveals the value of the resistance. The resistance can be re-measured multiple times as the PIC can recharge the capacitor by using the pin as an output - so the PIC can also discover how long the button is held down for (especially useful if the button controls movement).

However when the PIC pin is an 'input only' things get a bit more complicated.

Using the internal weak-pull-up

Not all 'input only' pins are only inputs :-)

For example, the PIC10F206 'input only' GP3 pin can be programmed to enable (or disable) a 'weak pull-up', which is specified as 16-23k (typically 21k at Vdd 5v5).

Whilst this allows the pin to source current (for capacitor charge-up) plainly the 'button press' resistance will have to be higher (and the capacitor value rather lower) than 'normal' (when the 'pull Hi' is more than 100x lower, at 180R). Further, the low currents involved makes measurement less reliable as leakage currents etc. have much more effect, however multiple measurements are still possible as the capacitor can still be recharged 'under control'.

Using a second pin to assist

If the input only pin has no internal weak pull-up (or more than half a dozen or so button have to be 'sensed'), it is still possible to use the 'multiple measurement' approach if one of the PIC pins dedicated to some other task can be used to 'assist' the button sense pin.

For example, a pin otherwise dedicated to serial transmission can be used when no serial transmission is needed. A diode can be fitted so that when the Tx pin is outputting '1', current flows to 'charge up' the button sense capacitor. When a measurement is needed, the Tx is switched to input with 'weak pull-up' (so the Serial line id still idle = Hi), and the 'input only' pin used to sense how long the button takes to discharge the capacitor.

Additional measurements can be made by setting the Tx to output '1' and recharge the cap.

Input only with no recharge

If no second pin is available, then the push-buttons have to be wired up to generate a 'pulse' that can be measured.

Typically, the pulse is generated as the button resistance charges or discharges a capacitor - so the time taken gives the resistance value. However, once the stored charge is 'used up', the button has to be released before another 'pulse' can be generated.  This means the code only has 'one chance' to detect the button, can't perform multiple measurements to improve 'which button' accuracy and can't discover when a button is being held down.

A further disadvantage is that, unless the pin can be set to  'Interrupt' the PIC on a 'state change', your 'main loop' code will need to be checking at frequent intervals.

Using a charged capacitor to generate a '1' pulse
The sense pin is pulled Lo with a 'lowish value' resistor (so 'no button press' = '0'). A separate capacitor is charged up with a 'high value' resistor.

When a button is pressed, it connects the (charged) capacitor to the sense pin with a lowish value resistor, so a '1' is seen. Up to half a dozen buttons (each with a different resistor) can be wired to the same sense pin.

When the button is pushed (and the charge on the capacitor pulls the pin Hi) the capacitor will discharge via a combination of the push-button resistor and the sense pin 'lo pull' resistor. The Hi time reveals the button resistance.

The button has to be released before the capacitor can charge up for another 'sense' cycle. The combination of capacitor 're-charge' resistor and button resistor has to be high enough to allow the sense pin to reach '0' (with it's 'lo pull') after the capacitor has discharged.

Sense times can be 'long' as the capacitor has to discharges from Vdd to 0v7 (and the '1' lasts until the capacitor reaches 0v7). Further, a 'sharp cut off' can be achieved by adding a diode between button and sense pin (which also allows the 'charge resistor' value to be lower than otherwise).


Using a discharged capacitor to generate a '0' pulse
The sense pin is held Hi with a high value resistor (so no press = '1'). The capacitor is wired to Gnd with a 'highish' value resistor. The buttons are wired between the sense pin and the capacitor with various 'lowish' value resistors.

When a button is pressed, the sense pin is pulled Lo as the capacitor charges up via the button resistance. The Lo time reveals the resistance.

The button has to be released (and the capacitor discharged via it's own lo pull) before a second button can be detected

Sense times will typically be 'short' as the '0' time ends when the capacitor charge exceeds 0v7.



This note last modified: 25th Jun 2017 03:22.

[top]

(+) 001a One pin controls motor Fwd off Reverse

(+) 001c One pin controls 3 relays

(+) 0020 I2C bit banging

(+) 0021 I2C code

(+) 0021 Serial link - (9600 baud)

(+) 0028 RS422 RS485 drive with one PIC pin

(+) 0030 D to A conversion - (R2R DAC)

(+) 0031 Ternary DAC - (R3R)

(+) 0032 Hybrid ternary bit conversion - (code)

(+) 0035 Pulse Width Modulation - (PWM)

(+) 0040 Gearing axis sensor

(+) 005a TYC50 AC motor details

(+) 0061 16F54 2char month map - (VTI)

(+) 0062 DDmmmYYYY character map - (VTI)

(+) 1000 PIC16F684 tips and tricks

(+) 2000 18Fx tips and tricks

(+) 6500 18Fxx data Table output - (max rate)

(+) 6501 18Fxx Return with value LUT - (max rate)

(+) 6502 18Fxx extended instruction data output - (max rate)

(+) 6530 simple data transmission

(+) 6540 Using RS485

Next subject :- index

[top]