Monday, October 5, 2009

First steps..


PIC's are a good microcontroller family to get started with. As there are only 35 or so instructions to learn, programming them in assembly is relatively simple. That's good, because Microchips assembler is freely available from their website. Alternatively, you can get use a C compiler or PicBasic which might appeal if you've never tried assembly language, but you have to pay money for them...

Actually programming or 'blowing' your code into a PIC is potentially as free as producing the code, although you might prefer to buy an inexpensive programmer because that should eliminate one of the several variables that can conspire against you at the beginning.

There's 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 PIC's, 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

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

The 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:







One of the best midrange PICs for a beginner is the PIC16F84. It uses Flash technology for the program memory, and can be reprogrammed around 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 PIC's with A-D converters. Once you've got to grips with the family, taking advantage of the extra features of the more advanced chips ought to be straightforward.

The (slightly) bad news is that it takes a while to come to terms with the basics. For example, most people will be aware of the more common 'Von Neuman' architecture, where program and data memory are shared. PICs use the 'Harvard' architecture, where program memory is separate from data. This has a number of advantages, as we'll see later.

Testing the programmer:

Once I'd built the programmer, I wanted 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 (you might need to use right-click, Save As).

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

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.

You'll need to have 4bit.HEX it in the same directory as PICPROG.EXE (contained in PGM84V34.ZIP). Then, at the DOS command line, type:

picprog 4bit.hex 1

(assuming 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 LED's and try to ensure that the clock is running - an oscilloscope is useful here.

Please note that if you're not confident using DOS commands, you may choose to use IC-Prog, the Windows application that I recomend you start out with. Details on the next page....

On to the next Post>> configuring your PC to program PICs...