Embedded Systems/PIC Microcontroller
| A Wikibookian has nominated this page for cleanup because:
Needs better formatting, etc
| A Wikibookian has nominated this page for cleanup because:
Needs better formatting, etc
Manufactured by Microchip, the ("Peripheral Interface Controller" ) microcontroller is popular among engineers and hobbyists alike. PIC microcontrollers come in a variety of "flavors", each with different components and capabilities.
Many types of electronic projects can be constructed easily with the PIC family of microprocessors, among them clocks, very simple video games, robots, servo controllers, and many more. The PIC is a very general purpose microcontroller that can come with many different options, for very reasonable prices.
General Instruments produced a chip called the PIC1650, described as a Programmable Intelligent Computer. This chip is the mother of all PIC chips
, functionally close to the current 16C54. It was intended as a peripheral for their CP1600 microprocessor. Maybe that is why most people think PIC stands for Peripheral Interface Controller. Microchip has never used PIC as an abbreviation, just as PIC. And recently Microchip has started calling its PICs microcontrollers PICmicro MCU's.
Which PIC to UseEdit
How do you find a PIC that is right for you out of nearly 2000 different models of PIC microcontrollers?
The Microchip website has an excellent Product Selector Tool. You simply enter your minimum requirements and optionally desired requirements, and the resulting part numbers are displayed with the basic features listed.
You can buy your PIC processors directly from Microchip Direct, Microchip's online store. Pricing is the same or sometimes better than many distributors.
Rule Number 1: only pick a microprocessor you can actually obtain. PICs are all similar, and therefore you don't need to be too picky about which model to use.
If there is only 1 kind of PIC available in your school storeroom, use it. If you order from a company such as Newark or DigiKey , ignore any part that is "out of stock"—only order parts that are "in stock". This will save you lots of time in creating your project.
Recommended "first PIC"Edit
At one time, the PIC16F84 was far and away the best PIC for hobbyists. But Microchip, Parallax, and Holtek are now manufacturing many chips that are even better and often even cheaper, because of the higher level of production.
- I'd like a list of the top 4 or so PIC recommendations, and *why* they were recommended, so that when better/cheaper chips become available, it's easy to confirm and add them to the list.
PIC: Select a chip and buy one.
Many people recommend the following PICs as a good choice for the "first PIC" for a hobbyist, take in count the revision numbers (like the A in 16F628A):
- PIC18F4620: it has 13 analog inputs—Wouter van Ooijen recommends that hobbyists use the largest and most capable chip available , and this is it (as of 2006-01). ~$9
PIC16F877A -- the largest chip of the 16F87x family; has 8 analog inputs—recommended by Wouter (#2); AmQRP; PICList. ~$8
- PIC16F877A , this is probably the most popular PIC used by the hobbyist community that is still under production. This is the best PIC of its family and used to be "the PIC" for bigger hobbyist projects, along with the PIC16F84 for smaller ones. Features 14KB of program memory, 368 bytes of RAM, a 40 pin package, 2 CPP modules, 8 ADC channels capable of 10-bit each. It also counts with the UART and MSSP, which is a SSP capable of being master, controlling any devices connected to the I2c and SPI busses. The lack of internal oscillator, as opposed to the other PICs mentioned until now, is something to be aware of. Also, this PIC is relatively expensive for the features included. This may be caused by Microchip to force the migration to better chips. --recommended by Ivaneduardo747; Wouter (#2); AmQRP --. ~$9
PIC16F88 -- has 7 analog inputs—recommended by AmQRP; SparkFun. ~$5
- PIC16F88, this is enhanced version of the PIC16F628A. It has all the features of the 16F628, plus twice the program memory, 7KB; seven 10-bit ADCs, a SSP (Synchronous Serial Port), capable of receiving messages sent over I2C and SPI busses. It also supports self-programming, a feature used by some development boards to avoid the need of using a programmer, saving the cost of buying a programmer. --recommended by Ivaneduardo747; AmQRP -- SparkFun. ~$5
PIC16F628 -- Cheaper than the PIC16F84A, with a built-in 4 MHz clock and a UART, but lacks any analog inputs—recommended by Wouter (#3); AmQRP -- ~$4
- PIC16F628A, this is a good starter PIC because of its compatibility with what used to be one of the hobbyist's favorite PICs: the PIC16F84. This way, the beginner can select from a vast catalog of projects and programs, specially when created in low level languages like the PIC Assembler. It features a 18 pin package, 3.5KB of Flash Memory, can execute up to 5 million instructions per second (MIPS) using a 20MHZ crystal. The lack of an Analog-Digital Converter (ADC) is something to point out. As opposed to the PIC16F84A it has an UART, which is capable of generating and receiving RS-232 signals, which is very useful for debugging. Some people use to find ironic that this chip is cheaper than the less-featured PIC16F84A. -- recommended by Ivaneduardo747; Wouter (#3) AmQRP -- ~$5
- PIC16F1936, a powerful mid-range PIC, comes with an 11 channel, 10-bit ADC; two indirect pointer registers; XLP (extreme low power) for low power consumption on battery powered devices. -- recommended by some people on the PIClist as a faster, better, cheaper replacement for the 16F877. -- ~$3
- PIC12F683, a small 8-pin microcontroller. It is a good microcontroller for small applications due to its small size and relatively high power and diverse features, like 4 ADC channels and internal 4MHZ oscillator. --recommended by Ivaneduardo747; . ~$2.50
Of the many new parts Microchip has introduced since 2003, are any of them significantly better for hobbyists in some way than these chips ? Todo: Does "Starting out PIC Programming: What would be a good PIC chip to start out with?" have any useful recommendations to add to the above?
There are several different "families":
More selection tipsEdit
- The "F" Suffix implies that the chip has reprogrammable Flash memory.
PIC10F—in super-tiny 6 pin packages PIC12F—in tiny 8-pin packages PIC14F PIC16F—in tiny 14-pin packages PIC18F PIC24F PIC24E PIC24H dsPIC30F dsPIC33F dsPIC33E
- The "C" suffix implies that the chip uses EPROM memory. A few of these chips used to be erased with a very expensive Ultra-Violet eraser. This method was primarily used by companies. But most of these chips are specifically made so that once you write it you can't change it: it's OTP (one-time programmable). People used to check their programs minutely before programming them into such chips. Recently, this chips are becoming less used as the cost of Flash memory decreases, but some of them are still used because of their reliability or reduced costs.
PIC12C PIC16C PIC17C PIC18C
Each family has one "full" member with all the goodies and a subset of variant members that lack one thing or another. For example, on the 16F84 family, the 16F84 was the fully featured PIC, with Flash memory and twice the program space of the 16F83. The family was also composed by the 16C84 and 16C83, one of the few reprogrammable C suffix PICs. For prototyping, we generally use the "full" version to make sure we can get the prototype working at all. During prototyping we want to tweak code, reprogram, and test, over and over until it works. So we use one of the above "Flash" families, not the "OTP" families, unless required. For short production, the C parts are recommended. For very long production lines some PICs with mask-programmed ROMs where used. Now in-factory preprogramming is available from Microchip.
Each member of each family generally comes in several different packages. Hobbyists generally use the plastic dual inline package (often called DIP or PDIP) because it's the easiest to stick in a solderless breadboard and tinker with. (The "wide-DIP" works just as well). They avoid using ceramic dual inline package (CDIP), not because ceramic is bad (it's just as easy to plug into a solderless breadboard), but because the plastic parts work just as well and are much cheaper.
(Later, for mass production, we may figure out which is the cheapest cut-down version that just barely has enough goodies to work, and comes in the cheapest package that has just barely enough pins for this particular application
... perhaps even a OTP chip).
And then each different package, for each member of each family, comes in both a "commercial temperature range" and a "industrial temperature range".
The PIC 16 family is considered to be a good, general purpose family of PICs. PIC 16s generally have 3 output ports to work with. Here are some models in this family that were once common:
- PIC 16C54 - The original PIC model, the 'C54 is available in an 18 pin DIP, with 12 I/O pins.
- PIC 16C55 - available in a 28-pin DIP package, with 20 available I/O pins
- PIC 16C56 - Same form-factor as the 'C54, but more features
- PIC 16C57 - same form-factor as the 'C55, but more features
- PIC 16C71 - has 4 available ADC, which are mapped to the same pins as Port A (dual-use pins).
- PIC 16C84 - has the ability to erase and reprogram in-circuit EEPROMs
Many programs written for the PIC16x family are available for free on the Internet.
Flash-based chips such as the PIC16F88 are far more convenient to develop on, and can run code written for the above chips with little or no changes.
The PIC12x series is the smallest series with 8 pins and up to 6 available I/O pins. These are used when space and/or cost is a factor.
The PIC 18x series are available in a 28 and 40-pin DIP package. They have more ports, more ADC, etc... PIC 18s are generally considered to be very high-end microcontrollers, and are even sometimes called full-fledged CPUs.
Microchip is currently (as of 2007) producing 6 Flash microcontrollers with a USB interface. All are in the PIC18Fx family. (The 28 pin PIC18F2450, PIC18F2455, PIC18F2550; and the 40/44 pin PIC18F4450, PIC18F4455, PIC18F4550 ).
The PIC StackEdit
The PIC stack is a dedicated bank of registers (separate from programmer-accessible registers) that can only be used to store return addresses during a function call (or interrupt).
- 12 bit: A PIC microcontroller with a 12 bit core (the first generation of PIC microcontrollers) ( including most PIC10, some PIC12, a few PIC16 ) only has 2 registers in its hardware stack. Subroutines in a 12-bit PIC program may only be nested 2 deep, before the stack overflows, and data is lost. People who program 12 bit PICs spend a lot of effort working around this limitation. (These people are forced to rely heavily on techniques that avoid using the hardware stack. For example, macros, state machines, and software stacks).
- 14 bit: A PIC microcontroller with a 14 bit core (most PIC16) has 8 registers in the hardware stack. This makes function calls much easier to use, even though people who program them should be aware of some remaining gotchas .
- 16 bit: A PIC microcontroller with a 16 bit core (all PIC18) has a "31-level deep" hardware stack depth. This is more than deep enough for most programs people write.
Many algorithms involving pushing data to, then later pulling data from, some sort of stack. People who program such algorithms on the PIC must use a separate software stack for data (reminiscent of Forth). (People who use other microprocessors often share a single stack for both subroutine return addresses and this "stack data").
Call-tree analysis can be used to find the deepest possible subroutine nesting used by a program. (Unless the program uses w:recursion). As long as the deepest possible nesting of the "main" program, plus the deepest possible nesting of the interrupt routines, give a total sum less than the size of the stack of the microcontroller it runs on, then everything works fine. Some compilers automatically do such call-tree analysis, and if the hardware stack is insufficient, the compiler automatically switches over to using a "software stack". Assembly-language programmers are forced to do such analysis by hand.
What else do you needEdit
Versions of BASIC, C, Forth, and a few other programming languages are available for PICmicros. See Embedded Systems/PIC Programming.
You need a device called a "downloader" to transfer compiled programs from your PC and burn them into the microcontroller. (Unfortunately "programming" has 2 meanings—see Embedded Systems/Terminology#programming).)
There are 2 styles of downloaders. If you have your PIC in your system and you want to change the software,
- with a "IC programmer" style device, you must pull out the PIC, plug it into the "IC programmer", reprogram, then put the PIC back in your system.
- with a "in circuit programmer" style device (ICSP), you don't touch the PIC itself—you plug a cable from the programmer directly into a header that you have (hopefully) placed next to the PIC, reprogram, then unplug the cable.
An (incomplete) list of programmers includes:
- BobProg - Simple ICSP programmer with external power supply 
- JDM Programmer modified for LVP micrcontrollers 
- In Circuit Programmer for PIC16F84 PIC16F84 Programmer
- IC Programmer ICProg Programs : 12Cxx, 16Cxxx, 16Fxx, 16F87x, 18Fxxx, 16F7x, 24Cxx, 93Cxx, 90Sxxx, 59Cxx, 89Cx051, 89S53, 250x0, PIC, AVR , 80C51 etc.
- Many other programmers are listed at MassMind.
Many people prefer to use a "bootloader" for programming whenever possible. Bootloaders are covered in detail in chapter Bootloaders and Bootsectors .
The most important part of any electronic circuit is the power supply. The PIC programmer requires a +5 volt and a +13 volt regulated power supply. The need for two power supplies is due to the different programming algorithms:
- High Power Programming Mode - In this mode, we enter the programming mode of the PIC by driving the RB7(Data) and RB6(CLOCK) pins of the PIC low while driving the MCLR pin from 0 to VCC(+13v).
- Low Power Programming Mode - This alogrithm requires only +5v for the programming operation. In this algorithm, we drive RB3(PGM) from VDD to GND to enter the programming mode and then set MCLR to VDD(+5v).
The PIC microcontrollers all have built-in RC oscillator circuits available, although they are slow, and have high granularity. External oscillator circuits may be applied as well, up to a maximum frequency of 20 MHz. PIC instructions require 4 clock cycles for each machine instruction cycle, and therefore can run at a maximum effective rate of 5 MHz. However, certain PICs have a PLL (phase locked loop) multiplier built in. The user can enable the Times 4 multiplier, thus yielding a virtual oscillator frequency of 4 X External Oscillator. For example, with a maximum allowable oscillator of 16 MHz, the virtual oscillator runs at 64 MHz. Thus, the PIC will perform 64 / 4 = 16 MIPS (million instructions per second). Certain pics also have built-in oscillators, usually 4Mhz for precisely 1MIPS, or a low-power imprecise 48 kHz. This frees up to two I/O pins for other purposes. The pins can also be used to produce a frequency if you want to synchronize other hardware to the same clock as one PIC's internal one.
Continue with Embedded Systems/PIC Programming.
There is a lot of information about using PIC microcontrollers (and electronics design in general) in the PICList archives. If you are really stumped, you might consider subscribing to the PICList, asking your question ... and answering someone else's question in return. The PICList archives are hosted at MassMind
- A Guide To PIC Microcontroller Documentation goes into more detail.
- RC Airplane/RCAP discusses a project that uses a PIC16F876A.
- the Parallax SX FAQ by Guenther Daubach
- Microchip PIC: the original manufacturer's web site
- Getting Starting with PICmicro controllers by Wouter van Ooijen
- "The PIC 16F628A: Why the PIC 16F84 is now obsolete."
- "The PIC 16F88: Why the PIC 16F84 is now Really obsolete."
- "Free PIC resources and projects with descriptions, schematics and source code."
- "Programming PICmicros in the C programming language"
- "Programming PICmicros in other programming languages: Forth, JAL, BASIC, Python, etc."
- The "8-bit PIC® Microcontroller Solutions brochure" describes how big the PIC hardware stack is in each PIC microcontroller family, and other major differences between families.