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:
- The Low Pin Count Demo Board,7 which includes a PIC 16F690.8
- The 28-pin Demo Board,9 which includes a PIC 16F886.10
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:
- Identify a device:
pk2cmd -P
- Upload a hex file:
pk2cmd -P pic16f690 -M -F foo.hex
- Power up the target:
pk2cmd -P pic16f690 -T
- Power down the target:
pk2cmd -P pic16f690 -W
- Update the PICkit 2 firmware:
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.
References
- 1. http://www.microchip.com
- 2. http://www.paintyourdragon.com/wordpress/?p=45
- 3. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en023805
- 4. http://www.microchip.com/Developmenttools/ProductDetails.aspx?PartNO=PG164130
- 5. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en023805
- 6. http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=PG164120
- 7. http://ww1.microchip.com/downloads/en/DeviceDoc/Low%20Pin%20Count%20User%20Guide%2051556a.pdf
- 8. http://ww1.microchip.com/downloads/en/DeviceDoc/41262E.pdf
- 9. http://ww1.microchip.com/downloads/en/DeviceDoc/41301A.pdf
- 10. http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf