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

(+) 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


The PIC16F684

This 14 pin device, available (eBay, China) for 50p, 'has it all' when it comes to 'sensors' = but the low pin count makes it rather difficult to 'use it all' :-)

The first 'trick' (which saves 2 pins) is to use the internal OSC. This is a max. of 8MHz, so CPU is 2 MIPS and OSC is 125nS. The ADC divider would need to be set to /16 giving a Tad of 2uS (which is faster than using the internal R/C Tad of 4uS) and thus a (10 bit) conversion time (11 Tad) of  22uS = 45kHz.

Using the DAC

Any of eight different i/o pins can be 'sensed' using the 10 bit DAC. If each switch in a keypad switches in a different (total) resistance, a single pin using the DAC could detect 2^10 = 1024 different keys :-)

The max. recommended i/p resistance of 10k (which we should be able to stretch a bit for 'static' switches) and accuracy of the resistors will limit the max. resistance 'chain', however it should be quite possible to detect which key of a 4x4 matrix 16 key keypad is pressed, something that would normally 'require' 8 i/o lines !

The same applies to 'configuration' switch bits. However here multiple switches could be open or closed so each switch must 'add' half the resistance of the previous. So long as all possible switch configurations lead to 'unique' total resistance value, the switch 'bit pattern' can be read (more or less directly) off the MSB's of the DAC !

Accuracy of the resistors in the chain limits the number of 'bits' that can be accurately sensed. A 1% 10k 'top' resistor means the 'bottom' resistor has to contribute a value that is greater than 1% * 10k = 100 ohms so 'halving' would have to stop after 7bits (10, 5, 2.5, 1.25, 625, 312, 156)

To improve on this, the contribution of each resistor in the 'ladder' would have to be 'measured' in advance and the actual values programmed (or stored e.g. using the EEPROM) in the PIC.


Using the comparators

If the DAC is unavailable, the PIC16F684 contains 2 comparators which in one mode (CM '010') can be used to compare the voltage on either one of 2 pairs of i/o pins against an internal (32 step) reference (CVref). By performing compares and switching the internal Vref, it is possible to perform a software controlled 'successive approximation' A-D.

Although changing the Vref can take up to 10uS, the comparators can be used to detect up to 32 different voltage levels and thus the state of multiple switches or keuys on a keypad.

The same trick can be used to sense the 'bit patturn' voltage set by a bank of up to 4 switches (assuming each switch is a factor 2 resistance different from the last) on a single pin.

Digital input as 'comparator'

To 'read' a '1', the voltage on an i/o pin has to cross the threshold. If that pin is connected to a R-C network, measuring the time taken to charge up the capacitor, from 'zero' to the 'threshold' voltage, means it's possible to infer the value of R.

So we wire a set of the keys/switches to Vcc via a 'chain resistor' network and a common capacitor. When a key is pressed, the capacitor starts to charge up. By measuring the 'time taken to cross the threshold' it's possible to work out which key is pressed (so long as only one key is pressed and the 'sense' time is 'fast' compared to the key press time) or which patturn of switches are closed.

The 'sense' pin is used to discharge the capacitor and hold it at 'Lo' thus ensuring a known 'starting' voltage. The timing is started when the 'sense' pin is switched back from output 'Lo' to input mode.

The 'chain resistor' network and capacitor values have to be chosen to ensure the 'charge up step time' is long enough to be detected (i.e. multiples of OSC) but short enough for 'human input' (100mS)

NB. don't forget that a series resistor will be needed to limit the discharge current at the end of a sense cycle (when the pin is enabled as output '0' again)


Trinary logic output

In 'digital' mode, the i/o pins actually have 3 states = Hi ('1'), Lo ('0) and 'off' (tristate). This can be used to output data in 'trinary' (as opposed to binary) logic = and the 'state count' multiplies up very quickly.

Pins in 'binary' mode define 2^n states, so 4 pins = 2^4 i.e. 16 states. In 'trinary' mode the equation is 3^n, so 4 pins = 3^4 i.e. 81 states, and for 16 staes you only need 3 pins (3^3 actually gives you 27 states, so you could use 2bits in 'trinary' mode (9) plus 1bit in binary)


Using a single pin as both input and output

The next trick is to use an i/o pin as both an input and an output, although an external diode (or pull-up) may be required to do so.


Double up the 'serial transmission' pins

Since 3 (or 4) pins will be needed to support 'serial transmission', finding ways to 'double use' these pins is a priority.

At the very least, it should be possible to use one of the pins as the 'activity LED' driver line



This note last modified: 31st Aug 2017 09:11.

[top]

(+) 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]