Update

I updated this in November 2015.

Introduction

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.

Most of this information is available elsewhere online, both from Microchip’s website1, and from other places.2

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 23 programmer. There are probably other good choices, but this was the way I went.

Hardware

The PICkit 2 described below is now obsolete, but mine still works. The PICkit 34 is its spiritual successor.

The PICkit 25 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.6

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:

Software

PK2CMD

This program talks to the PICkit2. I just copy both the pk2cmd executable and the PIC data file to somewhere on my $PATH.

Key pk2cmd commands include:

pk2cmd -P
pk2cmd -P pic16f690 -M -F foo.hex
pk2cmd -P pic16f690 -T
pk2cmd -P pic16f690 -W
pk2cmd -D PK2V023200.hex

GNU toolchain

Happily this is now available in homebrew:

$ brew install gputils

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

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

Missing bits

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.