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

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


Comms must be based on a full 'request / acknowledge' handshake approach because the Pi end will be using s/w to 'fetch' the bits and that means variable bit-timing.

One good thing about Req/Ack is that the number of Data i/o lines (width) can be whatever we like, although (rto keep i/o pin count down) we normally use a sigle bit for serial transmission. As with all serial Tx, LSB is sent first.

Note that the PIC /ack and Data drive i/o pins MUST be 'open collector' with EXTERNAL pull-up resistors to 3v3 (the Pi GPIO uses 3v3 and the typical PIC 5v).

loop:
PIC                  Pi
wait for /req lo     idle (req hi)
                     pull /req lo, wait for /ack lo
set data bit         
pull /ack lo
                     sample data bit
wait for req hi
                     release req hi, wait for ack hi
release data,
release ack hi
loop                     
                     loop


Above 'works' so long as the Pi 'knows in advance' what data length to expect (eg. 10 bits DAC o/p from PIC) and so long as neither end gets 'out of step'.

'Synchronisation' can be supported by using the Data line 'outside the handshake' as a 'ready to send first bit' flag. This means the PIC sets data lo when it's ready with bit 0, and the Pi checks the data bit each time after finding ack Hi (and before pulling /req lo). If the Pi gets 'out of step' then the data bit will be found unexpectedly Lo.

idle:
PIC                  Pi
                     idle (req hi)
                     wait for PIC power-on (Data Lo)
if ready with bit0,
pull Data Lo,
else leave Data Hi
                     Data Lo found, start the fetch count

loop:
wait for /req lo     
                     pull /req lo, wait for /ack lo
set data bit         
pull /ack lo
wait for req hi
                     sample data bit
                     release req hi, wait for ack hi
if end of Data
set Data Lo,
else set Data Hi
release ack hi
loop                 sample Data =
                      if Hi, continue fetch count, loop
					  if Lo, end count (error if incomplete), idle

The above should be 'fool proof'. If the PIC is powered off, the 3v3 pull-up resistor on Data will ensure the Pi sees it as hi. If the Pi finds Data unexpectedly Lo, it can drop the bits recieved so far and start the count again.

Uisng additional i/o pins as 'select' bits it's even possible for the Pi to 'talk' to multiple PIC's using the same 3 wires (one additional i/o will be needed per PIC).

If 'select' is used, the PIC will need to assign 4 pins, however even the 14pin 16F684 should have sufficient pins (especially if the internal OSC is used).


This note last modified: 18th Nov 2016 08:35.

[top]

(+) 6540 Using RS485

Next subject :- index

[top]