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 - a basic overview

PIC basics

PIC differences

There are 4 slightly different 8bit CPU's, starting with the 'base-line' 12bit instruction set (12F and 165x devices), the mid-range 14bit instruction set (, the enhanced mid-range (14bit) set and the 'high end' 18F device with a 16 bit instruction set. Then there are the 16bit CPU (24F and dsPIC) and finally the PIC32 32bit CPU devices. For more on this, see wikipedia PIC instruction sets

Normally, you would only expect to program the 8bit devices - especially the base-line 12bit set - in assembler - and the 18F devices (and above) in C.
 
The mid-tange and enhanced mid-range devices can be programmed in either.

The 12F range consists of the 8 pin devices with 4 or 6 i/o pins. The 16F devices just have a lot more i/o lines (at least 12). The 18F have a much more advanced CPU, more built-in circuits and cost a lot more :-)

The 12F and 16F PIC's have similar internal "extra's" = almost all have counter/timers, many have internal OSC circuits and analogue comparators, some have A-D converters and serial data support circuits (I2C etc).
 
Due to the relatively low program space count (a minimum of 512 and a maximum of only a few thousand) these devices would almost always be programmed in 'assembler'.
 
However all the PIC 12F and 16F5x series PIC's have the same 'base-line' CPU 12bit instruction set, which makes it easy to 'migrate' code from one device to another (although the 'mnemonics' (instruction 'names') are rather hard to remember as they make so little sense)
 
Other 16F devices have the mid-range CPU with an enhanced 14bit instruction set.

The 18F devices have a lot more program space and would normally be programmed in C.

Microchip make their basic assembler / C compiler software available for free. This gives them a considerable advantage in the hobbyist market
 
Their assembler has good macro support, so it's quite possible to replace the Microchip 'mnemonics' with something rather more logical.

Using the older low-end PIC16Fxx (PIC16F54, PIC16F57, PIC16F59) series

Whilst these chips may be still available today (2016), they are essentially obsolete and should not really be considered for new projects. I have stock left over from the 'good old days' - which is why I continue to use them - however new users should go straight to the 16F16xx 'enhanced' series or (better) the 18F series and skip the 16F5x series completely.

In case the 16F5x becomes 'too old to support', you can download the full data sheet (pdf) from my page here

The PIC16F54 (in an 18 pin PDIP package) really is about the most basic usable 'entry level' compute device you can buy. For 44p each (Qty 10 off price, CPC) you get a 5v 'TTL' device capable of running from 2v to 5.5v and from 4MHz (<3v) up to the official maximum of 20MHz (at 4.5v+). Unofficially they can be overclocked up to 32MHz at 5.5v (and will act like a fuse above 6v), and all this for less than the cost of most single function 74xx 'support' IC's !

If you need more i/o pins, instruction space or registers (RAM), the 16F57 (28pin, 20 i/o, 2k instructions, 72 bytes of RAM) comes in at 69p and the 16FG59 (40pin, 32 i/o, 2k instructions, 134 bytes RAM) at 84p, however you should note that the more 'advanced' 16F16xx starts to come in at these prices (eg the 16F1615 is only 86p (10off, Farnell)) !

Note that in many cases it will be cheaper to use a second PIC than to purchase a 'dedicated' counter, shift register, UART, I2C comms etc. chip

The 16F54's 18 pins consist of :-

Power (Vdd) & GND, the 2x OSC pins (supporting various clock generation methods), the Reset pin (MCLR), the 'clock' (T0CLKI) for the single** 8bit counter (TMR0) and 12 'general purpose' i/o pins (one 8 bit set, one 4 bit set)
**There are actually two embedded counters - the second is the Watch Dog Timer (WDT). The WDT runs from it's own free-running internal clock, and, if enabled, will 'time-out' (if not 'restarted' every 18mS, or at some multiple of 2x 18mS if using the 'pre-scaler') causing the chip to 'Reset'. Note that a Power-On-Reset (detected by monitoring the Vdd supply voltage) clears 'everything' (and imposes a power-on delay of approx 18mS (1 WDT time)), whilst a 'normal' WDT Reset or MCLR (pin) Reset does not.

The 'pre-scaler' is a binary clock divider with 8 modes (1/2 to 1/256) which can only be used with either the WDT or TMR0. TMR0 is clocked from the 'instruction' clock (OSC/4) or, optionally, from an external dedicated pin (T0CLKI)

One thing the 16F5x lacks is any sort of 'interrupt' facility (other than WDT/Reset) - something that almost every other PIC has. However it does have a 2 level 'subroutine call/return ' stack

Power on, WDT time-out and /MCLR (pulling the 'Reset' input pin Lo) all 'set' the program counter (PC) to "all 1's" and the CPU executes the instruction at the top of the program Flash memory (address 0x01FF in the '54). If this is a 'NOP', the PC 'rolls over' to address 0x0000 and continues execution from that point. Two 'status' bits can be used to determine the 'cause' of the Reset (WDT or MCLR/WDT after Sleep) so (with clever programming) a 'single level' Interrupt is possible (although the state of most registers is lost)

The 16F54 8bit CPU supports 33** instructions (each 12bits wide) held in a 512 instruction word Flash memory. It has 25 general purpose (byte) registers (plus 7 special function registers). The 8bit ALU performs "2's complement" addition and subtraction on single byte values. The CPU takes 4 OSC cycles to execute 1 instruction, so, when driven at it's official maximum 20MHz, this 44p chip is running at 5 MIPS ! (it achieves 8MIPS at 32MHz overclock)

** The '33' instructions include both 'compute' instructions and 'control' instructions, which makes things a bit confusing. At least one instruction is 'redundant' (the 'Clear Accumulator' instruction does exactly the same as 'AND Accumulator with 0x00', even to the detail of only setting the Z status bit). All instructions execute in 1 CPU CLK, except the 7 'jump' type instructions which take 2 CPU CLK's.
 
Confusion is caused by the .PDF data-sheet 'PIC-speak', the misleading usage of a 'general' name for a specific function.
 
So, for example, 'PIC-speak' typically refers to function control Latches, the Accumulator and the Indirect Addressing Pointer as 'registers' whilst the RAM locations are called 'register file' addresses.
 
The fact that the control Latches are mapped into the Register address space then adds to the overall confusion (the Indirect Addressing pointer ('File Select Register' or FSR) is 'register 4' (and to invoke it, you address regsiter 0). The (bottom 8 bits of the) Program Counter is 'register 2', the i/o pin Data Latches (Port A and B) are 'register 5' and 'register 6'). The Timer/counter is register 1 and the ALU Status bits can be found in register 3.
 
Of course not everything is 'mapped' - the Accumulator (which in PIC-speak is the 'W register') has no register address and other notable exceptions are the subroutine return address 'stack' and the i/o ports direction control latch ('tri-state latch' or TRIS in PIC-speak)

One thing to note is that CALLing a subroutine clears the 9th bit (for the 16F54, that's the top bit) of the program counter !

In other words, all subroutines must start within the first 256 memory locations (otherwise they are not 'reachable' via a CALL). The return stack holds all bits of the 'from' address (so you can CALL from a higher address). The direct GOTO instruction supports 9 addresses bits, so subroutines above the 256 address 'boundary' can be reached via an 'indirect' call (i.e. you CALL to a location within the first 265 that then performs a GOTO the actual subroutine above 256)

Note that the 16F57 and 16F59 uses 'paging' to address locations above 512 and the '256 address Subroutine boundary' then applies to each page

Each instruction executes in 4 OSC cycles. A simple R/C circuit connected to one OSC pin allows operation up to 4MHz, in which case OSC/4 (the instruction cycle clock) is output on the other OSC pin. Faster speeds (up to 20MHz) require the use of a 'resonator' or crystal oscillator (and uses both pins - OSC2 is the 'driver' and can be used to 'pick off' the OSC clock for an external circuit).

The max. frequency achieved depends on the supply voltage (Vdd). At 3.3v Vdd, OSC is limited to 4MHz. At 4.5v it's 10MHz, and only above 4.5v will it achieve 20MHz. These speeds are all with a Crystal - if you use the R-C circuit (to keep costs down) the max speed (at 5v) is 4MHz (1 MIPS)

The data sheet specifies 20MHz all the way up to the standard operating max. Vdd 5.5v, however users have reported overclocking to 32MHz at 5.5v and above (Vdd must be kept under 6.5v, the device data sheet 'absolute max rating' = and the data-sheet also warns that "exposure to max rating for extended periods may effect device reliability")

The wide voltage range makes these devices well suited to battery operation (4x '1.2v' NiMH rechargeable will give at least 4.8v (or up to 5.6v when fully charged). Power dissipation is OSC and Vdd dependent. Max Vdd, max OSC means max. Pdisp so perhaps not the best approach for battery operation :-)

The 16F684 is the 'top end' of the 'bottom' range PIC's

This fantastic little PIC's comes in a 14 pin DIL/SOIC package with 2k program instruction space and 128bytes RAM (and 256 EEPROM). However, it's key advantage is it's 10 bit ADC and the price = 48p each

The 10 off / SOIC-14 package price is £4.74, eBay (Post free from China) as of Nov 2016, so 48p each. Adding another 10p (or less) for the SOIC PCB (which gets us wiring holes) takes this to 69.5p. The DIL version best price I could find is 78.5p (10off price £7.69, eBay China).
 
Only the simpler (2k program, 128 RAM, 8bit DAC, no anlaogue but with serial SPI/I2C slave capability) 28pin PIC16F72 (in TSOP) can beat this price (10pcs can be had for £2.88).
 
The best price I could find for a DIP package PIC was for the 16F630 (5pcs for £2) a basic DIP14 device with no DAC (but 1k program, 64 RAM and 128 eeprom)
 
NB. DO NOT be tempted to buy from the 'UK' sellers who simply buy in lots of 10pcs from China and then resell them one or two at a time to mugs for 3 to 5 times the price

The 16F684 requires minimal use of external components, so a single SOIC 20/24/28 'adapter' PCB may be all you need to mount everything (I purchase SOIC-28 adapters in lots of 20pcs for £2.19 (so about 10p each) from 'ynaan' on eBay = these typically arrive in less than a week via airmail)

The 16F684 offers the choice of an internal 8MHz OSC divided by /1 to /64 (so you can get a max. of 2 MIPS without using any external components at all). The 31kHz OSC (used for the WD counter) can also be used (for ultra low power consumption.
 
An external R-C OSC capability exists that requires the use of a single pin. The formula for OSC period is Time = R(k) * C(pf) ln [(Vdd/(Vdd-Vih)] (where Vih = OSC pin input Vhi = 0.9*Vdd). Using max. recommended values of R=100k and C=22pf at Vdd=5v gets OSC=20MHz. A crystal controlled OSC requires the use of 2 pins.
 
The maximum OSC is specified as 20MHz (Vdd 4.5v and higher), however, as with other PICs, at 5.5v you can expect to overclock to at least 24MHz. From 3v ro 4.5v the max. OSC is 10MHz (and below 3v the max. is 4MHz, all the way down to Vdd min. which is 2v !!).
 
As well as a 10bit DAC, this PIC includes 2 analogue comparators, 3 timers, a 10bit PWM and an 8 level circular stack - but has no serial communications circuits (so you will need to read all about 'bit banging' :-) )
 
The 16F684 i/o pins (organised as 2 PORTs, A and C, each of 6 bits), when used as inputs, can even have internal 'weak pull ups' (which can further save on the sue of external components).
 
Note that the RAM is organised into 2 Banks of 128. In addition to various control latches ('special purpose registers'), the first Bank addresses 96 'general purpose' registers, the second 32. Normal instructions use 7 bit addressing + a 'Bank select' bit (from the Status Register), however the Indirect Address pointer is 8 bits and overrides (but does not 'overwrite') the Bank Select bit.

It's worth noting that i/o pins with an 'analogue' capability will power-on with the 'digital' control circuits (including the input biffer) 'disabled'. So trying to 'read' the pin (without setting 'digital' mode) will always get you '0' :-)

(+) I2C bit banging

Using a typical mid-range PIC (PIC18F2455, 2550, 4455, 4550)

The PIC18F2455 is an 8bit device with 24kb of flash program space (i.e. 12k x 16bit instructions), 2kb of RAM and 256 bytes of EEPROM. The most significant feature, however, is the built in USB 2.0 interface (the other chips in the range, 18F2550, 4455 & 4550 offer differing pin counts and flash program space). These devices are quite complex and whilst it is still possible (just) to program them in assembler, it is simpler to program them in the high level 'C' language.

The PIC programming tools allow assembler code to be combined with C, so you can always code key parts of your program in assembler.

To support USB 2.0, these devices must run at 48MHz internally (they have an internal 4x PLL so you only need a 12MHz external OSC circuit). The CPU clk is OSC(internal)/4, giving a max CPU speed of 12 MIPS

Overclocking is also possible on these devices, however 'your mileage will vary' from 50MHz-60MHz

The 'top of the 16Fxxx' range (the 'K' devices) have internal OSC. circuits (and a 4x PLL) that can run the CPU at 64MHz, so the CPU, at OSC/4, is delivering 16 MIPS (most of these devices have two OSC circuits (or a dual-use main OSC) that supports a 32,768Hz (i.e. supports a RTC - Real Time Clock - function)

Note, however, that these devices typically cost more than £5 each = so you should ONLY consider using them in applications where it's totally impossible to use the Pi Zero

Using the high-end PIC (PIC24FJ64 and above)

The PIC24FJ64 contains a 16bit CPU, 64k of flash and 8k of RAM. Whilst it would be possible to program these devices in 'assembler' the complexity of the tasks you are likely to use it for makes using the 'C' programming language almost mandatory. The 24F series CPU is clocked at OSC/2 (so a '32MHz' 24F has the same CPU clock as a 64MHz 18F part). Note that the high end devices are 3v3 (although some have i/o pins that are 5v 'tolerant')

The free version of the Microchip C compiler is 'non-optimised', however it does include a link to 60 day 'trial' license of the Pro (optimised) function (after which it reverts to non-optimised).
 
Users report little difference between 'optimised' and 'non-optimised' code.

Whilst they can be be had for as little as £3 each (PIC24FJ644, 5off price, eBay China), these devices have been effectively wiped out by the £4 Pi Zero.

Any project that needs the 'power' of a 'high end' PIC (such as the PIC24FJ64) can be done 10x simpler and faster using the Pi Zero (and at a considerably lower total cost)

Next page :- PIC16F5x tips and tricks

[top]