Program PICs with a PICkit2 using the command line on Linux

From Curuxa


This tutorial explains how to use pk2cmd from the Linux command line.

pk2cmd is the official Microchip programmer that connects to the PICkit2 and lets you test, read and write executables to and from Microchip microcontrollers.


First of all, check the package manager from your distribution (APT/apt-get/Synaptic/aptitude, Portage/emerge, RPM/YaST...). If you find pk2cmd there you can install it easily and jump to next section.

If pk2cmd is not available in your repositories, you can use repository for Debian/Ubuntu/derivatives.

If you couldn't find pk2cmd yet, you have to download/install it manually.

Using pk2cmd

The following are full commands for using the most basic features available in pk2cmd.

Remember to replace PIC models and paths in these sample commands.

If you have downloaded the pk2cmd binary or compiled it yourself, you have to put "./" at the beginning of each command.

List supported models

The following command shows a list of supported microcontrolled by your PICkit2:

pk2cmd -?P

Read program

You usually don't need to do this, but if you want to retrieve the full program stored in the microcontroller, run:

pk2cmd -PPIC16Fxx -GF/home/my_user/my_program.hex

Now you've got a new binary file called /home/my_user/my_program.hex, but humans can't read it. You can disassemble it by doing...

gpdasm -p16fxx my_program.hex > my_program.asm

Write program

To burn a program (stored in a .hex file) into your microcontroller, run:

pk2cmd -PPIC16Fxxx -M -F/home/my_user/my_program.hex

Verify program

The following command verifies that the given local binary file (.hex) is the same program already burnt into the microcontroller:

pk2cmd -PPIC16Fxxx -Y -Fmy_program.hex

Run the program

Once you have your program burnt into the microcontroller, you can power your circuit from the PICkit2, so the program will start running:

pk2cmd -PPIC16Fxxx -A4 -T

The voltage supplied to the microcontroller is specified as "-Ax", so "-A4" will power the device at four volts, "-A3" at 3V, "-A2.5" at 2.5V...

Stop the program

While your device is being powered (so your program is running) you can stop it by doing...

pk2cmd -PPIC16Fxxx -R