Developing programs for PICs can be fascinating, rewarding and, dare I say it, a little addictive. However, from a cursory view, it may appear too daunting or mysterious and be passed over for something easier. This page is intended to provide an overview of the process and make it easier for you to get involved. It isn't a tutorial on programming, just some thoughts on how to get started.
A point of clarification: This is not the PIC-AXE variety. I never did get into that one and don't really intend to.
Like any worthwhile endeavour, you will need to spend some time learning what is involved. Don't expect to become expert in five minutes.
What is a PIC? - A PIC is really just a small, programmable computer chip (actually more precisely called a microcontroller) that does absolutely nothing on its own. It has certain outputs for controlling things based on voltages at its inputs - nothing more, nothing less. Give it a supply voltage, connect some other electronics or mechanical things to it, tell it what to do through program instructions and it will do something useful for you.
PICs are used for controlling almost any electrical or electronic circuit. Examples are: Liquid Crystal Displays, stepper motors, relays, transceivers, appliances such as washing machines and other timer-related devices, lights, security, etc. The possibilities are really only limited by your imagination and program creation skills. The image in this section shows two different circuits on a prototyping breadboard. At the left is a circuit controlling a stepper motor while at the right, the circuit controls a Liquid Crystal Display or LCD. The PIC is the left and rightmost ICs in the image.
There is probably no limit but within reason, it is limited to what your program tells it to do. It is simply a matter of, "What information can I (need I?) provide as inputs and what information do I need as outputs to do what I want to do?" Answer those questions and you have the basis of your new program.
Many years ago, when I was involved in teaching computer use to some degree, I used to say to students, "If you forget most of what I say, please try to remember 3 things":
  
1. A computer does what it is told,
  
2. It does only what it is told, and
  
3. It must be told to do it
When it comes to PIC programming, these 3 things become very important because if you haven't given it correct instructions, it ain't going to do what you want.
What do you need to get started? - There are a number of things that you will require in order to create and run your own PIC programs.
1. Reference Material - A newcomer and even an experienced hand needs reference material as it is impossible to remember everything. There are hundreds of web sites that will help in some shape or form. Most of these will have program code snippets available but they will generally be more suited to more experienced users.
One of the sources I recommend for new starts is the excellent tutorial material produced by the American QRP CLub and this is what is pictured. The Club has a complete, downloadable tutorial series consisting of 20 something pdf files. The tutorial will take you from a complete novice with very simple instructions and projects through to an accomplished programmer. Each stage builds on the previous as you progress through the tutorial. You will still need to apply yourself as it won't happen automatically.
Instructions on downloading and setting up the Microchip programming suite, MPLAB, are included in the turorial.
Here is the link to the
AQRP PIC Programming Tutorial and downloads.
2. Programming Editor - An experienced programmer may easily get by with an ordinary text editor such as Windows Notepad or similar although I could never see myself getting used to it. Using a purpose-designed editor will save you a lot of frustration trying to find errors in your code.
Microchip's MPLAB Integrated Development Environment (IDE) provides and integrated editor. The editor provides automatic colour-coding of program componets, text searching, file management and many other enhancements. This makes your program far easier to follow and correct and is worth the setup time and effort.
There are many other components provided in the MPLAB IDE. Some of these are:
Project and File Management - The more complex projects may have many files making up their source code. In addition to these there may be library files, include files and linker scripts to name a few. It is easy to lose sight of what fits where but the Project Management facility keeps them all where they should be for you.
Viewing Windows - Viewing windows consist of Disassembly listing, hardware stack, program memory, file registers, EEPROM, memory usage gauge, stopwatch and others.
Simulator - Simulation of your program is possible. Various settings are available but this will always run much, much slower than the program in real time. The computer has other things to do including refreshing the monitor. In any case, if it was to run as quick as the PIC in real time, it would be far too fast for you to see it. The simulator allows you to step through one line at a time and to change various things on the fly.
Debugger - Many tools are available in the Debugger. These range from setting program breakpoints, stopwatch, stimulus controller where you can change the status of things on the fly, and others.
You will also be able to download PIC datasheets from the
Microchip web site. Look in the Design column for required information.
3. Programmer - Programmer in this context does not mean 'someone to write the program for you'. It means the device that actually puts your new program in the PIC. Some PICS have In Circuit Programming capability but many have not. You will still need a PC to drive/control this programming function.
The programmer I use (pictured at left) was covered in Silicon Chip magazine, 2003. It was available from ALTRONICS as Kit K9505. The programmer runs off a 9V battery and the serial port of a PC. The PIC is inserted into the green zero insertion force (ZIF) socket at centre bottom of the image.
4. Programmer Software - The programmer mentioned above, in conjunction with the free download program
ICPROG has never missed a beat.
The image at left shows the ICPROG programming screen with a compiled PIC program (*.HEX) opened within and ready to be dumped into an unsuspecting PIC. A few program settings, a click on the programming icon and the job is done.
The program software is very easy to use and the whole process only takes but a few seconds to complete. The PIC is then removed, placed in your circuit and it is at this point that you will find out if your program does what you expect it to. If not, make the necessary changes, reprogram the PIC and try again.
16F84A - A simple, very popular PIC is the 16F84A. Although there are many different PIC chips, this is probably the most common. It has 18 pins, requires a 5V supply and is currently available for less than $10.00.
| Pin Name |
Pin Number |
Purpose/Features |
| RA0, RA1, RA2, RA3 |
17, 18, 1, 2 |
Bi-directional port and known as Port A - can be configured as an input or an output and can be changed as you wish during program operation. The number following RA is the bit number (0 to 4). With RA4/T0CKI included, we have a 5-bit directional port where each individual bit can be configured as input or output. |
| RA4/T0CKI |
3 |
Dual-purpose. It can be used for bit 5 as part of the bi-directional port or as another clock input that operates an internal timer. It operates in isolation from the main clock. |
| RB0/INT through to RB7 |
6, 7, 8, 9, 10, 11, 12, 13 |
Second bi-directional port and known as Port B. It behaves in exactly the same way as RA (Port A) except there are 8 bits involved. The secondary INT operation of RB0/INT provides an input that can be monitored. If the pin goes high, it can be used to cause the program to restart, stop or any other single function. |
| VSS (supply), VDD (ground) |
5, 14 |
5V supply and ground respectively. |
| OSC1/CLK IN |
16 |
Used with an external clock or oscillator. There are basically 3 options - crystal, RC or high speed oscillator. |
| OSC2/CLKOUT |
15 |
Part of the external clock. Can also provide a clock/4 signal when using RC or high speed oscillator. |
| MCLR (active low) |
4 |
Memory Clear - used to erase internal memory locations - normally connected to positive rail if not used in the program. |
As you can now see, the 16F84A PIC provides 2 bi-directional input/output ports, 2 independent clock inputs, a memory clear and an interrupt. There are 1024 memory locations for your program and 64 bytes of non-volatile memory or EEPROM if needed.
As the PIC can carry out its instructions very quickly, it will be necessary to create delays in your program to allow connected devices to catch up. To explain, whatever you have connected to the PIC may not be able to respond in a microsecond or so that the PIC will take to carry out an instruction.
So, there you have it. A quick exposure to PIC programming. I hope to hear you on air talking about your latest development or your continuing frustrations in creating it.
Created 2007 by
Kevin Crockett. Comments and suggestions may be e-mailed to