Every once in a while I like playing with Microchip PIC microcontrollers. Partly this is historical: the first microcontrollers I used were PICs, but I still enjoy writing code for them and they're cheap-as-chips (plus I've already got a goodly number just lying around).
However, because this is a pleasure I enjoy quite infrequently, I sometimes forget how everything fits together, so it seemed sensible to document it.
Almost all my PIC experience uses the relatively modern 16Fxxx series of chips, which Microchip refer to as Mid-Range Core devices.
I always program in PIC assembler using the GNU toolchain, and use Microchip's PICkit 2 programmer. There are probably other good choices, but this was the way I went.
The PICkit 2 is a handy little gadget which sits between the USB bus and a PIC. It uses the USB HID protocols, which seems to make it 'just work' as far as the Mac's concerned. Besides programming the PIC, the PICkit 2 will also supply power to the target board assuming that the current is within USB limits.
To talk to the PICkit 2, Microchip have made available a command line tool: PK2CMD. This is available in both source and binary formats on the Microchip site.
Microchip also produce a number of demo boards which are basically just a PIC, a few handy peripherals, and a small prototyping area. I've played with these so far:
- The Low Pin Count Demo Board, which includes a PIC 16F690.
- The 28-pin Demo Board, which includes a PIC 16F886.
This program talks to the PICkit2. I usually install the executables in something like /usr/local/PK2CMDv1-20MacOSX, then symlink that to /usr/local/pk2cmd. pk2cmd needs to find a file of PIC data, so you always need to supply -B/usr/local/pk2cmd.
Key pk2cmd commands include:
- Upload a hex file:
pk2cmd -B/usr/local/pk2cmd -P pic16f690 -M -F foo.hex
- Power up the target:
pk2cmd -B/usr/local/pk2cmd -P pic16f690 -T
- Power down the target:
pk2cmd -B/usr/local/pk2cmd -P pic16f690 -W
- Update the PICkit 2 firmware:
pk2cmd -D PK2V023200.hex
I use the GNU tools to assemble and link my code. As the Paint your Dragon blogger notes, you need to download these from the gputils page at sourceforge, then compile and install them in the usual way:
% tar xvzf gputils-*.tar.gz % cd gputils-* % ./configure % make % make install
A sample Makefile
Once everything's installed it's just like normal. Here's a sample Makefile:
ASM_FLAGS = -p P16F690 PK2_FLAGS = -P PIC16F690 -B/usr/local/pk2cmd PK2_CMD = pk2cmd $(PK2_FLAGS) TARGET = flash.hex %.hex: %.asm gpasm $(ASM_FLAGS) $< all: $(TARGET) test: all upload clean: rm -f *.o *.cod *.hex *.lst *.err upload: $(PK2_CMD) -M -F $(TARGET) power_up: $(PK2_CMD) -T power_down: $(PK2_CMD) -W .PHONY: all clean upload power_up power_down
Microchip provide more software for Windows users. Part of this is the MPLAB IDE which I'm happy to replace with emacs et al. On the other hand Microchip provide in-circuit debug support which has no equivalent on the Mac.