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)


Conversion from 7 bit binary to hybrid control (3 ternary + 2 binary)

The 3 hybrid ternary bits require 2 control bits each, i.e. 6 control bits for all 3.
The 'top' two hybrid bits are binary, so that's 2 more control bits = 8 bits in total (lucky, that :-) )

There are two basic ways of converting a 7 bit 'count' into the 'hybrid' control byte = by calculation or by LUT (Look-Up-Table)

A LUT will be faster (and the output control 'bit definitions' can be whatever you like) but, on the other hand, a calculation may deliver a solution in fewer total instructions, which is vital on the low end devices (eg. the 16F54 which only has 512 instruction space !)

Control LUT

A hybrid DAC built with 3 ternary + 2 binary control bits has 108 distinct states, so the LUT will have 108 entries (one for each control byte), plus a few extra 'steering' instructions.

The advantages of a LUT is that the 'conversion' doesn't have to be 'linear' = for example you could code a (1/4) sine-wave function (for AC waveform generation) or a "+/-" offset about a middle value.

If the hybrid DAC output is controlling a servo motor, and the +/- value is derived from a speed feedback circuit, you might want the LUT to code a (fast) 'speed up' ramp (from zero) and a 'gradual change' about the 'mid point' ... which would be 'offset' from the 'count 50' code.

For example, 'count 128' (0x80 = top bit set) could be defined as the 'mid point', with (say) +/- 32 counts either side (so 0x60 - 0xA0, 64 values) for 'mid speed control', leaving 108-64 = 40 counts for 'speed up from zero to (low) mid point'

The observant will note that the 'speed up' count runs from 0x00 - 0x5F, which is 96 counts.

Since we want a 'fast speed up' (and to avoid wasting too many extra LUT locations), counts of less than 0x60 would be 'divided by 4' (or even 8) before being used as a look-up address, so the 'extra' locations for speed-up will be reduced to 24 (or 12), resulting a 64+24 = 88 (76) entry table.


Defining the control byte

One problem with all DAC outputs is how to prevent 'glitches' when the 'drive' bit values are changed. This is especially difficult with a tri-state system (because the 'set tri-state (TRIS)' command and 'set PORT (output latch) bits' commands both require the control bits to be in the Accumulator = so there will be at least a 2 instruction delay between setting tri-state and setting bits.

To minimise 'glitches', the output bits could be changed 'one step at a time' in such a way that there are no 'big jumps' in the output value - although that means keeping a copy of the previous settings (as reading the PORT gets the pins 'state' (not the output latch settings), and reading TRIS gets 0's)

On the other hand, in many applications, a 'glitch' that is 'corrected' after 2 CLK's of a 1 MIPS CPU may not be noticeable


Of course things are a little simpler if only 4 (3 ternary + 1 binary) bits are being used with a 16F5x device (all the 16F5x devices have a 4 bit 'PORT A', which means the 'control' byte can can be formed of 4 TRIS bits + 4 PORT bits = grouping into 4 bits means the SWAPF command can be used to 'switch nibbles' between TRIS A and PORT A).







This note last modified: 17th Aug 2017 16:21.

[top]

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