Using pk2cmd to transfer a hex file onto the dsPIC30F4011

Since upgrading to MPLAB X, I’ve had difficulty using the PICkit2 from within the IDE, so I’ve mainly been using the PICkit2 helper application to transfer my compiled hex files to the dsPIC. However, there is another handy way of doing it from the command line – the free pk2cmd utility. This software can be downloaded from

pk2cmd has lots of options, but here’s a typical command:

pk2cmd.exe -M -PdsPIC30F4011 -Fprogram.hex -T

These are the meanings of the command line arguments:

  • -M tells pk2cmd to write all memory areas of the target device.
  • -PdsPIC30F4011 tells pk2cmd that the microcontroller is a dsPIC30F4011.
  • -Fprogram.hex tells pk2cmd that the hex file to be transferred to the microcontroller is “program.hex”.
  • -T tells pk2cmd to supply power to the device once programming is complete.

I’ve been using this alongside Microchip’s free XC16 compiler, which I use to compile my C code. I’ve been editing the C code in a plain text editor (Notepad++), so I don’t actually need to use MPLAB X at all. Here’s an example of the type of makefile I use to manage the XC16 build process:

MP_CC_DIR="D:\Program Files\Microchip\xc16\v1.00\bin"
MP_CC="D:\Program Files\Microchip\xc16\v1.00\bin\xc16-gcc.exe"

all: program.hex

program.cof: main.c
	$(MP_CC) main.c -o program.cof -mcpu=30F4011 -Wall -Wl,--script=p30F4011.gld

program.hex: program.cof
	$(MP_CC_DIR)\\xc16-bin2hex program.cof -a -omf=elf

I have Microsoft nmake installed (I think it came with ,Visual C++ Express) but GNU make (e.g. from MinGW) or any other common version will probably work without much modification. Of course, you don’t actually need a makefile, but typing in the xc16-gcc command yourself is a little bit unwieldy.

For convenience, I copied the files “pk2cmd.exe” and “PK2DeviceFile.dat” directly into my program folder. Here’s what it looks like in the command window when I build a new hex file and program it onto the dsPIC:

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s