8 April 2008

6. Chip Programming

Now, if you want to get this running in hardware, it’s time to talk about how to program the real chips. There’s essentially three ways of doing that.

The first, and definitely easiest (and generally most reliable) way is to plonk your chip in a ZIF-socketed programmer and use a windows program to communicate with the programmer board. Generally these programmers use USB these days, which is pretty handy. It does mean that you need to remove the chip from whatever its in though, in order to program it.

The second way, which is essentially the same as the first, is to use an ICSP. The In-Circuit Serial Programmer is just a fancy way of defining the pins that need connecting in order to program the device. On pics there is a clock and data line, Vdd (positive supply), ground and most likely a “higher voltage” programming line as well. If you put 12v or so on the Vpp line, the chip will wake up in programming mode, allowing you to read and write flash, eeprom and “config” settings. As you can imagine, in option one, these connections where made directly with the chip. With an ICSP “socket” you can leave the chip in place and still program it. You do need to generally disconnect the main supply, however.

The third way which is definitely the fastest and most convenient is to use a bootloader. This is a small program that is resident in your pic that communicates with a pc-based application in order to download the real program you want to store (and run). Of course, in order to get this bootloader in there in the first place, you have to use method one or two – after that you can use a bootloader. This is actually quite neat, since most bootloaders use a serial interface to work, you can quickly download a program over the serial port and then immediately see the results of it. Any debug can be done over the serial link as well, making the process very neat.

In the direct programming methods (one and two) you’ll also need to set config options for the pic. These are also known as “fuses”, even though I’m pretty sure these days they’re just a chunk of memory like everything else. They do have the distinction however of allowing the functionality of the pic to change before it actually starts running a program. An example is the MCLR pin – which can often either be a digital IO pin, or a pin which when connected to ground, resets the pic. Clearly the chip needs to know which is the correct option before it actually starts executing a program.

18f chips tend to have richer (that is, more complex) sets of config bits than the 16f series. However, the main flags to be concerned with are:

Oscillator – do you want the chip to run with an internal clock , or ouse an external crystal (with associated circuitry, usually just two caps and the crystal.

MCLR – usally it’s nice to have a press button reset, so this is handy to leave on, unless you need the IO. You’ll need to tie the pin to Vdd via a resistor, with the button pulling it to ground when pressed. Tip: Don’t leave MCLR floating (ie, unconnected), otherwise the chip will be in a constant state of resetting and never actually do anything. If the pin is configured as an IO pin, of course you don’t need to worry about this.

Watchdog timer – pics can set a watchdog timer to reset the chip if there’s a problem. Turn this off for the moment.

Just about everything else can be switched off, at least, in these early days.

In SourceBoost, you set the config bytes with the data pragma:


Have a look through the include file for your pic (eg, pic16f88.h) in the sourceboost include directory to see the options for your pic. Match this with what you want from your datasheet, and “&” them altogether. This is the config for the 16f88 pic, a very handy general purpose chip. It’s got 4k words of flash, 256 bytes of EEPROM and 384 bytes of RAM.

You’ll need a pic, run Vdd to 5 volts and Vss to ground, attach a led to one of the pins (in this case, we’re going to use RA0) and route the led to ground via a resistor – 330R should do it. Route MCLR to Vdd via a resistor (say 10k). Take the .hex file generated by the compiler, use your programming utility to program the pic and see if you can get that led flashing.

Once you’re this far, you’re reading to get a bootloader going.

No comments: