Linux terminal only PIC programming

Thursday, 25 September 2014 Posted in Blog

Finally after a lot of reading and some test and trial, i came up with a setup to program the PIC16F88 using only the terminal command line.

The setup im using is, Xubuntu has the O.S. ubuntu, mint or even debian should work without absolutly any change.
The compiler im using is the SDCC - Small Device C Compiler which has a GPL license. But we need to install first GPUTILS, since SDCC will use some tools from it.
Next we need to install MPLABX from microchip which is a free GUI IDE, but we are only interested on the command line tool to control the fisical programmer to our PIC.
And finally i used my PICKit 3 programmer, which was the only thing i spent money for.
Also i used VIM has a text editor, you can also use nano, emacs or any other text editor, and a Makefile.

The first step will be to download and install MPLABX.
A little word here, the only reason we need to install MPLAX is because we need to get our hands on the Microchip debugger (MDB), this has a bash script that we will be calling from the Makefile to program the chip with the PicKit3.
In this page, you can download the latest installer for linux.
Once the download is finished go into the folder you've downloaded it and type in the terminal :

chmod +x

This will make the file executable.
Next type :


This will start the a nice gui installer, for now don't change anything and let him install in the default folders.

Second we need to install gputils, for that we can just use aptitude, like this :

sudo aptitude install gputils

We can proceed now to SDCC - Small Device C Compiler.
Go to SDCC sourceforge and download the latest.
Now untar with the comand :

tar xvf sdcc-3.4.0-i386-unknown-linux2.5.tar.bz2

Get into the newly created folder and :

sudo cp -r * /usr/local

We have SDCC up and running.You could install SDCC with aptitude but it wont be up to date and a lot of header files will be missing.

So finally our system has all the tools necessary to compile and program our PIC.
I'm using a very simple blink an LED program to show this, you can find eveything here.
Now let's look at the makefile :

# GNU/Linux specific Make directives.

# Declare tools.
SHELL = /bin/sh
CC = sdcc
LD = gplink # For some reason gplink malfunctions so im only gona call sdcc
            # which will call gplink properly for me and do its thing. 

MCU = 16f88
ARCH = pic14
SOURCES = test1.c
PROGRAMMER = ./prog.txt # This is the file with the commands for the PICKIT3

CFLAGS  = -m$(ARCH) -p$(MCU) --use-non-free -L /usr/local/share/sdcc/lib/src/pic14


CLEANFILES = test1.o test1.asm test1.lst test1.cod

.SUFFIXES: .c .o
.PHONY: clean

# Compile
all: clean $(EXECUTABLE)

    $(CC) $(CFLAGS) $<


    rm -rf $(CLEANFILES)
    /opt/microchip/mplabx/mplab_ide/bin/ $(PROGRAMMER)
    rm MPLABXLog*

So after you have typed :


in the terminal, you will get your .hex file ready to flash the chip, and here is where it cames the Microchip debugger (MDB).
Now when you write on the terminal :

make flash

You will be calling the microchip bash script and give it the file prog.txt has an argument.
The script is going to read and execute line by line all the commands inside the text file.
Let's take a look.

Device PIC16F88
set system.disableerrormsg true
Hwtool pickit3 -p
Program "./test1.hex"
Reset MCLR

The very important thing here to enfasize is fourth line, Program "./test1.hex".
This has to be changed to the name of the .hex file to be flashed into the PIC.
And that's it, download modify whatever, have fun :).

You can get the three files, Makefile, prog.txt and a small blink LED example file in the downloads section.