Starting with PICmicro controllers
intro, first steps, tips, links, etc.
(C) 2002 .. 2005 Wouter van Ooijen ([email protected])
Last change made at 2005 AUG 18. The latest version of this document can be found at http://www.voti.nl/swp.
Unaltered duplication is allowed. Translations require approval of the author, which will most likely be granted if the quality of the translation is adequate.
This is not a fool’s guide: fools are much more fool than I am clever, and fools should not program PICs (nor anything else) anyway.
I don’t accept any responsibility for errors in this text or the consequences thereof, but I appreciate constructive comments.
This document is still under construction: you will find some remarks in square brackets  where I plan to write additional text, if I ever find the time. Don’t hestitate to write text yourself if you think something is missing!
What’s in a name
Are you sure?
Are you really sure?
Selecting a PIC – what is available (table)
Select a language
Select a programmer
Buy a PIC
Your first target circuit
The PIC architecture
Your first program
16×84 to 16F628
Tips and pitfalls
Links and references
So you think you want to start using PIC microcontrollers? Read on! This text will offer advice on how to proceed.
Writing this text I had to make some assumptions about you, otherwise the number of options would be even larger than it is now. So if you do not match the description below, some information might be irrelevant to you, and some recommendations might not be appropriate. Be your own judge.
This text is intended for someone who
|wants to start using PIC microcontrollers but has no prior experience in microcontrollers|
|has more time than money, but not excessively so (typical situation for a hobbyist or small-scale professional)|
|is more interested in experimenting and building prototypes than in assembling large series|
|has (at least) basic knowledge of both electronics and programming|
Note that I do not assume a master’s degree in electronics or programming. But on the electronics side understanding the basic working of a resistor, capacitor, diode, LED, transistor etc., and being able to apply at least Ohm’s law are mandatory. On the programming site you should have some programming experience, so terms like variable, assignment, if, while, call, goto and return ring a bell.
Starting with PICs requires you to make a lot of choices. This document tries to help you doing so, sometimes by making a strong suggestion, but always – I hope – by providing you the information you need to make the right decision for your particular situation.
If all you want is a list of pre-cooked advices don’t read on, just ask on comp.arch.embedded or www.piclist.com or any other forum and you will get all possible combinations of advices, just pic the one you prefer 😉
What’s in a name
A long, long time ago (when computer chips died when a cat came near on a dry day) General Instruments produced a chip called the PIC1650, described as a Programmable Intelligent Computer. (Check Richard Ottosen’s Gizmo page, it has links to the original PIC1650 documents.) This chip is the mother of all PIC chips, functionally close to the current 16C54/16F54. It was intended as a peripheral for their CP1600 microprocessor. Maybe that is why most people think PIC stands for Peripheral Interface Controller. As far as I know Microchip has never used PIC as an abbreviation, just as PIC. And recently Microchip has started calling its PICs “PICmicro microcontrollers”. Maybe they heard that PIC sounds like the Dutch word for dick and wanted to spare me the frowns from Dutch readers.
Are you sure?
But first let’s take two steps back. You think you want to start using microcontrollers? I hope you realize what you are up to. It is surprisingly easy to use a microcontroller to perform some nice tricks like flashing a LED, or even controlling a simple robot. But these simple tricks will whet your appetite for more complex applications, and then the big struggle will start. On the hardware side microcontrollers are powerful and complex chips, so you are in to some serious reading, especially of the data sheet of the chip you will be using. I know the 390 pages of an 18F4520 data sheet look intimidating, but you will have to bite that bullet some time. On the software side things can get quite complex too, especially if you want to do more than one thing at the same time. Programming is difficult, and microcontroller programming (beyond the blink-a-LED level) even more.
When all you ever want to do is blink a few LEDs, switch some relays etc. you might be better off with an environment that hides the ugly hardware details (and most of the power!) from you, like a BASIC Stamp, essentially a PIC or SX chip with a BASIC interpreter. If so, get a Stamp and put this document aside until you are in for more.
Are you really sure?
When you have decided to use a microcontroller the next step is to decide which one. This text is about PICs, but you should at least be aware that other options exist, like the Freescale (used to be Motorola) 68HC, Atmel AVR, and the 8051’s and ARM’s in all their varieties from various manufacturers. There are firm proponents of each of these microcontroller families (and many others), and probably for good reasons. Asking ‘what chip should I choose’ on an appropriate internet newsgroup will give you an idea of how religious this issue is to some people.
Criteria that you can take into account when choosing a microcontroller (family) are:
|availability (both today and in the future)|
|ease of use (as a start consider only controllers with flash memory that can be (re)programmed in-circuit)|
|quality and price of development tools|
|support from friends, neighbors, clubs, newsgroups, mailing lists|
|availability of application notes, reference designs, hobbyist web pages|
|features of the chip (IO pins, UART, A/D, D/A, counters, speed, code size, data size, etc.)|
|ease of migrating to smaller (cheaper) or larger (more capable) chips|
Personally I started with PICs and have not looked into the other ones much. Motorola’s 68HC family is often used for somewhat more complex tasks than PICs, but tends to be more difficult to buy. The smaller AVR’s are much like the PICs in price and performance. (Hence PIC-or-AVR debates tend to be very hot.) There are so many 8051 clones around that it is difficult to say something in general about this family. The ARM is a much more powerfull CPU than the averave PIC, AVR or 8051 (but the newer dsPICs come close), but it is not so widely used and there are not DIP versions.
If you want to hear reasons why you should choose PICs I can give you a few:
|availability! As far as I know every PIC type ever sold by Microchip is still available today (contrast this to for instance the Atmel’s AVR line). Like all chip manufacturers Microchip is sometimes unable to supply the wildly varying demand, but their reputation on this aspect is very good (Motorola – now Freescale – is an example of a supplier that has a very bad reputation on this point). PICs are also available from many shops – check any electronics shop. If they sell microcontrollers at all they will most probably sell PICs – and maybe some other brands too.|
|widely used! PICs are used by a lot of people, lots of books are written about PICs, there are many many web pages, newsgroups and mailing lists about PICs.|
|extensive product line!|
If you want to hear reasons why you should not choose PICs I can give you a few too:
|mediocre architecture! The architecture of the 12/14 bit core PIC CPU is arcane and bizzare. When you are doing assembly language programmaing, or when you want to write a compiler, this can be a serious disadvantage. The 16 bit core architecture (18F’s) is much better. I don’t know the 24 bit dsPIC architecture very well, but it is said to be almost normal.|
|no GCC (or other free C compiler) is available!|
OK, so you have not been scared away and are firmly decided to start using PICs. May you live in interesting times, and never say I did not warn you!
Selecting a PIC – what is available?
So which PIC should you choose to start with? Some 10 years ago this question was easy to answer: the 16F84 (or, before that chip was available, the now discontinued 16c84). These were the only affordable flash PICs and hence THE hobbyist PICs. (EPROM based chips are either program-once or require the use of an UV-eraser.) You will still find lots of designs in electronics magazines and on the internet using these chips. The 16F84A is the slightly newer (cheaper and faster!) version.
Next came the 16F628: 18 pins like the 16F84, pin-compatible, but twice the FLASH, more RAM, more peripherals, and cheaper than the 16F84! One would expect the hobby community to make an instant switch, but that did not happen. There was (and still is) so much information on the internet about 16F84(A) that a lot of beginners still choose this chip to start with. After their start they make their own webpage, which only strengthens the position of the 16F84. In my opinion starting with a 16F84 is a bad choice on pure technical grounds, but if you found a nice introduction that features the 16F84 you might want to use it. Just be aware that you are trying to learn to drive a car on a Ford T.
Note that there are some that say that the 16F84 is still the best PIC to start with, because it is (slightly) easier to use than the later chips. This is mainly caused by the smaller code size (so code paging is not an issue) and by the lack of any analog peripherals (so there is no need to switch them off before the pins are used in digital mode). Personaly I think the added complexity of a 16F628A, 16F88, 16F630, etc is a small price to pay for the lower price and extra features.
After the 16F628 Microchip has steadily widened its offering of flash chips with types that are ever more attractive. The 16F628 was replaced by the cheaper 16F628A, the 16F648A offers twice the code size and more RAM. The 40-pin 16F877 has much more of everything, and it has smaller 16F87x cousins with less pins or less memory if you don’t need its full power. The 16F87x series was replaced by the 16F87xA series (cheaper, but beware that the programming algorithm is different). The 14-pins 16F630 and 16F676 appeared, offering a very good bang-for-your-bucks. The 8-pins 12F629 and 12F675 are nice for very small projects.
The above chips (16F and 12F) are all 14-bit core chips. This refers to the properties of the computing element (CPU) in the chip. If I remember correctly the 40-pin 18F452 chip was the first 16-bit core FLASH chip. This CPU is much easier to program, and can address larger amounts of code and data than the 14-bit CPU. Think of the 18F452 as a 16F877 with a redesigned engine.
The flood has not stopped: there are lots of 18F chips (40, 28 and 18 pins DIP, even more pins in various SMD housings), and a realy hughe variety of 12F/16F chips (40, 28, 18, 14 and 8 pins, with 20 pins coming soon). Microchip is also transferring its older 12-bit core chips to flash versions, check for instance the 12F509. The latest additions are the tiny 10F chips: these are very small 6-pin SMD chips, but they are also available in 8-pin DIP (with two unused pins).
Microchip has also expanded its product line into the territory of the DSPs (Digital Signal Processors) with the dsPIC30F family. These are in my opinion intersting chips, but they are so unlike the other PICs that they should be treated as an entirely different product line.
I recently found one reason to prefer the 16F628 over any other PIC: this chip has provides a clock option where one external resistor, which carries only DC, determines the clock frequency. A lot of other PICs provide the option to determine the clock frequency with an external resistor + capacitor, but in that case both components carry AC and practical capacitor values are very small so stray capacitance can have a big influence on the frequency. With the 16F628 it is very practical to let an external potentiometer determine the speed of the PIC, which can lead to a very simple circuit.
So which chip should you choose? If that has not been decided for you by some webpage or book you want to use, or by the development tools you like, or by what you local electrnics shop has in stock, or what your wallet allows you, let’s see what I can advise.
Your first impulse might be to take the cheapest chip that seems capable of what you want (the most obvious criteria are often the number of I/O pins and the availablity of peripherals like an UART or and A/D converter). If you want to follow this path I would advise you to take a chip that is at least one step ‘fatter’ than your original choice. That will give you some breathing room when your application expands, and some I/O pins to aid in debugging (so you could attach for instance an LCD). In fact I would advise you to take the fattest chip that still has the same architecture (CPU core), treating the 12/14 bit cores as the same. The extras will be very usefull while debugging, and the extra cost is small. If and when your application is fully debugged you can transfer to the chip you originaly had in mind. (Or just keep the fat chip. Why change a winning team?)
Some people take this reasoning to the extreme: forget all the small chips, forget the 12/14 bit cores, take a big 18F, or even a dsPIC. I agree to some extent, but if you want to do more than a few PIC projects this approach might get a little expensive, because the 18F and dsPIC families don’t yet have the realy small and cheap chips you can find in the 12/14 bit families.
So my advise is: first decide which family you want to use. If you want to do a large number of projects, look low (12/14 bit core). If you need the CPU horsepower, large memories, or other goodies look high (18F, or even dsPIC). Next take a fat DIP chip within that family. Within the 12/14 bit cores that would be the 16F877A (12/14 bit core). The developents in the 16 bit cores (18F) are too fast to follow. The 18F452 used to be the king here, but it has been replaced by the 18F4520, and even larger chips are available (18F4620). I am not sufficiently experienced with these chips to advice on dsPICs, but the 30F4013 is currently the fattest 40-pin dsPIC.
If you are relatively new to electronics you might kill a few chips in your learning process. It might be a good idea to let these chips be somehat less expensive than the fat ones I advised you. For this purpose I suggest one of the 14-pins chips from the 14-bit core family. The lowly 16F630 is cheap, the 16F688 is the fattest chip in this group. The 8 and 6 pin chips are slightly cheaper, but have so few I/O pins that I would not recommend them a starters.
For more detail: Starting with PICmicro controllers
For All Your PCB Need, One-stop PCB Solution Services Appear:
EasyEDA: free PCB design tool, 200,000+ engineers are using
PCBs: 10pcs 2-layer 10x10cm only $8.21, on-time global shipping
Components: save 50% on cost, save on freight if shipped with ordered PCBs