First Steps

I like PICs because there are only 35 or so instructions to learn, so programming them in assembly is relatively simple. That's good, because Microchip's assembler is freely available from their website. Alternatively, you can get use C compilers which might appeal if you've never tried assembly language. Personally, I think that it's useful to learn assembly language as it forces you to really understand how the microcontroller works, and this will help if you do decide to graduate to a higher-level language in the future.

Programming hardware

Actually programming or 'flashing' your code into a PIC is potentially completely free, but you might prefer to buy an inexpensive programmer because that should eliminate one of the many variables that will conspire against you at the beginning.

There are plenty of designs on the web, complete with the software you need. I built the JDM Programmer, a well-known device that is powered from the serial port. To test it, it's worth downloading PGM84V34.ZIP, which contains 3 DOS executables that enable you to test the programmer, program a PIC and read the code in a PIC.

This programmer works with most serially-programmable PICs, although you need to build an adapter for any that are larger than 18 pin devices. See the diagram on the JDM site which shows you how to place 8-pin devices

PIC programmer (10KB)

I've redrawn the schematic in a form that makes more sense (well, to me, at least!). I added a couple of LEDs, and you might notice that I've wired D6 differently to the original design - simply because I didn't have the right zener at the time...

A Veroboard prototype worked well enough, but as I had some spare real-estate on the PCB that I designed for the Olde 'Scope repair, the programmer took on a more professional appearance:

Which PIC to use?

One of the best midrange PICs for a beginner is the PIC16F84A. It uses flash technology for the program memory, and can be reprogrammed at least 1000 times. These devices have 13 IO ports, and support interrupts, as well as having 64 bytes of non-volatile EEPROM memory that your program can use as permanent storage. There are many variations on the theme, including PICs with A-D converters. Once you've got to grips with the family, taking advantage of the extra features of the more advanced chips is very straightforward.

Surely not the 16F84?

Be aware that there are many of people who argue against using the venerable 16F84, claiming that it is obsolete. This is simply nonsense. As of late 2013, the PC16F84A is still very much available - it is currently "in production", and is available from all the usual vendors. OK, there are better PICs available, and they often cost less, so yes, you could argue that the 16F84 is "inferior" and "expensive". But it's certainly not obsolete.

So if there are "better" PICs to choose from, why would I recommend the 16F84?

Remember, this is a tutorial for absolute beginners, and there is a steep learning curve ahead. From that point of view, it's a definite advantage to start with a simple chip that requires the minimum of configuration. The newer PICs pack in a lot more functionality, but we simply don't want that yet. To turn off the extra cleverness - most of which is enabled by default - we'd have to put extra code in our start-up routines, and then we'd have to explain it to some extent. Far better to just choose a PIC that doesn't need all that. The transition to better PICs can come later.

There's no doubt that I wouldn't use a 16F84 for a new project today. But I still regard them as a valuable tool for teaching, and continue to use them for this purpose.

Testing the programmer

Once I'd built the programmer, I was itching to programme a PIC. Surprisingly, a quick web search failed to find anything simple enough, so I wrote a simple 4 bit counter which I managed to get working after much head-scratching. You can download the .HEX file here.

This simple program counts from 0 to 15. To build it, you need a PIC16F84A, 4 LEDs and 4 resistors, and a crystal. The frequency of the crystal determines the count-rate - around 1 second with a 4MHz crystal. For the purposes of experimenting with PICs, I'd recommend using a solderless prototyping board. The connections are very simple, as shown here:

Click for larger version (5KB)

You might need to add 33pF capacitors from each crystal pin to ground, but I found that there's enough stray capacitance if you're using prototype board. The reset pin can be connected directly to +5V because the 16F84 has an on-chip reset circuit.

When it comes to programming the PIC, you have a choice. I'm about to describe how I used the command-line programming tools found in PGM84V34.ZIP, which comes from the JDM website. However, if you're uncomforable about using DOS commands, you may prefer to skip ahead and follow all the advice on the next page, which will result in a Windows programming application being installed on your machine. If you do that, please come back here and make sure you're able to get this counter working before trying anything else!

Save 4bit.HEX in the same directory as PICPROG.EXE (contained in PGM84V34.ZIP). Then, at the DOS command line, type:

    picprog 4bit.hex 1
   

This assumes the programmer is connected to Com1. In about 10 seconds, it's all done. Don't worry about the verify failure - this happens because the verify routine examines the whole chip contents, whereas the programme routine has only written to the locations specified in the .HEX file.

Remove the PIC from the programmer and place it in your circuit. Apply power and watch those LEDs count binary. If nothing happens, double-check the polarity of the LEDs and try to ensure that the clock is running - an oscilloscope is useful here.